diff --git a/frontend/src/ts/collections/custom-themes.ts b/frontend/src/ts/collections/custom-themes.ts index 70bc23792f76..73971a446f50 100644 --- a/frontend/src/ts/collections/custom-themes.ts +++ b/frontend/src/ts/collections/custom-themes.ts @@ -31,11 +31,10 @@ const customThemesCollection = createCollection( staleTime: Infinity, startSync: true, queryKey: queryKeys.root(), - queryClient, + enabled: isAuthenticated, getKey: (it) => it._id, queryFn: async () => { - if (!isAuthenticated()) return [] as CustomThemeItem[]; const response = await Ape.users.getCustomThemes(); if (response.status !== 200) { diff --git a/frontend/src/ts/collections/inbox.ts b/frontend/src/ts/collections/inbox.ts index ea6f9fa6066c..79c3a28dd0f5 100644 --- a/frontend/src/ts/collections/inbox.ts +++ b/frontend/src/ts/collections/inbox.ts @@ -40,7 +40,7 @@ const inboxCollection = createCollection( queryCollectionOptions({ staleTime: 1000 * 60 * 5, queryKey: queryKeys.root(), - + enabled: isAuthenticated, queryFn: async () => { const addStatus = (item: MonkeyMail): InboxItem => ({ ...item, diff --git a/frontend/src/ts/collections/presets.ts b/frontend/src/ts/collections/presets.ts index 334085f427ef..0748fc7762aa 100644 --- a/frontend/src/ts/collections/presets.ts +++ b/frontend/src/ts/collections/presets.ts @@ -22,6 +22,7 @@ const queryKeys = { // oxlint-disable-next-line typescript/explicit-function-return-type export function usePresetsLiveQuery() { return useLiveQuery((q) => { + if (!isAuthenticated()) return undefined; return q .from({ preset: presetsCollection }) .orderBy(({ preset }) => preset.name, "asc"); @@ -33,10 +34,9 @@ const presetsCollection = createCollection( staleTime: Infinity, queryKey: queryKeys.root(), queryClient, + enabled: isAuthenticated, getKey: (it) => it._id, queryFn: async () => { - if (!isAuthenticated()) return []; - const response = await Ape.presets.get(); if (response.status !== 200) { diff --git a/frontend/src/ts/collections/result-filter-presets.ts b/frontend/src/ts/collections/result-filter-presets.ts index 1ef6fb3c47ad..5aad875f992d 100644 --- a/frontend/src/ts/collections/result-filter-presets.ts +++ b/frontend/src/ts/collections/result-filter-presets.ts @@ -14,6 +14,7 @@ import { } from "../utils/strings"; import { applyIdWorkaround, tempId } from "./utils/misc"; import { fetchUserFromApi } from "../ape/user"; +import { isAuthenticated } from "../states/core"; const queryKeys = { root: () => [...baseKey("resultFilterPresets", { isUserSpecific: true })], @@ -24,8 +25,8 @@ const resultFilterPresetsCollection = createCollection( staleTime: Infinity, startSync: true, queryKey: queryKeys.root(), - queryClient, + enabled: isAuthenticated, getKey: (it) => it._id, queryFn: async () => { const userData = await fetchUserFromApi(); @@ -43,9 +44,10 @@ const resultFilterPresetsCollection = createCollection( // oxlint-disable-next-line typescript/explicit-function-return-type export function useResultFilterPresetsLiveQuery() { - return useLiveQuery((q) => - q.from({ presets: resultFilterPresetsCollection }), - ); + return useLiveQuery((q) => { + if (!isAuthenticated()) return undefined; + return q.from({ presets: resultFilterPresetsCollection }); + }); } type ActionType = { diff --git a/frontend/src/ts/collections/results.ts b/frontend/src/ts/collections/results.ts index eea9274e41e0..3650bef1ef1a 100644 --- a/frontend/src/ts/collections/results.ts +++ b/frontend/src/ts/collections/results.ts @@ -89,6 +89,7 @@ export function useResultStatsLiveQuery( options?: { lastTen?: true } | { groupByDay?: true }, ) { return useLiveQuery((q) => { + if (!isAuthenticated()) return undefined; const state = queryState(); if (state === undefined) return undefined; @@ -164,6 +165,7 @@ export function useResultsLiveQuery(options: { limit: Accessor; }) { return useLiveQuery((q) => { + if (!isAuthenticated()) return undefined; const state = options.queryState(); const sorting = options.sorting(); const limit = options.limit(); @@ -217,8 +219,8 @@ const resultsCollection = createCollection( queryCollectionOptions({ staleTime: Infinity, queryKey: queryKeys.root(), + enabled: isAuthenticated, queryFn: async () => { - if (!isAuthenticated()) return []; const tagIds = await getTagsOnce(); const knownTagIds = new Set([...tagIds.map((it) => it._id)]); //const options = parseLoadSubsetOptions(ctx.meta?.loadSubsetOptions); @@ -603,6 +605,7 @@ export function useUserAverage10LiveQuery(options: { return useLiveQuery((q) => { //disable query + if (!isAuthenticated()) return undefined; if (!options.isEnabled()) return undefined; return q diff --git a/frontend/src/ts/collections/tags.ts b/frontend/src/ts/collections/tags.ts index 037f8895f0cb..01872f4a3913 100644 --- a/frontend/src/ts/collections/tags.ts +++ b/frontend/src/ts/collections/tags.ts @@ -25,6 +25,7 @@ import { Language } from "@monkeytype/schemas/languages"; import { applyIdWorkaround, isTempId, tempId } from "./utils/misc"; import { fetchUserFromApi } from "../ape/user"; import { updateTagsInFilterStorage } from "../states/result-filters"; +import { isAuthenticated } from "../states/core"; export type TagItem = UserTag & { active: boolean }; @@ -37,6 +38,7 @@ const tagsCollection = createCollection( staleTime: Infinity, queryKey: queryKeys.root(), queryClient, + enabled: isAuthenticated, getKey: (it) => it._id, queryFn: async () => { const activeIds = activeTagsLS.get(); @@ -44,13 +46,16 @@ const tagsCollection = createCollection( if (userData === undefined) return []; - return (userData.tags ?? []) + const results = (userData.tags ?? []) .map((tag) => ({ ...tag, name: tag.name.replace(/_/g, " "), active: activeIds.includes(tag._id), })) .map(applyIdWorkaround); + + updateTagsInFilterStorage(results.map((it) => it._id) ?? []); + return results; }, }), ); @@ -58,6 +63,7 @@ const tagsCollection = createCollection( // oxlint-disable-next-line typescript/explicit-function-return-type export function useTagsLiveQuery() { return useLiveQuery((q) => { + if (!isAuthenticated()) return undefined; return q .from({ tag: tagsCollection }) .orderBy(({ tag }) => tag.name, "asc"); @@ -84,6 +90,7 @@ export async function getTagsOnce() { // oxlint-disable-next-line typescript/explicit-function-return-type export function useActiveTagsLiveQuery() { return useLiveQuery((q) => { + if (!isAuthenticated()) return undefined; return q .from({ tag: tagsCollection }) .where(({ tag }) => eq(tag.active, true)) diff --git a/frontend/src/ts/components/pages/account/Charts.tsx b/frontend/src/ts/components/pages/account/Charts.tsx index 4ae7cd06411b..273caceb71a4 100644 --- a/frontend/src/ts/components/pages/account/Charts.tsx +++ b/frontend/src/ts/components/pages/account/Charts.tsx @@ -8,6 +8,7 @@ import { } from "../../../collections/results"; import { type TagItem, useTagsLiveQuery } from "../../../collections/tags"; import { getConfig } from "../../../config/store"; +import { isAuthenticated } from "../../../states/core"; import { FaSolidIcon } from "../../../types/font-awesome"; import { Formatting } from "../../../utils/format"; import { @@ -34,6 +35,7 @@ export function Charts(props: { const tags = useTagsLiveQuery(); const resultsQuery = useLiveQuery((q) => { + if (!isAuthenticated()) return undefined; const state = props.queryState(); if (state === undefined) return undefined; return q diff --git a/frontend/src/ts/db.ts b/frontend/src/ts/db.ts index 7ccc52509428..5e3cc3d9fd05 100644 --- a/frontend/src/ts/db.ts +++ b/frontend/src/ts/db.ts @@ -38,7 +38,6 @@ import { XpBreakdown } from "@monkeytype/schemas/results"; import { setXpBarData } from "./states/header"; import { FunboxMetadata } from "@monkeytype/funbox"; import { __nonReactive } from "./collections/tags"; -import { updateTagsInFilterStorage } from "./states/result-filters"; import { fetchUserFromApi } from "./ape/user"; import { SnapshotInitError } from "./utils/snapshot-init-error"; @@ -168,8 +167,6 @@ export async function initSnapshot(): Promise { snap.lbMemory = userData.lbMemory; } - updateTagsInFilterStorage(userData.tags?.map((it) => it._id) ?? []); - snap.connections = convertConnections(connectionsData); dbSnapshot = snap;