profit-planet-frontend/src/app/utils/dashboardPlatforms.ts
2026-03-15 18:34:19 +01:00

160 lines
4.8 KiB
TypeScript

export type DashboardPlatformIconName =
| 'ShoppingBagIcon'
| 'LinkIcon'
| 'UsersIcon'
| 'UserCircleIcon'
export type DashboardPlatformColorClass =
| 'bg-blue-500'
| 'bg-teal-500'
| 'bg-amber-600'
| 'bg-purple-500'
export type DashboardPlatform = {
id: string
title: string
description: string
href: string
icon: DashboardPlatformIconName
color: DashboardPlatformColorClass
isActive: boolean
disabled?: boolean
disabledText?: string
}
export const DASHBOARD_PLATFORMS_STORAGE_KEY = 'pp.dashboardPlatforms.v1'
export const DASHBOARD_PLATFORMS_UPDATED_EVENT = 'pp:dashboard-platforms-updated'
export const DASHBOARD_PLATFORMS_ICON_OPTIONS: Array<{ value: DashboardPlatformIconName; label: string }> = [
{ value: 'ShoppingBagIcon', label: 'Shopping Bag' },
{ value: 'LinkIcon', label: 'Link' },
{ value: 'UsersIcon', label: 'Users' },
{ value: 'UserCircleIcon', label: 'User Profile' }
]
export const DASHBOARD_PLATFORMS_COLOR_OPTIONS: Array<{ value: DashboardPlatformColorClass; label: string }> = [
{ value: 'bg-blue-500', label: 'Blue' },
{ value: 'bg-teal-500', label: 'Teal' },
{ value: 'bg-amber-600', label: 'Amber' },
{ value: 'bg-purple-500', label: 'Purple' }
]
export const DEFAULT_DASHBOARD_PLATFORMS: DashboardPlatform[] = [
{
id: 'shop',
title: 'Browse Shop',
description: 'Explore sustainable products',
icon: 'ShoppingBagIcon',
href: '/shop',
color: 'bg-blue-500',
isActive: true,
disabledText: 'This is currently disabled.'
},
{
id: 'affiliate-links',
title: 'Browse Affiliate Links',
description: 'Discover affiliate offers and links',
icon: 'LinkIcon',
href: '/affiliate-links',
color: 'bg-teal-500',
isActive: true
},
{
id: 'referral-management',
title: 'Referral Management',
description: 'Create and manage referral links',
icon: 'UsersIcon',
href: '/referral-management',
color: 'bg-amber-600',
isActive: true
},
{
id: 'profile',
title: 'Edit Profile',
description: 'Update your information',
icon: 'UserCircleIcon',
href: '/profile',
color: 'bg-purple-500',
isActive: true
}
]
function isRecord(value: unknown): value is Record<string, unknown> {
return !!value && typeof value === 'object' && !Array.isArray(value)
}
function createFallbackId(): string {
return `platform_${Date.now()}_${Math.random().toString(16).slice(2)}`
}
function normalizePlatform(input: unknown): DashboardPlatform | null {
if (!isRecord(input)) return null
const id = typeof input.id === 'string' && input.id.trim() ? input.id.trim() : createFallbackId()
const title = typeof input.title === 'string' ? input.title : ''
const description = typeof input.description === 'string' ? input.description : ''
const href = typeof input.href === 'string' ? input.href : ''
const icon = input.icon as DashboardPlatformIconName
const color = input.color as DashboardPlatformColorClass
const isActive = typeof input.isActive === 'boolean' ? input.isActive : true
const disabled = typeof input.disabled === 'boolean' ? input.disabled : false
const disabledText = typeof input.disabledText === 'string' ? input.disabledText : undefined
const iconOk = DASHBOARD_PLATFORMS_ICON_OPTIONS.some(o => o.value === icon)
const colorOk = DASHBOARD_PLATFORMS_COLOR_OPTIONS.some(o => o.value === color)
if (!title.trim() || !href.trim() || !iconOk || !colorOk) return null
return {
id,
title,
description,
href,
icon,
color,
isActive,
disabled,
disabledText
}
}
export function loadDashboardPlatforms(): DashboardPlatform[] {
if (typeof window === 'undefined') return DEFAULT_DASHBOARD_PLATFORMS
try {
const raw = window.localStorage.getItem(DASHBOARD_PLATFORMS_STORAGE_KEY)
if (!raw) return DEFAULT_DASHBOARD_PLATFORMS
const parsed = JSON.parse(raw) as unknown
if (!Array.isArray(parsed)) return DEFAULT_DASHBOARD_PLATFORMS
const normalized = parsed
.map(normalizePlatform)
.filter((x): x is DashboardPlatform => Boolean(x))
return normalized.length ? normalized : DEFAULT_DASHBOARD_PLATFORMS
} catch {
return DEFAULT_DASHBOARD_PLATFORMS
}
}
export function saveDashboardPlatforms(platforms: DashboardPlatform[]): void {
if (typeof window === 'undefined') return
window.localStorage.setItem(DASHBOARD_PLATFORMS_STORAGE_KEY, JSON.stringify(platforms))
window.dispatchEvent(new Event(DASHBOARD_PLATFORMS_UPDATED_EVENT))
}
export function subscribeDashboardPlatformsUpdated(callback: () => void): () => void {
if (typeof window === 'undefined') return () => {}
const handler = () => callback()
window.addEventListener(DASHBOARD_PLATFORMS_UPDATED_EVENT, handler)
window.addEventListener('storage', handler)
return () => {
window.removeEventListener(DASHBOARD_PLATFORMS_UPDATED_EVENT, handler)
window.removeEventListener('storage', handler)
}
}