import axios from "axios"; import { DateTime } from "luxon"; import { QueryClient } from "react-query"; type UserInfo = { name: string; patreon: boolean; timezone: string | null; }; export type GuildInfo = { id: string; name: string; }; type EmbedField = { title: string; value: string; inline: boolean; }; export type Reminder = { attachment: string | null; attachment_name: string | null; avatar: string | null; channel: string; content: string; embed_author: string; embed_author_url: string | null; embed_color: number; embed_description: string; embed_footer: string; embed_footer_url: string | null; embed_image_url: string | null; embed_thumbnail_url: string | null; embed_title: string; embed_fields: EmbedField[] | null; enabled: boolean; expires: DateTime | null; interval_seconds: number | null; interval_days: number | null; interval_months: number | null; name: string; restartable: boolean; tts: boolean; uid: string; username: string; utc_time: DateTime; }; type ChannelInfo = { id: string; name: string; }; type RoleInfo = { id: string; name: string; }; type Template = { id: number; name: string; attachment: string | null; attachment_name: string | null; avatar: string | null; channel: string; content: string; embed_author: string; embed_author_url: string | null; embed_color: number; embed_description: string; embed_footer: string; embed_footer_url: string | null; embed_image_url: string | null; embed_thumbnail_url: string | null; embed_title: string; embed_fields: EmbedField[] | null; }; const USER_INFO_STALE_TIME = 120_000; const GUILD_INFO_STALE_TIME = 300_000; const OTHER_STALE_TIME = 15_000; export const fetchUserInfo = () => ({ queryKey: ["USER_INFO"], queryFn: () => axios.get("/dashboard/api/user").then((resp) => resp.data) as Promise, staleTime: USER_INFO_STALE_TIME, }); export const fetchUserGuilds = () => ({ queryKey: ["USER_GUILDS"], queryFn: () => axios.get("/dashboard/api/user/guilds").then((resp) => resp.data) as Promise, staleTime: USER_INFO_STALE_TIME, }); export const fetchGuildChannels = (guild: string) => ({ queryKey: ["GUILD_CHANNELS", guild], queryFn: () => axios.get(`/dashboard/api/guild/${guild}/channels`).then((resp) => resp.data) as Promise< ChannelInfo[] >, staleTime: GUILD_INFO_STALE_TIME, }); export const fetchGuildRoles = (guild: string) => ({ queryKey: ["GUILD_ROLES", guild], queryFn: () => axios.get(`/dashboard/api/guild/${guild}/roles`).then((resp) => resp.data) as Promise< RoleInfo[] >, staleTime: GUILD_INFO_STALE_TIME, }); export const fetchGuildReminders = (guild: string) => ({ queryKey: ["GUILD_REMINDERS", guild], queryFn: () => axios .get(`/dashboard/api/guild/${guild}/reminders`) .then((resp) => resp.data) .then((value) => value.map((reminder) => ({ ...reminder, utc_time: DateTime.fromISO(reminder.utc_time), expires: reminder.expires === null ? null : DateTime.fromISO(reminder.expires), })), ) as Promise, staleTime: OTHER_STALE_TIME, }); export const mutateGuildReminder = (guild: string) => ({ mutationFn: (reminder: Reminder) => axios.patch(`/dashboard/api/guild/${guild}/reminders`, { ...reminder, utc_time: reminder.utc_time.toFormat("yyyy-LL-dd'T'HH:mm:ss"), }), }); export const fetchGuildTemplates = (guild: string) => ({ queryKey: ["GUILD_TEMPLATES", guild], queryFn: () => axios.get(`/dashboard/api/guild/${guild}/templates`).then((resp) => resp.data) as Promise< Template[] >, staleTime: OTHER_STALE_TIME, });