From b12decc80286dfab35703c80d2fe91b9521dfa20 Mon Sep 17 00:00:00 2001 From: Garret Patti <42485635+garretpatti@users.noreply.github.com> Date: Sun, 19 Apr 2026 23:09:28 -0400 Subject: [PATCH] search existing tags for default --- src/app/manage/tags/mappings/[id]/page.tsx | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/app/manage/tags/mappings/[id]/page.tsx b/src/app/manage/tags/mappings/[id]/page.tsx index a266800..aaf0f4c 100644 --- a/src/app/manage/tags/mappings/[id]/page.tsx +++ b/src/app/manage/tags/mappings/[id]/page.tsx @@ -1,6 +1,6 @@ 'use client' -import { useEffect, useState, useRef, useCallback } from 'react' +import { useEffect, useMemo, useState, useRef, useCallback } from 'react' import { useParams } from 'next/navigation' import type { Tag, TagCategory, ImportedTag, TagMapping, Library } from '@/types' @@ -335,6 +335,19 @@ function ImportedTagRow({ onMapped: () => void onIgnore: () => void }) { + // Auto-match: if prefix mapping exists, find a tag in that category matching the stripped name + const autoMatchedTagId = useMemo(() => { + const colonIdx = importedTag.name.indexOf(': ') + if (colonIdx <= 0) return '' + const prefix = importedTag.name.slice(0, colonIdx).trim().toLowerCase() + const mappedCategoryId = prefixMappings[prefix] + if (!mappedCategoryId) return '' + const strippedName = importedTag.name.slice(colonIdx + 2).trim().toLowerCase() + const group = tagsByCategory.find((g) => g.category.id === mappedCategoryId) + const match = group?.tags.find((t) => t.name.toLowerCase() === strippedName) + return match?.id ?? '' + }, [importedTag.name, prefixMappings, tagsByCategory]) + const [selectedTagId, setSelectedTagId] = useState('') const [saving, setSaving] = useState(false) const [error, setError] = useState(null) @@ -343,6 +356,11 @@ function ImportedTagRow({ const [newTagCategoryId, setNewTagCategoryId] = useState('') const [creatingTag, setCreatingTag] = useState(false) + // Apply auto-match when it changes (e.g. prefix mappings updated) + useEffect(() => { + if (autoMatchedTagId) setSelectedTagId(autoMatchedTagId) + }, [autoMatchedTagId]) + const startCreating = () => { // Apply prefix mapping defaults if the imported tag has a colon prefix const colonIdx = importedTag.name.indexOf(': ')