Pages: create a tag inline from the Tags popover and attach it
release gate / check (push) Canceled after 0s

Adds a name input + color swatches + Add to the page Tags popover: creates
the tag via POST /api/tags and attaches it to the page in one step, alongside
the existing toggle list of owner tags.
This commit is contained in:
Andrew S Erwin
2026-12-01 08:02:03 -06:00
parent d622a42894
commit 11947fcbb5
+40
View File
@@ -67,6 +67,8 @@ export function PagesClient({ initialId }: { initialId: string | null }) {
const [collections, setCollections] = useState<{ id: string; name: string; icon: string | null; views?: { id: string; name: string }[] }[]>([]) const [collections, setCollections] = useState<{ id: string; name: string; icon: string | null; views?: { id: string; name: string }[] }[]>([])
const [tagsOpen, setTagsOpen] = useState(false) const [tagsOpen, setTagsOpen] = useState(false)
const [allTags, setAllTags] = useState<{ id: string; name: string; color: string }[]>([]) const [allTags, setAllTags] = useState<{ id: string; name: string; color: string }[]>([])
const [newTagName, setNewTagName] = useState('')
const [newTagColor, setNewTagColor] = useState('#8b5cf6')
const [historyOpen, setHistoryOpen] = useState(false) const [historyOpen, setHistoryOpen] = useState(false)
const [revisions, setRevisions] = useState<PageRevisionSummary[]>([]) const [revisions, setRevisions] = useState<PageRevisionSummary[]>([])
const [revision, setRevision] = useState<PageRevisionDetail | null>(null) const [revision, setRevision] = useState<PageRevisionDetail | null>(null)
@@ -406,6 +408,22 @@ export function PagesClient({ initialId }: { initialId: string | null }) {
notifyPagesChanged() notifyPagesChanged()
} }
// Create a tag inline from the page and attach it in one step.
const createAndAttachTag = async () => {
const name = newTagName.trim()
if (!name || !activeId) return
const res = await fetch('/api/tags', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, color: newTagColor }) })
if (!res.ok) {
const e = await res.json().catch(() => null)
toast.error(e?.error ?? 'Could not create tag')
return
}
const tag = await res.json()
setAllTags((cur) => (cur.some((t) => t.id === tag.id) ? cur : [...cur, tag]))
setNewTagName('')
await toggleTag(tag.id, true)
}
useEffect(() => { useEffect(() => {
if (!linkOpen) return if (!linkOpen) return
const q = linkQuery.trim() const q = linkQuery.trim()
@@ -536,6 +554,28 @@ export function PagesClient({ initialId }: { initialId: string | null }) {
))} ))}
{(page?.tags ?? []).length === 0 && <span className="text-xs text-muted-foreground">No tags</span>} {(page?.tags ?? []).length === 0 && <span className="text-xs text-muted-foreground">No tags</span>}
</div> </div>
<div className="mb-2 flex items-center gap-1.5 border-t pt-2">
<Input
value={newTagName}
onChange={(e) => setNewTagName(e.target.value)}
onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault(); void createAndAttachTag() } }}
placeholder="New tag name"
className="h-8 flex-1 text-sm"
aria-label="New tag name"
/>
<Button size="sm" variant="outline" disabled={!newTagName.trim()} onClick={() => void createAndAttachTag()}>Add</Button>
</div>
<div className="mb-2 flex items-center gap-1">
{['#8b5cf6', '#3b82f6', '#10b981', '#f59e0b', '#ef4444', '#ec4899', '#6b7280'].map((c) => (
<button
key={c}
onClick={() => setNewTagColor(c)}
aria-label={`Tag color ${c}`}
className={cn('h-4 w-4 rounded-full border', newTagColor === c ? 'ring-2 ring-primary/60' : 'border-transparent')}
style={{ backgroundColor: c }}
/>
))}
</div>
<div className="max-h-64 overflow-y-auto"> <div className="max-h-64 overflow-y-auto">
{allTags.map((t) => { {allTags.map((t) => {
const on = (page?.tags ?? []).some((x) => x.tag.id === t.id) const on = (page?.tags ?? []).some((x) => x.tag.id === t.id)