2026-09-10 11:10:51 +08:00
|
|
|
import { useRef, useState } from "react";
|
|
|
|
|
import { Loader2 } from "lucide-react";
|
2026-08-10 19:33:52 +08:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
|
2026-09-10 11:10:51 +08:00
|
|
|
import { localizedChannelDisplayName } from "@/components/settings/channels/ChannelIdentity";
|
2026-08-10 19:33:52 +08:00
|
|
|
import { ChannelCatalogRow, ChannelSetupPanel } from "@/components/settings/channels/ChannelSetupPanel";
|
2026-09-10 11:10:51 +08:00
|
|
|
import { DismissibleStatusMessage, RestartRequiredNotice, SettingsGroup } from "@/components/settings/shared/SettingsControls";
|
2026-08-10 19:33:52 +08:00
|
|
|
import type { NanobotFeaturesPayload } from "@/lib/types";
|
2026-09-10 11:10:51 +08:00
|
|
|
import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog";
|
2026-08-10 19:33:52 +08:00
|
|
|
|
|
|
|
|
export function ChannelsSettings({
|
2026-09-10 11:10:51 +08:00
|
|
|
token, nanobotFeatures, loading, actionKey, chatAppsDocsUrl, showBrandLogos,
|
|
|
|
|
error, requiresRestartPending, onAction, onFeaturesUpdate, onDismissStatus,
|
|
|
|
|
onRestart, isRestarting,
|
2026-08-10 19:33:52 +08:00
|
|
|
}: {
|
|
|
|
|
token: string;
|
|
|
|
|
nanobotFeatures: NanobotFeaturesPayload | null;
|
|
|
|
|
loading: boolean;
|
|
|
|
|
actionKey: string | null;
|
|
|
|
|
chatAppsDocsUrl?: string;
|
|
|
|
|
showBrandLogos: boolean;
|
|
|
|
|
error: string | null;
|
|
|
|
|
requiresRestartPending: boolean;
|
|
|
|
|
onAction: (action: "enable" | "disable", name: string) => void;
|
|
|
|
|
onFeaturesUpdate: (payload: NanobotFeaturesPayload) => void;
|
|
|
|
|
onDismissStatus: () => void;
|
|
|
|
|
onRestart?: () => void;
|
|
|
|
|
isRestarting?: boolean;
|
|
|
|
|
}) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const [selectedChannelName, setSelectedChannelName] = useState<string | null>(null);
|
2026-09-10 11:10:51 +08:00
|
|
|
const [connectRequestId, setConnectRequestId] = useState(0);
|
|
|
|
|
const triggerRef = useRef<HTMLElement | null>(null);
|
|
|
|
|
const dialogRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
|
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 selectedChannel = channels.find((feature) => feature.name === selectedChannelName);
|
2026-08-10 19:33:52 +08:00
|
|
|
|
|
|
|
|
return (
|
2026-09-10 11:10:51 +08:00
|
|
|
<div className="settings-stack">
|
|
|
|
|
{error ? <DismissibleStatusMessage message={error} isError onDismiss={onDismissStatus} /> : null}
|
2026-08-10 19:33:52 +08:00
|
|
|
{requiresRestartPending ? (
|
2026-09-10 11:10:51 +08:00
|
|
|
<RestartRequiredNotice message={t("settings.channels.restartRequired")}
|
|
|
|
|
onRestart={onRestart} isRestarting={isRestarting} />
|
2026-08-10 19:33:52 +08:00
|
|
|
) : null}
|
2026-09-10 11:10:51 +08:00
|
|
|
{loading && !nanobotFeatures ? (
|
|
|
|
|
<div className="flex h-36 items-center justify-center text-sm text-muted-foreground">
|
|
|
|
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" aria-hidden />
|
|
|
|
|
{t("settings.channels.loading")}
|
|
|
|
|
</div>
|
|
|
|
|
) : channels.length ? (
|
|
|
|
|
<SettingsGroup>
|
|
|
|
|
{channels.map((feature) => (
|
|
|
|
|
<ChannelCatalogRow key={feature.name} feature={feature} showBrandLogos={showBrandLogos}
|
|
|
|
|
actionKey={actionKey} onAction={onAction}
|
|
|
|
|
onSelect={(connect = false) => {
|
|
|
|
|
triggerRef.current = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
|
|
|
|
setConnectRequestId(connect ? 1 : 0);
|
|
|
|
|
setSelectedChannelName(feature.name);
|
|
|
|
|
}} />
|
|
|
|
|
))}
|
|
|
|
|
</SettingsGroup>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="px-3 py-12 text-center text-sm text-muted-foreground">
|
|
|
|
|
{t("settings.channels.empty")}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
<Dialog open={Boolean(selectedChannel)} onOpenChange={(open) => { if (!open) setSelectedChannelName(null); }}>
|
|
|
|
|
<DialogContent ref={dialogRef} aria-describedby={undefined} className="max-h-[85dvh] w-[min(calc(100vw-2rem),40rem)] max-w-none overflow-y-auto p-0 outline-none"
|
|
|
|
|
onOpenAutoFocus={(event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
dialogRef.current?.focus({ preventScroll: true });
|
|
|
|
|
}}
|
|
|
|
|
onCloseAutoFocus={(event) => { event.preventDefault(); triggerRef.current?.focus(); }}>
|
|
|
|
|
<DialogTitle className="sr-only">{t("settings.nav.channels")}</DialogTitle>
|
|
|
|
|
{selectedChannel ? <ChannelSetupPanel token={token} feature={selectedChannel} actionKey={actionKey}
|
|
|
|
|
chatAppsDocsUrl={chatAppsDocsUrl} showBrandLogos={showBrandLogos}
|
|
|
|
|
onAction={onAction} onFeaturesUpdate={onFeaturesUpdate} connectRequestId={connectRequestId} /> : null}
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
2026-08-10 19:33:52 +08:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|