cordis/archive/guilds-manager-subscriptions.ts
2022-02-06 19:24:25 -06:00

27 lines
754 B
TypeScript

import { useCallback, useEffect, useMemo, useState } from 'react';
import * as uuid from 'uuid';
import CombinedGuild from '../../guild-combined';
import GuildsManager from "../../guilds-manager";
// Subscribes to changes in the list of guilds in the manager
function useGuildListSubscription(guildsManager: GuildsManager): [ guilds: CombinedGuild[] ] {
const [ refreshId, setRefreshId ] = useState<string>(uuid.v4());
const refresh = useCallback(() => {
setRefreshId(uuid.v4());
}, []);
useEffect(() => {
guildsManager.on('update-guilds', refresh);
return () => {
guildsManager.off('update-guilds', refresh);
}
}, []);
const guilds = useMemo(() => {
return guildsManager.guilds.slice();
}, [ refreshId ]);
return [ guilds ];
}