26 lines
699 B
TypeScript
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 ];
|
||
|
}
|