import { useQuery } from '@tanstack/react-query';

import { IssuesService } from '@/client/services/IssuesService';

export function useWorkspaceIssues(workspaceName: string | null) {
  return useQuery({
    queryKey: ['workspace', workspaceName, 'issues'],
    queryFn: () => {
      if (!workspaceName) throw new Error('No workspace name');
      return IssuesService.issueList({
        workspaceName,
        state: 'Open',
        limit: 50,
      }).then((r) => r.data);
    },
    enabled: !!workspaceName,
    staleTime: 1000 * 60 * 2,
  });
}
