2026-01-14 22:23:11 +01:00
|
|
|
import { useEffect } from 'react';
|
2026-03-05 17:14:23 +08:00
|
|
|
import { useSubplebbitStats } from '@bitsocialhq/bitsocial-react-hooks';
|
2024-05-14 22:26:50 +02:00
|
|
|
import { create } from 'zustand';
|
|
|
|
|
|
|
|
|
|
export type SubplebbitsStatsState = {
|
|
|
|
|
subplebbitsStats: { [subplebbitAddress: string]: any };
|
2026-01-14 22:23:11 +01:00
|
|
|
setSubplebbitStats: (subplebbitAddress: string, stats: any) => void;
|
2024-05-14 22:26:50 +02:00
|
|
|
};
|
|
|
|
|
|
2026-01-14 22:23:11 +01:00
|
|
|
export const useSubplebbitsStatsStore = create<SubplebbitsStatsState>((set) => ({
|
2024-05-14 22:26:50 +02:00
|
|
|
subplebbitsStats: {},
|
|
|
|
|
setSubplebbitStats: (subplebbitAddress: string, subplebbitStats: any) =>
|
|
|
|
|
set((state) => ({
|
|
|
|
|
subplebbitsStats: { ...state.subplebbitsStats, [subplebbitAddress]: subplebbitStats },
|
|
|
|
|
})),
|
|
|
|
|
}));
|
|
|
|
|
|
2026-01-14 22:23:11 +01:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
};
|