2026-07-26 16:55:20 +07:00
|
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
|
|
|
import { useCallback, useEffect } from "react";
|
2026-07-26 16:14:32 +07:00
|
|
|
|
|
|
|
|
import { messagesApi, voiceApi } from "@/lib/api";
|
|
|
|
|
import type { AttachmentRecord, Channel, MessageRecord } from "@/lib/types";
|
|
|
|
|
import type { WsEventType } from "@/lib/ws/types";
|
|
|
|
|
|
|
|
|
|
type WsHook = {
|
|
|
|
|
on: <E extends WsEventType>(
|
|
|
|
|
eventType: E,
|
|
|
|
|
handler: (data: unknown) => void,
|
|
|
|
|
) => () => void;
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
// ── Query keys factory ───────────────────────────
|
2026-07-26 16:14:32 +07:00
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
const msgKeys = {
|
|
|
|
|
list: (guildId: string, channelId?: string) =>
|
|
|
|
|
["messages", guildId, channelId ?? "__all__"] as const,
|
|
|
|
|
images: (guildId: string) => ["messages-images", guildId] as const,
|
|
|
|
|
review: (channelId?: string) =>
|
|
|
|
|
["messages-review", channelId ?? "__all__"] as const,
|
|
|
|
|
detail: (id: string) => ["message-detail", id] as const,
|
|
|
|
|
};
|
2026-07-26 16:14:32 +07:00
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
// ── Messages list (paginated, cursor-based) ──────
|
2026-07-26 16:14:32 +07:00
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
export function useMessages(guildId: string, channelId?: string) {
|
|
|
|
|
return useQuery<MessageRecord[]>({
|
|
|
|
|
queryKey: msgKeys.list(guildId, channelId),
|
|
|
|
|
queryFn: async () => {
|
2026-07-26 16:14:32 +07:00
|
|
|
const result = await messagesApi.list(
|
|
|
|
|
guildId,
|
|
|
|
|
50,
|
|
|
|
|
channelId || undefined,
|
|
|
|
|
);
|
2026-07-26 16:55:20 +07:00
|
|
|
return result.data;
|
|
|
|
|
},
|
|
|
|
|
enabled: !!guildId,
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-07-26 16:14:32 +07:00
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
export function useMessagesHasMore(guildId: string, channelId?: string) {
|
|
|
|
|
return useQuery({
|
|
|
|
|
queryKey: [...msgKeys.list(guildId, channelId), "cursor"],
|
|
|
|
|
queryFn: async () => {
|
|
|
|
|
const result = await messagesApi.list(
|
|
|
|
|
guildId,
|
|
|
|
|
50,
|
|
|
|
|
channelId || undefined,
|
|
|
|
|
);
|
|
|
|
|
return { cursor: result.nextCursor, hasMore: result.nextCursor !== null };
|
|
|
|
|
},
|
|
|
|
|
enabled: !!guildId,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useLoadMore() {
|
|
|
|
|
const qc = useQueryClient();
|
|
|
|
|
return useMutation({
|
|
|
|
|
mutationFn: async ({
|
|
|
|
|
guildId,
|
|
|
|
|
channelId,
|
|
|
|
|
cursor,
|
|
|
|
|
}: {
|
|
|
|
|
guildId: string;
|
|
|
|
|
channelId?: string;
|
|
|
|
|
cursor: string;
|
|
|
|
|
}) => {
|
2026-07-26 16:14:32 +07:00
|
|
|
const result = await messagesApi.list(
|
|
|
|
|
guildId,
|
|
|
|
|
50,
|
|
|
|
|
channelId || undefined,
|
|
|
|
|
cursor,
|
|
|
|
|
);
|
2026-07-26 16:55:20 +07:00
|
|
|
return { data: result.data, cursor: result.nextCursor };
|
|
|
|
|
},
|
|
|
|
|
onSuccess: (data, vars) => {
|
|
|
|
|
const key = msgKeys.list(vars.guildId, vars.channelId);
|
|
|
|
|
qc.setQueryData<MessageRecord[]>(key, (old) =>
|
|
|
|
|
old ? [...old, ...data.data] : data.data,
|
|
|
|
|
);
|
|
|
|
|
qc.setQueryData([...key, "cursor"], {
|
|
|
|
|
cursor: data.cursor,
|
|
|
|
|
hasMore: data.cursor !== null,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-07-26 16:14:32 +07:00
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
// ── Channels list ────────────────────────────────
|
2026-07-26 16:46:53 +07:00
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
export function useTextChannels(guildId: string) {
|
|
|
|
|
return useQuery<Channel[]>({
|
|
|
|
|
queryKey: ["text-channels", guildId],
|
|
|
|
|
queryFn: () => voiceApi.getTextChannels(guildId),
|
|
|
|
|
enabled: !!guildId,
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-07-26 16:14:32 +07:00
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
// ── Search ───────────────────────────────────────
|
2026-07-26 16:14:32 +07:00
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
export function useSearch() {
|
|
|
|
|
return useQuery<MessageRecord[]>({
|
|
|
|
|
queryKey: ["messages-search"],
|
|
|
|
|
queryFn: () => Promise.resolve([]),
|
|
|
|
|
enabled: false,
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-07-26 16:14:32 +07:00
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
// ── Images ───────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export function useImages(guildId: string) {
|
|
|
|
|
return useQuery<MessageRecord[]>({
|
|
|
|
|
queryKey: msgKeys.images(guildId),
|
|
|
|
|
queryFn: async () => {
|
|
|
|
|
const result = await messagesApi.getImages(guildId, 50);
|
|
|
|
|
return result.data;
|
|
|
|
|
},
|
|
|
|
|
enabled: !!guildId,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Review ───────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export function useReview(channelId?: string) {
|
|
|
|
|
return useQuery<MessageRecord[]>({
|
|
|
|
|
queryKey: msgKeys.review(channelId),
|
|
|
|
|
queryFn: async () => {
|
|
|
|
|
const result = await messagesApi.getReview(50, channelId || undefined);
|
|
|
|
|
return result.results;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Detail ───────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export function useMessageDetail(id: string | null) {
|
|
|
|
|
const detail = useQuery<MessageRecord>({
|
|
|
|
|
queryKey: msgKeys.detail(id ?? ""),
|
|
|
|
|
queryFn: () => messagesApi.getDetail(id!),
|
|
|
|
|
enabled: !!id,
|
|
|
|
|
});
|
|
|
|
|
const attachments = useQuery<AttachmentRecord[]>({
|
|
|
|
|
queryKey: [...msgKeys.detail(id ?? ""), "attachments"],
|
|
|
|
|
queryFn: async () => {
|
|
|
|
|
if (!id) return [];
|
|
|
|
|
const res = await messagesApi.getAttachments(
|
|
|
|
|
detail.data?.channel_id ?? "",
|
|
|
|
|
10,
|
|
|
|
|
);
|
|
|
|
|
return res.data;
|
|
|
|
|
},
|
|
|
|
|
enabled: !!id && !!detail.data?.channel_id,
|
|
|
|
|
});
|
2026-07-26 16:14:32 +07:00
|
|
|
return {
|
2026-07-26 16:55:20 +07:00
|
|
|
message: detail.data ?? null,
|
|
|
|
|
attachments: attachments.data ?? [],
|
|
|
|
|
loading: detail.isLoading || attachments.isLoading,
|
|
|
|
|
error: detail.error,
|
2026-07-26 16:14:32 +07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
// ── Mutations ────────────────────────────────────
|
2026-07-26 16:14:32 +07:00
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
export function useReanalyze() {
|
|
|
|
|
const qc = useQueryClient();
|
|
|
|
|
return useMutation({
|
|
|
|
|
mutationFn: (id: string) => messagesApi.reanalyze(id),
|
|
|
|
|
});
|
2026-07-26 16:14:32 +07:00
|
|
|
}
|
|
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
export function useReanalyzeBatch() {
|
|
|
|
|
return useMutation({
|
|
|
|
|
mutationFn: (guildId: string) => messagesApi.reanalyzeBatch(guildId),
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-07-26 16:14:32 +07:00
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
// ── WS sync helpers ──────────────────────────────
|
|
|
|
|
|
|
|
|
|
export function useMessagesWsSync(ws: WsHook, guildId: string) {
|
|
|
|
|
const qc = useQueryClient();
|
2026-07-26 16:14:32 +07:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!guildId) return;
|
2026-07-26 16:55:20 +07:00
|
|
|
const key = msgKeys.list(guildId);
|
|
|
|
|
const unsub1 = ws.on("message_created", (data) => {
|
|
|
|
|
qc.setQueryData<MessageRecord[]>(key, (old) =>
|
|
|
|
|
old ? [data as MessageRecord, ...old] : [data as MessageRecord],
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
const unsub2 = ws.on("message_updated", (data) => {
|
|
|
|
|
qc.setQueryData<MessageRecord[]>(key, (old) =>
|
|
|
|
|
old
|
|
|
|
|
? old.map((m) =>
|
|
|
|
|
m.id === (data as MessageRecord).id ? (data as MessageRecord) : m,
|
|
|
|
|
)
|
|
|
|
|
: old,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
const unsub3 = ws.on("message_deleted", (data) => {
|
|
|
|
|
qc.setQueryData<MessageRecord[]>(key, (old) =>
|
|
|
|
|
old ? old.filter((m) => m.id !== (data as unknown as string)) : old,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
const unsub4 = ws.on("message_analyzed", (data) => {
|
|
|
|
|
qc.setQueryData<MessageRecord[]>(key, (old) =>
|
|
|
|
|
old
|
|
|
|
|
? old.map((m) =>
|
|
|
|
|
m.id === (data as MessageRecord).id ? (data as MessageRecord) : m,
|
|
|
|
|
)
|
|
|
|
|
: old,
|
|
|
|
|
);
|
|
|
|
|
});
|
2026-07-26 16:14:32 +07:00
|
|
|
return () => {
|
|
|
|
|
unsub1();
|
|
|
|
|
unsub2();
|
|
|
|
|
unsub3();
|
|
|
|
|
unsub4();
|
|
|
|
|
};
|
2026-07-26 16:55:20 +07:00
|
|
|
}, [ws, guildId, qc]);
|
2026-07-26 16:14:32 +07:00
|
|
|
}
|