import { followClient } from "@client/lib/api-fetch" import { getProviders } from "@client/lib/auth" import { getHydrateData } from "@client/lib/helper" import type { LoginHydrateData } from "@client/pages/(login)/login/metadata" import { isBizId, sortByAlphabet } from "@follow/utils/utils" import type { InboxSubscriptionResponse, ListSubscriptionResponse, SubscriptionWithFeed, } from "@follow-app/client-sdk" import { useQuery } from "@tanstack/react-query" type GetUserSubscriptionsResponse = ( | SubscriptionWithFeed | ListSubscriptionResponse | InboxSubscriptionResponse )[] const UN_CATEGORIZED = "Uncategorized" const groupSubscriptions = ( subscriptions: GetUserSubscriptionsResponse, ): Record => { const groupFolder = {} as Record for (const subscription of subscriptions.filter((s) => !s.isPrivate) || []) { if (!subscription.category && "feeds" in subscription) { subscription.category = UN_CATEGORIZED } if (subscription.category) { if (!groupFolder[subscription.category]) { groupFolder[subscription.category] = [] } groupFolder[subscription.category]!.push(subscription) } } // Move the un-categorized to the last if (Object.keys(groupFolder)[0] === UN_CATEGORIZED) { const temp = groupFolder[UN_CATEGORIZED] delete groupFolder[UN_CATEGORIZED] Reflect.defineProperty(groupFolder, UN_CATEGORIZED, { value: temp?.sort((a, b) => (a.title && b.title ? sortByAlphabet(a.title, b.title) : 0)) || [], writable: true, configurable: true, enumerable: true, }) } return groupFolder } const fetchUserSubscriptions = async (userId: string | undefined) => { const res = await followClient.api.subscriptions.get({ userId, }) return res.data } export const useUserSubscriptionsQuery = (userId: string | undefined) => { return useQuery({ queryKey: ["subscriptions", "group", userId], queryFn: async () => { return fetchUserSubscriptions(userId) }, select: groupSubscriptions, enabled: !!userId, initialData: getHydrateData(`subscriptions.$get,query:userId=${userId}`) as any as Awaited< ReturnType >, }) } export const fetchUser = async (handleOrId: string | undefined) => { const handle = isBizId(handleOrId || "") ? handleOrId : `${handleOrId}`.startsWith("@") ? `${handleOrId}`.slice(1) : handleOrId const res = await followClient.api.profiles.getProfile({ id: handleOrId, handle }) return res.data } export type User = Awaited> export const useUserQuery = (handleOrId: string | undefined) => { return useQuery({ queryKey: ["profiles", handleOrId], queryFn: () => fetchUser(handleOrId), enabled: !!handleOrId, initialData: getHydrateData(`profiles.$get,query:id=${handleOrId}`) as any as User, }) } export interface AuthProvider { name: string id: string color: string icon: string icon64: string iconDark64?: string } const getTypedProviders = async () => { const providers = await getProviders() return providers.data as Record } export const useAuthProviders = () => { return useQuery({ queryKey: ["providers"], queryFn: async () => getTypedProviders(), initialData: getHydrateData(`betterAuth`) as LoginHydrateData, }) } export { getTypedProviders as getAuthProviders }