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