-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseProjectStatusMonitor.ts
82 lines (69 loc) · 2.4 KB
/
useProjectStatusMonitor.ts
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { useEffect, useState } from 'react';
import { useLazyQuery } from '@apollo/client';
import { GET_CHAT_DETAILS } from '@/graphql/request';
import { logger } from '@/app/log/logger';
interface ProjectStatus {
isReady: boolean;
projectId?: string;
projectName?: string;
error?: string;
}
/**
* Separate the logic to monitor the project status from the component, origin code from {@link ProjectProvider}
* @param chatId chatId
* @returns return ProjectStatus object
*/
export function useProjectStatusMonitor(chatId: string): ProjectStatus {
const [isReady, setIsReady] = useState(false);
const [projectId, setProjectId] = useState<string | undefined>(undefined);
const [projectName, setProjectName] = useState<string | undefined>(undefined);
const [error, setError] = useState<string | undefined>(undefined);
const [getChatDetails, { loading }] = useLazyQuery(GET_CHAT_DETAILS, {
fetchPolicy: 'network-only',
onCompleted: (data) => {
if (data?.getChatDetails?.project) {
setIsReady(true);
setProjectId(data.getChatDetails.project.id);
setProjectName(data.getChatDetails.project.projectName);
}
},
onError: (err) => {
setError(`Error checking project status: ${err.message}`);
},
});
useEffect(() => {
if (!chatId) return;
// eslint-disable-next-line prefer-const
let pollingInterval: NodeJS.Timeout;
let attemptCount = 0;
const MAX_ATTEMPTS = 60; // About 2 minutes of polling
const checkProjectStatus = async () => {
attemptCount++;
try {
await getChatDetails({
variables: { chatId },
});
} catch (err) {
logger.error('Error polling for project:', err);
}
// Stop polling if project is ready or max attempts reached
if (isReady || attemptCount >= MAX_ATTEMPTS) {
clearInterval(pollingInterval);
if (attemptCount >= MAX_ATTEMPTS && !isReady) {
setError(
'Project creation is taking longer than expected. Please check back later.'
);
}
}
};
// Check immediately once
checkProjectStatus();
// Setup polling interval (check every 6 seconds)
pollingInterval = setInterval(checkProjectStatus, 6000);
// Cleanup function
return () => {
clearInterval(pollingInterval);
};
}, [chatId, getChatDetails, isReady]);
return { isReady, projectId, projectName, error };
}