Files
hackerspace.sh/app/(app)/incidents/[id]/page.tsx
T
Moheeb Zara de51000584 feat: forum, polymorphic comments, custom tiers, multi-invite, secrets encryption
Big feature batch driven by the super-audit findings and the user's
follow-up requests.

Schema migration 021 (folded into schema.sql Section 13):
- forum_threads + polymorphic comments table keyed on (entity_type,
  entity_id). Entity types: forum_thread, proposal, incident, policy.
  Trigger maintains forum_threads.comment_count and last_comment_at.
- space_tiers per-space with monthly_price_cents, billing_cadence,
  is_system, sort_order. Trigger seeds plus/basic/associate on every
  space INSERT and backfills existing spaces. space_members.tier_id FK
  added and backfilled from the legacy enum value.
- space_role_labels (rename/recolor built-in roles per space).
  space_custom_roles + space_member_custom_roles for non-privileged
  extra labels. RLS gates by admin/board.
- space_invites: multi-code invites with expires_at, max_uses,
  uses_count, is_enabled. Legacy spaces.invite_code backfilled as a
  permanent enabled invite.
- secrets.encrypted_value bytea + encryption_version. Secrets RLS
  tightened. New comms_channels INSERT policy: any member can create;
  default channels are protected from deletion.
- knowledge_base.render_markdown boolean (default true).

Secrets encryption-at-rest:
- AES-256-GCM in lib/secrets/crypto.ts. Master key from
  SECRETS_ENCRYPTION_KEY env (64 hex chars).
- New revealSecret(id) server action: only path to plaintext, writes
  secret.revealed to activity_log.
- /ops/page.tsx no longer selects value or encrypted_value on the list
  endpoint - the original screenshot bug where ciphertext was already
  in the page bundle before "Reveal" is fixed.

UI:
- /forum index, /forum/new, /forum/[id]. Pin/lock/delete actions for
  admin/board.
- CommentThread component embedded on /proposals/[id], /incidents/[id],
  /policies/[slug]. Markdown rendering via react-markdown + remark-gfm.
- /comms sidebar: inline "+ New channel" form.
- /ops KB: row click opens markdown-rendering viewer modal.
- /settings: new Tiers tab and Invites tab. Inline price edit and
  archive/delete on tiers; copy/enable/disable/delete on invites.
- AtlasLogo replaces the terminal icon on the landing nav and footer.
  public/logo.svg wired as the favicon in app/layout.tsx metadata.
- Sidebar has a Forum nav link.

Server actions added:
- lib/actions/forum.ts, comms.ts, tiers.ts, roles.ts, invites.ts.
- lib/actions/secrets.ts rewritten with updateSecret + revealSecret.

Auth flow:
- joinSpace honors codes from space_invites first (enabled, expiry,
  use cap), falls back to legacy spaces.invite_code, increments
  uses_count on success.

Env:
- SECRETS_ENCRYPTION_KEY documented in .env.example and set on the
  Droplet's /opt/hackerspace-ops/.env.production.

