Skip to content

Commit

Permalink
refactor UI context to ShowAnagramHelper (#1868)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverabrahams authored Dec 20, 2024
1 parent 1170d95 commit f98e690
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { useCallback, useEffect, useMemo, useState } from 'react';
import { useCurrentClue } from '../context/CurrentClue';
import { useData } from '../context/Data';
import { useProgress } from '../context/Progress';
import { useShowAnagramHelper } from '../context/ShowAnagramHelper';
import { useTheme } from '../context/Theme';
import { useUIState } from '../context/UI';
import { biasedShuffle } from '../utils/biasedShuffle';
import { getCellsWithProgressForGroup } from '../utils/getCellsWithProgressForGroup';
import { Clue } from './Clue';
Expand All @@ -21,7 +21,7 @@ export const AnagramHelper = () => {
const [solving, setSolving] = useState(false);
const [shuffledLetters, setShuffledLetters] = useState<string[]>([]);
const theme = useTheme();
const { setShowAnagramHelper } = useUIState();
const { setShowAnagramHelper } = useShowAnagramHelper();
const { entries, cells } = useData();
const { currentEntryId } = useCurrentClue();
const { progress } = useProgress();
Expand Down
4 changes: 2 additions & 2 deletions libs/@guardian/react-crossword/src/components/Controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import type { EntryID } from '../@types/Entry';
import { useCurrentClue } from '../context/CurrentClue';
import { useData } from '../context/Data';
import { useProgress } from '../context/Progress';
import { useShowAnagramHelper } from '../context/ShowAnagramHelper';
import { useTheme } from '../context/Theme';
import { useUIState } from '../context/UI';
import { useValidAnswers } from '../context/ValidAnswers';
import { useClearUserInput } from '../hooks/useClearUserInput';
import { useUpdateCell } from '../hooks/useUpdateCell';
Expand Down Expand Up @@ -142,7 +142,7 @@ const RevealClue = (props: ButtonProps) => {
};

const AnagramHelper = (props: ButtonProps) => {
const { toggleAnagramHelper } = useUIState();
const { toggleAnagramHelper } = useShowAnagramHelper();

return (
<ClueButton onClick={toggleAnagramHelper} {...props}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import { CurrentCellProvider } from './CurrentCell';
import { CurrentClueProvider } from './CurrentClue';
import { DataProvider } from './Data';
import { ProgressProvider } from './Progress';
import { ShowAnagramHelperProvider } from './ShowAnagramHelper';
import { ThemeProvider } from './Theme';
import { UIStateProvider } from './UI';
import { ValidAnswersProvider } from './ValidAnswers';

export const ContextProvider = ({
Expand All @@ -47,7 +47,7 @@ export const ContextProvider = ({

return (
<ThemeProvider theme={theme}>
<UIStateProvider>
<ShowAnagramHelperProvider>
<DataProvider
entries={entries}
solutionAvailable={solutionAvailable}
Expand All @@ -66,7 +66,7 @@ export const ContextProvider = ({
</CurrentCellProvider>
</ProgressProvider>
</DataProvider>
</UIStateProvider>
</ShowAnagramHelperProvider>
</ThemeProvider>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,34 @@ type Context = {
toggleAnagramHelper: () => void;
};

const UIStateContext = createContext<Context | undefined>(undefined);
const ShowAnagramHelperContext = createContext<Context | undefined>(undefined);

export const UIStateProvider = ({ children }: { children: ReactNode }) => {
export const ShowAnagramHelperProvider = ({
children,
}: {
children: ReactNode;
}) => {
const [showAnagramHelper, setShowAnagramHelper] = useState(false);

const toggleAnagramHelper = useCallback(() => {
setShowAnagramHelper((prev) => !prev);
}, [setShowAnagramHelper]);

return (
<UIStateContext.Provider
<ShowAnagramHelperContext.Provider
value={{ showAnagramHelper, setShowAnagramHelper, toggleAnagramHelper }}
>
{children}
</UIStateContext.Provider>
</ShowAnagramHelperContext.Provider>
);
};

export const useUIState = () => {
const context = useContext(UIStateContext);
export const useShowAnagramHelper = () => {
const context = useContext(ShowAnagramHelperContext);

if (!context) {
throw new Error(
'UIStateContext does not exist. Have you used a Crossword subcomponent outside a Crossword component?',
'ShowAnagramHelperContext does not exist. Have you used a Crossword subcomponent outside a Crossword component?',
);
}

Expand Down
4 changes: 2 additions & 2 deletions libs/@guardian/react-crossword/src/layouts/ScreenLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { textSans12, textSans14 } from '@guardian/source/foundations';
import type { ReactNode } from 'react';
import { memo } from 'react';
import type { LayoutProps } from '../@types/Layout';
import { useShowAnagramHelper } from '../context/ShowAnagramHelper';
import { useTheme } from '../context/Theme';
import { useUIState } from '../context/UI';

const CluesHeader = memo(({ children }: { children: ReactNode }) => {
const theme = useTheme();
Expand Down Expand Up @@ -36,7 +36,7 @@ const Layout = ({
}: LayoutProps) => {
const { textColor, clueMinWidth, clueMaxWidth } = useTheme();

const { showAnagramHelper } = useUIState();
const { showAnagramHelper } = useShowAnagramHelper();
const theme = useTheme();

const gridWidth = Math.max(actualGridWidth, 300);
Expand Down

0 comments on commit f98e690

Please sign in to comment.