feat(frontend): implement tag list, create, and edit pages

- /tags: list with search + clear button, sort/order controls, offset pagination
  Fix infinite requests when search matches no tags (track initialLoaded flag)
- /tags/new: create form with name, notes, color picker, category, is_public
- /tags/[id]: edit form + TagRuleEditor for implied-tag rules + delete
- TagBadge: colored pill with optional onclick and size prop
- TagRuleEditor: manage implied-tag rules (search to add, × to remove)
- Mock: tag/category CRUD, rules CRUD, search/sort, 5 mock categories

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-05 23:14:04 +03:00
parent b9cace2997
commit f7d7e8ce37
6 changed files with 1370 additions and 11 deletions
@@ -0,0 +1,56 @@
<script lang="ts">
import type { Tag } from '$lib/api/types';
interface Props {
tag: Tag;
onclick?: () => void;
size?: 'sm' | 'md';
}
let { tag, onclick, size = 'md' }: Props = $props();
const color = tag.color ?? tag.category_color;
const style = color ? `background-color: #${color}` : '';
</script>
{#if onclick}
<button class="badge {size}" {style} {onclick} type="button">
{tag.name}
</button>
{:else}
<span class="badge {size}" {style}>{tag.name}</span>
{/if}
<style>
.badge {
display: inline-flex;
align-items: center;
border-radius: 5px;
font-family: inherit;
background-color: var(--color-tag-default);
color: rgba(255, 255, 255, 0.9);
white-space: nowrap;
border: none;
cursor: default;
}
.badge.md {
height: 28px;
padding: 0 10px;
font-size: 0.85rem;
}
.badge.sm {
height: 22px;
padding: 0 7px;
font-size: 0.75rem;
}
button.badge {
cursor: pointer;
}
button.badge:hover {
filter: brightness(1.15);
}
</style>
@@ -0,0 +1,233 @@
<script lang="ts">
import { api, ApiError } from '$lib/api/client';
import type { Tag, TagOffsetPage, TagRule } from '$lib/api/types';
import TagBadge from './TagBadge.svelte';
interface Props {
tagId: string;
rules: TagRule[];
onRulesChange: (rules: TagRule[]) => void;
}
let { tagId, rules, onRulesChange }: Props = $props();
let allTags = $state<Tag[]>([]);
let search = $state('');
let busy = $state(false);
let error = $state('');
$effect(() => {
api.get<TagOffsetPage>('/tags?limit=200&sort=name&order=asc').then((p) => {
allTags = p.items ?? [];
});
});
// IDs already used in rules
let usedIds = $derived(new Set(rules.map((r) => r.then_tag_id)));
let filteredTags = $derived(
allTags.filter(
(t) =>
t.id !== tagId &&
!usedIds.has(t.id) &&
(!search.trim() || t.name?.toLowerCase().includes(search.toLowerCase())),
),
);
function tagForId(id: string | undefined) {
return allTags.find((t) => t.id === id);
}
async function addRule(thenTagId: string) {
if (busy) return;
busy = true;
error = '';
try {
const rule = await api.post<TagRule>(`/tags/${tagId}/rules`, {
then_tag_id: thenTagId,
is_active: true,
apply_to_existing: false,
});
onRulesChange([...rules, rule]);
search = '';
} catch (e) {
error = e instanceof ApiError ? e.message : 'Failed to add rule';
} finally {
busy = false;
}
}
async function removeRule(thenTagId: string) {
if (busy) return;
busy = true;
error = '';
try {
await api.delete(`/tags/${tagId}/rules/${thenTagId}`);
onRulesChange(rules.filter((r) => r.then_tag_id !== thenTagId));
} catch (e) {
error = e instanceof ApiError ? e.message : 'Failed to remove rule';
} finally {
busy = false;
}
}
</script>
<div class="editor" class:busy>
<p class="desc">
When this tag is applied, also apply:
</p>
{#if error}
<p class="error" role="alert">{error}</p>
{/if}
<!-- Current rules -->
{#if rules.length > 0}
<div class="rule-list">
{#each rules as rule (rule.then_tag_id)}
{@const t = tagForId(rule.then_tag_id)}
<div class="rule-row">
{#if t}
<TagBadge tag={t} size="sm" />
{:else}
<span class="unknown">{rule.then_tag_name ?? rule.then_tag_id}</span>
{/if}
<button
class="remove-btn"
onclick={() => removeRule(rule.then_tag_id!)}
aria-label="Remove rule"
>×</button>
</div>
{/each}
</div>
{:else}
<p class="empty">No rules — when this tag is applied, nothing extra happens.</p>
{/if}
<!-- Add rule -->
<div class="add-section">
<div class="section-label">Add rule</div>
<input
class="search"
type="search"
placeholder="Search tags to add…"
bind:value={search}
autocomplete="off"
/>
{#if search.trim()}
<div class="tag-pick">
{#each filteredTags as t (t.id)}
<TagBadge tag={t} size="sm" onclick={() => addRule(t.id!)} />
{:else}
<span class="empty">No matching tags</span>
{/each}
</div>
{/if}
</div>
</div>
<style>
.editor {
display: flex;
flex-direction: column;
gap: 8px;
}
.editor.busy {
opacity: 0.6;
pointer-events: none;
}
.desc {
font-size: 0.82rem;
color: var(--color-text-muted);
margin: 0;
}
.rule-list {
display: flex;
flex-wrap: wrap;
gap: 6px;
align-items: center;
}
.rule-row {
display: inline-flex;
align-items: center;
gap: 2px;
}
.remove-btn {
background: none;
border: none;
color: var(--color-text-muted);
font-size: 1rem;
line-height: 1;
cursor: pointer;
padding: 1px 3px;
border-radius: 3px;
}
.remove-btn:hover {
color: var(--color-danger);
}
.unknown {
font-size: 0.75rem;
color: var(--color-text-muted);
font-family: monospace;
}
.section-label {
font-size: 0.75rem;
font-weight: 600;
color: var(--color-text-muted);
text-transform: uppercase;
letter-spacing: 0.05em;
margin-bottom: 4px;
}
.add-section {
display: flex;
flex-direction: column;
gap: 6px;
}
.search {
width: 100%;
box-sizing: border-box;
height: 32px;
padding: 0 10px;
border-radius: 6px;
border: 1px solid color-mix(in srgb, var(--color-accent) 30%, transparent);
background-color: var(--color-bg-primary);
color: var(--color-text-primary);
font-size: 0.85rem;
font-family: inherit;
outline: none;
}
.search:focus {
border-color: var(--color-accent);
}
.tag-pick {
display: flex;
flex-wrap: wrap;
gap: 5px;
max-height: 100px;
overflow-y: auto;
}
.empty {
font-size: 0.8rem;
color: var(--color-text-muted);
margin: 0;
}
.error {
font-size: 0.8rem;
color: var(--color-danger);
margin: 0;
}
</style>