Files
5chan/src/stores/use-post-number-store.ts
T
plebeius 7821f9cd82 perf(post-rendering): reduce quoted backlink rerenders via scoped store subscription
Replaced duplicated quotedByMap construction with useQuotedByMap() hook that subscribes only to post numbers referenced in each thread. Updated registerComments() to skip no-op writes, eliminating unnecessary numberToCid identity changes during feed scrolling.
2026-02-12 18:41:21 +08:00

45 lines
1.2 KiB
TypeScript

import { create } from 'zustand';
import type { Comment } from '@plebbit/plebbit-react-hooks';
interface PostNumberState {
numberToCid: Record<number, string>;
cidToNumber: Record<string, number>;
registerComments: (comments: Comment[]) => void;
}
const usePostNumberStore = create<PostNumberState>((set) => ({
numberToCid: {},
cidToNumber: {},
registerComments: (comments: Comment[]) => {
if (!comments?.length) return;
set((state) => {
let nextNumberToCid = state.numberToCid;
let nextCidToNumber = state.cidToNumber;
let hasUpdates = false;
for (const c of comments) {
const num = c?.number;
const cid = c?.cid;
if (typeof num === 'number' && cid && (nextNumberToCid[num] !== cid || nextCidToNumber[cid] !== num)) {
if (!hasUpdates) {
nextNumberToCid = { ...state.numberToCid };
nextCidToNumber = { ...state.cidToNumber };
hasUpdates = true;
}
nextNumberToCid[num] = cid;
nextCidToNumber[cid] = num;
}
}
if (!hasUpdates) {
return state;
}
return { numberToCid: nextNumberToCid, cidToNumber: nextCidToNumber };
});
},
}));
export default usePostNumberStore;