mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
bitsocialhq/pkc-react-hooks is a fork of plebbit-react-hooks that we're maintaining temporarily as an attempt to modernize the original repository using AI tools, possibly merging changes back into plebbit-react-hooks, which should also be rebranded to pkc-react-hooks eventually, under the org pkcprotocol.
35 lines
1.3 KiB
TypeScript
35 lines
1.3 KiB
TypeScript
import { useEffect } from 'react';
|
|
import { useSubplebbitStats } from '@bitsocialhq/pkc-react-hooks';
|
|
import { create } from 'zustand';
|
|
|
|
export type SubplebbitsStatsState = {
|
|
subplebbitsStats: { [subplebbitAddress: string]: any };
|
|
setSubplebbitStats: (subplebbitAddress: string, stats: any) => void;
|
|
};
|
|
|
|
export const useSubplebbitsStatsStore = create<SubplebbitsStatsState>((set) => ({
|
|
subplebbitsStats: {},
|
|
setSubplebbitStats: (subplebbitAddress: string, subplebbitStats: any) =>
|
|
set((state) => ({
|
|
subplebbitsStats: { ...state.subplebbitsStats, [subplebbitAddress]: subplebbitStats },
|
|
})),
|
|
}));
|
|
|
|
/**
|
|
* Component that fetches stats for a single subplebbit and stores them.
|
|
* Render one of these for each subplebbit you want to track stats for.
|
|
*/
|
|
export const SubplebbitStatsCollector = ({ subplebbitAddress }: { subplebbitAddress: string }) => {
|
|
const stats = useSubplebbitStats({ subplebbitAddress });
|
|
const setSubplebbitStats = useSubplebbitsStatsStore((state) => state.setSubplebbitStats);
|
|
|
|
useEffect(() => {
|
|
// Only update store when we have actual stats (not just loading state)
|
|
if (stats && stats.allPostCount !== undefined) {
|
|
setSubplebbitStats(subplebbitAddress, stats);
|
|
}
|
|
}, [stats, subplebbitAddress, setSubplebbitStats]);
|
|
|
|
return null; // This is a data-fetching component, renders nothing
|
|
};
|