diff --git a/src/app/groups/recent-group-list.tsx b/src/app/groups/recent-group-list.tsx index b8f8ff6..0491650 100644 --- a/src/app/groups/recent-group-list.tsx +++ b/src/app/groups/recent-group-list.tsx @@ -1,11 +1,18 @@ 'use client' import { getGroupsAction } from '@/app/groups/actions' -import { getRecentGroups } from '@/app/groups/recent-groups-helpers' +import { + getRecentGroups, + getStarredGroups, + starGroup, + unstarGroup, +} from '@/app/groups/recent-groups-helpers' import { Button } from '@/components/ui/button' import { Skeleton } from '@/components/ui/skeleton' import { getGroups } from '@/lib/api' -import { Calendar, Loader2, Users } from 'lucide-react' +import { StarFilledIcon } from '@radix-ui/react-icons' +import { Calendar, Loader2, Star, Users } from 'lucide-react' import Link from 'next/link' +import { useRouter } from 'next/navigation' import { useEffect, useState } from 'react' import { z } from 'zod' @@ -19,11 +26,12 @@ type RecentGroups = z.infer type State = | { status: 'pending' } - | { status: 'partial'; groups: RecentGroups } + | { status: 'partial'; groups: RecentGroups; starredGroups: string[] } | { status: 'complete' groups: RecentGroups groupsDetails: Awaited> + starredGroups: string[] } type Props = { @@ -35,12 +43,20 @@ export function RecentGroupList() { useEffect(() => { const groupsInStorage = getRecentGroups() - setState({ status: 'partial', groups: groupsInStorage }) + const starredGroups = getStarredGroups() + setState({ status: 'partial', groups: groupsInStorage, starredGroups }) getGroupsAction(groupsInStorage.map((g) => g.id)).then((groupsDetails) => { - setState({ status: 'complete', groups: groupsInStorage, groupsDetails }) + setState({ + status: 'complete', + groups: groupsInStorage, + groupsDetails, + starredGroups, + }) }) }, []) + const router = useRouter() + if (state.status === 'pending') { return (

@@ -66,49 +82,85 @@ export function RecentGroupList() { return (

) } diff --git a/src/app/groups/recent-groups-helpers.ts b/src/app/groups/recent-groups-helpers.ts index 5d52141..5ccb5ae 100644 --- a/src/app/groups/recent-groups-helpers.ts +++ b/src/app/groups/recent-groups-helpers.ts @@ -7,10 +7,13 @@ export const recentGroupsSchema = z.array( }), ) +export const starredGroupsSchema = z.array(z.string()) + export type RecentGroups = z.infer export type RecentGroup = RecentGroups[number] const STORAGE_KEY = 'recentGroups' +const STARRED_GROUPS_STORAGE_KEY = 'starredGroups' export function getRecentGroups() { const groupsInStorageJson = localStorage.getItem(STORAGE_KEY) @@ -28,3 +31,28 @@ export function saveRecentGroup(group: RecentGroup) { JSON.stringify([group, ...recentGroups.filter((rg) => rg.id !== group.id)]), ) } + +export function getStarredGroups() { + const starredGroupsJson = localStorage.getItem(STARRED_GROUPS_STORAGE_KEY) + const starredGroupsRaw = starredGroupsJson + ? JSON.parse(starredGroupsJson) + : [] + const parseResult = starredGroupsSchema.safeParse(starredGroupsRaw) + return parseResult.success ? parseResult.data : [] +} + +export function starGroup(groupId: string) { + const starredGroups = getStarredGroups() + localStorage.setItem( + STARRED_GROUPS_STORAGE_KEY, + JSON.stringify([...starredGroups, groupId]), + ) +} + +export function unstarGroup(groupId: string) { + const starredGroups = getStarredGroups() + localStorage.setItem( + STARRED_GROUPS_STORAGE_KEY, + JSON.stringify(starredGroups.filter((g) => g !== groupId)), + ) +}