690605:1247 ADR-034-134 #11 fix Step 2
This commit is contained in:
@@ -147,7 +147,7 @@ export default function OcrSandboxPromptManager() {
|
||||
fallbackUsed?: boolean;
|
||||
} | null>(null);
|
||||
const [selectedPromptVersion, setSelectedPromptVersion] = useState<number | undefined>(undefined);
|
||||
const { state: sandboxState, jobId: sandboxJobId, reset: resetSandbox } =
|
||||
const { state: sandboxState, jobId: sandboxJobId, reset: resetSandbox, startPolling } =
|
||||
useSandboxRun(() => {
|
||||
// เมื่อ sandbox เสร็จสิ้น: รีเฟรชรายการเวอร์ชัน
|
||||
versionsQuery.refetch();
|
||||
@@ -285,24 +285,8 @@ export default function OcrSandboxPromptManager() {
|
||||
selectedPromptVersion
|
||||
);
|
||||
toast.success('AI Extraction started');
|
||||
// Poll สำหรับผลลัพธ์ AI
|
||||
const pollInterval = setInterval(async () => {
|
||||
try {
|
||||
const result = await adminAiService.getSandboxJobStatus(requestPublicId);
|
||||
if (result.status === 'completed') {
|
||||
clearInterval(pollInterval);
|
||||
// Trigger sandbox state update via useSandboxRun
|
||||
toast.success(t('ai.prompt.sandboxSuccess'));
|
||||
versionsQuery.refetch();
|
||||
} else if (result.status === 'failed') {
|
||||
clearInterval(pollInterval);
|
||||
toast.error(result.errorMessage || 'AI Extraction failed');
|
||||
}
|
||||
} catch (_err) {
|
||||
clearInterval(pollInterval);
|
||||
toast.error('Poll error occurred');
|
||||
}
|
||||
}, 1000);
|
||||
// เริ่ม polling ผ่าน useSandboxRun hook
|
||||
startPolling(requestPublicId);
|
||||
} catch (err: unknown) {
|
||||
const error = err as { response?: { data?: { message?: string } } };
|
||||
toast.error(error.response?.data?.message || 'AI Extraction failed');
|
||||
|
||||
@@ -155,11 +155,24 @@ export function useSandboxRun(onCompleted?: () => void) {
|
||||
setJobId(response.requestPublicId);
|
||||
return response.requestPublicId;
|
||||
}, []);
|
||||
/**
|
||||
* เริ่ม polling สำหรับ jobId ที่มีอยู่แล้ว (สำหรับ 2-step flow)
|
||||
* @param jobId - requestPublicId ของ job ที่ submit ไปแล้ว
|
||||
*/
|
||||
const startPolling = useCallback((jobIdParam: string) => {
|
||||
setState({
|
||||
isRunning: true,
|
||||
progress: 30,
|
||||
statusText: 'ai.prompt.statusPending',
|
||||
result: null,
|
||||
});
|
||||
setJobId(jobIdParam);
|
||||
}, []);
|
||||
/** รีเซ็ตสถานะทั้งหมด (ใช้ก่อนรันใหม่) */
|
||||
const reset = useCallback(() => {
|
||||
if (timerRef.current) clearInterval(timerRef.current);
|
||||
setJobId(null);
|
||||
setState({ isRunning: false, progress: 0, statusText: '', result: null });
|
||||
}, []);
|
||||
return { state, jobId, submit, reset };
|
||||
return { state, jobId, submit, reset, startPolling };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user