-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(dashboard): add twitch user select shadcn component
[skip ci]
- Loading branch information
Showing
6 changed files
with
1,147 additions
and
365 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,103 @@ | ||
import { useMutation, useQuery } from '@tanstack/vue-query'; | ||
import type { GetResponse as RewardsResponse } from '@twir/api/messages/rewards/rewards'; | ||
import type { | ||
TwitchGetUsersResponse, | ||
TwitchSearchChannelsRequest, | ||
TwitchSearchChannelsResponse, | ||
} from '@twir/api/messages/twitch/twitch'; | ||
import { ComputedRef, isRef, MaybeRef, Ref } from 'vue'; | ||
import { useMutation, useQuery } from '@tanstack/vue-query' | ||
import { isRef } from 'vue' | ||
|
||
import { protectedApiClient, unprotectedApiClient } from '@/api/twirp.js'; | ||
import type { GetResponse as RewardsResponse } from '@twir/api/messages/rewards/rewards' | ||
import type { TwitchGetUsersResponse, TwitchSearchChannelsRequest, TwitchSearchChannelsResponse } from '@twir/api/messages/twitch/twitch' | ||
import type { ComputedRef, MaybeRef, Ref } from 'vue' | ||
|
||
import { protectedApiClient, unprotectedApiClient } from '@/api/twirp.js' | ||
|
||
type TwitchIn = Ref<string[]> | Ref<string> | ComputedRef<string> | ComputedRef<string[]> | string[] | ||
export const useTwitchGetUsers = (opts: { | ||
ids?: TwitchIn, | ||
export function useTwitchGetUsers(opts: { | ||
ids?: TwitchIn | ||
names?: TwitchIn | ||
}) => useQuery({ | ||
queryKey: ['twitch', 'search', 'users', opts.ids, opts.names], | ||
queryFn: async (): Promise<TwitchGetUsersResponse> => { | ||
let ids = isRef(opts?.ids) | ||
? Array.isArray(opts.ids.value) ? opts.ids.value : [opts.ids.value] | ||
: opts?.ids ?? ['']; | ||
let names = isRef(opts?.names) | ||
? Array.isArray(opts.names.value) ? opts.names.value : [opts.names.value] | ||
: opts?.names ?? ['']; | ||
|
||
names = names.filter(n => n !== ''); | ||
ids = ids.filter(n => n !== ''); | ||
|
||
if (ids.length === 0 && names.length === 0) { | ||
return { | ||
users: [], | ||
}; | ||
} | ||
|
||
const call = await unprotectedApiClient.twitchGetUsers({ | ||
ids, | ||
names, | ||
}); | ||
|
||
return call.response; | ||
}, | ||
}); | ||
|
||
export const useTwitchSearchChannels = (params: Ref<TwitchSearchChannelsRequest>) => useQuery({ | ||
queryKey: ['twitch', 'search', 'channels', params], | ||
queryFn: async (): Promise<TwitchSearchChannelsResponse> => { | ||
const rawParams = isRef(params) ? params.value : params; | ||
|
||
if (!rawParams.query) { | ||
return { channels: [] }; | ||
} | ||
|
||
const call = await unprotectedApiClient.twitchSearchChannels(rawParams); | ||
return call.response; | ||
}, | ||
}); | ||
|
||
export const useTwitchRewards = () => useQuery({ | ||
queryKey: ['twitchRewards'], | ||
queryFn: async (): Promise<RewardsResponse> => { | ||
const call = await protectedApiClient.rewardsGet({}); | ||
return call.response; | ||
}, | ||
}); | ||
|
||
export const useTwitchSearchCategories = (query: string | Ref<string>) => useQuery({ | ||
queryKey: ['twitchSearchCategories', query || ''], | ||
queryFn: async () => { | ||
const input = isRef(query) ? query.value : query; | ||
if (!input) return { categories: [] }; | ||
|
||
const call = await protectedApiClient.twitchSearchCategories({ query: input }); | ||
return call.response; | ||
}, | ||
}); | ||
|
||
export const useTwitchGetCategories = (ids: MaybeRef<string[]> | ComputedRef<string[]>) => useQuery({ | ||
queryKey: ['twitchGetCategories', ids || ''], | ||
queryFn: async () => { | ||
const input = isRef(ids) ? ids.value : ids; | ||
if (!input) return { categories: [] }; | ||
|
||
const call = await protectedApiClient.twitchGetCategories({ ids: input }); | ||
return call.response; | ||
}, | ||
}); | ||
|
||
export const twitchSetChannelInformationMutation = () => useMutation({ | ||
mutationKey: ['twitchSetChannelInformation'], | ||
mutationFn: async (req: { categoryId: string, title: string }) => { | ||
await protectedApiClient.twitchSetChannelInformation(req); | ||
}, | ||
}); | ||
}) { | ||
return useQuery({ | ||
queryKey: ['twitch', 'search', 'users', opts.ids, opts.names], | ||
queryFn: async (): Promise<TwitchGetUsersResponse> => { | ||
let ids = isRef(opts?.ids) | ||
? Array.isArray(opts.ids.value) ? opts.ids.value : [opts.ids.value] | ||
: opts?.ids ?? [''] | ||
let names = isRef(opts?.names) | ||
? Array.isArray(opts.names.value) ? opts.names.value : [opts.names.value] | ||
: opts?.names ?? [''] | ||
|
||
names = names.filter(n => n !== '') | ||
ids = ids.filter(n => n !== '') | ||
|
||
if (ids.length === 0 && names.length === 0) { | ||
return { | ||
users: [], | ||
} | ||
} | ||
|
||
const call = await unprotectedApiClient.twitchGetUsers({ | ||
ids, | ||
names, | ||
}) | ||
|
||
return call.response | ||
}, | ||
}) | ||
} | ||
|
||
export function useTwitchSearchChannels(params: Ref<TwitchSearchChannelsRequest>) { | ||
return useQuery({ | ||
queryKey: ['twitch', 'search', 'channels', params], | ||
queryFn: async (): Promise<TwitchSearchChannelsResponse> => { | ||
const rawParams = isRef(params) ? params.value : params | ||
|
||
if (!rawParams.query) { | ||
return { channels: [] } | ||
} | ||
|
||
const call = await unprotectedApiClient.twitchSearchChannels(rawParams) | ||
return call.response | ||
}, | ||
}) | ||
} | ||
|
||
export function useTwitchRewards() { | ||
return useQuery({ | ||
queryKey: ['twitchRewards'], | ||
queryFn: async (): Promise<RewardsResponse> => { | ||
const call = await protectedApiClient.rewardsGet({}) | ||
return call.response | ||
}, | ||
}) | ||
} | ||
|
||
export function useTwitchSearchCategories(query: string | Ref<string>) { | ||
return useQuery({ | ||
queryKey: ['twitchSearchCategories', query || ''], | ||
queryFn: async () => { | ||
const input = isRef(query) ? query.value : query | ||
if (!input) return { categories: [] } | ||
|
||
const call = await protectedApiClient.twitchSearchCategories({ query: input }) | ||
return call.response | ||
}, | ||
}) | ||
} | ||
|
||
export function useTwitchGetCategories(ids: MaybeRef<string[]> | ComputedRef<string[]>) { | ||
return useQuery({ | ||
queryKey: ['twitchGetCategories', ids || ''], | ||
queryFn: async () => { | ||
const input = isRef(ids) ? ids.value : ids | ||
if (!input) return { categories: [] } | ||
|
||
const call = await protectedApiClient.twitchGetCategories({ ids: input }) | ||
return call.response | ||
}, | ||
}) | ||
} | ||
|
||
export function twitchSetChannelInformationMutation() { | ||
return useMutation({ | ||
mutationKey: ['twitchSetChannelInformation'], | ||
mutationFn: async (req: { categoryId: string, title: string }) => { | ||
await protectedApiClient.twitchSetChannelInformation(req) | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.