Files
hackerspace.sh/components/comments/comment-thread.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

128 lines
4.6 KiB
TypeScript

'use client'
import { useState } from 'react'
import { useRouter } from 'next/navigation'
import { toast } from 'sonner'
import ReactMarkdown from 'react-markdown'
import remarkGfm from 'remark-gfm'
import { addComment, deleteComment } from '@/lib/actions'
import { Trash2, MessageCircle } from 'lucide-react'
export type CommentEntityType = 'forum_thread' | 'proposal' | 'incident' | 'policy'
export interface CommentRow {
id: string
body: string
created_at: string
edited_at: string | null
author_id: string | null
author_name: string | null
}
interface Props {
entityType: CommentEntityType
entityId: string
comments: CommentRow[]
currentMemberId: string | null
canModerate: boolean
locked?: boolean
}
export function CommentThread({ entityType, entityId, comments, currentMemberId, canModerate, locked }: Props) {
const router = useRouter()
const [body, setBody] = useState('')
const [saving, setSaving] = useState(false)
async function handleSubmit(e: React.FormEvent) {
e.preventDefault()
if (!body.trim() || locked) return
setSaving(true)
const result = await addComment({ entity_type: entityType, entity_id: entityId, body: body.trim() })
setSaving(false)
if ('error' in result && result.error) { toast.error(result.error); return }
setBody('')
router.refresh()
}
async function handleDelete(commentId: string) {
if (!confirm('Delete this comment?')) return
const result = await deleteComment(commentId)
if (result.error) { toast.error(result.error); return }
toast.success('Comment deleted')
router.refresh()
}
return (
<section className="bg-card border border-border rounded p-5">
<div className="flex items-center gap-2 mb-4">
<MessageCircle className="w-4 h-4 text-muted-foreground" />
<p className="font-mono text-[10px] tracking-widest text-muted-foreground uppercase">
Discussion{comments.length > 0 ? ` (${comments.length})` : ''}
</p>
</div>
{comments.length === 0 && (
<p className="font-sans text-sm text-muted-foreground mb-4">No comments yet.</p>
)}
{comments.length > 0 && (
<ul className="space-y-4 mb-6">
{comments.map(c => {
const mine = currentMemberId && c.author_id === currentMemberId
const canDelete = mine || canModerate
return (
<li key={c.id} className="bg-background/60 border border-border rounded p-3">
<div className="flex items-center gap-2 mb-1.5">
<span className="font-sans text-xs font-medium text-foreground">{c.author_name ?? 'Removed member'}</span>
<span className="font-mono text-[10px] text-muted-foreground">
{new Date(c.created_at).toLocaleString()}
{c.edited_at && ' · edited'}
</span>
{canDelete && (
<button
onClick={() => handleDelete(c.id)}
className="ml-auto text-muted-foreground hover:text-red-500 transition p-1"
title="Delete"
>
<Trash2 className="w-3.5 h-3.5" />
</button>
)}
</div>
<div className="prose prose-sm prose-neutral dark:prose-invert max-w-none break-words">
<ReactMarkdown remarkPlugins={[remarkGfm]}>{c.body}</ReactMarkdown>
</div>
</li>
)
})}
</ul>
)}
{locked ? (
<p className="font-sans text-sm text-muted-foreground border-t border-border pt-3">
This thread is locked. New comments are disabled.
</p>
) : (
<form onSubmit={handleSubmit} className="border-t border-border pt-4 space-y-2">
<textarea
value={body}
onChange={e => setBody(e.target.value)}
rows={3}
maxLength={10000}
placeholder="Write a comment (markdown supported)..."
className="w-full bg-background border border-border text-foreground font-sans text-sm rounded px-3 py-2 focus:outline-none focus:border-primary"
/>
<div className="flex items-center justify-end">
<button
type="submit"
disabled={saving || !body.trim()}
className="bg-primary text-white text-xs font-sans px-3 py-1.5 rounded hover:bg-primary/90 transition disabled:opacity-50"
>
{saving ? 'Posting...' : 'Post comment'}
</button>
</div>
</form>
)}
</section>
)
}