/ chandeskpro / src / hooks / useThread.ts
swadhinbiswas Initial release v0.1.0 · 1/16/2026
ed7a4e4
36 lines · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { useQuery } from '@tanstack/react-query';
import { fetchThread } from '../utils/apiClient';
import { useSettingsStore } from '../stores/settingsStore';
import { useWatchlistStore } from '../stores/watchlistStore';
import { useEffect } from 'react';
import type { Thread } from '../types/api';

export function useThread(board: string, threadId: number, enabled: boolean = true) {
    const autoRefreshInterval = useSettingsStore((state) => state.autoRefreshInterval);
    const watchedThreads = useWatchlistStore((state) => state.watchedThreads);
    const updateThreadStatus = useWatchlistStore((state) => state.updateThreadStatus);

    const isWatched = watchedThreads.some((t) => t.id === threadId);

    const query = useQuery({
        queryKey: ['thread', board, threadId],
        queryFn: () => fetchThread(board, threadId),
        staleTime: 10000, // 10 seconds
        refetchInterval: isWatched && autoRefreshInterval > 0 ? autoRefreshInterval * 1000 : false,
        enabled,
        retry: 2,
    });

    // Update watchlist when thread data changes
    useEffect(() => {
        if (query.data && isWatched) {
            const threadData = query.data as Thread;
            const replyCount = threadData.posts.length - 1; // Exclude OP
            updateThreadStatus(threadId, replyCount);
        }
    }, [query.data, isWatched, threadId, updateThreadStatus]);

    return query;
}