query cache settings, somehow still fetching logs again sometimes

This commit is contained in:
gempir 2020-11-08 16:04:08 +01:00
parent 6683af7f5d
commit ed7c5023df
8 changed files with 23 additions and 9 deletions

View file

@ -1,5 +1,6 @@
import { Button, TextField } from "@material-ui/core";
import React, { FormEvent, useContext } from "react";
import { useQueryCache } from "react-query";
import styled from "styled-components";
import { store } from "../store";
import { Settings } from "./Settings";
@ -28,6 +29,7 @@ const FiltersWrapper = styled.div`
export function Filters() {
const { setCurrents, state } = useContext(store);
const queryCache = useQueryCache();
const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
@ -35,7 +37,11 @@ export function Filters() {
if (e.target instanceof HTMLFormElement) {
const data = new FormData(e.target);
setCurrents(data.get("channel") as string | null, data.get("username") as string | null);
const channel = data.get("channel") as string | null;
const username = data.get("username") as string | null;
queryCache.invalidateQueries(`${channel}:${username}`);
setCurrents(channel, username);
}
};

View file

@ -1,7 +1,7 @@
import { useContext } from "react";
import { useQuery } from "react-query";
import { isUserId } from "../services/isUserId";
import { store } from "../store";
import { QueryDefaults, store } from "../store";
export type AvailableLogs = Array<{ month: string, year: string }>;
@ -33,7 +33,7 @@ export function useAvailableLogs(channel: string | null, username: string | null
}
return [];
});
}, QueryDefaults);
return data ?? [];
}

View file

@ -1,4 +1,5 @@
import { useQuery } from "react-query";
import { QueryDefaults } from "../store";
import { BttvChannelEmotesResponse } from "../types/Bttv";
import { ThirdPartyEmote } from "../types/ThirdPartyEmote";
@ -11,7 +12,7 @@ export function useBttvChannelEmotes(channelId: string): Array<ThirdPartyEmote>
return fetch(`https://api.betterttv.net/3/cached/users/twitch/${channelId}`).then(res =>
res.json() as Promise<BttvChannelEmotesResponse>
);
});
}, QueryDefaults);
if (isLoading) {
return [];

View file

@ -1,4 +1,5 @@
import { useQuery } from "react-query";
import { QueryDefaults } from "../store";
import { BttvGlobalEmotesResponse } from "../types/Bttv";
import { ThirdPartyEmote } from "../types/ThirdPartyEmote";
@ -7,7 +8,7 @@ export function useBttvGlobalEmotes(): Array<ThirdPartyEmote> {
return fetch("https://api.betterttv.net/3/cached/emotes/global").then(res =>
res.json() as Promise<BttvGlobalEmotesResponse>
);
});
}, QueryDefaults);
if (isLoading || !data) {
return [];

View file

@ -1,4 +1,5 @@
import { useQuery } from "react-query";
import { QueryDefaults } from "../store";
import { EmoteSet, FfzChannelEmotesResponse } from "../types/Ffz";
import { ThirdPartyEmote } from "../types/ThirdPartyEmote";
@ -11,7 +12,7 @@ export function useFfzChannelEmotes(channelId: string): Array<ThirdPartyEmote> {
return fetch(`https://api.frankerfacez.com/v1/room/id/${channelId}`).then(res =>
res.json() as Promise<FfzChannelEmotesResponse>
);
});
}, QueryDefaults);
if (isLoading || !data?.sets) {
return [];

View file

@ -1,4 +1,5 @@
import { useQuery } from "react-query";
import { QueryDefaults } from "../store";
import { EmoteSet, FfzGlobalEmotesResponse } from "../types/Ffz";
import { ThirdPartyEmote } from "../types/ThirdPartyEmote";
@ -7,7 +8,7 @@ export function useFfzGlobalEmotes(): Array<ThirdPartyEmote> {
return fetch("https://api.frankerfacez.com/v1/set/global").then(res =>
res.json() as Promise<FfzGlobalEmotesResponse>
);
});
}, QueryDefaults);
if (isLoading || !data?.sets) {
return [];

View file

@ -1,7 +1,7 @@
import { useContext } from "react";
import { useQuery } from "react-query";
import { isUserId } from "../services/isUserId";
import { store } from "../store";
import { QueryDefaults, store } from "../store";
import { Emote, LogMessage, UserLogResponse } from "../types/log";
@ -34,7 +34,7 @@ export function useLog(channel: string, username: string, year: string, month: s
}
return [];
});
}, QueryDefaults);
return data ?? [];
}

View file

@ -78,3 +78,7 @@ const StateProvider = ({ children }: { children: JSX.Element }): JSX.Element =>
};
export { store, StateProvider };
export const QueryDefaults = {
staleTime: 5 * 60 * 1000,
};