* feat(webui): unify the Apps catalog * fix(webui): serve all channel branding locally * fix(webui): refine Apps navigation and sidebar
189 lines
9.2 KiB
TypeScript
189 lines
9.2 KiB
TypeScript
import { useCallback, useLayoutEffect, useRef, useState } from "react";
|
|
import { X } from "lucide-react";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { CatalogControls, CatalogToggle, CatalogToolbar } from "@/components/settings/shared/CatalogToolbar";
|
|
import { CatalogSkeleton } from "@/components/settings/shared/CatalogSkeleton";
|
|
import type { ChannelFeatureAction } from "@/channel-plugins/types";
|
|
import { ChannelCatalogRow } from "@/components/settings/channels/ChannelCatalogRow";
|
|
import { localizedChannelDisplayName } from "@/components/settings/channels/ChannelIdentity";
|
|
import { ChannelHelpMenu } from "@/components/settings/channels/ChannelHelpMenu";
|
|
import { ChannelSetupPanel } from "@/components/settings/channels/ChannelSetupPanel";
|
|
import { DismissibleStatusMessage, RestartRequiredNotice, SettingsGroup } from "@/components/settings/shared/SettingsControls";
|
|
import type { NanobotFeaturesPayload } from "@/lib/types";
|
|
import { Dialog, DialogClose, DialogContent, DialogTitle } from "@/components/ui/dialog";
|
|
|
|
type ChannelFilter = "all" | "enabled";
|
|
|
|
export function ChannelsSettings({
|
|
token, nanobotFeatures, loading, actionKey, chatAppsDocsUrl, showBrandLogos, query,
|
|
error, requiresRestartPending, onAction, onFeaturesUpdate, onDismissStatus,
|
|
onRestart, isRestarting, onConfigureMcp, onBeforeCloseChange,
|
|
}: {
|
|
token: string;
|
|
nanobotFeatures: NanobotFeaturesPayload | null;
|
|
loading: boolean;
|
|
actionKey: string | null;
|
|
chatAppsDocsUrl?: string;
|
|
showBrandLogos: boolean;
|
|
query: string;
|
|
error: string | null;
|
|
requiresRestartPending: boolean;
|
|
onAction: ChannelFeatureAction;
|
|
onFeaturesUpdate: (payload: NanobotFeaturesPayload) => void;
|
|
onDismissStatus: () => void;
|
|
onConfigureMcp?: (name: string) => void;
|
|
onBeforeCloseChange?: (handler: (() => Promise<boolean>) | null) => void;
|
|
onRestart?: () => void;
|
|
isRestarting?: boolean;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const [selectedChannelName, setSelectedChannelName] = useState<string | null>(null);
|
|
const [filter, setFilter] = useState<ChannelFilter>("enabled");
|
|
const [connectRequestId, setConnectRequestId] = useState(0);
|
|
const filterInitializedRef = useRef(false);
|
|
const triggerRef = useRef<HTMLButtonElement | null>(null);
|
|
const dialogRef = useRef<HTMLDivElement | null>(null);
|
|
const beforeCloseRef = useRef<(() => Promise<boolean>) | null>(null);
|
|
const closePendingRef = useRef(false);
|
|
const channels = (nanobotFeatures?.features ?? [])
|
|
.filter((feature) => feature.type === "channel" && feature.settings_visible !== false)
|
|
.sort((left, right) => Number(!left.ready) - Number(!right.ready)
|
|
|| localizedChannelDisplayName(left, t).localeCompare(localizedChannelDisplayName(right, t)));
|
|
const hasEnabledConfigurableChannels = channels.some((feature) =>
|
|
feature.enabled && !feature.capabilities?.includes("always_enabled"));
|
|
const restartRequired = requiresRestartPending || Boolean(nanobotFeatures?.requires_restart);
|
|
const hasPendingChannelRestart = channels.some((feature) => feature.restart_pending);
|
|
useLayoutEffect(() => {
|
|
if (!nanobotFeatures || filterInitializedRef.current) return;
|
|
filterInitializedRef.current = true;
|
|
setFilter(hasEnabledConfigurableChannels ? "enabled" : "all");
|
|
}, [hasEnabledConfigurableChannels, nanobotFeatures]);
|
|
const visibleChannels = channels.filter((feature) =>
|
|
(filter === "all" || feature.enabled)
|
|
&& `${feature.name} ${localizedChannelDisplayName(feature, t)}`.toLocaleLowerCase()
|
|
.includes(query.trim().toLocaleLowerCase()));
|
|
const channelGroups = filter === "enabled"
|
|
? [{ key: "enabled", label: null, channels: visibleChannels }]
|
|
: [
|
|
{
|
|
key: "builtin",
|
|
label: t("settings.channels.noInstallNeeded"),
|
|
channels: visibleChannels.filter((feature) => !feature.requires_dependencies),
|
|
},
|
|
{
|
|
key: "dependencies",
|
|
label: t("settings.channels.installNeeded"),
|
|
channels: visibleChannels.filter((feature) => feature.requires_dependencies),
|
|
},
|
|
].filter((group) => group.channels.length);
|
|
const selectedChannel = channels.find((feature) => feature.name === selectedChannelName);
|
|
const requestChannelClose = useCallback(() => {
|
|
if (closePendingRef.current) return;
|
|
const beforeClose = beforeCloseRef.current;
|
|
if (!beforeClose) {
|
|
setSelectedChannelName(null);
|
|
return;
|
|
}
|
|
closePendingRef.current = true;
|
|
void beforeClose().then((canClose) => {
|
|
if (canClose) setSelectedChannelName(null);
|
|
}).finally(() => {
|
|
closePendingRef.current = false;
|
|
});
|
|
}, []);
|
|
const setBeforeChannelClose = useCallback(
|
|
(handler: (() => Promise<boolean>) | null) => {
|
|
beforeCloseRef.current = handler;
|
|
onBeforeCloseChange?.(handler);
|
|
},
|
|
[onBeforeCloseChange],
|
|
);
|
|
|
|
return (
|
|
<div className="catalog-section">
|
|
<CatalogToolbar>
|
|
<CatalogControls>
|
|
<CatalogToggle label={t("settings.channels.enabledOnly")} checked={filter === "enabled"}
|
|
onChange={(enabled) => {
|
|
filterInitializedRef.current = true;
|
|
setFilter(enabled ? "enabled" : "all");
|
|
}} />
|
|
</CatalogControls>
|
|
</CatalogToolbar>
|
|
{error && !selectedChannel ? <DismissibleStatusMessage message={error} isError onDismiss={onDismissStatus} /> : null}
|
|
{restartRequired && !hasPendingChannelRestart ? (
|
|
<RestartRequiredNotice message={t("settings.channels.restartRequired")}
|
|
onRestart={onRestart} isRestarting={isRestarting} />
|
|
) : null}
|
|
{loading && !nanobotFeatures ? (
|
|
<SettingsGroup>
|
|
<CatalogSkeleton label={t("settings.channels.loading")} layout="channels" />
|
|
</SettingsGroup>
|
|
) : visibleChannels.length ? (
|
|
channelGroups.map((group) => (
|
|
<section
|
|
key={group.key}
|
|
aria-labelledby={group.label ? `channel-group-${group.key}` : undefined}
|
|
aria-label={group.label ? undefined : t("settings.channels.filterEnabled")}
|
|
className={group.label ? "space-y-2" : undefined}
|
|
>
|
|
{group.label ? (
|
|
<h2 id={`channel-group-${group.key}`} className="px-4 text-[12px] font-medium text-muted-foreground">{group.label}</h2>
|
|
) : null}
|
|
<SettingsGroup>
|
|
<div className="grid grid-cols-1 gap-x-4 gap-y-1 min-[640px]:grid-cols-2">
|
|
{group.channels.map((feature) => (
|
|
<ChannelCatalogRow key={feature.name} feature={feature} showBrandLogos={showBrandLogos}
|
|
actionKey={actionKey} restartRequired={restartRequired} onAction={onAction}
|
|
onRestart={onRestart} isRestarting={isRestarting}
|
|
onSelect={(trigger, connect = false) => {
|
|
triggerRef.current = trigger;
|
|
beforeCloseRef.current = null;
|
|
setConnectRequestId(connect ? 1 : 0);
|
|
setSelectedChannelName(feature.name);
|
|
}} />
|
|
))}
|
|
</div>
|
|
</SettingsGroup>
|
|
</section>
|
|
))
|
|
) : !error ? (
|
|
<div className="px-3 py-12 text-center text-sm text-muted-foreground">
|
|
{t(channels.length ? "settings.channels.noResults" : "settings.channels.empty")}
|
|
</div>
|
|
) : null}
|
|
<Dialog open={Boolean(selectedChannel)} onOpenChange={(open) => { if (!open) requestChannelClose(); }}>
|
|
<DialogContent ref={dialogRef} showCloseButton={false} aria-describedby={undefined} className="max-h-[85dvh] w-[min(calc(100vw-2rem),40rem)] max-w-none overflow-hidden p-0 outline-none"
|
|
onOpenAutoFocus={(event) => {
|
|
event.preventDefault();
|
|
dialogRef.current?.focus({ preventScroll: true });
|
|
}}
|
|
onCloseAutoFocus={(event) => {
|
|
event.preventDefault();
|
|
triggerRef.current?.focus({ preventScroll: true });
|
|
}}>
|
|
<DialogTitle className="sr-only">{t("settings.nav.channels")}</DialogTitle>
|
|
<div className="max-h-[85dvh] min-h-0 overflow-y-auto overscroll-contain [scrollbar-width:none] [&::-webkit-scrollbar]:hidden">
|
|
<div className="relative">
|
|
<div className="absolute end-6 top-6 z-10 flex h-10 items-center gap-1">
|
|
{selectedChannel ? <ChannelHelpMenu feature={selectedChannel} chatAppsDocsUrl={chatAppsDocsUrl} /> : null}
|
|
<DialogClose className="grid h-8 w-8 place-items-center rounded-full text-muted-foreground outline-none transition-colors hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring">
|
|
<X className="h-4 w-4" aria-hidden />
|
|
<span className="sr-only">{t("common.close")}</span>
|
|
</DialogClose>
|
|
</div>
|
|
{error ? <DismissibleStatusMessage message={error} isError onDismiss={onDismissStatus} /> : null}
|
|
{selectedChannel ? <ChannelSetupPanel token={token} feature={selectedChannel} actionKey={actionKey}
|
|
showBrandLogos={showBrandLogos}
|
|
onAction={onAction} onFeaturesUpdate={onFeaturesUpdate} connectRequestId={connectRequestId}
|
|
onConfigureMcp={onConfigureMcp}
|
|
onBeforeCloseChange={setBeforeChannelClose} /> : null}
|
|
</div>
|
|
</div>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</div>
|
|
);
|
|
}
|