mirror of
https://github.com/ostris/ai-toolkit.git
synced 2026-02-20 20:33:57 +00:00
43 lines
1.0 KiB
TypeScript
43 lines
1.0 KiB
TypeScript
'use client';
|
|
|
|
import { useEffect, useState } from 'react';
|
|
import { Job } from '@prisma/client';
|
|
import { apiClient } from '@/utils/api';
|
|
|
|
export default function useJob(jobID: string, reloadInterval: null | number = null) {
|
|
const [job, setJob] = useState<Job | null>(null);
|
|
const [status, setStatus] = useState<'idle' | 'loading' | 'success' | 'error'>('idle');
|
|
|
|
const refreshJob = () => {
|
|
setStatus('loading');
|
|
apiClient
|
|
.get(`/api/jobs?id=${jobID}`)
|
|
.then(res => res.data)
|
|
.then(data => {
|
|
console.log('Job:', data);
|
|
setJob(data);
|
|
setStatus('success');
|
|
})
|
|
.catch(error => {
|
|
console.error('Error fetching datasets:', error);
|
|
setStatus('error');
|
|
});
|
|
};
|
|
|
|
useEffect(() => {
|
|
refreshJob();
|
|
|
|
if (reloadInterval) {
|
|
const interval = setInterval(() => {
|
|
refreshJob();
|
|
}, reloadInterval);
|
|
|
|
return () => {
|
|
clearInterval(interval);
|
|
};
|
|
}
|
|
}, [jobID]);
|
|
|
|
return { job, setJob, status, refreshJob };
|
|
}
|