Pages: restore tag editing via a Tags picker in the editor
release gate / check (push) Canceled after 3m5s
release gate / check (push) Canceled after 3m5s
The Library redesign removed the Untagged-Pages drag-to-tag source, leaving
no UI to tag a page. Add a Tags popover in the page editor meta row: current
tags as removable chips plus a toggle list of the owner's tags, using the
existing POST/DELETE /api/pages/{id}/tags metadata endpoints.
This commit is contained in:
@@ -4,7 +4,7 @@ import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { toast } from 'sonner'
|
||||
import {
|
||||
CornerUpLeft, Download, FileText, History, Layers, Link2, Pin, PinOff, Plus, RotateCcw, Trash2, X,
|
||||
CornerUpLeft, Download, FileText, History, Layers, Link2, Pin, PinOff, Plus, RotateCcw, Tags, Trash2, X,
|
||||
} from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
@@ -65,6 +65,8 @@ export function PagesClient({ initialId }: { initialId: string | null }) {
|
||||
const [pageLinkQuery, setPageLinkQuery] = useState('')
|
||||
const [embedOpen, setEmbedOpen] = useState(false)
|
||||
const [collections, setCollections] = useState<{ id: string; name: string; icon: string | null; views?: { id: string; name: string }[] }[]>([])
|
||||
const [tagsOpen, setTagsOpen] = useState(false)
|
||||
const [allTags, setAllTags] = useState<{ id: string; name: string; color: string }[]>([])
|
||||
const [historyOpen, setHistoryOpen] = useState(false)
|
||||
const [revisions, setRevisions] = useState<PageRevisionSummary[]>([])
|
||||
const [revision, setRevision] = useState<PageRevisionDetail | null>(null)
|
||||
@@ -374,6 +376,36 @@ export function PagesClient({ initialId }: { initialId: string | null }) {
|
||||
.catch(() => { /* transient; reopening retries */ })
|
||||
}, [embedOpen])
|
||||
|
||||
// Page tags: the picker lists the owner's tags; toggling uses the dedicated
|
||||
// metadata endpoints (no content version involved).
|
||||
useEffect(() => {
|
||||
if (!tagsOpen) return
|
||||
fetch('/api/tags')
|
||||
.then((res) => (res.ok ? res.json() : []))
|
||||
.then((data) => setAllTags(Array.isArray(data) ? data : []))
|
||||
.catch(() => { /* transient; reopening retries */ })
|
||||
}, [tagsOpen])
|
||||
|
||||
const toggleTag = async (tagId: string, attach: boolean) => {
|
||||
if (!activeId || page?.id !== activeId) return
|
||||
const res = attach
|
||||
? await fetch(`/api/pages/${activeId}/tags`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ tagId }) })
|
||||
: await fetch(`/api/pages/${activeId}/tags/${tagId}`, { method: 'DELETE' })
|
||||
if (!res.ok) { toast.error('Could not update tags'); return }
|
||||
const tag = allTags.find((t) => t.id === tagId)
|
||||
setPage((cur) => {
|
||||
if (!cur || cur.id !== activeId) return cur
|
||||
const tags = cur.tags ?? []
|
||||
return {
|
||||
...cur,
|
||||
tags: attach
|
||||
? (tags.some((t) => t.tag.id === tagId) ? tags : [...tags, { tag: tag ?? { id: tagId, name: '', color: '#888888' } }])
|
||||
: tags.filter((t) => t.tag.id !== tagId),
|
||||
}
|
||||
})
|
||||
notifyPagesChanged()
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!linkOpen) return
|
||||
const q = linkQuery.trim()
|
||||
@@ -490,6 +522,35 @@ export function PagesClient({ initialId }: { initialId: string | null }) {
|
||||
{parentOptions.map((p) => <option key={p.id} value={p.id}>{p.title}</option>)}
|
||||
</select>
|
||||
</label>
|
||||
<Popover open={tagsOpen} onOpenChange={setTagsOpen}>
|
||||
<PopoverTrigger asChild>
|
||||
<Button size="sm" variant="outline"><Tags className="mr-1 h-3.5 w-3.5" />Tags{(page?.tags?.length ?? 0) > 0 ? ` (${page?.tags?.length})` : ''}</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent align="start" className="w-72 max-w-[calc(100vw-2rem)] p-2">
|
||||
<div className="mb-2 flex flex-wrap gap-1">
|
||||
{(page?.tags ?? []).map((t) => (
|
||||
<span key={t.tag.id} className="flex items-center gap-1 rounded px-1.5 py-0.5 text-xs" style={{ backgroundColor: `${t.tag.color}22`, color: t.tag.color }}>
|
||||
{t.tag.name}
|
||||
<button onClick={() => void toggleTag(t.tag.id, false)} aria-label={`Remove tag ${t.tag.name}`} className="hover:opacity-70"><X className="h-3 w-3" /></button>
|
||||
</span>
|
||||
))}
|
||||
{(page?.tags ?? []).length === 0 && <span className="text-xs text-muted-foreground">No tags</span>}
|
||||
</div>
|
||||
<div className="max-h-64 overflow-y-auto">
|
||||
{allTags.map((t) => {
|
||||
const on = (page?.tags ?? []).some((x) => x.tag.id === t.id)
|
||||
return (
|
||||
<button key={t.id} onClick={() => void toggleTag(t.id, !on)} className="flex w-full items-center gap-2 rounded px-2 py-1.5 text-left text-sm hover:bg-muted">
|
||||
<span className="h-2.5 w-2.5 shrink-0 rounded-full" style={{ backgroundColor: t.color }} />
|
||||
<span className="truncate">{t.name}</span>
|
||||
{on && <span className="ml-auto text-xs text-muted-foreground">✓</span>}
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
{allTags.length === 0 && <p className="px-2 py-3 text-center text-xs text-muted-foreground">No tags yet. Create one from Tags first.</p>}
|
||||
</div>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
<Popover open={pageLinkOpen} onOpenChange={(open) => { setPageLinkOpen(open); if (!open) setPageLinkQuery('') }}>
|
||||
<PopoverTrigger asChild><Button size="sm" variant="outline"><FileText className="mr-1 h-3.5 w-3.5" />Link a page</Button></PopoverTrigger>
|
||||
<PopoverContent align="start" className="w-80 max-w-[calc(100vw-2rem)] p-2">
|
||||
|
||||
Reference in New Issue
Block a user