103 lines
5.1 KiB
TypeScript
103 lines
5.1 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { BookOpen /* , Database, SearchCheck */ } from "lucide-react";
|
|
import { useLocation, useNavigate } from "react-router-dom";
|
|
import { toast } from "sonner";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
|
import { getStoredAuth } from "@/core/auth";
|
|
// import { canAccessAssistantKnowledge } from "@/core/llmwiki/assistant-knowledge-access-policy";
|
|
// import {
|
|
// useAssistantKnowledgeVisibility,
|
|
// useUpdateAssistantKnowledgeVisibility,
|
|
// } from "@/core/system-settings/hooks";
|
|
import { useLlmWikiRuntime } from "@/strategy-components/api/llmwiki";
|
|
import {
|
|
loadOrdinaryKnowledgeLanding,
|
|
saveOrdinaryKnowledgeLanding,
|
|
type OrdinaryKnowledgeLanding,
|
|
} from "@/core/system-settings/api";
|
|
|
|
const LANDING_KEY = ["system-settings", "ordinary-knowledge-landing"] as const;
|
|
|
|
export function useOrdinaryKnowledgeLanding(enabled = true) {
|
|
return useQuery({
|
|
queryKey: LANDING_KEY,
|
|
queryFn: ({ signal }) => loadOrdinaryKnowledgeLanding(signal),
|
|
enabled,
|
|
staleTime: 30_000,
|
|
retry: false,
|
|
});
|
|
}
|
|
export const KNOWLEDGE_NORMAL_VIEW_VALUE = "normal";
|
|
export const KNOWLEDGE_NORMAL_VIEW_SEARCH = `?view=${KNOWLEDGE_NORMAL_VIEW_VALUE}`;
|
|
|
|
export function KnowledgeProductNav() {
|
|
const location = useLocation();
|
|
const navigate = useNavigate();
|
|
const client = useQueryClient();
|
|
// const assistantVisibility = useAssistantKnowledgeVisibility();
|
|
const isAdmin = getStoredAuth()?.system_role === "admin";
|
|
const landing = useOrdinaryKnowledgeLanding(isAdmin);
|
|
const saveLanding = useMutation({
|
|
mutationFn: saveOrdinaryKnowledgeLanding,
|
|
onSuccess: (mode) => { client.setQueryData(LANDING_KEY, mode); toast.success("普通知识库默认页面已更新"); },
|
|
onError: (error) => toast.error(error.message),
|
|
});
|
|
const runtime = useLlmWikiRuntime();
|
|
const normalAvailable = runtime.data?.provider === "weknora" && Boolean(runtime.data.api_base_url?.trim());
|
|
// const assistantAllowed = canAccessAssistantKnowledge(
|
|
// isAdmin,
|
|
// assistantVisibility.isError ? undefined : assistantVisibility.data,
|
|
// );
|
|
// const saveAssistantVisibility = useUpdateAssistantKnowledgeVisibility();
|
|
const retrievalDebug = location.pathname.includes("/knowledge/retrieval-debug");
|
|
const assistant = location.pathname.includes("/knowledge/assistant");
|
|
const ordinary = !assistant && !retrievalDebug;
|
|
|
|
return (
|
|
<div className="flex flex-wrap items-center gap-2 border-b bg-card px-6 py-2">
|
|
<Button size="sm" disabled={!normalAvailable} title={normalAvailable ? "普通知识库" : "管理员配置知识服务地址后可用"} variant={ordinary ? "secondary" : "ghost"} onClick={() => navigate(`/page/workspace/knowledge${KNOWLEDGE_NORMAL_VIEW_SEARCH}`)}>
|
|
<BookOpen />普通知识库
|
|
</Button>
|
|
{/* 助手知识库、检索调试暂时隐藏,后续放开时去掉这段注释。
|
|
{assistantAllowed && (
|
|
<Button size="sm" variant={assistant ? "secondary" : "ghost"} onClick={() => navigate("/page/workspace/knowledge/assistant")}>
|
|
<Database />助手知识库
|
|
</Button>
|
|
)}
|
|
{isAdmin && (
|
|
<Button size="sm" variant={retrievalDebug ? "secondary" : "ghost"} onClick={() => navigate("/page/workspace/knowledge/retrieval-debug")}>
|
|
<SearchCheck />检索调试
|
|
</Button>
|
|
)}
|
|
*/}
|
|
{isAdmin && (
|
|
<div className="ml-auto flex flex-wrap items-center gap-2 text-xs text-muted-foreground">
|
|
普通知识库默认页面
|
|
<Select disabled={landing.isLoading || saveLanding.isPending} value={landing.data ?? "wiki"} onValueChange={(value) => saveLanding.mutate(value as OrdinaryKnowledgeLanding)}>
|
|
<SelectTrigger className="h-8 w-40" aria-label="普通知识库默认页面"><SelectValue /></SelectTrigger>
|
|
<SelectContent><SelectItem value="wiki">本系统 Wiki 索引</SelectItem><SelectItem value="weknora">知识库管理页面</SelectItem></SelectContent>
|
|
</Select>
|
|
{/* 助手知识库日常展示暂时隐藏,和上面两个入口一起放开。
|
|
助手知识库日常展示
|
|
<Select
|
|
disabled={assistantVisibility.isLoading || saveAssistantVisibility.isPending}
|
|
value={assistantVisibility.data === true ? "visible" : "hidden"}
|
|
onValueChange={(value) =>
|
|
saveAssistantVisibility.mutate(value === "visible", {
|
|
onSuccess: (visible) => toast.success(visible ? "已向普通用户开放助手知识库" : "已隐藏普通用户的助手知识库"),
|
|
onError: (error) => toast.error(error.message),
|
|
})
|
|
}
|
|
>
|
|
<SelectTrigger className="h-8 w-52" aria-label="助手知识库日常展示"><SelectValue /></SelectTrigger>
|
|
<SelectContent><SelectItem value="hidden">隐藏(仅管理员可用)</SelectItem><SelectItem value="visible">开放给普通用户</SelectItem></SelectContent>
|
|
</Select>
|
|
*/}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|