Files
DeepTutor/web/features/multi-user/components/GrantEditor.tsx
Bingxi Zhao (Frank) da856ad670 release: v1.6.9
Ship folder-based workspaces and data migration, daily practice, redesigned
provider and model settings, and streaming conversation improvements.
Synchronize README translations and document the archive/permanent-delete
upgrade behavior in assets/releases/ver1-6-9.md.

Reduce initial JavaScript by deduplicating English resources and loading
animation features after hydration. Keep the existing route size budgets.
Update regression fixtures and browser audits for scoped URLs and the new
settings and learning interfaces.
2026-09-21 02:30:40 +08:00

863 lines
32 KiB
TypeScript

"use client";
import { useEffect, useMemo, useState } from "react";
import {
AlertCircle,
CheckCircle2,
GraduationCap,
Loader2,
Save,
} from "lucide-react";
import { useTranslation } from "react-i18next";
import McpToolGroups from "@/components/common/McpToolGroups";
import { toggleToolName as toggleName } from "@/lib/mcp-tool-groups";
import { fetchAdminResources, fetchUserGrant, saveUserGrant } from "../api";
import type {
GrantPayload,
LearningPolicy,
MultiUserResources,
} from "../types";
type SaveState = "idle" | "saving" | "saved" | "error";
function emptyGrant(userId: string): GrantPayload {
return {
version: 2,
user_id: userId,
models: { llm: [] },
knowledge_bases: [],
skills: [],
partners: [],
enabled_tools: null,
mcp_tools: null,
exec_enabled: null,
learning_policy: null,
};
}
const LEARNING_AGE_BANDS = ["6-8", "9-12", "13-15"] as const;
function conservativeLearningPolicy(): LearningPolicy {
return {
age_band: "9-12",
locked_persona: "teacher",
allowed_capabilities: ["chat", "immersive_reading"],
default_capability: "immersive_reading",
allowed_surfaces: ["chat", "reading"],
reading: {
allow_upload: false,
material_ids: [],
extensions: [],
},
};
}
function hasModel(grant: GrantPayload, profileId: string, modelId?: string) {
return grant.models.llm.some((item) => {
if (item.profile_id !== profileId) return false;
if (!modelId) return true;
return Array.isArray(item.model_ids) && item.model_ids.includes(modelId);
});
}
function grantFingerprint(grant: GrantPayload): string {
return JSON.stringify(grant);
}
function SectionTitle({ children }: { children: React.ReactNode }) {
return (
<h3 className="mb-2 text-xs font-semibold uppercase tracking-wide text-[var(--muted-foreground)]">
{children}
</h3>
);
}
function CheckRow({
label,
description,
checked,
disabled,
onToggle,
}: {
label: string;
description?: string;
checked: boolean;
disabled: boolean;
onToggle: () => void;
}) {
return (
<label className="flex cursor-pointer items-start gap-2 rounded-lg border border-[var(--border)]/60 p-2 text-[var(--foreground)]">
<input
type="checkbox"
checked={checked}
disabled={disabled}
onChange={onToggle}
className="mt-0.5"
/>
<span className="min-w-0">
<span className="block truncate">{label}</span>
{description ? (
<span className="block truncate text-[11px] text-[var(--muted-foreground)]">
{description}
</span>
) : null}
</span>
</label>
);
}
/**
* Default-vs-custom switch for a whitelist field. ``null`` selects the
* "default" mode; what that resolves to server-side depends on the field —
* built-in tools default to *all*, MCP tools default to *none* (deny until
* explicitly granted) — so the default-mode label is caller-supplied.
*/
function ModeSwitch({
isCustom,
disabled,
onDefault,
onCustom,
defaultLabel,
}: {
isCustom: boolean;
disabled: boolean;
onDefault: () => void;
onCustom: () => void;
defaultLabel?: string;
}) {
const { t } = useTranslation();
const base =
"rounded-md px-2 py-0.5 text-[11px] font-medium transition-colors disabled:cursor-not-allowed disabled:opacity-45";
return (
<div className="mb-2 inline-flex gap-1 rounded-lg bg-[var(--muted)]/50 p-0.5">
<button
type="button"
disabled={disabled}
onClick={onDefault}
className={`${base} ${
!isCustom
? "bg-[var(--card)] text-[var(--foreground)] shadow-sm"
: "text-[var(--muted-foreground)] hover:text-[var(--foreground)]"
}`}
>
{defaultLabel ?? t("Default · all")}
</button>
<button
type="button"
disabled={disabled}
onClick={onCustom}
className={`${base} ${
isCustom
? "bg-[var(--card)] text-[var(--foreground)] shadow-sm"
: "text-[var(--muted-foreground)] hover:text-[var(--foreground)]"
}`}
>
{t("Custom")}
</button>
</div>
);
}
export function GrantEditor({
userId,
lockLearningPolicy = false,
}: {
userId: string;
lockLearningPolicy?: boolean;
}) {
const { t } = useTranslation();
const [resources, setResources] = useState<MultiUserResources | null>(null);
const [grant, setGrant] = useState<GrantPayload>(() => emptyGrant(userId));
const [loading, setLoading] = useState(true);
const [savedFingerprint, setSavedFingerprint] = useState("");
const [saveState, setSaveState] = useState<SaveState>("idle");
const [message, setMessage] = useState("");
useEffect(() => {
let cancelled = false;
Promise.all([fetchAdminResources(), fetchUserGrant(userId)])
.then(([nextResources, nextGrant]) => {
if (cancelled) return;
setResources(nextResources);
setGrant(nextGrant);
setSavedFingerprint(grantFingerprint(nextGrant));
})
.catch((error) => {
setSaveState("error");
setMessage(
error instanceof Error ? error.message : t("Failed to load grants"),
);
})
.finally(() => {
if (!cancelled) setLoading(false);
});
return () => {
cancelled = true;
};
}, [userId, t]);
const currentFingerprint = useMemo(() => grantFingerprint(grant), [grant]);
const dirty =
Boolean(savedFingerprint) && currentFingerprint !== savedFingerprint;
const kbIds = useMemo(
() =>
new Set(
grant.knowledge_bases.map((item) =>
String(item.resource_id || item.id || ""),
),
),
[grant.knowledge_bases],
);
const skillIds = useMemo(
() =>
new Set(
grant.skills.map((item) => String(item.skill_id || item.id || "")),
),
[grant.skills],
);
const partnerIds = useMemo(
() =>
new Set(
grant.partners.map((item) => String(item.partner_id || item.id || "")),
),
[grant.partners],
);
const selectedModelCount = useMemo(
() =>
grant.models.llm.reduce((total, item) => {
if (Array.isArray(item.model_ids)) return total + item.model_ids.length;
return total + 1;
}, 0),
[grant.models.llm],
);
const saving = saveState === "saving";
const controlsDisabled = loading || saving;
function toggleModel(profileId: string, modelId: string) {
setGrant((current) => {
const next = structuredClone(current) as GrantPayload;
const items = next.models.llm;
const existing = items.find((item) => item.profile_id === profileId);
if (!existing) {
items.push({
profile_id: profileId,
model_ids: [modelId],
source: "admin",
});
return next;
}
const modelIds = new Set(
Array.isArray(existing.model_ids) ? existing.model_ids : [],
);
if (modelIds.has(modelId)) modelIds.delete(modelId);
else modelIds.add(modelId);
existing.model_ids = Array.from(modelIds);
next.models.llm = items.filter((item) =>
Array.isArray(item.model_ids) ? item.model_ids.length > 0 : true,
);
return next;
});
}
function toggleKb(resourceId: string, name: string) {
setGrant((current) => {
const next = structuredClone(current) as GrantPayload;
const exists = kbIds.has(resourceId);
next.knowledge_bases = exists
? next.knowledge_bases.filter(
(item) => String(item.resource_id || item.id || "") !== resourceId,
)
: [
...next.knowledge_bases,
{ resource_id: resourceId, name, access: "read", source: "admin" },
];
return next;
});
}
function toggleSkill(name: string) {
setGrant((current) => {
const next = structuredClone(current) as GrantPayload;
const exists = skillIds.has(name);
next.skills = exists
? next.skills.filter(
(item) => String(item.skill_id || item.id || "") !== name,
)
: [...next.skills, { skill_id: name, access: "use", source: "admin" }];
return next;
});
}
function togglePartner(partnerId: string, name: string) {
setGrant((current) => {
const next = structuredClone(current) as GrantPayload;
const exists = partnerIds.has(partnerId);
next.partners = exists
? next.partners.filter(
(item) => String(item.partner_id || item.id || "") !== partnerId,
)
: [...next.partners, { partner_id: partnerId, name, source: "admin" }];
return next;
});
}
function setToolList(
key: "enabled_tools" | "mcp_tools",
value: string[] | null,
) {
setGrant((current) => ({ ...current, [key]: value }));
}
function enableLearningPolicy() {
setGrant((current) => ({
...current,
learning_policy: conservativeLearningPolicy(),
}));
}
function disableLearningPolicy() {
setGrant((current) => ({ ...current, learning_policy: null }));
}
function setLearningAgeBand(ageBand: LearningPolicy["age_band"]) {
setGrant((current) =>
current.learning_policy
? {
...current,
learning_policy: { ...current.learning_policy, age_band: ageBand },
}
: current,
);
}
function updateReadingPolicy(
update: (reading: LearningPolicy["reading"]) => LearningPolicy["reading"],
) {
setGrant((current) => {
if (!current.learning_policy) return current;
const reading = current.learning_policy.reading;
return {
...current,
learning_policy: {
...current.learning_policy,
allowed_surfaces: current.learning_policy.allowed_surfaces ?? [
"chat",
"reading",
],
reading: update(reading),
},
};
});
}
function toggleReadingMaterial(materialId: string) {
updateReadingPolicy((reading) => ({
...reading,
material_ids: reading.material_ids.includes(materialId)
? reading.material_ids.filter((id) => id !== materialId)
: [...reading.material_ids, materialId],
}));
}
function toggleReadingExtension(extensionId: string) {
updateReadingPolicy((reading) => ({
...reading,
extensions: reading.extensions.includes(extensionId)
? reading.extensions.filter((id) => id !== extensionId)
: [...reading.extensions, extensionId],
}));
}
// Named apart from the imported `toggleName` helper it wraps, and narrowed to
// the one key that still uses it: MCP rows go through McpToolGroups now.
function toggleGrantTool(key: "enabled_tools", name: string) {
setGrant((current) => {
const list = current[key];
if (list === null) return current;
return { ...current, [key]: toggleName(list, name) };
});
}
async function save() {
setSaveState("saving");
setMessage("");
try {
const saved = await saveUserGrant(userId, grant);
setGrant(saved);
setSavedFingerprint(grantFingerprint(saved));
setSaveState("saved");
setMessage(t("Saved just now"));
} catch (error) {
setSaveState("error");
setMessage(error instanceof Error ? error.message : t("Failed to save"));
}
}
const status = loading
? t("Loading assignments...")
: saveState === "saving"
? t("Saving changes...")
: saveState === "error"
? message || t("Failed to save")
: saveState === "saved" && !dirty
? message || t("Saved just now")
: dirty
? t("Unsaved changes")
: t("Ready");
const statusTone =
saveState === "error"
? "text-red-600 dark:text-red-400"
: saveState === "saved" && !dirty
? "text-emerald-700 dark:text-emerald-300"
: "text-[var(--muted-foreground)]";
const toolsSummary =
grant.enabled_tools === null
? t("all tools")
: t("{{count}} tools", { count: grant.enabled_tools.length });
// MCP tools deny-by-default for non-admin users: ``null`` grants none until
// the admin switches to Custom and picks specific tool names.
const mcpSummary =
grant.mcp_tools === null ? t("no MCP") : t("{{count}} MCP tools", { count: grant.mcp_tools.length });
if (loading && !resources) {
return (
<div className="border-t border-[var(--border)] bg-[var(--background)]/40 p-4">
<div className="flex h-[420px] items-center justify-center rounded-2xl border border-[var(--border)] bg-[var(--card)] text-sm text-[var(--muted-foreground)]">
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
{t("Loading assignments...")}
</div>
</div>
);
}
return (
<div className="border-t border-[var(--border)] bg-[var(--background)]/40 p-4">
<div className="flex h-[620px] max-h-[calc(100vh-170px)] min-h-[420px] flex-col overflow-hidden rounded-2xl border border-[var(--border)] bg-[var(--card)] shadow-sm">
<div className="shrink-0 border-b border-[var(--border)] px-5 py-4">
<div className="flex flex-wrap items-start justify-between gap-3">
<div>
<h2 className="text-sm font-semibold text-[var(--foreground)]">
{t("Assign access")}
</h2>
<p className="mt-0.5 text-xs text-[var(--muted-foreground)]">
{t("Admin resources stay linked server-side; users only receive allowed access.")}
</p>
</div>
<div className="flex flex-wrap gap-1.5 text-[11px] text-[var(--muted-foreground)]">
<span className="rounded-full bg-[var(--muted)]/60 px-2 py-1">
{t("{{count}} models", { count: selectedModelCount })}
</span>
<span className="rounded-full bg-[var(--muted)]/60 px-2 py-1">
{t("{{count}} KBs", { count: kbIds.size })}
</span>
<span className="rounded-full bg-[var(--muted)]/60 px-2 py-1">
{t("{{count}} skills", { count: skillIds.size })}
</span>
<span className="rounded-full bg-[var(--muted)]/60 px-2 py-1">
{t("{{count}} partners", { count: partnerIds.size })}
</span>
<span className="rounded-full bg-[var(--muted)]/60 px-2 py-1">
{toolsSummary}
</span>
<span className="rounded-full bg-[var(--muted)]/60 px-2 py-1">
{mcpSummary}
</span>
</div>
</div>
</div>
<div className="min-h-0 flex-1 overflow-y-auto px-5 py-5 [scrollbar-gutter:stable]">
<div className="grid gap-5 md:grid-cols-3">
<section className="min-w-0 md:col-span-3">
<SectionTitle>{t("Learning policy")}</SectionTitle>
<div className="rounded-lg border border-[var(--border)]/60 p-3">
<CheckRow
label={t("Enable learning policy")}
description={t(
"Teacher persona; Chat and Immersive Reading only",
)}
checked={Boolean(grant.learning_policy)}
disabled={controlsDisabled || lockLearningPolicy}
onToggle={() =>
grant.learning_policy
? disableLearningPolicy()
: enableLearningPolicy()
}
/>
{grant.learning_policy && (
<div className="mt-3 grid gap-3 sm:grid-cols-3">
<label className="text-xs text-[var(--foreground)]">
<span className="mb-1 block text-[11px] text-[var(--muted-foreground)]">
{t("Age band")}
</span>
<select
value={grant.learning_policy.age_band}
disabled={controlsDisabled}
onChange={(event) =>
setLearningAgeBand(
event.target.value as LearningPolicy["age_band"],
)
}
className="h-8 w-full rounded-md border border-[var(--border)] bg-[var(--background)] px-2"
>
{LEARNING_AGE_BANDS.map((band) => (
<option key={band} value={band}>
{band}
</option>
))}
</select>
</label>
<label className="text-xs text-[var(--foreground)]">
<span className="mb-1 block text-[11px] text-[var(--muted-foreground)]">
{t("Persona")}
</span>
<select
value={grant.learning_policy.locked_persona}
disabled
className="h-8 w-full rounded-md border border-[var(--border)] bg-[var(--background)] px-2"
>
<option value="teacher">{t("Teacher")}</option>
</select>
</label>
<div className="text-xs">
<span className="mb-1 block text-[11px] text-[var(--muted-foreground)]">
{t("Modes")}
</span>
<div className="flex h-8 items-center gap-1.5">
<GraduationCap
size={14}
className="text-[var(--muted-foreground)]"
/>
<span>{t("Chat · Immersive Reading")}</span>
</div>
</div>
<div className="grid gap-3 sm:col-span-3 lg:grid-cols-2">
<div>
<div className="mb-1 text-[11px] text-[var(--muted-foreground)]">
{t("Assigned reading materials")}
</div>
<div className="grid gap-1 sm:grid-cols-2">
{(resources?.reading_materials ?? []).map(
(material) => (
<CheckRow
key={material.material_id}
label={material.title || material.filename}
description={material.filename}
checked={Boolean(
grant.learning_policy?.reading.material_ids.includes(
material.material_id,
),
)}
disabled={controlsDisabled}
onToggle={() =>
toggleReadingMaterial(material.material_id)
}
/>
),
)}
{(resources?.reading_materials ?? []).length === 0 ? (
<p className="text-[11px] leading-relaxed text-[var(--muted-foreground)]">
{t(
"Upload books in Reading before assigning them.",
)}
</p>
) : null}
</div>
<label className="mt-2 flex items-center gap-2 text-xs">
<input
type="checkbox"
checked={grant.learning_policy.reading.allow_upload}
disabled={controlsDisabled}
onChange={(event) =>
updateReadingPolicy((reading) => ({
...reading,
allow_upload: event.target.checked,
}))
}
/>
{t("Allow learner uploads")}
</label>
</div>
<div>
<div className="mb-1 text-[11px] text-[var(--muted-foreground)]">
{t("Reading extensions")}
</div>
<div className="grid gap-1 sm:grid-cols-2">
{(resources?.reading_extensions ?? []).map(
(extension) => (
<CheckRow
key={extension.id}
label={extension.name}
description={`${extension.id} · ${extension.version}`}
checked={Boolean(
grant.learning_policy?.reading.extensions.includes(
extension.id,
),
)}
disabled={controlsDisabled}
onToggle={() =>
toggleReadingExtension(extension.id)
}
/>
),
)}
</div>
</div>
</div>
</div>
)}
</div>
</section>
<section className="min-w-0">
<SectionTitle>{t("Models")}</SectionTitle>
<div className="space-y-1.5 text-xs">
{(resources?.models.llm || []).map((profile) => (
<div
key={profile.profile_id}
className="rounded-lg border border-[var(--border)]/60 p-2"
>
<div className="mb-1 truncate text-[var(--muted-foreground)]">
{profile.name}
</div>
{(profile.models || []).map((model) => (
<label
key={model.model_id}
className="flex cursor-pointer items-center gap-2 py-1 text-[var(--foreground)]"
>
<input
type="checkbox"
checked={hasModel(
grant,
profile.profile_id,
model.model_id,
)}
disabled={controlsDisabled}
onChange={() =>
toggleModel(profile.profile_id, model.model_id)
}
/>
<span className="truncate">{model.name}</span>
</label>
))}
</div>
))}
</div>
</section>
<section className="min-w-0">
<SectionTitle>{t("Knowledge")}</SectionTitle>
<div className="space-y-1.5 text-xs">
{(resources?.knowledge_bases || []).map((kb) => (
<CheckRow
key={kb.resource_id}
label={kb.name}
checked={kbIds.has(kb.resource_id)}
disabled={controlsDisabled}
onToggle={() => toggleKb(kb.resource_id, kb.name)}
/>
))}
</div>
</section>
<section className="min-w-0">
<SectionTitle>{t("Skills")}</SectionTitle>
<div className="space-y-1.5 text-xs">
{(resources?.skills || []).map((skill) => (
<CheckRow
key={skill.name}
label={skill.name}
checked={skillIds.has(skill.name)}
disabled={controlsDisabled}
onToggle={() => toggleSkill(skill.name)}
/>
))}
</div>
</section>
<section className="min-w-0">
<SectionTitle>{t("Partners")}</SectionTitle>
<div className="space-y-1.5 text-xs">
{(resources?.partners || []).length === 0 ? (
<p className="px-1 text-[11px] leading-relaxed text-[var(--muted-foreground)]">
{t("No partners yet. Create one under Partners to assign it.")}
</p>
) : (
(resources?.partners || []).map((partner) => (
<CheckRow
key={partner.partner_id}
label={partner.name || partner.partner_id}
description={partner.description}
checked={partnerIds.has(partner.partner_id)}
disabled={controlsDisabled}
onToggle={() =>
togglePartner(
partner.partner_id,
partner.name || partner.partner_id,
)
}
/>
))
)}
</div>
</section>
<section className="min-w-0">
<SectionTitle>{t("System tools")}</SectionTitle>
<ModeSwitch
isCustom={grant.enabled_tools !== null}
disabled={controlsDisabled}
onDefault={() => setToolList("enabled_tools", null)}
onCustom={() =>
setToolList(
"enabled_tools",
(resources?.tools || []).map((tool) => tool.name),
)
}
/>
{grant.enabled_tools !== null && (
<div className="space-y-1.5 text-xs">
{(resources?.tools || []).map((tool) => (
<CheckRow
key={tool.name}
label={tool.name}
description={tool.description}
checked={grant.enabled_tools!.includes(tool.name)}
disabled={controlsDisabled}
onToggle={() =>
toggleGrantTool("enabled_tools", tool.name)
}
/>
))}
</div>
)}
</section>
<section className="min-w-0">
<SectionTitle>{t("MCP tools")}</SectionTitle>
<ModeSwitch
isCustom={grant.mcp_tools !== null}
disabled={controlsDisabled}
defaultLabel={t("Default · none")}
onDefault={() => setToolList("mcp_tools", null)}
// Custom starts empty: the admin picks the services to assign,
// rather than un-picking hundreds of tools they never meant to
// grant by switching modes.
onCustom={() => setToolList("mcp_tools", [])}
/>
{grant.mcp_tools === null ? (
<p className="px-1 text-[11px] leading-relaxed text-[var(--muted-foreground)]">
{t("MCP tools proxy host-side capabilities, so they stay denied by default. Switch to Custom to assign specific services.")}
</p>
) : null}
{grant.mcp_tools !== null &&
(resources?.mcp_tools?.length ? (
<div className="text-xs">
{/* Bulk affordance: with ~40 curated services, granting
everything must not mean forty clicks. Mirrors the
partner picker, which has had All/None all along. */}
<div className="mb-1.5 flex items-center gap-2 px-1">
<button
type="button"
disabled={controlsDisabled}
onClick={() =>
setToolList(
"mcp_tools",
(resources.mcp_tools ?? []).map(
(tool) => tool.name,
),
)
}
className="rounded px-1.5 py-0.5 text-[11px] text-[var(--muted-foreground)] hover:text-[var(--foreground)] disabled:opacity-50"
>
{t("All")}
</button>
<button
type="button"
disabled={controlsDisabled}
onClick={() => setToolList("mcp_tools", [])}
className="rounded px-1.5 py-0.5 text-[11px] text-[var(--muted-foreground)] hover:text-[var(--foreground)] disabled:opacity-50"
>
{t("None")}
</button>
</div>
<McpToolGroups
tools={resources.mcp_tools}
selected={grant.mcp_tools}
onChange={(next) => setToolList("mcp_tools", next)}
disabled={controlsDisabled}
renderTool={({ tool, checked, onToggle }) => (
<CheckRow
key={tool.name}
label={tool.name}
description={tool.description}
checked={checked}
disabled={controlsDisabled}
onToggle={onToggle}
/>
)}
/>
</div>
) : (
<p className="text-xs text-[var(--muted-foreground)]">
{t("No MCP servers configured.")}
</p>
))}
</section>
<section className="min-w-0">
<SectionTitle>{t("Code execution")}</SectionTitle>
<div className="space-y-1.5 text-xs">
<CheckRow
label={t("Allow code execution")}
description={t("Follows the deployment sandbox policy. Uncheck to disable exec for this user.")}
checked={grant.exec_enabled !== false}
disabled={controlsDisabled}
onToggle={() =>
setGrant((current) => ({
...current,
exec_enabled:
current.exec_enabled === false ? null : false,
}))
}
/>
</div>
</section>
</div>
</div>
<div className="flex shrink-0 items-center justify-between gap-3 border-t border-[var(--border)] bg-[var(--card)] px-5 py-3">
<div
aria-live="polite"
className={`flex min-w-0 items-center gap-1.5 text-xs ${statusTone}`}
>
{saveState === "error" ? (
<AlertCircle className="h-3.5 w-3.5 shrink-0" />
) : saveState === "saved" && !dirty ? (
<CheckCircle2 className="h-3.5 w-3.5 shrink-0" />
) : null}
<span className="truncate">{status}</span>
</div>
<button
onClick={save}
disabled={controlsDisabled || !dirty}
className="inline-flex shrink-0 items-center gap-1.5 rounded-lg bg-[var(--foreground)] px-3 py-1.5 text-xs font-medium text-[var(--background)] transition-opacity hover:opacity-90 disabled:cursor-not-allowed disabled:opacity-45"
>
{saving ? (
<Loader2 className="h-3 w-3 animate-spin" />
) : saveState === "saved" && !dirty ? (
<CheckCircle2 className="h-3 w-3" />
) : (
<Save className="h-3 w-3" />
)}
{saving
? t("Saving...")
: saveState === "saved" && !dirty
? t("Saved")
: t("Save assignments")}
</button>
</div>
</div>
</div>
);
}