| import { useMemo, useCallback } from 'react'; |
| import type { GitHubComment } from '@/lib/electron'; |
| import { useAppStore } from '@/store/app-store'; |
| import { useGitHubIssueComments } from '@/hooks/queries'; |
|
|
| interface UseIssueCommentsResult { |
| comments: GitHubComment[]; |
| totalCount: number; |
| loading: boolean; |
| loadingMore: boolean; |
| hasNextPage: boolean; |
| error: string | null; |
| loadMore: () => void; |
| refresh: () => void; |
| } |
|
|
| export function useIssueComments(issueNumber: number | null): UseIssueCommentsResult { |
| const { currentProject } = useAppStore(); |
|
|
| |
| const { data, isLoading, isFetchingNextPage, hasNextPage, fetchNextPage, refetch, error } = |
| useGitHubIssueComments(currentProject?.path, issueNumber ?? undefined); |
|
|
| |
| const comments = useMemo(() => { |
| return data?.pages.flatMap((page) => page.comments) ?? []; |
| }, [data?.pages]); |
|
|
| |
| const totalCount = data?.pages[0]?.totalCount ?? 0; |
|
|
| const loadMore = useCallback(() => { |
| if (hasNextPage && !isFetchingNextPage) { |
| fetchNextPage(); |
| } |
| }, [hasNextPage, isFetchingNextPage, fetchNextPage]); |
|
|
| const refresh = useCallback(() => { |
| refetch(); |
| }, [refetch]); |
|
|
| return { |
| comments, |
| totalCount, |
| loading: isLoading, |
| loadingMore: isFetchingNextPage, |
| hasNextPage: hasNextPage ?? false, |
| error: error instanceof Error ? error.message : null, |
| loadMore, |
| refresh, |
| }; |
| } |
|
|