2026-07-27 21:54:31 +07:00
|
|
|
import { useEffect } from "react";
|
2026-08-01 09:19:39 +07:00
|
|
|
import useSWR, { useSWRConfig } from "swr";
|
|
|
|
|
import { useAction } from "@/hooks/use-action";
|
2026-07-26 16:14:32 +07:00
|
|
|
import { messagesApi, voiceApi } from "@/lib/api";
|
|
|
|
|
import type { AttachmentRecord, Channel, MessageRecord } from "@/lib/types";
|
2026-07-27 21:54:31 +07:00
|
|
|
import type { WsHook } from "@/lib/ws-hook";
|
2026-07-26 16:14:32 +07:00
|
|
|
|
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-08-01 09:19:39 +07:00
|
|
|
search: (query: string) => ["messages-search", query] as const,
|
2026-07-26 16:55:20 +07:00
|
|
|
};
|
2026-07-26 16:14:32 +07:00
|
|
|
|
2026-08-01 09:19:39 +07:00
|
|
|
type MessagePage = { data: MessageRecord[]; nextCursor: string | null };
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Single source of truth for the paginated message list. Both useMessages and
|
|
|
|
|
* useMessagesHasMore derive from this one SWR key, so the cursor probe no
|
|
|
|
|
* longer triggers a duplicate API call.
|
|
|
|
|
*/
|
2026-08-07 10:44:03 +07:00
|
|
|
function useMessagesPage(
|
|
|
|
|
guildId: string,
|
|
|
|
|
channelId?: string,
|
|
|
|
|
initialPage?: MessagePage,
|
|
|
|
|
) {
|
2026-08-01 09:19:39 +07:00
|
|
|
const key = guildId ? msgKeys.list(guildId, channelId) : null;
|
2026-08-07 10:44:03 +07:00
|
|
|
return useSWR<MessagePage>(
|
|
|
|
|
key,
|
|
|
|
|
() => messagesApi.list(guildId, 50, channelId || undefined),
|
|
|
|
|
{ fallbackData: initialPage },
|
2026-08-01 09:19:39 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
// ── Messages list (paginated, cursor-based) ──────
|
2026-07-26 16:14:32 +07:00
|
|
|
|
2026-08-07 10:44:03 +07:00
|
|
|
export function useMessages(
|
|
|
|
|
guildId: string,
|
|
|
|
|
channelId?: string,
|
|
|
|
|
initialPage?: MessagePage,
|
|
|
|
|
) {
|
|
|
|
|
const page = useMessagesPage(guildId, channelId, initialPage);
|
2026-08-01 09:19:39 +07:00
|
|
|
return {
|
|
|
|
|
...page,
|
|
|
|
|
data: page.data?.data,
|
|
|
|
|
refetch: () => page.mutate(),
|
|
|
|
|
};
|
2026-07-26 16:55:20 +07:00
|
|
|
}
|
2026-07-26 16:14:32 +07:00
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
export function useMessagesHasMore(guildId: string, channelId?: string) {
|
2026-08-01 09:19:39 +07:00
|
|
|
const page = useMessagesPage(guildId, channelId);
|
|
|
|
|
return {
|
|
|
|
|
data: {
|
|
|
|
|
cursor: page.data?.nextCursor ?? null,
|
|
|
|
|
hasMore: page.data ? page.data.nextCursor !== null : undefined,
|
2026-07-26 16:55:20 +07:00
|
|
|
},
|
2026-08-01 09:19:39 +07:00
|
|
|
};
|
2026-07-26 16:55:20 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useLoadMore() {
|
2026-08-01 09:19:39 +07:00
|
|
|
const { mutate } = useSWRConfig();
|
|
|
|
|
return useAction(
|
|
|
|
|
async ({
|
2026-07-26 16:55:20 +07:00
|
|
|
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-08-01 09:19:39 +07:00
|
|
|
const key = msgKeys.list(guildId, channelId);
|
|
|
|
|
await mutate(
|
|
|
|
|
key,
|
|
|
|
|
(old: MessagePage | undefined): MessagePage | undefined =>
|
|
|
|
|
old
|
|
|
|
|
? {
|
|
|
|
|
data: [...old.data, ...result.data],
|
|
|
|
|
nextCursor: result.nextCursor,
|
|
|
|
|
}
|
|
|
|
|
: result,
|
|
|
|
|
{ revalidate: false },
|
2026-07-26 16:55:20 +07:00
|
|
|
);
|
2026-08-01 09:19:39 +07:00
|
|
|
return result;
|
2026-07-26 16:55:20 +07:00
|
|
|
},
|
2026-08-01 09:19:39 +07:00
|
|
|
);
|
2026-07-26 16:55:20 +07:00
|
|
|
}
|
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) {
|
2026-08-01 09:19:39 +07:00
|
|
|
return useSWR<Channel[]>(guildId ? ["text-channels", guildId] : null, () =>
|
|
|
|
|
voiceApi.getTextChannels(guildId),
|
|
|
|
|
);
|
2026-07-26 16:55:20 +07:00
|
|
|
}
|
2026-07-26 16:14:32 +07:00
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
// ── Images ───────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export function useImages(guildId: string) {
|
2026-08-01 09:19:39 +07:00
|
|
|
return useSWR<MessageRecord[]>(
|
|
|
|
|
guildId ? msgKeys.images(guildId) : null,
|
|
|
|
|
async () => {
|
2026-07-26 16:55:20 +07:00
|
|
|
const result = await messagesApi.getImages(guildId, 50);
|
|
|
|
|
return result.data;
|
|
|
|
|
},
|
2026-08-01 09:19:39 +07:00
|
|
|
);
|
2026-07-26 16:55:20 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Review ───────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export function useReview(channelId?: string) {
|
2026-08-01 09:19:39 +07:00
|
|
|
return useSWR<MessageRecord[]>(
|
|
|
|
|
msgKeys.review(channelId),
|
|
|
|
|
async () => {
|
2026-07-26 16:55:20 +07:00
|
|
|
const result = await messagesApi.getReview(50, channelId || undefined);
|
|
|
|
|
return result.results;
|
|
|
|
|
},
|
2026-08-01 09:19:39 +07:00
|
|
|
{
|
|
|
|
|
refreshInterval: 15_000,
|
|
|
|
|
},
|
|
|
|
|
);
|
2026-07-26 16:55:20 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Detail ───────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export function useMessageDetail(id: string | null) {
|
2026-08-01 09:19:39 +07:00
|
|
|
const detail = useSWR<MessageRecord>(id ? msgKeys.detail(id) : null, () =>
|
2026-08-01 10:21:23 +07:00
|
|
|
messagesApi.getDetail(id ?? ""),
|
2026-08-01 09:19:39 +07:00
|
|
|
);
|
2026-08-01 10:21:23 +07:00
|
|
|
const channelId = id ? detail.data?.channel_id : undefined;
|
2026-08-01 09:19:39 +07:00
|
|
|
const attachments = useSWR<AttachmentRecord[]>(
|
2026-08-01 10:21:23 +07:00
|
|
|
channelId ? [...msgKeys.detail(id ?? ""), "attachments"] : null,
|
2026-08-01 09:19:39 +07:00
|
|
|
async () => {
|
2026-08-01 10:21:23 +07:00
|
|
|
// Guard: only fetch when we actually have a channel id — a revalidate
|
|
|
|
|
// can race the detail load and see detail.data === undefined.
|
|
|
|
|
const cid = detail.data?.channel_id;
|
|
|
|
|
if (!cid) return [];
|
2026-08-01 10:59:54 +07:00
|
|
|
// messageId filter: attachment list must show only this message's
|
|
|
|
|
// images, not the latest images from everyone in the channel.
|
2026-08-01 22:09:02 +07:00
|
|
|
const res = await messagesApi.getAttachments(
|
|
|
|
|
cid,
|
|
|
|
|
10,
|
|
|
|
|
undefined,
|
|
|
|
|
id ?? "",
|
|
|
|
|
);
|
2026-07-26 16:55:20 +07:00
|
|
|
return res.data;
|
|
|
|
|
},
|
2026-08-01 09:19:39 +07:00
|
|
|
);
|
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-08-01 09:19:39 +07:00
|
|
|
// ── Search ───────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export function useMessageSearch(query: string, enabled: boolean) {
|
|
|
|
|
return useSWR<MessageRecord[]>(
|
|
|
|
|
enabled && query.trim().length >= 2 ? msgKeys.search(query) : null,
|
|
|
|
|
async () => {
|
|
|
|
|
const res = await messagesApi.search(query, 50);
|
|
|
|
|
return res.results;
|
|
|
|
|
},
|
|
|
|
|
);
|
2026-07-26 16:55:20 +07:00
|
|
|
}
|
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) {
|
2026-08-01 09:19:39 +07:00
|
|
|
const { mutate } = useSWRConfig();
|
2026-07-26 16:14:32 +07:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!guildId) return;
|
2026-08-02 10:27:41 +07:00
|
|
|
// Patch every message-list key for this guild (all channels + "**filtered**").
|
|
|
|
|
// The updater receives the SWR key so we can honor its channel filter:
|
|
|
|
|
// a live `message_created`/updated for channel B must NOT be prepended to
|
|
|
|
|
// a list that is filtered down to channel A.
|
2026-08-01 09:19:39 +07:00
|
|
|
const patchLists = (
|
2026-08-02 10:27:41 +07:00
|
|
|
matcher: (key: unknown, msg: { channel_id?: string }) => boolean,
|
2026-08-01 09:19:39 +07:00
|
|
|
updater: (old: MessagePage | undefined) => MessagePage | undefined,
|
2026-08-02 10:27:41 +07:00
|
|
|
msg: { channel_id?: string },
|
2026-08-01 09:19:39 +07:00
|
|
|
) => {
|
|
|
|
|
void mutate(
|
|
|
|
|
(key) =>
|
2026-08-02 10:27:41 +07:00
|
|
|
Array.isArray(key) &&
|
|
|
|
|
key[0] === "messages" &&
|
|
|
|
|
key[1] === guildId &&
|
|
|
|
|
matcher(key, msg),
|
2026-08-01 09:19:39 +07:00
|
|
|
updater,
|
|
|
|
|
{ revalidate: false },
|
2026-07-26 16:55:20 +07:00
|
|
|
);
|
2026-08-01 09:19:39 +07:00
|
|
|
};
|
2026-08-02 10:27:41 +07:00
|
|
|
// A list key [messages, guildId, channelId] is "channel N" when channelId
|
|
|
|
|
// is a non-empty string and matches the incoming message; "__all__" (or
|
|
|
|
|
// any non-channel) lists accept every message of the guild.
|
|
|
|
|
const matchesFilter = (key: unknown[], msg: { channel_id?: string }) => {
|
|
|
|
|
const channelId = key[2] as string | undefined;
|
|
|
|
|
if (!channelId || channelId === "__all__") return true;
|
|
|
|
|
return msg.channel_id === channelId;
|
|
|
|
|
};
|
2026-08-01 09:19:39 +07:00
|
|
|
|
|
|
|
|
const unsub1 = ws.on("message_created", (data) => {
|
|
|
|
|
const msg = data as MessageRecord;
|
2026-08-02 10:27:41 +07:00
|
|
|
patchLists(
|
|
|
|
|
(_k, m) => matchesFilter(_k as unknown[], m),
|
|
|
|
|
(old) => (old ? { ...old, data: [msg, ...old.data] } : old),
|
|
|
|
|
msg,
|
|
|
|
|
);
|
2026-07-26 16:55:20 +07:00
|
|
|
});
|
|
|
|
|
const unsub2 = ws.on("message_updated", (data) => {
|
2026-08-02 10:27:41 +07:00
|
|
|
const msg = data as Partial<MessageRecord> & { id: string };
|
|
|
|
|
// The gateway broadcasts a PARTIAL update ({ id, edited_content,
|
|
|
|
|
// edited_at, ... }) — merge it over the existing record instead of
|
|
|
|
|
// replacing it, or the card would lose username/content/channel/etc.
|
|
|
|
|
patchLists(
|
|
|
|
|
(_k, m) =>
|
|
|
|
|
(m as Partial<MessageRecord>).channel_id === undefined ||
|
|
|
|
|
matchesFilter(_k as unknown[], m),
|
|
|
|
|
(old) =>
|
|
|
|
|
old
|
|
|
|
|
? {
|
|
|
|
|
...old,
|
|
|
|
|
data: old.data.map((m) =>
|
|
|
|
|
m.id === msg.id ? { ...m, ...msg } : m,
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
: old,
|
|
|
|
|
msg,
|
|
|
|
|
);
|
|
|
|
|
void mutate(
|
|
|
|
|
msgKeys.detail(msg.id),
|
|
|
|
|
(old: MessageRecord | undefined) => (old ? { ...old, ...msg } : old),
|
|
|
|
|
{ revalidate: false },
|
2026-07-26 16:55:20 +07:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
const unsub3 = ws.on("message_deleted", (data) => {
|
2026-08-01 09:19:39 +07:00
|
|
|
const { id } = data as { id: string };
|
2026-08-02 10:27:41 +07:00
|
|
|
patchLists(
|
|
|
|
|
() => true,
|
|
|
|
|
(old) =>
|
|
|
|
|
old ? { ...old, data: old.data.filter((m) => m.id !== id) } : old,
|
|
|
|
|
{ channel_id: undefined },
|
2026-07-26 16:55:20 +07:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
const unsub4 = ws.on("message_analyzed", (data) => {
|
2026-08-01 09:19:39 +07:00
|
|
|
const msg = data as MessageRecord;
|
2026-08-02 10:27:41 +07:00
|
|
|
// message_analyzed carries the FULL record — replace is fine.
|
|
|
|
|
patchLists(
|
|
|
|
|
(_k, m) => matchesFilter(_k as unknown[], m),
|
|
|
|
|
(old) =>
|
|
|
|
|
old
|
|
|
|
|
? { ...old, data: old.data.map((m) => (m.id === msg.id ? msg : m)) }
|
|
|
|
|
: old,
|
|
|
|
|
msg,
|
2026-07-26 16:55:20 +07:00
|
|
|
);
|
2026-08-01 09:19:39 +07:00
|
|
|
void mutate(msgKeys.detail(msg.id), msg, { revalidate: false });
|
2026-07-26 16:55:20 +07:00
|
|
|
});
|
2026-07-26 16:14:32 +07:00
|
|
|
return () => {
|
|
|
|
|
unsub1();
|
|
|
|
|
unsub2();
|
|
|
|
|
unsub3();
|
|
|
|
|
unsub4();
|
|
|
|
|
};
|
2026-08-01 09:19:39 +07:00
|
|
|
}, [ws, guildId, mutate]);
|
2026-07-26 16:14:32 +07:00
|
|
|
}
|