cordis/src/client/webapp/elements/require/guilds-manager-subscriptions.ts
Michael Peters 0d36daacca guilds-manager element
Almost to the root node!
2021-12-29 21:16:19 -06:00

26 lines
699 B
TypeScript

import { useCallback, useEffect, useMemo, useState } from 'react';
import * as uuid from 'uuid';
import CombinedGuild from '../../guild-combined';
import GuildsManager from "../../guilds-manager";
export 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 ];
}