Skip to content

Commit

Permalink
fix: Indentify and fix bug
Browse files Browse the repository at this point in the history
- `currentCard()` is called multiple times which triggers the expensive `upcomingCardIds()`
- Adding a static `currentCard` variable reduced calls (and thus multiple React re-renders)
  • Loading branch information
DafyddLlyr committed Jul 10, 2024
1 parent 9e1d63e commit 9e3796b
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions editor.planx.uk/src/pages/FlowEditor/lib/store/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export interface PreviewStore extends Store.Store {
upToNodeId: Store.nodeId,
visited?: Array<string>,
) => Array<string>;
currentCard: () => Store.node | null;
currentCard: ({ id: Store.nodeId } & Store.node) | null;
setCurrentCard: () => void;
hasPaid: () => boolean;
previousCard: (
node: Store.node | null,
Expand Down Expand Up @@ -136,18 +137,15 @@ export const previewStore: StateCreator<
return res;
},

currentCard() {
setCurrentCard() {
const { upcomingCardIds, flow } = get();
const upcoming = upcomingCardIds();

if (upcoming.length > 0) {
const id = upcoming[0];
return {
id,
...flow[id],
};
set({ currentCard: { id, ...flow[id] } });
} else {
return null;
set({ currentCard: null });
}
},

Expand Down Expand Up @@ -287,6 +285,7 @@ export const previewStore: StateCreator<
_nodesPendingEdit,
changedNode,
updateSectionData,
setCurrentCard,
} = get();

if (!flow[id]) throw new Error(`id "${id}" not found`);
Expand Down Expand Up @@ -370,6 +369,7 @@ export const previewStore: StateCreator<
}
}
updateSectionData();
setCurrentCard();
},

resultData(flagSet, overrides) {
Expand Down Expand Up @@ -652,6 +652,8 @@ export const previewStore: StateCreator<

return currentRequestedFiles || emptyFileList;
},

currentCard: null,
});

const knownNots = (
Expand Down

0 comments on commit 9e3796b

Please sign in to comment.