summaryrefslogtreecommitdiff
path: root/app/routes
diff options
context:
space:
mode:
authoryyamashita <yyamashita@hetzner.yyamashita.com>2026-08-15 18:57:59 +0900
committeryyamashita <yyamashita@hetzner.yyamashita.com>2026-08-15 18:57:59 +0900
commit59a9f4984c3acd29bc746a78e30849f50fa555ee (patch)
treeb9dab024c0c5d1d68e023b8f49eb0716b97965bd /app/routes
parenta100fb28f92fa7c5d2cf701d118c4c7b898c9bf2 (diff)
Add Claude-powered band auto-registration from live info text
Extracts band/artist names from pasted live info via Claude (API or CLI adapter), matches against existing bands, and lets the user review and bulk-register new ones through a polling job queue. Also adds a livehouse events import script and recent-bands/artists home page sections. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Diffstat (limited to 'app/routes')
-rw-r--r--app/routes/band-auto-register-job.tsx211
-rw-r--r--app/routes/band-auto-register.tsx88
-rw-r--r--app/routes/band-index.tsx1
-rw-r--r--app/routes/home.tsx108
4 files changed, 405 insertions, 3 deletions
diff --git a/app/routes/band-auto-register-job.tsx b/app/routes/band-auto-register-job.tsx
new file mode 100644
index 0000000..31f8ba4
--- /dev/null
+++ b/app/routes/band-auto-register-job.tsx
@@ -0,0 +1,211 @@
+import { useEffect, useState } from "react";
+import { data, Form, Link, redirect, useLoaderData, useNavigation, useRevalidator } from "react-router";
+import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
+import { createBand, getIpAddress, getParseJob, toSlug } from "~/lib/db.server";
+import type { BandMatch } from "~/lib/claude.server";
+
+export async function loader({ params }: LoaderFunctionArgs) {
+ const job = getParseJob(params.id as string);
+ if (!job) throw data("Not found", { status: 404 });
+ return { job };
+}
+
+export async function action({ request, params }: ActionFunctionArgs) {
+ const fd = await request.formData();
+ const toRegister: { name: string }[] = JSON.parse(
+ (fd.get("toRegister") as string) || "[]"
+ );
+ const ip = getIpAddress(request);
+ const registered: { id: string; name: string }[] = [];
+ const errors: string[] = [];
+
+ for (const { name } of toRegister) {
+ const id = crypto.randomUUID();
+ const baseSlug = toSlug(name) || id.slice(0, 8);
+ let ok = false;
+ for (let attempt = 0; attempt < 3 && !ok; attempt++) {
+ const slug = attempt === 0 ? baseSlug : `${baseSlug}-${id.slice(0, 6)}`;
+ try {
+ createBand({ id, slug, name, area: null, description: null, status: "active", links: [], members: [], message: "ライブ情報から自動登録", ip_address: ip });
+ registered.push({ id, name });
+ ok = true;
+ } catch (e) {
+ if (e instanceof Error && e.message.includes("UNIQUE constraint failed")) continue;
+ errors.push(`${name}: ${e instanceof Error ? e.message : "不明なエラー"}`);
+ ok = true;
+ }
+ }
+ }
+
+ return redirect(`/bands/auto-register/jobs/${params.id as string}?registered=${registered.length}&errors=${errors.length}`);
+}
+
+export default function ParseJobPage() {
+ const { job } = useLoaderData<typeof loader>();
+ const revalidator = useRevalidator();
+ const nav = useNavigation();
+ const busy = nav.state !== "idle";
+
+ // read flash params from URL
+ const [flashMsg, setFlashMsg] = useState<string | null>(null);
+ useEffect(() => {
+ const sp = new URLSearchParams(window.location.search);
+ const reg = sp.get("registered");
+ const err = sp.get("errors");
+ if (reg !== null) {
+ const parts: string[] = [];
+ if (Number(reg) > 0) parts.push(`${reg}件を登録しました`);
+ if (Number(err) > 0) parts.push(`${err}件が失敗`);
+ setFlashMsg(parts.join("、") || "変更なし");
+ // clean URL
+ window.history.replaceState(null, "", window.location.pathname);
+ }
+ }, []);
+
+ // poll while job is still running
+ useEffect(() => {
+ if (job.status !== "pending" && job.status !== "processing") return;
+ const t = setTimeout(() => revalidator.revalidate(), 2000);
+ return () => clearTimeout(t);
+ }, [job.status, job.updated_at, revalidator]);
+
+ const matches: BandMatch[] = job.status === "done" && job.result
+ ? (JSON.parse(job.result) as BandMatch[])
+ : [];
+
+ return (
+ <main>
+ <div className="page-header">
+ <Link to="/bands/auto-register" className="back">←</Link>
+ <h1>解析ジョブ</h1>
+ </div>
+
+ {flashMsg && (
+ <p style={{ color: "#34d399", marginBottom: "1rem" }}>{flashMsg}</p>
+ )}
+
+ <div className="job-meta">
+ <JobStatusBadge status={job.status} />
+ <span className="muted" style={{ fontSize: ".8rem" }}>
+ {job.created_at.slice(0, 16).replace("T", " ")}
+ </span>
+ </div>
+
+ <div style={{ background: "#111827", borderRadius: 6, padding: ".75rem 1rem", marginTop: ".75rem", fontSize: ".8rem", color: "#6b7280", whiteSpace: "pre-wrap", maxHeight: "8rem", overflow: "auto" }}>
+ {job.input_text}
+ </div>
+
+ {(job.status === "pending" || job.status === "processing") && (
+ <div style={{ marginTop: "2rem", display: "flex", alignItems: "center", gap: ".75rem" }}>
+ <span className="spinner" />
+ <span className="muted">Claudeが解析中です…</span>
+ </div>
+ )}
+
+ {job.status === "error" && (
+ <p className="error" style={{ marginTop: "1rem" }}>{job.error_message}</p>
+ )}
+
+ {job.status === "done" && matches.length > 0 && (
+ <ReviewSection matches={matches} busy={busy} />
+ )}
+ </main>
+ );
+}
+
+function JobStatusBadge({ status }: { status: string }) {
+ const map: Record<string, { label: string; color: string }> = {
+ pending: { label: "待機中", color: "#6b7280" },
+ processing: { label: "解析中", color: "#fbbf24" },
+ done: { label: "完了", color: "#34d399" },
+ error: { label: "エラー", color: "#f87171" },
+ };
+ const s = map[status] ?? { label: status, color: "#6b7280" };
+ return (
+ <span style={{ color: s.color, fontWeight: 600, fontSize: ".875rem" }}>
+ ● {s.label}
+ </span>
+ );
+}
+
+function ReviewSection({ matches, busy }: { matches: BandMatch[]; busy: boolean }) {
+ type Sel = { include: boolean; name: string; treatAsNew: boolean };
+ const [sels, setSels] = useState<Record<number, Sel>>(() => {
+ const init: Record<number, Sel> = {};
+ for (let i = 0; i < matches.length; i++) {
+ const m = matches[i];
+ init[i] = { include: m.confidence !== "exact", name: m.extracted, treatAsNew: m.confidence === "new" };
+ }
+ return init;
+ });
+
+ function update(i: number, patch: Partial<Sel>) {
+ setSels((prev) => ({ ...prev, [i]: { ...prev[i], ...patch } }));
+ }
+
+ const toRegister = matches
+ .map((m, i) => ({ m, s: sels[i] }))
+ .filter(({ s }) => s.include && s.treatAsNew)
+ .map(({ s }) => ({ name: s.name }));
+
+ return (
+ <>
+ <section style={{ marginTop: "1.5rem" }}>
+ <h2>解析結果</h2>
+ <div className="auto-register-list">
+ {matches.map((m, i) => {
+ const s = sels[i];
+ return (
+ <div key={i} className={`auto-register-row conf-${m.confidence}`}>
+ <div className="ar-left">
+ {(m.confidence === "new" || (m.confidence === "fuzzy" && s.treatAsNew)) && (
+ <input type="checkbox" checked={s.include} onChange={(e) => update(i, { include: e.target.checked })} />
+ )}
+ </div>
+ <div className="ar-body">
+ <div className="ar-name">
+ {s.include && s.treatAsNew
+ ? <input className="ar-name-input" value={s.name} onChange={(e) => update(i, { name: e.target.value })} />
+ : <span>{m.extracted}</span>
+ }
+ </div>
+ {m.confidence === "exact" && (
+ <div className="ar-status ar-exact">
+ <span className="ar-badge">登録済</span>
+ {m.matched_id && <Link to={`/bands/of/${m.matched_id}`} className="ar-link">{m.matched_name}</Link>}
+ </div>
+ )}
+ {m.confidence === "fuzzy" && (
+ <div className="ar-status ar-fuzzy">
+ <span className="ar-badge">要確認</span>
+ <span className="ar-candidate">候補: {m.matched_name}</span>
+ <button type="button" className="btn-text" onClick={() => update(i, { treatAsNew: !s.treatAsNew, include: !s.treatAsNew })}>
+ {s.treatAsNew ? "既存バンドとして扱う" : "新規として登録する"}
+ </button>
+ </div>
+ )}
+ {m.confidence === "new" && (
+ <div className="ar-status ar-new">
+ <span className="ar-badge">新規</span>
+ </div>
+ )}
+ </div>
+ </div>
+ );
+ })}
+ </div>
+ </section>
+
+ <Form method="post">
+ <input type="hidden" name="toRegister" value={JSON.stringify(toRegister)} />
+ <div className="actions" style={{ marginTop: "1rem" }}>
+ <button type="submit" disabled={busy || toRegister.length === 0}>
+ {busy ? "登録中..." : `${toRegister.length}件を登録する`}
+ </button>
+ <Link to="/bands/auto-register" className="btn">新しいテキストを投入</Link>
+ <Link to="/bands" className="btn">バンド一覧へ</Link>
+ </div>
+ </Form>
+ </>
+ );
+}
diff --git a/app/routes/band-auto-register.tsx b/app/routes/band-auto-register.tsx
new file mode 100644
index 0000000..227908d
--- /dev/null
+++ b/app/routes/band-auto-register.tsx
@@ -0,0 +1,88 @@
+import { Form, Link, redirect, useLoaderData, useNavigation } from "react-router";
+import type { ActionFunctionArgs } from "react-router";
+import { createParseJob, getIpAddress, listRecentParseJobs } from "~/lib/db.server";
+
+export function loader() {
+ return { jobs: listRecentParseJobs(10) };
+}
+
+export async function action({ request }: ActionFunctionArgs) {
+ const fd = await request.formData();
+ const liveText = (fd.get("liveText") as string | null)?.trim() ?? "";
+ if (!liveText) return { error: "テキストを入力してください" };
+ const job = createParseJob(liveText);
+ void getIpAddress(request); // unused but kept for symmetry
+ return redirect(`/bands/auto-register/jobs/${job.id}`);
+}
+
+const STATUS_LABEL: Record<string, string> = {
+ pending: "待機中",
+ processing: "解析中",
+ done: "完了",
+ error: "エラー",
+};
+
+const STATUS_COLOR: Record<string, string> = {
+ pending: "#6b7280",
+ processing: "#fbbf24",
+ done: "#34d399",
+ error: "#f87171",
+};
+
+export default function BandAutoRegister() {
+ const { jobs } = useLoaderData<typeof loader>();
+ const nav = useNavigation();
+ const busy = nav.state !== "idle";
+
+ return (
+ <main>
+ <div className="page-header">
+ <Link to="/bands" className="back">←</Link>
+ <h1>ライブ情報から自動登録</h1>
+ </div>
+
+ <Form method="post">
+ <div>
+ <label>ライブ情報テキスト <span className="req">*</span></label>
+ <textarea
+ name="liveText"
+ rows={10}
+ placeholder={"出演バンド・アーティストの情報をペーストしてください。\n\n例:\n2024.08.10 @ 渋谷CLUB QUATTRO\nopen 17:30 / start 18:00\n\nband1 / Band Two / バンド三"}
+ />
+ </div>
+ <div className="actions">
+ <button type="submit" disabled={busy}>
+ {busy ? "送信中..." : "キューに追加する"}
+ </button>
+ </div>
+ </Form>
+
+ {jobs.length > 0 && (
+ <section style={{ marginTop: "2rem" }}>
+ <h2>最近のジョブ</h2>
+ <div className="job-list">
+ {jobs.map((job) => (
+ <Link
+ key={job.id}
+ to={`/bands/auto-register/jobs/${job.id}`}
+ className="job-row"
+ >
+ <span
+ className="job-status"
+ style={{ color: STATUS_COLOR[job.status] ?? "#6b7280" }}
+ >
+ {STATUS_LABEL[job.status] ?? job.status}
+ </span>
+ <span className="job-preview">
+ {job.input_text.slice(0, 60).replace(/\n/g, " ")}
+ {job.input_text.length > 60 ? "…" : ""}
+ </span>
+ <span className="job-time">{job.created_at.slice(0, 16).replace("T", " ")}</span>
+ </Link>
+ ))}
+ </div>
+ </section>
+ )}
+ </main>
+ );
+}
diff --git a/app/routes/band-index.tsx b/app/routes/band-index.tsx
index 0b47da5..bed6df9 100644
--- a/app/routes/band-index.tsx
+++ b/app/routes/band-index.tsx
@@ -11,6 +11,7 @@ export default function BandIndex() {
<main>
<div className="page-header">
<h1>Bands</h1>
+ <Link to="/bands/auto-register" className="btn" style={{ fontSize: ".8rem" }}>ライブから一括登録</Link>
<Link to="/bands/new">+ Band</Link>
</div>
diff --git a/app/routes/home.tsx b/app/routes/home.tsx
index 9795ba8..591585d 100644
--- a/app/routes/home.tsx
+++ b/app/routes/home.tsx
@@ -1,9 +1,111 @@
-import { redirect } from "react-router";
+import { Link, useLoaderData } from "react-router";
+import { listBands, listArtists, listBandLists, listRecentBands, listRecentArtists } from "~/lib/db.server";
export function loader() {
- return redirect("/bands");
+ return {
+ bandCount: listBands().length,
+ artistCount: listArtists().length,
+ listCount: listBandLists().length,
+ recentBands: listRecentBands(8),
+ recentArtists: listRecentArtists(8),
+ };
}
export default function Home() {
- return null;
+ const { bandCount, artistCount, listCount, recentBands, recentArtists } = useLoaderData<typeof loader>();
+ return (
+ <main>
+ <div style={{ marginBottom: "2.5rem" }}>
+ <h1 style={{ fontSize: "2rem", fontWeight: 700, color: "#f9fafb", marginBottom: ".5rem" }}>
+ whois.band
+ </h1>
+ <p className="muted">バンドとアーティストの情報管理サイト</p>
+ </div>
+
+ <div style={{ display: "flex", flexDirection: "column", gap: "1px", background: "#1f2937", border: "1px solid #1f2937", borderRadius: "6px", overflow: "hidden", marginBottom: "2.5rem" }}>
+ <SectionRow to="/bands" label="Bands" count={bandCount} newTo="/bands/new" />
+ <SectionRow to="/artists" label="Artists" count={artistCount} newTo="/artists/new" />
+ <SectionRow to="/lists" label="Lists" count={listCount} newTo="/lists/new" />
+ </div>
+
+ <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: "2rem" }}>
+ <RecentSection title="最近追加されたバンド" viewAllTo="/bands">
+ {recentBands.length === 0 ? (
+ <p className="muted" style={{ fontSize: ".875rem" }}>まだありません</p>
+ ) : (
+ <ul className="band-list">
+ {recentBands.map((band) => (
+ <li key={band.id}>
+ <Link to={`/bands/of/${band.id}`}>{band.name}</Link>
+ {band.status === "hiatus" && (
+ <span className="muted" style={{ fontSize: ".75rem" }}>活動休止</span>
+ )}
+ </li>
+ ))}
+ </ul>
+ )}
+ </RecentSection>
+
+ <RecentSection title="最近追加されたアーティスト" viewAllTo="/artists">
+ {recentArtists.length === 0 ? (
+ <p className="muted" style={{ fontSize: ".875rem" }}>まだありません</p>
+ ) : (
+ <ul className="band-list">
+ {recentArtists.map((artist) => (
+ <li key={artist.id}>
+ <Link to={`/artists/of/${artist.id}`}>{artist.name}</Link>
+ </li>
+ ))}
+ </ul>
+ )}
+ </RecentSection>
+ </div>
+ </main>
+ );
+}
+
+function SectionRow({
+ to,
+ label,
+ count,
+ newTo,
+}: {
+ to: string;
+ label: string;
+ count: number;
+ newTo: string;
+}) {
+ return (
+ <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", padding: ".875rem 1.25rem", background: "#0f1117" }}>
+ <div style={{ display: "flex", alignItems: "baseline", gap: "1rem" }}>
+ <Link to={to} style={{ fontWeight: 600, fontSize: "1rem", color: "#e5e7eb" }}>
+ {label}
+ </Link>
+ <span className="muted" style={{ fontSize: ".8rem" }}>{count}件</span>
+ </div>
+ <Link to={newTo} className="btn" style={{ fontSize: ".8rem" }}>
+ + 追加
+ </Link>
+ </div>
+ );
+}
+
+function RecentSection({
+ title,
+ viewAllTo,
+ children,
+}: {
+ title: string;
+ viewAllTo: string;
+ children: React.ReactNode;
+}) {
+ return (
+ <section style={{ marginBottom: 0 }}>
+ <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginBottom: ".75rem" }}>
+ <h2 style={{ margin: 0 }}>{title}</h2>
+ <Link to={viewAllTo} className="muted" style={{ fontSize: ".75rem" }}>すべて見る</Link>
+ </div>
+ {children}
+ </section>
+ );
}