Extract admin dashboard from ghabilee-frontend2 into a dedicated Next.js app for backoffice.ghabilee.ir (no SEO indexing / Clarity).
64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
const STORAGE_PREFIX = 'ghabilee:event-create-draft'
|
|
const CURRENT_VERSION = 1
|
|
|
|
interface StoredEventDraft<T> {
|
|
version: number
|
|
savedAt: string
|
|
step: number
|
|
data: T
|
|
}
|
|
|
|
const storageKey = (userId: string) => `${STORAGE_PREFIX}:${userId}`
|
|
|
|
export function loadEventDraft<T>(userId: string): StoredEventDraft<T> | null {
|
|
if (typeof window === 'undefined') return null
|
|
|
|
try {
|
|
const raw = localStorage.getItem(storageKey(userId))
|
|
|
|
if (!raw) return null
|
|
|
|
const parsed = JSON.parse(raw) as Partial<StoredEventDraft<T>>
|
|
|
|
if (parsed.version !== CURRENT_VERSION || !parsed.data || typeof parsed.data !== 'object') {
|
|
localStorage.removeItem(storageKey(userId))
|
|
|
|
return null
|
|
}
|
|
|
|
return {
|
|
version: CURRENT_VERSION,
|
|
savedAt: typeof parsed.savedAt === 'string' ? parsed.savedAt : new Date().toISOString(),
|
|
step: typeof parsed.step === 'number' && parsed.step >= 1 && parsed.step <= 4 ? parsed.step : 1,
|
|
data: parsed.data,
|
|
}
|
|
} catch {
|
|
localStorage.removeItem(storageKey(userId))
|
|
|
|
return null
|
|
}
|
|
}
|
|
|
|
export function saveEventDraft(userId: string, data: StoredEventDraft<unknown>['data'], step: number): void {
|
|
if (typeof window === 'undefined') return
|
|
|
|
const draft: StoredEventDraft<unknown> = {
|
|
version: CURRENT_VERSION,
|
|
savedAt: new Date().toISOString(),
|
|
step: Math.min(Math.max(step, 1), 4),
|
|
data,
|
|
}
|
|
|
|
try {
|
|
localStorage.setItem(storageKey(userId), JSON.stringify(draft))
|
|
} catch {
|
|
// Storage may be unavailable or full; the in-memory wizard remains usable.
|
|
}
|
|
}
|
|
|
|
export function clearEventDraft(userId: string): void {
|
|
if (typeof window === 'undefined') return
|
|
|
|
localStorage.removeItem(storageKey(userId))
|
|
}
|