2026-02-26 16:42:36 +08:00
|
|
|
import { useCallback, useMemo } from 'react';
|
2024-06-13 17:50:05 +02:00
|
|
|
import { create } from 'zustand';
|
2026-04-20 22:11:36 +07:00
|
|
|
import localForageLru from '@bitsocial/bitsocial-react-hooks/dist/lib/localforage-lru/index.js';
|
2024-06-13 17:50:05 +02:00
|
|
|
|
|
|
|
|
interface HideStoreState {
|
|
|
|
|
hiddenCids: { [key: string]: boolean };
|
|
|
|
|
hide: (cid: string) => void;
|
|
|
|
|
unhide: (cid: string) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const hideStore = localForageLru.createInstance({
|
|
|
|
|
name: 'hideStore',
|
|
|
|
|
size: 1000,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const useHideStore = create<HideStoreState>((set) => ({
|
|
|
|
|
hiddenCids: {},
|
|
|
|
|
hide: (cid: string) => {
|
|
|
|
|
set((state) => ({
|
|
|
|
|
hiddenCids: { ...state.hiddenCids, [cid]: true },
|
|
|
|
|
}));
|
|
|
|
|
hideStore.setItem(cid, true);
|
|
|
|
|
},
|
|
|
|
|
unhide: (cid: string) => {
|
|
|
|
|
set((state) => {
|
|
|
|
|
const newHiddenCids = { ...state.hiddenCids };
|
|
|
|
|
delete newHiddenCids[cid];
|
|
|
|
|
return { hiddenCids: newHiddenCids };
|
|
|
|
|
});
|
|
|
|
|
hideStore.removeItem(cid);
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const initializeHideStore = async () => {
|
|
|
|
|
const entries: [string, boolean][] = await hideStore.entries();
|
|
|
|
|
const hiddenCids: { [key: string]: boolean } = {};
|
|
|
|
|
entries.forEach(([key, value]) => {
|
|
|
|
|
hiddenCids[key] = value;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useHideStore.setState((state) => ({
|
|
|
|
|
hiddenCids: { ...hiddenCids, ...state.hiddenCids },
|
|
|
|
|
}));
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-14 10:43:52 +02:00
|
|
|
initializeHideStore();
|
|
|
|
|
|
2024-06-13 17:50:05 +02:00
|
|
|
const useHide = ({ cid }: { cid: string }) => {
|
2026-02-26 16:42:36 +08:00
|
|
|
const hidden = useHideStore((state) => !!state.hiddenCids[cid]);
|
2024-06-13 17:50:05 +02:00
|
|
|
const hide = useHideStore((state) => state.hide);
|
|
|
|
|
const unhide = useHideStore((state) => state.unhide);
|
|
|
|
|
|
|
|
|
|
const hideCallback = useCallback(() => hide(cid), [hide, cid]);
|
|
|
|
|
const unhideCallback = useCallback(() => unhide(cid), [unhide, cid]);
|
|
|
|
|
|
2026-02-26 16:42:36 +08:00
|
|
|
return useMemo(() => ({ hidden, hide: hideCallback, unhide: unhideCallback }), [hidden, hideCallback, unhideCallback]);
|
2024-06-13 17:50:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default useHide;
|