forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseSummaryApps.ts
More file actions
59 lines (49 loc) · 1.44 KB
/
Copy pathuseSummaryApps.ts
File metadata and controls
59 lines (49 loc) · 1.44 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
58
59
'use client';
import useSWR from 'swr';
import { useAuthToken, authenticatedFetcher, useAuthFetch } from '@/hooks/useAuthToken';
export function useSummaryApps() {
const { token, loading: tokenLoading } = useAuthToken();
const { fetchWithAuth } = useAuthFetch();
const swrKey = token ? ['/api/omi/summary-apps', token] : null;
const { data, error, isLoading, mutate } = useSWR<any[]>(swrKey, authenticatedFetcher, {
revalidateOnFocus: false,
});
const addSummaryApp = async (appId: string) => {
const res = await fetchWithAuth('/api/omi/summary-apps', {
method: 'POST',
body: JSON.stringify({ appId }),
});
if (!res.ok) {
let message = `HTTP ${res.status}`;
try {
const j = await res.json();
message = j?.error || j?.message || message;
} catch {}
throw new Error(message);
}
return res.json();
};
const removeSummaryApp = async (appId: string) => {
const res = await fetchWithAuth('/api/omi/summary-apps', {
method: 'DELETE',
body: JSON.stringify({ appId }),
});
if (!res.ok) {
let message = `HTTP ${res.status}`;
try {
const j = await res.json();
message = j?.error || j?.message || message;
} catch {}
throw new Error(message);
}
return res.json();
};
return {
summaryApps: data,
isLoading: tokenLoading || isLoading,
error,
mutate,
addSummaryApp,
removeSummaryApp,
};
}