Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default function JobsPage() {
const { status: jobsStatus, dismissError } = useJobsStatus(
tab === "documents",
);
const isProcessing = jobsStatus.type === "processing";

const {
isLoading: isDocumentsLoading,
Expand All @@ -56,7 +57,7 @@ export default function JobsPage() {
handleNext: handleNextDocument,
handlePrevious: handlePreviousDocument,
hasPrevious: hasPreviousDocument,
} = useDocuments(undefined, tab === "documents");
} = useDocuments(undefined, tab === "documents", isProcessing);

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ const statusLabels = Object.values(DocumentStatus).map((status) => ({
value: status,
}));

export function useDocuments(jobId?: string, enabled = true) {
export function useDocuments(
jobId?: string,
enabled = true,
isProcessing = false,
) {
const namespace = useNamespace();
const trpc = useTRPC();
const [statuses, _setStatuses] = useState<DocumentStatus[]>([]);
Expand All @@ -36,7 +40,7 @@ export function useDocuments(jobId?: string, enabled = true) {
},
{
placeholderData: keepPreviousData,
refetchInterval: 15_000, // Refetch every 15 seconds
refetchInterval: isProcessing ? 5_000 : 15_000,
enabled,
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function useJobsStatus(enabled: boolean) {
// Track when the hook was mounted to only show errors for jobs that failed after
const [mountedAt] = useState(() => new Date());

// Check for pending jobs
// Check for pending jobs - use dynamic refetch interval based on results
const { data: pendingData } = useQuery(
trpc.ingestJob.all.queryOptions(
{
Expand All @@ -40,15 +40,18 @@ export function useJobsStatus(enabled: boolean) {
},
{
enabled,
refetchInterval: 15_000,
placeholderData: keepPreviousData,
refetchInterval: (query) => {
const hasPending = (query.state.data?.records.length ?? 0) > 0;
return hasPending ? 5_000 : 15_000;
},
Comment on lines +44 to +47
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: magic numbers 1_500 and 15_000 should be extracted as named constants at the top of the file

Suggested change
refetchInterval: (query) => {
const hasPending = (query.state.data?.records.length ?? 0) > 0;
return hasPending ? 1_500 : 15_000;
},
const REFRESH_INTERVAL_PROCESSING = 1_500;
const REFRESH_INTERVAL_IDLE = 15_000;
// ...
refetchInterval: (query) => {
const hasPending = (query.state.data?.records.length ?? 0) > 0;
return hasPending ? REFRESH_INTERVAL_PROCESSING : REFRESH_INTERVAL_IDLE;
},

Context Used: Context from dashboard - Guidelines for writing clean, maintainable, and human-readable code. Apply these rules when writing ... (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/web/src/app/app.agentset.ai/(dashboard)/[slug]/[namespaceSlug]/documents/use-pending-jobs.ts
Line: 44:47

Comment:
**style:** magic numbers `1_500` and `15_000` should be extracted as named constants at the top of the file

```suggestion
const REFRESH_INTERVAL_PROCESSING = 1_500;
const REFRESH_INTERVAL_IDLE = 15_000;

// ...

        refetchInterval: (query) => {
          const hasPending = (query.state.data?.records.length ?? 0) > 0;
          return hasPending ? REFRESH_INTERVAL_PROCESSING : REFRESH_INTERVAL_IDLE;
        },
```

**Context Used:** Context from `dashboard` - Guidelines for writing clean, maintainable, and human-readable code. Apply these rules when writing ... ([source](https://app.greptile.com/review/custom-context?memory=ae3f5924-b837-4792-9dae-0bd929e9f8dd))

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

},
),
);

const hasPendingJobs = pendingData ? pendingData.records.length > 0 : false;

// Check for recently failed jobs
// Check for recently failed jobs - use same dynamic interval
const { data: failedData } = useQuery(
trpc.ingestJob.all.queryOptions(
{
Expand All @@ -59,8 +62,8 @@ export function useJobsStatus(enabled: boolean) {
},
{
enabled,
refetchInterval: 15_000,
placeholderData: keepPreviousData,
refetchInterval: hasPendingJobs ? 5_000 : 15_000,
},
),
);
Expand Down