Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make type for current cell context a Cell type #1864

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions libs/@guardian/react-crossword/src/components/Clues.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const Label = memo(({ direction }: { direction: Direction }) => {
});

export const Clues = ({ direction, Header }: Props) => {
const { entries, getId } = useData();
const { entries, getId, cells } = useData();
const { progress } = useProgress();
const { currentEntryId, setCurrentEntryId } = useCurrentClue();
const { setCurrentCell } = useCurrentCell();
Expand All @@ -64,9 +64,11 @@ export const Clues = ({ direction, Header }: Props) => {
const selectClue = useCallback(
(entry: CAPIEntry) => {
setCurrentEntryId(entry.id);
setCurrentCell({ x: entry.position.x, y: entry.position.y });
setCurrentCell(
cells.getByCoords({ x: entry.position.x, y: entry.position.y }),
);
},
[setCurrentCell, setCurrentEntryId],
[cells, setCurrentCell, setCurrentEntryId],
);

/**
Expand Down
11 changes: 6 additions & 5 deletions libs/@guardian/react-crossword/src/components/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ export const Grid = () => {
}

if (delta.x !== 0) {
setCurrentCell({ x: newX, y: newY });
setCurrentCell(newCell);
setCurrentEntryId(possibleAcross ?? possibleDown);
return;
}

if (delta.y !== 0) {
setCurrentCell({ x: newX, y: newY });
setCurrentCell(newCell);
setCurrentEntryId(possibleDown ?? possibleAcross);
return;
}
Expand Down Expand Up @@ -292,11 +292,12 @@ export const Grid = () => {
let newEntryId = currentEntryId;

// Get the entry IDs that apply to the clicked cell:
const entryIdsForCell = cells.getByCoords({
const clickedCell = cells.getByCoords({
x: clickedCellX,
y: clickedCellY,
})?.group;
});

const entryIdsForCell = clickedCell?.group;
// If there are no entries for this cell (i.e. it's a black one),
// set the selected entry to undefined
if (isUndefined(entryIdsForCell)) {
Expand Down Expand Up @@ -352,7 +353,7 @@ export const Grid = () => {
}

// Set the new current cell and entry:
setCurrentCell({ x: clickedCellX, y: clickedCellY });
setCurrentCell(clickedCell);
setCurrentEntryId(newEntryId);
inputRef.current?.focus();
},
Expand Down
8 changes: 4 additions & 4 deletions libs/@guardian/react-crossword/src/context/CurrentCell.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { Dispatch, SetStateAction } from 'react';
import { useState } from 'react';
import { createContext, type ReactNode, useContext } from 'react';
import type { Coords } from '../@types/crossword';
import type { Cell } from '../@types/crossword';

type Context = {
currentCell?: Coords;
setCurrentCell: Dispatch<SetStateAction<Coords | undefined>>;
currentCell?: Cell;
setCurrentCell: Dispatch<SetStateAction<Cell | undefined>>;
};

const CurrentCellContext = createContext<Context | undefined>(undefined);

export const CurrentCellProvider = ({ children }: { children: ReactNode }) => {
const [currentCell, setCurrentCell] = useState<Coords | undefined>(undefined);
const [currentCell, setCurrentCell] = useState<Cell | undefined>(undefined);

return (
<CurrentCellContext.Provider value={{ currentCell, setCurrentCell }}>
Expand Down
Loading