add an instant cache refresh to the editor

This commit is contained in:
41666 2022-02-04 11:59:11 -05:00
parent 68b2b7323b
commit 0836d548b2
6 changed files with 66 additions and 11 deletions

View file

@ -19,12 +19,14 @@ type GuildContextT = {
) => Promise<PresentableGuild | null | false>;
getGuildSlug: (id: string) => Promise<GuildSlug | null>;
uncacheGuild: (id: string) => void;
uncacheRemoteGuild: (id: string) => Promise<void>;
};
export const GuildContext = React.createContext<GuildContextT>({
getFullGuild: (id: string) => Promise.reject(new Error('Not implemented')),
getGuildSlug: (id: string) => Promise.reject(new Error('Not implemented')),
uncacheGuild: (id: string) => {},
uncacheGuild: (id: string) => void 0,
uncacheRemoteGuild: (id: string) => Promise.resolve(void 0),
});
export const useGuildContext = () => React.useContext(GuildContext);
@ -108,6 +110,9 @@ export const GuildProvider = (props: { children: React.ReactNode }) => {
uncacheGuild: (id: string) => {
sessionStorage.removeItem(`guild-${id}`);
},
uncacheRemoteGuild: async (id: string) => {
await authedFetch(`/guilds/${id}/cache`, { method: 'DELETE' });
},
};
return (

View file

@ -25,7 +25,7 @@ const Editor = (props: EditorProps) => {
const { authedFetch } = useAuthedFetch();
const { pushRecentGuild } = useRecentGuilds();
const appShellProps = useAppShellProps();
const { getFullGuild, uncacheGuild } = useGuildContext();
const { getFullGuild, uncacheGuild, uncacheRemoteGuild } = useGuildContext();
const [guild, setGuild] = React.useState<PresentableGuild | null | false>(null);
const [pending, setPending] = React.useState(false);
@ -52,8 +52,10 @@ const Editor = (props: EditorProps) => {
setGuild(guild);
};
fetchGuild();
}, [serverID, getFullGuild]);
if (guild === null) {
fetchGuild();
}
}, [serverID, getFullGuild, guild]);
React.useCallback((serverID) => pushRecentGuild(serverID), [pushRecentGuild])(serverID);
@ -105,10 +107,32 @@ const Editor = (props: EditorProps) => {
setPending(false);
};
const onRefreshCache = async () => {
if (
Number(sessionStorage.getItem('rp_editor_last_refresh')) >
Date.now() - 1000 * 60 * 2
) {
console.error('Slow down there partner!');
return;
}
console.log('Refreshing cache');
uncacheGuild(serverID);
await uncacheRemoteGuild(serverID);
sessionStorage.setItem('rp_editor_last_pull', String(Date.now()));
sessionStorage.setItem('rp_editor_last_refresh', String(Date.now()));
setGuild(null);
};
return (
<>
<Title title={`Editing ${guild.guild.name} - Roleypoly`} />
<EditorTemplate {...appShellProps} guild={guild} onGuildChange={onGuildChange} />
<EditorTemplate
{...appShellProps}
guild={guild}
onGuildChange={onGuildChange}
onRefreshCache={onRefreshCache}
/>
</>
);
};