forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseAnnouncements.ts
More file actions
206 lines (176 loc) · 5.13 KB
/
Copy pathuseAnnouncements.ts
File metadata and controls
206 lines (176 loc) · 5.13 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'use client';
import useSWR from 'swr';
import { useAuthToken, authenticatedFetcher, useAuthFetch } from '@/hooks/useAuthToken';
export type AnnouncementType = 'changelog' | 'feature' | 'announcement';
export interface ChangelogItem {
title: string;
description: string;
icon?: string;
}
export interface ChangelogContent {
title: string;
changes: ChangelogItem[];
}
export interface FeatureStep {
title: string;
description: string;
image_url?: string;
video_url?: string;
highlight_text?: string;
}
export interface FeatureContent {
title: string;
steps: FeatureStep[];
}
export interface AnnouncementCTA {
text: string;
action: string;
}
export interface AnnouncementContent {
title: string;
body: string;
image_url?: string;
cta?: AnnouncementCTA;
}
export type TriggerType = 'immediate' | 'version_upgrade' | 'firmware_upgrade';
export type PlatformType = 'ios' | 'android';
export interface AnnouncementTargeting {
app_version_min?: string;
app_version_max?: string;
firmware_version_min?: string;
firmware_version_max?: string;
device_models?: string[];
platforms?: PlatformType[];
trigger?: TriggerType;
test_uids?: string[]; // If set, only these users see the announcement (for testing)
}
export interface AnnouncementDisplay {
priority?: number;
start_at?: string;
expires_at?: string;
dismissible?: boolean;
show_once?: boolean;
}
export interface Announcement {
id: string;
type: AnnouncementType;
created_at: string;
active: boolean;
// Legacy fields (kept for backward compatibility)
app_version?: string;
firmware_version?: string;
device_models?: string[];
expires_at?: string;
// New optional targeting and display fields
targeting?: AnnouncementTargeting;
display?: AnnouncementDisplay;
content: ChangelogContent | FeatureContent | AnnouncementContent;
}
export interface CreateAnnouncementData {
type: AnnouncementType;
// Legacy fields
app_version?: string;
firmware_version?: string;
device_models?: string[];
expires_at?: string;
// New optional targeting and display fields
targeting?: AnnouncementTargeting;
display?: AnnouncementDisplay;
content: ChangelogContent | FeatureContent | AnnouncementContent;
}
export interface UpdateAnnouncementData {
active?: boolean;
// Legacy fields
app_version?: string;
firmware_version?: string;
device_models?: string[];
expires_at?: string;
// New optional targeting and display fields
targeting?: AnnouncementTargeting;
display?: AnnouncementDisplay;
content?: ChangelogContent | FeatureContent | AnnouncementContent;
}
export function useAnnouncements(typeFilter?: AnnouncementType) {
const { token, loading: tokenLoading } = useAuthToken();
const { fetchWithAuth } = useAuthFetch();
const url = typeFilter ? `/api/omi/announcements?type=${typeFilter}` : '/api/omi/announcements';
const swrKey = token ? [url, token] : null;
const { data, error, isLoading, mutate } = useSWR<Announcement[]>(swrKey, authenticatedFetcher, {
revalidateOnFocus: false,
});
const createAnnouncement = async (announcementData: CreateAnnouncementData) => {
const id = crypto.randomUUID();
const res = await fetchWithAuth('/api/omi/announcements', {
method: 'POST',
body: JSON.stringify({ ...announcementData, id }),
});
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);
}
const result = await res.json();
mutate();
return result;
};
const updateAnnouncement = async (id: string, updates: UpdateAnnouncementData) => {
const res = await fetchWithAuth(`/api/omi/announcements/${id}`, {
method: 'PUT',
body: JSON.stringify(updates),
});
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);
}
const result = await res.json();
mutate();
return result;
};
const deleteAnnouncement = async (id: string, hardDelete = false) => {
const res = await fetchWithAuth(`/api/omi/announcements/${id}?hard=${hardDelete}`, {
method: 'DELETE',
});
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);
}
mutate();
return true;
};
const toggleActive = async (id: string, active: boolean) => {
// Optimistic update - update UI immediately
const previousData = data;
mutate(
data?.map((a) => (a.id === id ? { ...a, active } : a)),
false // Don't revalidate yet
);
try {
await updateAnnouncement(id, { active });
} catch (error) {
// Revert on error
mutate(previousData, false);
throw error;
}
};
return {
announcements: data || [],
isLoading: tokenLoading || isLoading,
error,
mutate,
createAnnouncement,
updateAnnouncement,
deleteAnnouncement,
toggleActive,
};
}