Files
photon-tbw/src/lib/app/session.ts
T

35 lines
778 B
TypeScript

import { browser } from '$app/environment'
import type { CommunityView } from '$lib/api/types'
interface SessionStorage {
lastSeenCommunity?: CommunityView
postDraft?: {
community: CommunityView | null
title: string
body?: string
image: FileList | null
url?: string
nsfw: boolean
loading: boolean
}
}
export const setSessionStorage = (
key: keyof SessionStorage,
value: SessionStorage[typeof key],
) => {
if (!browser) return
if (value == undefined) {
sessionStorage.removeItem(key)
} else {
sessionStorage.setItem(key, JSON.stringify(value))
}
}
export const getSessionStorage = (
key: keyof SessionStorage,
): SessionStorage[typeof key] => {
if (!browser) return
return JSON.parse(sessionStorage.getItem(key)!)
}