summaryrefslogtreecommitdiff
path: root/apps/mobile/hooks/useTasks.ts
diff options
context:
space:
mode:
authorsoryu <soryu@soryu.co>2026-01-18 02:58:27 +0000
committersoryu <soryu@soryu.co>2026-01-18 02:58:27 +0000
commitfcf9e70d54bd737d2dea848d25314120f37db503 (patch)
treebc304a9e153485f7686830614b2ddae4d4ff182e /apps/mobile/hooks/useTasks.ts
parentf84a7f2d820f6f432be2b1d78d6bf833b5b19380 (diff)
downloadsoryu-fcf9e70d54bd737d2dea848d25314120f37db503.tar.gz
soryu-fcf9e70d54bd737d2dea848d25314120f37db503.zip
[WIP] Heartbeat checkpoint - 2026-01-18 02:58:27 UTC
Diffstat (limited to 'apps/mobile/hooks/useTasks.ts')
-rw-r--r--apps/mobile/hooks/useTasks.ts204
1 files changed, 204 insertions, 0 deletions
diff --git a/apps/mobile/hooks/useTasks.ts b/apps/mobile/hooks/useTasks.ts
new file mode 100644
index 0000000..4d56f63
--- /dev/null
+++ b/apps/mobile/hooks/useTasks.ts
@@ -0,0 +1,204 @@
+import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
+import {
+ listTasks,
+ getTask,
+ startTask,
+ stopTask,
+ getTaskOutput,
+ sendTaskMessage,
+ type TaskSummary,
+ type TaskWithSubtasks,
+ type TaskOutputResponse,
+} from '../lib/api';
+
+// Query keys for consistent cache management
+export const taskKeys = {
+ all: ['tasks'] as const,
+ lists: () => [...taskKeys.all, 'list'] as const,
+ list: () => [...taskKeys.lists()] as const,
+ details: () => [...taskKeys.all, 'detail'] as const,
+ detail: (id: string) => [...taskKeys.details(), id] as const,
+ output: (id: string) => [...taskKeys.all, 'output', id] as const,
+};
+
+/**
+ * Hook to fetch the list of all tasks
+ * Automatically refetches every 5 seconds for live updates
+ */
+export function useTasks() {
+ return useQuery({
+ queryKey: taskKeys.list(),
+ queryFn: async () => {
+ const response = await listTasks();
+ return response.tasks;
+ },
+ refetchInterval: 5000, // Poll every 5 seconds for updates
+ staleTime: 2000, // Consider data stale after 2 seconds
+ });
+}
+
+/**
+ * Hook to fetch a specific task with its subtasks
+ */
+export function useTask(taskId: string | null) {
+ return useQuery({
+ queryKey: taskKeys.detail(taskId ?? ''),
+ queryFn: () => getTask(taskId!),
+ enabled: !!taskId,
+ refetchInterval: 5000,
+ });
+}
+
+/**
+ * Hook to fetch task output history
+ */
+export function useTaskOutput(taskId: string | null) {
+ return useQuery({
+ queryKey: taskKeys.output(taskId ?? ''),
+ queryFn: () => getTaskOutput(taskId!),
+ enabled: !!taskId,
+ refetchInterval: 3000, // More frequent updates for output
+ });
+}
+
+/**
+ * Hook to start a task
+ */
+export function useStartTask() {
+ const queryClient = useQueryClient();
+
+ return useMutation({
+ mutationFn: startTask,
+ onSuccess: (updatedTask) => {
+ // Invalidate task list to refetch
+ queryClient.invalidateQueries({ queryKey: taskKeys.lists() });
+ // Update the specific task in cache
+ queryClient.setQueryData(
+ taskKeys.detail(updatedTask.id),
+ (old: TaskWithSubtasks | undefined) => {
+ if (old) {
+ return { ...old, ...updatedTask };
+ }
+ return old;
+ }
+ );
+ },
+ });
+}
+
+/**
+ * Hook to stop a task
+ */
+export function useStopTask() {
+ const queryClient = useQueryClient();
+
+ return useMutation({
+ mutationFn: stopTask,
+ onSuccess: (updatedTask) => {
+ // Invalidate task list to refetch
+ queryClient.invalidateQueries({ queryKey: taskKeys.lists() });
+ // Update the specific task in cache
+ queryClient.setQueryData(
+ taskKeys.detail(updatedTask.id),
+ (old: TaskWithSubtasks | undefined) => {
+ if (old) {
+ return { ...old, ...updatedTask };
+ }
+ return old;
+ }
+ );
+ },
+ });
+}
+
+/**
+ * Hook to send a message to a task
+ */
+export function useSendTaskMessage() {
+ const queryClient = useQueryClient();
+
+ return useMutation({
+ mutationFn: ({ taskId, message }: { taskId: string; message: string }) =>
+ sendTaskMessage(taskId, message),
+ onSuccess: (_, { taskId }) => {
+ // Invalidate task output to refetch
+ queryClient.invalidateQueries({ queryKey: taskKeys.output(taskId) });
+ },
+ });
+}
+
+/**
+ * Helper to group tasks by status for display
+ */
+export function groupTasksByStatus(tasks: TaskSummary[]) {
+ const groups = {
+ running: [] as TaskSummary[],
+ pending: [] as TaskSummary[],
+ blocked: [] as TaskSummary[],
+ completed: [] as TaskSummary[],
+ };
+
+ for (const task of tasks) {
+ switch (task.status) {
+ case 'running':
+ case 'initializing':
+ case 'starting':
+ groups.running.push(task);
+ break;
+ case 'pending':
+ groups.pending.push(task);
+ break;
+ case 'blocked':
+ case 'paused':
+ groups.blocked.push(task);
+ break;
+ case 'done':
+ case 'failed':
+ case 'merged':
+ groups.completed.push(task);
+ break;
+ }
+ }
+
+ return groups;
+}
+
+/**
+ * Get counts for dashboard display
+ */
+export function getTaskCounts(tasks: TaskSummary[]) {
+ const counts = {
+ total: tasks.length,
+ running: 0,
+ pending: 0,
+ blocked: 0,
+ completed: 0,
+ failed: 0,
+ };
+
+ for (const task of tasks) {
+ switch (task.status) {
+ case 'running':
+ case 'initializing':
+ case 'starting':
+ counts.running++;
+ break;
+ case 'pending':
+ counts.pending++;
+ break;
+ case 'blocked':
+ case 'paused':
+ counts.blocked++;
+ break;
+ case 'done':
+ case 'merged':
+ counts.completed++;
+ break;
+ case 'failed':
+ counts.failed++;
+ break;
+ }
+ }
+
+ return counts;
+}