import Link from 'next/link' import { redirect } from 'next/navigation' import { createClient } from '@/lib/supabase/server' import type { Policy, PolicyStatus } from '@/lib/types' export const dynamic = 'force-dynamic' const STATUS_STYLE: Record = { draft: 'text-muted-foreground bg-muted', active: 'text-primary bg-primary/10', deprecated: 'text-orange-600 bg-orange-50', superseded: 'text-muted-foreground bg-muted', } export default async function PoliciesPage() { const supabase = await createClient() const { data: { user } } = await supabase.auth.getUser() if (!user) redirect('/login') const { data: member } = await supabase .from('space_members') .select('space_id, role') .eq('user_id', user.id) .in('status', ['current', 'unverified', 'late']) .maybeSingle() if (!member?.space_id) redirect('/signup') const { data: policiesRaw } = await supabase .from('policies') .select('*') .eq('space_id', member.space_id) .order('slug', { ascending: true }) .order('version', { ascending: false }) const all = (policiesRaw ?? []) as unknown as Policy[] // For each slug, show the highest-version active row; fall back to highest-version overall. const bySlug = new Map() for (const p of all) { const existing = bySlug.get(p.slug) if (!existing) bySlug.set(p.slug, p) else if (p.status === 'active' && existing.status !== 'active') bySlug.set(p.slug, p) else if (p.status === existing.status && p.version > existing.version) bySlug.set(p.slug, p) } const summary = Array.from(bySlug.values()).sort((a, b) => a.slug.localeCompare(b.slug)) const isAdminOrBoard = member.role === 'admin' || member.role === 'board' return (

Policies

{isAdminOrBoard && ( New policy )}
{summary.length === 0 ? (

No policies yet.

Bylaws, code of conduct, station rules. Each lives here with full version history.

) : ( summary.map(p => (

{p.title}

{p.status}

{p.slug} {p.section_ref ? ` · §${p.section_ref}` : ''} {' · v'}{p.version}

)) )}
) }