2026-08-01 09:19:39 +07:00
|
|
|
import useSWR from "swr";
|
2026-07-26 16:14:32 +07:00
|
|
|
|
|
|
|
|
import { dashboardApi } from "@/lib/api";
|
|
|
|
|
import type {
|
2026-08-01 23:06:00 +07:00
|
|
|
DashboardActivity,
|
2026-07-26 16:14:32 +07:00
|
|
|
DashboardChannelDetail,
|
|
|
|
|
DashboardStats,
|
|
|
|
|
DashboardUserDetail,
|
2026-08-05 10:20:12 +07:00
|
|
|
TopReactedMessage,
|
2026-08-05 11:29:56 +07:00
|
|
|
TopReactor,
|
2026-07-26 16:14:32 +07:00
|
|
|
} from "@/lib/types";
|
|
|
|
|
|
2026-08-07 10:44:03 +07:00
|
|
|
/**
|
|
|
|
|
* Server-seeded SWR hooks.
|
|
|
|
|
*
|
|
|
|
|
* SSR pages fetch the initial payload on the server and hand it here as
|
|
|
|
|
* `initialData` — the first render is server data, and SWR takes over for
|
|
|
|
|
* revalidation from then on (no blank-spinner-first-load).
|
|
|
|
|
*/
|
|
|
|
|
export function useStats(initialData?: DashboardStats) {
|
|
|
|
|
return useSWR<DashboardStats>(
|
|
|
|
|
["dashboard-stats"],
|
|
|
|
|
() => dashboardApi.getStats(),
|
|
|
|
|
{ fallbackData: initialData },
|
2026-08-01 09:19:39 +07:00
|
|
|
);
|
2026-07-26 16:14:32 +07:00
|
|
|
}
|
|
|
|
|
|
2026-08-07 10:44:03 +07:00
|
|
|
export function useActivity(days = 14, initialData?: DashboardActivity) {
|
|
|
|
|
return useSWR<DashboardActivity>(
|
|
|
|
|
["dashboard-activity", days],
|
|
|
|
|
() => dashboardApi.getActivity(days),
|
|
|
|
|
{ fallbackData: initialData },
|
2026-08-01 23:06:00 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
export function useUsers(search?: string) {
|
2026-08-01 09:19:39 +07:00
|
|
|
return useSWR(
|
|
|
|
|
["dashboard-users", search ?? ""],
|
|
|
|
|
async () => {
|
|
|
|
|
const res = await dashboardApi.listUsers(20, undefined, search);
|
|
|
|
|
return res.data;
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
keepPreviousData: true,
|
|
|
|
|
},
|
|
|
|
|
);
|
2026-07-26 16:14:32 +07:00
|
|
|
}
|
|
|
|
|
|
2026-07-31 16:54:41 +07:00
|
|
|
export function useChannels(guildId?: string, search?: string) {
|
2026-08-01 09:19:39 +07:00
|
|
|
return useSWR(
|
|
|
|
|
["dashboard-channels", guildId ?? "__all__", search ?? ""],
|
|
|
|
|
async () => {
|
|
|
|
|
const res = await dashboardApi.listChannels(
|
|
|
|
|
20,
|
|
|
|
|
search,
|
|
|
|
|
guildId || undefined,
|
|
|
|
|
);
|
|
|
|
|
return res.data;
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
keepPreviousData: true,
|
|
|
|
|
},
|
|
|
|
|
);
|
2026-07-26 16:14:32 +07:00
|
|
|
}
|
|
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
export function useUserDetail(userId: string | null) {
|
2026-08-01 09:19:39 +07:00
|
|
|
return useSWR<DashboardUserDetail>(
|
2026-08-15 20:03:55 +07:00
|
|
|
userId ? (["dashboard-user", userId] as const) : null,
|
|
|
|
|
() => dashboardApi.getUserDetail(userId ?? ""),
|
2026-08-01 09:19:39 +07:00
|
|
|
);
|
2026-07-26 16:14:32 +07:00
|
|
|
}
|
|
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
export function useChannelDetail(channelId: string | null) {
|
2026-08-01 09:19:39 +07:00
|
|
|
return useSWR<DashboardChannelDetail>(
|
2026-08-15 20:03:55 +07:00
|
|
|
channelId ? (["dashboard-channel", channelId] as const) : null,
|
|
|
|
|
() => dashboardApi.getChannelDetail(channelId ?? ""),
|
2026-08-01 09:19:39 +07:00
|
|
|
);
|
2026-07-26 16:14:32 +07:00
|
|
|
}
|
2026-08-05 10:20:12 +07:00
|
|
|
|
2026-08-05 11:29:56 +07:00
|
|
|
export function useTopReactions() {
|
|
|
|
|
return useSWR<TopReactedMessage[]>(["dashboard-reactions"], () =>
|
|
|
|
|
dashboardApi.getTopReactions(20),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useTopReactors() {
|
|
|
|
|
return useSWR<TopReactor[]>(["dashboard-reactors"], () =>
|
|
|
|
|
dashboardApi.getTopReactors(20),
|
2026-08-05 10:20:12 +07:00
|
|
|
);
|
|
|
|
|
}
|