Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions frontend/src/ts/collections/custom-themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/ts/collections/inbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/ts/collections/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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) {
Expand Down
10 changes: 6 additions & 4 deletions frontend/src/ts/collections/result-filter-presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })],
Expand All @@ -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();
Expand All @@ -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 = {
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/ts/collections/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -164,6 +165,7 @@ export function useResultsLiveQuery(options: {
limit: Accessor<number>;
}) {
return useLiveQuery((q) => {
if (!isAuthenticated()) return undefined;
const state = options.queryState();
const sorting = options.sorting();
const limit = options.limit();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -603,6 +605,7 @@ export function useUserAverage10LiveQuery(options: {

return useLiveQuery((q) => {
//disable query
if (!isAuthenticated()) return undefined;
if (!options.isEnabled()) return undefined;

return q
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/ts/collections/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand All @@ -37,27 +38,32 @@ const tagsCollection = createCollection(
staleTime: Infinity,
queryKey: queryKeys.root(),
queryClient,
enabled: isAuthenticated,
getKey: (it) => it._id,
queryFn: async () => {
const activeIds = activeTagsLS.get();
const userData = await fetchUserFromApi();

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;
},
}),
);

// 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");
Expand All @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/ts/components/pages/account/Charts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions frontend/src/ts/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -168,8 +167,6 @@ export async function initSnapshot(): Promise<Snapshot | false> {
snap.lbMemory = userData.lbMemory;
}

updateTagsInFilterStorage(userData.tags?.map((it) => it._id) ?? []);

snap.connections = convertConnections(connectionsData);
dbSnapshot = snap;

Expand Down
Loading