2025-11-23 10:55:04 +08:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
import { createReactBlockSpec } from "@blocknote/react";
|
|
|
|
|
import { RiFileTextFill } from "react-icons/ri";
|
|
|
|
|
import { useRouter } from "next/navigation";
|
2026-01-15 20:54:21 +08:00
|
|
|
import { getSupabaseBrowserClient } from "@/lib/supabase/client";
|
2025-11-23 10:55:04 +08:00
|
|
|
|
|
|
|
|
const normalizeTitle = (value?: string | null) => {
|
|
|
|
|
if (!value || !value.trim()) {
|
|
|
|
|
return "未命名页面";
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const PageReferenceContent = ({ pageId, title }: { pageId: string; title: string }) => {
|
|
|
|
|
const router = useRouter();
|
2026-01-15 20:54:21 +08:00
|
|
|
const supabaseBrowser = getSupabaseBrowserClient();
|
2025-11-23 10:55:04 +08:00
|
|
|
const fallbackTitle = normalizeTitle(title);
|
|
|
|
|
const [resolvedTitle, setResolvedTitle] = useState(fallbackTitle);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setResolvedTitle(fallbackTitle);
|
|
|
|
|
}, [fallbackTitle]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!pageId) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
const applyTitle = (nextTitle?: string | null) => {
|
|
|
|
|
if (!cancelled) {
|
|
|
|
|
setResolvedTitle(normalizeTitle(nextTitle));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const fetchTitle = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const { data } = await supabaseBrowser
|
|
|
|
|
.from("documents")
|
|
|
|
|
.select("title")
|
|
|
|
|
.eq("id", pageId)
|
2026-01-15 20:54:21 +08:00
|
|
|
.single<{ title: string | null }>();
|
2025-11-23 10:55:04 +08:00
|
|
|
if (data) {
|
|
|
|
|
applyTitle(data.title);
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// ignore fetch errors,等待后续订阅同步
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void fetchTitle();
|
|
|
|
|
|
|
|
|
|
const channel = supabaseBrowser
|
|
|
|
|
.channel(`page-ref-${pageId}`)
|
|
|
|
|
.on(
|
|
|
|
|
"postgres_changes",
|
|
|
|
|
{ event: "UPDATE", schema: "public", table: "documents", filter: `id=eq.${pageId}` },
|
|
|
|
|
(payload) => {
|
|
|
|
|
const nextTitle = (payload.new as { title?: string } | null)?.title ?? null;
|
|
|
|
|
applyTitle(nextTitle);
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.subscribe();
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
supabaseBrowser.removeChannel(channel);
|
|
|
|
|
};
|
|
|
|
|
}, [pageId]);
|
|
|
|
|
|
|
|
|
|
const navigate = () => {
|
|
|
|
|
if (pageId) {
|
|
|
|
|
router.push(`/documents/${pageId}`);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
role="button"
|
|
|
|
|
tabIndex={0}
|
|
|
|
|
onClick={navigate}
|
|
|
|
|
onKeyDown={(event) => {
|
|
|
|
|
if ((event.key === "Enter" || event.key === " ") && pageId) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
navigate();
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
className="group mt-2 flex items-center gap-3 rounded-[4px] px-4 py-3 text-[#2563eb] hover:bg-[#f5f5f5]"
|
|
|
|
|
style={{ fontFamily: "Inter, system-ui, sans-serif" }}
|
|
|
|
|
>
|
|
|
|
|
<RiFileTextFill className="text-xl" aria-hidden />
|
|
|
|
|
<span className="text-base font-medium leading-none">{resolvedTitle}</span>
|
|
|
|
|
<span className="ml-auto text-sm opacity-0 transition-opacity duration-150 group-hover:opacity-100">
|
|
|
|
|
点击进入 →
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const pageReferenceBlock = createReactBlockSpec(
|
|
|
|
|
{
|
|
|
|
|
type: "pageReference",
|
|
|
|
|
propSchema: {
|
|
|
|
|
pageId: { default: "" },
|
|
|
|
|
title: { default: "未命名页面" },
|
|
|
|
|
},
|
|
|
|
|
content: "none",
|
|
|
|
|
},
|
|
|
|
|
() => ({
|
|
|
|
|
render: ({ block }) => (
|
|
|
|
|
<PageReferenceContent pageId={block.props.pageId} title={block.props.title} />
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
)();
|