refactor(api): replace URLSearchParams param with structured object in listMessages
This commit is contained in:
@@ -29,8 +29,9 @@ export function useMessages() {
|
||||
const [cursor, setCursor] = useState<string | null>(null);
|
||||
const [hasMore, setHasMore] = useState(false);
|
||||
const currentGuild = useRef<string | null>(null);
|
||||
const currentChannel = useRef<string | undefined>(undefined);
|
||||
|
||||
const fetchMessages = useCallback(async (guildId?: string) => {
|
||||
const fetchMessages = useCallback(async (guildId?: string, channelId?: string) => {
|
||||
if (!guildId) {
|
||||
setMessages([]);
|
||||
setCursor(null);
|
||||
@@ -38,14 +39,15 @@ export function useMessages() {
|
||||
return [];
|
||||
}
|
||||
currentGuild.current = guildId;
|
||||
currentChannel.current = channelId;
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const params = new URLSearchParams({
|
||||
limit: String(PAGE_SIZE),
|
||||
const result = await listMessages({
|
||||
guildId,
|
||||
channelId,
|
||||
limit: PAGE_SIZE,
|
||||
});
|
||||
const result = await listMessages(params);
|
||||
if (currentGuild.current === guildId) {
|
||||
setMessages(result.data);
|
||||
setCursor(result.nextCursor);
|
||||
@@ -65,12 +67,12 @@ export function useMessages() {
|
||||
if (!cursor || !currentGuild.current || loadingMore) return;
|
||||
setLoadingMore(true);
|
||||
try {
|
||||
const params = new URLSearchParams({
|
||||
limit: String(PAGE_SIZE),
|
||||
const result = await listMessages({
|
||||
guildId: currentGuild.current,
|
||||
channelId: currentChannel.current,
|
||||
cursor,
|
||||
limit: PAGE_SIZE,
|
||||
});
|
||||
const result = await listMessages(params);
|
||||
setMessages((prev) => [...prev, ...result.data]);
|
||||
setCursor(result.nextCursor);
|
||||
setHasMore(!!result.nextCursor);
|
||||
|
||||
@@ -151,10 +151,19 @@ export type DashboardTab = "live" | "messages" | "analytics";
|
||||
|
||||
// ─── Messages ────────────────────────────────────────────────────────────────
|
||||
|
||||
export function listMessages(
|
||||
params: URLSearchParams,
|
||||
): Promise<PageResult<MessageRecord>> {
|
||||
return request<PageResult<MessageRecord>>(`/api/messages?${params}`);
|
||||
export function listMessages(params: {
|
||||
guildId: string;
|
||||
channelId?: string;
|
||||
limit?: number;
|
||||
cursor?: string;
|
||||
}): Promise<PageResult<MessageRecord>> {
|
||||
const sp = new URLSearchParams({
|
||||
guildId: params.guildId,
|
||||
limit: String(params.limit ?? 100),
|
||||
...(params.channelId && { channelId: params.channelId }),
|
||||
...(params.cursor && { cursor: params.cursor }),
|
||||
});
|
||||
return request<PageResult<MessageRecord>>(`/api/messages?${sp}`);
|
||||
}
|
||||
|
||||
export function listReview(
|
||||
|
||||
Reference in New Issue
Block a user