Skip to content

Commit

Permalink
refactor: use skipTokens for typesafe disabling of query
Browse files Browse the repository at this point in the history
  • Loading branch information
6lr61 committed Sep 18, 2024
1 parent f46f6bf commit 4c23ff6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
9 changes: 5 additions & 4 deletions src/hooks/useBadges.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery } from "@tanstack/react-query";
import { skipToken, useQuery } from "@tanstack/react-query";
import { getBadges, type Badge, type BadgeSet } from "../utils/api/getBadges";
import { useContext } from "react";
import { AuthContext } from "../contexts/auth-state/AuthContext";
Expand All @@ -19,10 +19,11 @@ export function useBadges(userId?: string) {
const { authState } = useContext(AuthContext);

const { data } = useQuery({
enabled: !!authState,
enabled: Boolean(authState),
queryKey: ["twitchBadges", userId],
// @ts-expect-error authState is not undefinend when query is exectued
queryFn: () => getBadges(authState, userId ?? authState?.user.id),
queryFn: authState
? () => getBadges(authState, userId ?? authState.user.id)
: skipToken,
select: makeBadgeMap,
});

Expand Down
31 changes: 16 additions & 15 deletions src/hooks/useBttvEmotes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery } from "@tanstack/react-query";
import { skipToken, useQuery } from "@tanstack/react-query";
import { useMemo } from "react";

interface BetterTTVEmote {
Expand Down Expand Up @@ -96,21 +96,22 @@ export function useBttvEmotes(userId?: string) {
});

const { data: channel } = useQuery({
enabled: !!userId,
enabled: Boolean(userId),
queryKey: ["bttvChannelEmotes", userId],
queryFn: () =>
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
fetch(`https://api.betterttv.net/3/cached/users/twitch/${userId}`)
.then((response) => response.json())
.then(
({
channelEmotes,
sharedEmotes,
}: {
channelEmotes: BetterTTVEmote[];
sharedEmotes: BetterTTVEmote[];
}) => [...channelEmotes, ...sharedEmotes]
),
queryFn: userId
? () =>
fetch(`https://api.betterttv.net/3/cached/users/twitch/${userId}`)
.then((response) => response.json())
.then(
({
channelEmotes,
sharedEmotes,
}: {
channelEmotes: BetterTTVEmote[];
sharedEmotes: BetterTTVEmote[];
}) => [...channelEmotes, ...sharedEmotes]
)
: skipToken,
select: makeChannelFragments,
});

Expand Down
9 changes: 5 additions & 4 deletions src/hooks/useGetProfile.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { useContext } from "react";
import { AuthContext } from "../contexts/auth-state/AuthContext";
import { getUser } from "../utils/api/getUser";
import { useQuery } from "@tanstack/react-query";
import { skipToken, useQuery } from "@tanstack/react-query";

export function useGetProfile(login: string) {
const { authState } = useContext(AuthContext);

return useQuery({
enabled: !!authState,
enabled: Boolean(authState),
queryKey: ["userProfilePicture", login],
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
queryFn: () => getUser(authState!.token.value, authState!.client.id, login),
queryFn: authState
? () => getUser(authState.token.value, authState.client.id, login)
: skipToken,
});
}
27 changes: 14 additions & 13 deletions src/hooks/useSevenTvEmotes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery } from "@tanstack/react-query";
import { skipToken, useQuery } from "@tanstack/react-query";
import { useMemo } from "react";

enum EmoteSetFlag {
Expand Down Expand Up @@ -239,19 +239,20 @@ export function useSevenTvEmotes(userId?: string) {
});

const { data: channel } = useQuery({
enabled: !!userId,
enabled: Boolean(userId),
queryKey: ["sevenTvChannelEmotes", userId],
queryFn: () =>
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
fetch(`https://7tv.io/v3/users/twitch/${userId}`)
.then((response) => response.json())
.then(
({
emote_set: { emotes },
}: {
emote_set: { emotes: SevenTvEmoteModel[] };
}) => emotes
),
queryFn: userId
? () =>
fetch(`https://7tv.io/v3/users/twitch/${userId}`)
.then((response) => response.json())
.then(
({
emote_set: { emotes },
}: {
emote_set: { emotes: SevenTvEmoteModel[] };
}) => emotes
)
: skipToken,
select: makeChannelFragments,
});

Expand Down

0 comments on commit 4c23ff6

Please sign in to comment.