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) { async function removeRule(thenTagId: string) {
if (busy) return; if (busy) return;
busy = true; busy = true;
@ -86,12 +107,30 @@
<div class="rule-list"> <div class="rule-list">
{#each rules as rule (rule.then_tag_id)} {#each rules as rule (rule.then_tag_id)}
{@const t = tagForId(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} {#if t}
<TagBadge tag={t} size="sm" /> <TagBadge tag={t} size="sm" />
{:else} {:else}
<span class="unknown">{rule.then_tag_name ?? rule.then_tag_id}</span> <span class="unknown">{rule.then_tag_name ?? rule.then_tag_id}</span>
{/if} {/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 <button
class="remove-btn" class="remove-btn"
onclick={() => removeRule(rule.then_tag_id!)} onclick={() => removeRule(rule.then_tag_id!)}
@ -107,22 +146,29 @@
<!-- Add rule --> <!-- Add rule -->
<div class="add-section"> <div class="add-section">
<div class="section-label">Add rule</div> <div class="section-label">Add rule</div>
<input <div class="search-wrap">
class="search" <input
type="search" class="search"
placeholder="Search tags to add…" type="search"
bind:value={search} placeholder="Search tags to add…"
autocomplete="off" bind:value={search}
/> autocomplete="off"
{#if search.trim()} />
<div class="tag-pick"> {#if search}
{#each filteredTags as t (t.id)} <button class="search-clear" onclick={() => (search = '')} aria-label="Clear search">
<TagBadge tag={t} size="sm" onclick={() => addRule(t.id!)} /> <svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden="true">
{:else} <path d="M2 2l10 10M12 2L2 12" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/>
<span class="empty">No matching tags</span> </svg>
{/each} </button>
</div> {/if}
{/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">{search.trim() ? 'No matching tags' : 'All tags already added'}</span>
{/each}
</div>
</div> </div>
</div> </div>
@ -157,6 +203,31 @@
gap: 2px; 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 { .remove-btn {
background: none; background: none;
border: none; border: none;
@ -193,6 +264,12 @@
gap: 6px; gap: 6px;
} }
.search-wrap {
position: relative;
display: flex;
align-items: center;
}
.search { .search {
width: 100%; width: 100%;
box-sizing: border-box; box-sizing: border-box;
@ -211,6 +288,27 @@
border-color: var(--color-accent); 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 { .tag-pick {
display: flex; display: flex;
flex-wrap: wrap; 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 // Backwards-compatible reference for existing file-tag lookups
const MOCK_TAGS = mockTagsArr; const MOCK_TAGS = mockTagsArr;
// Tag rules: Map<tagId, Set<thenTagId>> // Tag rules: Map<tagId, Map<thenTagId, is_active>>
const tagRules = new Map<string, Set<string>>(); const tagRules = new Map<string, Map<string, boolean>>();
// Mutable in-memory state for file metadata and tags // Mutable in-memory state for file metadata and tags
const fileOverrides = new Map<string, Partial<typeof MOCK_FILES[0]>>(); const fileOverrides = new Map<string, Partial<typeof MOCK_FILES[0]>>();
@ -381,10 +381,10 @@ export function mockApiPlugin(): Plugin {
const tagRulesGetMatch = path.match(/^\/tags\/([^/]+)\/rules$/); const tagRulesGetMatch = path.match(/^\/tags\/([^/]+)\/rules$/);
if (method === 'GET' && tagRulesGetMatch) { if (method === 'GET' && tagRulesGetMatch) {
const tid = tagRulesGetMatch[1]; const tid = tagRulesGetMatch[1];
const ruleIds = [...(tagRules.get(tid) ?? new Set<string>())]; const ruleMap = tagRules.get(tid) ?? new Map<string, boolean>();
const items = ruleIds.map((thenId) => { const items = [...ruleMap.entries()].map(([thenId, isActive]) => {
const t = MOCK_TAGS.find((x) => x.id === thenId); 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); return json(res, 200, items);
} }
@ -395,10 +395,11 @@ export function mockApiPlugin(): Plugin {
const tid = tagRulesPostMatch[1]; const tid = tagRulesPostMatch[1];
const body = (await readBody(req)) as Record<string, unknown>; const body = (await readBody(req)) as Record<string, unknown>;
const thenId = body.then_tag_id as string; const thenId = body.then_tag_id as string;
if (!tagRules.has(tid)) tagRules.set(tid, new Set()); const isActive = body.is_active !== false;
tagRules.get(tid)!.add(thenId); if (!tagRules.has(tid)) tagRules.set(tid, new Map());
tagRules.get(tid)!.set(thenId, isActive);
const t = MOCK_TAGS.find((x) => x.id === thenId); 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} // DELETE /tags/{id}/rules/{then_id}