diff options
| author | Sebastien Castiel <sebastien@castiel.me> | 2023-12-05 17:39:05 -0500 |
|---|---|---|
| committer | Sebastien Castiel <sebastien@castiel.me> | 2023-12-05 17:39:05 -0500 |
| commit | ed55c696cd083bfaa5429082a9c5edd4ebde0cd8 (patch) | |
| tree | 9f387881f85716bcb4a05d6fce13bfe59fbc9c17 /src/app/groups/recent-groups-helpers.ts | |
| parent | 1fd6e48807d455f66d9cd79db64cbf9e9f947c6c (diff) | |
First version
Diffstat (limited to 'src/app/groups/recent-groups-helpers.ts')
| -rw-r--r-- | src/app/groups/recent-groups-helpers.ts | 30 |
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)]), + ) +} |
