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) | null) => void; onRestart?: () => void; isRestarting?: boolean; }) { const { t } = useTranslation(); const [selectedChannelName, setSelectedChannelName] = useState(null); const [filter, setFilter] = useState("enabled"); const [connectRequestId, setConnectRequestId] = useState(0); const filterInitializedRef = useRef(false); const triggerRef = useRef(null); const dialogRef = useRef(null); const beforeCloseRef = useRef<(() => Promise) | 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) | null) => { beforeCloseRef.current = handler; onBeforeCloseChange?.(handler); }, [onBeforeCloseChange], ); return (
{ filterInitializedRef.current = true; setFilter(enabled ? "enabled" : "all"); }} /> {error && !selectedChannel ? : null} {restartRequired && !hasPendingChannelRestart ? ( ) : null} {loading && !nanobotFeatures ? ( ) : visibleChannels.length ? ( channelGroups.map((group) => (
{group.label ? (

{group.label}

) : null}
{group.channels.map((feature) => ( { triggerRef.current = trigger; beforeCloseRef.current = null; setConnectRequestId(connect ? 1 : 0); setSelectedChannelName(feature.name); }} /> ))}
)) ) : !error ? (
{t(channels.length ? "settings.channels.noResults" : "settings.channels.empty")}
) : null} { if (!open) requestChannelClose(); }}> { event.preventDefault(); dialogRef.current?.focus({ preventScroll: true }); }} onCloseAutoFocus={(event) => { event.preventDefault(); triggerRef.current?.focus({ preventScroll: true }); }}> {t("settings.nav.channels")}
{selectedChannel ? : null} {t("common.close")}
{error ? : null} {selectedChannel ? : null}
); }