mirror of
https://github.com/spliit-app/spliit.git
synced 2026-03-02 03:16:13 +01:00
Mark a group as favorite (fixes #29)
This commit is contained in:
@@ -7,10 +7,13 @@ export const recentGroupsSchema = z.array(
|
||||
}),
|
||||
)
|
||||
|
||||
export const starredGroupsSchema = z.array(z.string())
|
||||
|
||||
export type RecentGroups = z.infer<typeof recentGroupsSchema>
|
||||
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)),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user