mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
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.
45 lines
1.2 KiB
TypeScript
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;
|