File size: 1,759 Bytes
0b9dc2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState, useEffect, useCallback } from 'react';

import { scheduleApi } from '../api';
import type { ScheduleRecord, CreateScheduleRequest, UpdateScheduleRequest } from '../api';

/**

 * Manages scheduled agent tasks with CRUD operations.

 * Fetches on mount and automatically re-fetches after each mutation.

 */
export function useSchedules() {
	const [schedules, setSchedules] = useState<ScheduleRecord[]>([]);
	const [loading, setLoading] = useState(false);
	const [error, setError] = useState<Error | null>(null);

	const refetch = useCallback(async () => {
		setLoading(true);
		setError(null);
		try {
			const res = await scheduleApi.list();
			setSchedules(res.schedules);
		} catch (e) {
			setError(e as Error);
		} finally {
			setLoading(false);
		}
	}, []);

	useEffect(() => {
		refetch();
	}, [refetch]);

	/** Creates a new schedule, registers it with the scheduler, and refreshes the list. */
	const create = useCallback(
		async (body: CreateScheduleRequest) => {
			const res = await scheduleApi.create(body);
			await refetch();
			return res;
		},
		[refetch],
	);

	/** Updates a schedule's config, reschedules the job, and refreshes the list. */
	const update = useCallback(
		async (scheduleId: string, body: UpdateScheduleRequest) => {
			const res = await scheduleApi.update(scheduleId, body);
			await refetch();
			return res;
		},
		[refetch],
	);

	/** Deletes a schedule, removes the job from the scheduler, and refreshes the list. */
	const remove = useCallback(
		async (scheduleId: string) => {
			await scheduleApi.delete(scheduleId);
			await refetch();
		},
		[refetch],
	);

	return { schedules, loading, error, refetch, create, update, remove };
}