FrontmatterCard.tsx 5.85 KB
"use client";

import { useState } from "react";
import { ChevronDown, ChevronRight, Tag } from "lucide-react";
import { cn } from "@/lib/utils";

const PRIMARY_KEYS = ["title", "type", "entity_type", "schema_version", "created", "updated"];
const CHIP_KEYS = ["tags", "brand", "series", "business_category", "sub_business", "role", "related"];
const HIDDEN_KEYS = [...PRIMARY_KEYS, ...CHIP_KEYS, "sources", "field_sources"];

function toStringArray(value: unknown): string[] {
  if (value === null || value === undefined) return [];
  if (Array.isArray(value)) return value.map((v) => String(v)).filter(Boolean);
  return [String(value)].filter(Boolean);
}

function FieldSourcesTree({ node, depth = 0 }: { node: unknown; depth?: number }) {
  if (Array.isArray(node)) {
    return (
      <ul className="space-y-0.5">
        {node.map((item, idx) => (
          <li key={idx} className="text-[11px] font-mono text-slate-500 truncate">
            - {String(item)}
          </li>
        ))}
      </ul>
    );
  }
  if (node && typeof node === "object") {
    return (
      <div className={cn("space-y-1.5", depth > 0 && "pl-3 border-l border-slate-200")}>
        {Object.entries(node as Record<string, unknown>).map(([key, value]) => (
          <div key={key}>
            <div className="text-[11px] font-bold text-slate-600">{key}</div>
            <FieldSourcesTree node={value} depth={depth + 1} />
          </div>
        ))}
      </div>
    );
  }
  return <div className="text-[11px] font-mono text-slate-500">{String(node)}</div>;
}

export default function FrontmatterCard({ data }: { data: Record<string, unknown> }) {
  const [sourcesOpen, setSourcesOpen] = useState(false);
  const [fieldSourcesOpen, setFieldSourcesOpen] = useState(false);

  const title = data.title ? String(data.title) : null;
  const sources = toStringArray(data.sources);
  const fieldSources = data.field_sources;

  const otherKeys = Object.keys(data).filter((k) => !HIDDEN_KEYS.includes(k));

  return (
    <div className="mb-8 rounded-2xl border border-slate-200 bg-slate-50/70 p-5">
      {title && <h1 className="text-lg font-bold text-slate-800 mb-2">{title}</h1>}

      <div className="flex flex-wrap items-center gap-x-4 gap-y-1 text-xs text-slate-500 mb-3">
        {data.type != null && (
          <span className="font-medium">
            类型: <span className="text-slate-700">{String(data.type)}</span>
          </span>
        )}
        {data.entity_type != null && (
          <span className="font-medium">
            实体类型: <span className="text-slate-700">{String(data.entity_type)}</span>
          </span>
        )}
        {data.schema_version != null && (
          <span className="font-medium">
            Schema: <span className="text-slate-700">{String(data.schema_version)}</span>
          </span>
        )}
        {data.created != null && (
          <span className="font-medium">
            创建: <span className="text-slate-700">{String(data.created)}</span>
          </span>
        )}
        {data.updated != null && (
          <span className="font-medium">
            更新: <span className="text-slate-700">{String(data.updated)}</span>
          </span>
        )}
      </div>

      <div className="space-y-2">
        {CHIP_KEYS.map((key) => {
          const values = toStringArray(data[key]);
          if (values.length === 0) return null;
          return (
            <div key={key} className="flex items-start gap-2">
              <span className="shrink-0 text-[11px] font-bold text-slate-400 uppercase tracking-wide pt-0.5 w-24">
                {key}
              </span>
              <div className="flex flex-wrap gap-1.5">
                {values.map((v, idx) => (
                  <span
                    key={idx}
                    className="inline-flex items-center gap-1 rounded-full bg-blue-50 text-blue-700 px-2 py-0.5 text-[11px] font-medium"
                  >
                    <Tag className="w-2.5 h-2.5" />
                    {v}
                  </span>
                ))}
              </div>
            </div>
          );
        })}

        {otherKeys.map((key) => (
          <div key={key} className="flex items-start gap-2">
            <span className="shrink-0 text-[11px] font-bold text-slate-400 uppercase tracking-wide pt-0.5 w-24">
              {key}
            </span>
            <span className="text-xs text-slate-600 break-all">{String(data[key])}</span>
          </div>
        ))}
      </div>

      {sources.length > 0 && (
        <div className="mt-3 pt-3 border-t border-slate-200">
          <button
            onClick={() => setSourcesOpen((o) => !o)}
            className="flex items-center gap-1 text-[11px] font-bold text-slate-500 hover:text-slate-700"
          >
            {sourcesOpen ? <ChevronDown className="w-3 h-3" /> : <ChevronRight className="w-3 h-3" />}
            来源文件 ({sources.length})
          </button>
          {sourcesOpen && (
            <ul className="mt-1.5 space-y-0.5 pl-4">
              {sources.map((s, idx) => (
                <li key={idx} className="text-[11px] font-mono text-slate-500 truncate">
                  {s}
                </li>
              ))}
            </ul>
          )}
        </div>
      )}

      {fieldSources != null && typeof fieldSources === "object" && (
        <div className="mt-3 pt-3 border-t border-slate-200">
          <button
            onClick={() => setFieldSourcesOpen((o) => !o)}
            className="flex items-center gap-1 text-[11px] font-bold text-slate-500 hover:text-slate-700"
          >
            {fieldSourcesOpen ? <ChevronDown className="w-3 h-3" /> : <ChevronRight className="w-3 h-3" />}
            字段来源明细
          </button>
          {fieldSourcesOpen && (
            <div className="mt-2 pl-4">
              <FieldSourcesTree node={fieldSources} />
            </div>
          )}
        </div>
      )}
    </div>
  );
}