forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseHomeTasks.ts
More file actions
57 lines (49 loc) · 1.98 KB
/
Copy pathuseHomeTasks.ts
File metadata and controls
57 lines (49 loc) · 1.98 KB
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
'use client';
import { useCallback, useMemo, useState } from 'react';
import { getActionItems, toggleActionItemCompleted } from '@/lib/api';
import { useAsyncResource } from '@/hooks/useAsyncResource';
import type { ActionItem } from '@/types/conversation';
/**
* The open action items the hub puts in front of you.
*
* Deliberately not `useActionItems`: that one pulls 500 items to drive the
* Tasks page's grouping, calendar and stats, none of which the hub renders. The
* hub shows a handful of rows, so it asks for a handful.
*/
const LIMIT = 20;
export interface UseHomeTasksReturn {
items: ActionItem[];
loading: boolean;
error: string | null;
complete: (id: string) => Promise<void>;
}
export function useHomeTasks(): UseHomeTasksReturn {
// Listed without `completed: false`: that is a server-side equality filter,
// and legacy documents whose `completed` field is missing or null are excluded
// by it even though every other surface treats them as open. The unfiltered
// list is active-first and includes those documents; openness is decided here
// the same way the Tasks page decides it.
const { data, loading, error } = useAsyncResource('home:tasks', () =>
getActionItems({ limit: LIMIT }),
);
// Completed ids are held here rather than refetching: the row should leave
// the list the moment it is ticked, and the hub has no other view of it.
const [completedIds, setCompletedIds] = useState<string[]>([]);
const items = useMemo(
() =>
(data?.items ?? []).filter(
(item) => !item.completed && !completedIds.includes(item.id),
),
[data, completedIds],
);
const complete = useCallback(async (id: string) => {
setCompletedIds((prev) => [...prev, id]);
try {
await toggleActionItemCompleted(id, true);
} catch (err) {
console.error('Failed to complete task:', err);
setCompletedIds((prev) => prev.filter((completed) => completed !== id));
}
}, []);
return { items, loading, error, complete };
}