feat(frontend): add activate/deactivate toggle for tag rules

- Toggle button (filled/hollow circle) on each rule row
- Inactive rules dim to 45% opacity
- Toggle via delete + recreate with new is_active value
- Mock: track is_active per rule (Map instead of Set)
- Show all available tags by default in add-rule picker

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Masahiko AMANO 2026-04-05 23:26:07 +03:00
parent 6e24060d99
commit 871250345a
2 changed files with 124 additions and 25 deletions

View File

@ -57,6 +57,27 @@
}
}
async function toggleRule(rule: TagRule) {
if (busy) return;
busy = true;
error = '';
const thenTagId = rule.then_tag_id!;
const newActive = !rule.is_active;
try {
await api.delete(`/tags/${tagId}/rules/${thenTagId}`);
const updated = await api.post<TagRule>(`/tags/${tagId}/rules`, {
then_tag_id: thenTagId,
is_active: newActive,
apply_to_existing: false,
});
onRulesChange(rules.map((r) => r.then_tag_id === thenTagId ? updated : r));
} catch (e) {
error = e instanceof ApiError ? e.message : 'Failed to update rule';
} finally {
busy = false;
}
}
async function removeRule(thenTagId: string) {
if (busy) return;
busy = true;
@ -86,12 +107,30 @@
<div class="rule-list">
{#each rules as rule (rule.then_tag_id)}
{@const t = tagForId(rule.then_tag_id)}
<div class="rule-row">
<div class="rule-row" class:inactive={!rule.is_active}>
{#if t}
<TagBadge tag={t} size="sm" />
{:else}
<span class="unknown">{rule.then_tag_name ?? rule.then_tag_id}</span>
{/if}
<button
class="toggle-btn"
class:active={rule.is_active}
onclick={() => toggleRule(rule)}
title={rule.is_active ? 'Deactivate rule' : 'Activate rule'}
aria-label={rule.is_active ? 'Deactivate rule' : 'Activate rule'}
>
{#if rule.is_active}
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" aria-hidden="true">
<circle cx="6" cy="6" r="5" stroke="currentColor" stroke-width="1.5"/>
<circle cx="6" cy="6" r="2.5" fill="currentColor"/>
</svg>
{:else}
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" aria-hidden="true">
<circle cx="6" cy="6" r="5" stroke="currentColor" stroke-width="1.5"/>
</svg>
{/if}
</button>
<button
class="remove-btn"
onclick={() => removeRule(rule.then_tag_id!)}
@ -107,6 +146,7 @@
<!-- Add rule -->
<div class="add-section">
<div class="section-label">Add rule</div>
<div class="search-wrap">
<input
class="search"
type="search"
@ -114,15 +154,21 @@
bind:value={search}
autocomplete="off"
/>
{#if search.trim()}
{#if search}
<button class="search-clear" onclick={() => (search = '')} aria-label="Clear search">
<svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden="true">
<path d="M2 2l10 10M12 2L2 12" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/>
</svg>
</button>
{/if}
</div>
<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>
<span class="empty">{search.trim() ? 'No matching tags' : 'All tags already added'}</span>
{/each}
</div>
{/if}
</div>
</div>
@ -157,6 +203,31 @@
gap: 2px;
}
.rule-row.inactive {
opacity: 0.45;
}
.toggle-btn {
display: flex;
align-items: center;
justify-content: center;
background: none;
border: none;
color: var(--color-text-muted);
cursor: pointer;
padding: 2px 3px;
border-radius: 3px;
line-height: 1;
}
.toggle-btn.active {
color: var(--color-accent);
}
.toggle-btn:hover {
color: var(--color-text-primary);
}
.remove-btn {
background: none;
border: none;
@ -193,6 +264,12 @@
gap: 6px;
}
.search-wrap {
position: relative;
display: flex;
align-items: center;
}
.search {
width: 100%;
box-sizing: border-box;
@ -211,6 +288,27 @@
border-color: var(--color-accent);
}
.search-clear {
position: absolute;
right: 6px;
display: flex;
align-items: center;
justify-content: center;
width: 20px;
height: 20px;
border-radius: 50%;
border: none;
background: none;
color: var(--color-text-muted);
cursor: pointer;
padding: 0;
}
.search-clear:hover {
color: var(--color-text-primary);
background-color: color-mix(in srgb, var(--color-accent) 20%, transparent);
}
.tag-pick {
display: flex;
flex-wrap: wrap;

View File

@ -183,8 +183,8 @@ const mockTagsArr: MockTag[] = TAG_NAMES.map((name, i) => {
// Backwards-compatible reference for existing file-tag lookups
const MOCK_TAGS = mockTagsArr;
// Tag rules: Map<tagId, Set<thenTagId>>
const tagRules = new Map<string, Set<string>>();
// Tag rules: Map<tagId, Map<thenTagId, is_active>>
const tagRules = new Map<string, Map<string, boolean>>();
// Mutable in-memory state for file metadata and tags
const fileOverrides = new Map<string, Partial<typeof MOCK_FILES[0]>>();
@ -381,10 +381,10 @@ export function mockApiPlugin(): Plugin {
const tagRulesGetMatch = path.match(/^\/tags\/([^/]+)\/rules$/);
if (method === 'GET' && tagRulesGetMatch) {
const tid = tagRulesGetMatch[1];
const ruleIds = [...(tagRules.get(tid) ?? new Set<string>())];
const items = ruleIds.map((thenId) => {
const ruleMap = tagRules.get(tid) ?? new Map<string, boolean>();
const items = [...ruleMap.entries()].map(([thenId, isActive]) => {
const t = MOCK_TAGS.find((x) => x.id === thenId);
return { tag_id: tid, then_tag_id: thenId, then_tag_name: t?.name ?? null, is_active: true };
return { tag_id: tid, then_tag_id: thenId, then_tag_name: t?.name ?? null, is_active: isActive };
});
return json(res, 200, items);
}
@ -395,10 +395,11 @@ export function mockApiPlugin(): Plugin {
const tid = tagRulesPostMatch[1];
const body = (await readBody(req)) as Record<string, unknown>;
const thenId = body.then_tag_id as string;
if (!tagRules.has(tid)) tagRules.set(tid, new Set());
tagRules.get(tid)!.add(thenId);
const isActive = body.is_active !== false;
if (!tagRules.has(tid)) tagRules.set(tid, new Map());
tagRules.get(tid)!.set(thenId, isActive);
const t = MOCK_TAGS.find((x) => x.id === thenId);
return json(res, 201, { tag_id: tid, then_tag_id: thenId, then_tag_name: t?.name ?? null, is_active: true });
return json(res, 201, { tag_id: tid, then_tag_id: thenId, then_tag_name: t?.name ?? null, is_active: isActive });
}
// DELETE /tags/{id}/rules/{then_id}