aboutsummaryrefslogtreecommitdiffstats
path: root/src/app/groups/recent-groups-helpers.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/app/groups/recent-groups-helpers.ts')
-rw-r--r--src/app/groups/recent-groups-helpers.ts30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/app/groups/recent-groups-helpers.ts b/src/app/groups/recent-groups-helpers.ts
new file mode 100644
index 0000000..5d52141
--- /dev/null
+++ b/src/app/groups/recent-groups-helpers.ts
@@ -0,0 +1,30 @@
+import { z } from 'zod'
+
+export const recentGroupsSchema = z.array(
+ z.object({
+ id: z.string().min(1),
+ name: z.string(),
+ }),
+)
+
+export type RecentGroups = z.infer<typeof recentGroupsSchema>
+export type RecentGroup = RecentGroups[number]
+
+const STORAGE_KEY = 'recentGroups'
+
+export function getRecentGroups() {
+ const groupsInStorageJson = localStorage.getItem(STORAGE_KEY)
+ const groupsInStorageRaw = groupsInStorageJson
+ ? JSON.parse(groupsInStorageJson)
+ : []
+ const parseResult = recentGroupsSchema.safeParse(groupsInStorageRaw)
+ return parseResult.success ? parseResult.data : []
+}
+
+export function saveRecentGroup(group: RecentGroup) {
+ const recentGroups = getRecentGroups()
+ localStorage.setItem(
+ STORAGE_KEY,
+ JSON.stringify([group, ...recentGroups.filter((rg) => rg.id !== group.id)]),
+ )
+}