deer-flow/web/src/core/api/hooks.ts
2025-05-08 11:53:50 +08:00

42 lines
1.1 KiB
TypeScript

// Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
// SPDX-License-Identifier: MIT
import { useEffect, useRef, useState } from "react";
import { useReplay } from "../replay";
import { fetchReplayTitle } from "./chat";
export function useReplayMetadata() {
const { isReplay } = useReplay();
const [title, setTitle] = useState<string | null>(null);
const isLoading = useRef(false);
const [error, setError] = useState<boolean>(false);
useEffect(() => {
if (!isReplay) {
return;
}
if (title || isLoading.current) {
return;
}
isLoading.current = true;
fetchReplayTitle()
.then((title) => {
setError(false);
setTitle(title ?? null);
if (title) {
document.title = `${title} - DeerFlow`;
}
})
.catch(() => {
setError(true);
setTitle("Error: the replay is not available.");
document.title = "DeerFlow";
})
.finally(() => {
isLoading.current = false;
});
}, [isLoading, isReplay, title]);
return { title, isLoading, hasError: error };
}