README: live site link added (https://hackerspace.sh).

266/266 unit tests pass. Production build clean. Migration 021 will be
applied on the next deploy via /opt/hackerspace-ops/deploy.sh's
idempotent migration runner.
2026-05-14 17:47:52 -07:00

136 lines
5.3 KiB
TypeScript

import Link from 'next/link'
import { redirect, notFound } from 'next/navigation'
import { createClient } from '@/lib/supabase/server'
import type { Incident, IncidentUpdateRow } from '@/lib/types'
import { IncidentActions } from './incident-actions'
import { MarkdownBody } from '@/components/markdown'
import { CommentThread } from '@/components/comments/comment-thread'
import { loadComments } from '@/components/comments/load-comments'
export const dynamic = 'force-dynamic'
export default async function IncidentDetailPage({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params
const supabase = await createClient()
const { data: { user } } = await supabase.auth.getUser()
if (!user) redirect('/login')
const { data: member } = await supabase
.from('space_members')
.select('id, space_id, role, display_name')
.eq('user_id', user.id)
.in('status', ['current', 'unverified', 'late'])
.maybeSingle()
if (!member?.space_id) redirect('/signup')
const { data: incidentRaw } = await supabase
.from('incidents')
.select('*')
.eq('id', id)
.eq('space_id', member.space_id)
.maybeSingle()
if (!incidentRaw) notFound()
const incident = incidentRaw as unknown as Incident
const isAdminOrBoard = member.role === 'admin' || member.role === 'board'
const isReporter = incident.reporter_id === member.id
const comments = await loadComments(supabase, 'incident', incident.id)
const { data: updatesRaw } = await supabase
.from('incident_updates')
.select('*')
.eq('incident_id', incident.id)
.order('created_at', { ascending: true })
const updates = (updatesRaw ?? []) as unknown as IncidentUpdateRow[]
const decisionMakers = incident.decision_maker_ids.length
? await supabase
.from('space_members')
.select('id, display_name')
.in('id', incident.decision_maker_ids)
: { data: [] as Array<{ id: string; display_name: string | null }> }
return (
<div className="min-h-screen bg-background">
<div className="bg-sidebar px-4 md:px-6 py-3 flex items-center gap-3">
<Link href="/incidents" className="text-white/70 hover:text-white font-sans text-sm">
← Incidents
</Link>
</div>
<div className="p-4 md:p-6 max-w-3xl space-y-6">
<header className="bg-card rounded border border-border p-5">
<h1 className="font-sans text-xl font-semibold text-foreground mb-2">{incident.title}</h1>
<p className="font-mono text-[10px] text-muted-foreground mb-4">
{incident.category} · {incident.severity} · status: {incident.status.replace(/_/g, ' ')}
{incident.is_anonymous ? ' · filed anonymously' : ''}
{' · '}
filed {new Date(incident.created_at).toLocaleString()}
</p>
<MarkdownBody content={incident.body} />
</header>
{incident.disposition && (
<section className="bg-card rounded border border-border p-5">
<p className="font-mono text-[10px] tracking-widest text-muted-foreground uppercase mb-3">
Disposition
</p>
<MarkdownBody content={incident.disposition} />
{decisionMakers.data && decisionMakers.data.length > 0 && (
<p className="font-mono text-[10px] text-muted-foreground mt-3">
Decision: {decisionMakers.data.map(m => m.display_name ?? 'Unknown').join(', ')}
{incident.decided_at ? ` · ${new Date(incident.decided_at).toLocaleString()}` : ''}
</p>
)}
</section>
)}
<section className="bg-card rounded border border-border p-5">
<p className="font-mono text-[10px] tracking-widest text-muted-foreground uppercase mb-3">
Updates ({updates.length})
</p>
{updates.length === 0 ? (
<p className="font-sans text-sm text-muted-foreground">No updates posted yet.</p>
) : (
<ul className="divide-y divide-border">
{updates.map(u => (
<li key={u.id} className="py-3">
<div className="flex items-center gap-2 mb-1">
<span className="font-sans text-sm font-medium text-foreground">{u.author_name ?? 'Unknown'}</span>
<span className="font-mono text-[10px] text-muted-foreground">
{u.visibility.replace(/_/g, ' ')}
</span>
<span className="font-mono text-[10px] text-muted-foreground/60 ml-auto">
{new Date(u.created_at).toLocaleString()}
</span>
</div>
<MarkdownBody content={u.body} />
</li>
))}
</ul>
)}
</section>
<IncidentActions
incidentId={incident.id}
status={incident.status}
isAdminOrBoard={isAdminOrBoard}
isReporter={isReporter}
hasAppeal={incident.appeal_proposal_id !== null}
appealProposalId={incident.appeal_proposal_id}
/>
<CommentThread
entityType="incident"
entityId={incident.id}
comments={comments}
currentMemberId={member.id}
canModerate={isAdminOrBoard}
/>
</div>
</div>
)
}