Files
5chan/src/stores/use-communities-loading-start-timestamps-store.ts
T
Tommaso CasaburiandGitHub d7703953fb refactor: migrate 5chan to the community hooks API (#1073)
* refactor(community-api): migrate 5chan to community hooks

* fix(review): address PR feedback

* fix(review): preserve legacy board context fallbacks

* fix(review): address latest bot feedback

* fix(review): use communityAddress in edit menu privileges
2026-03-13 13:25:10 +08:00

47 lines
1.6 KiB
TypeScript

import { useEffect, useMemo } from 'react';
import { create } from 'zustand';
interface CommunitiesLoadingStartTimestampsState {
timestamps: Record<string, number>;
addCommunities: (communityAddresses: string[]) => void;
}
const useCommunitiesLoadingStartTimestampsStore = create<CommunitiesLoadingStartTimestampsState>((set, get) => ({
timestamps: {},
addCommunities: (communityAddresses) => {
const { timestamps } = get();
const newTimestamps: Record<string, number> = {};
communityAddresses.forEach((communityAddress) => {
if (!timestamps[communityAddress]) {
newTimestamps[communityAddress] = Math.round(Date.now() / 1000);
}
});
if (Object.keys(newTimestamps).length) {
set((state) => ({ timestamps: { ...state.timestamps, ...newTimestamps } }));
}
},
}));
const useCommunitiesLoadingStartTimestamps = (communityAddresses?: string[]) => {
const timestampsStore = useCommunitiesLoadingStartTimestampsStore((state) => state.timestamps);
const addCommunities = useCommunitiesLoadingStartTimestampsStore((state) => state.addCommunities);
useEffect(() => {
if (communityAddresses) {
addCommunities(communityAddresses);
}
}, [communityAddresses, addCommunities]);
const communitiesLoadingStartTimestamps = useMemo(() => {
return communityAddresses?.map((communityAddress) => timestampsStore[communityAddress]) || [];
}, [timestampsStore, communityAddresses]);
return communitiesLoadingStartTimestamps;
};
export const useSubplebbitsLoadingStartTimestamps = useCommunitiesLoadingStartTimestamps;
export default useCommunitiesLoadingStartTimestamps;