feat(webui): improve setup flows across chat channels (NAN-112) (#5356)
* feat(webui): improve channel setup flows * fix(webui): refine channel setup interactions (NAN-112) * fix(webui): keep email setup sections channel-owned * fix(webui): remove concrete channel details from shared components * refactor(webui): remove unused channel setup resources * fix(webui): finish channel-owned setup boundaries * fix(webui): update channel switches optimistically * test(channels): remove obsolete absence assertions * fix(webui): flatten channel instance settings disclosure * fix(webui): complete channel setup and connection flows * fix(whatsapp): wait for session database release * fix(webui): preserve scrolling channel controls * fix(webui): stage channel connection checks * fix(channels): align verification actions * fix(email): expose connection checks * fix(webui): align channel field actions * fix(email): verify account credentials * fix(wecom): bypass system proxy for websocket * fix(channels): stabilize async connection cleanup * fix(webui): autosave channel credentials * fix(email): make provider presets non-destructive * test(weixin): isolate persisted QR credentials
This commit is contained in:
1 parent
3cc3012b3a
commit
e8a29a0087
291 files changed
+12194
-3979
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import type { FormEvent, ReactNode } from "react";
|
||||
import type { TFunction } from "i18next";
|
||||
import {
|
||||
ArrowUpDown,
|
||||
Check,
|
||||
@@ -17,7 +18,8 @@ import {
|
||||
} from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { channelUiPresentation } from "@/channel-plugins/registry";
|
||||
import { channelTranslator } from "@/channel-plugins/i18n";
|
||||
import { channelUiOwner, channelUiPresentation } from "@/channel-plugins/registry";
|
||||
import { SETTINGS_SEARCH_INPUT_CLASS } from "@/components/settings/shared/SettingsControls";
|
||||
import { AppsActionButton } from "@/components/settings/system/AppsSettings";
|
||||
import { Button } from "@/components/ui/button";
|
||||
@@ -302,7 +304,7 @@ function AutomationListItem({
|
||||
const tx = (key: string, fallback: string, values?: Record<string, unknown>) =>
|
||||
t(key, { defaultValue: fallback, ...(values ?? {}) });
|
||||
const status = automationStatus(job, tx);
|
||||
const origin = automationOriginLabel(job, tx);
|
||||
const origin = automationOriginLabel(job, t);
|
||||
const nextRun = formatAutomationNext(job, tx);
|
||||
const summary = automationSummary(job, tx);
|
||||
|
||||
@@ -381,7 +383,7 @@ function AutomationDetailPanel({
|
||||
const tx = (key: string, fallback: string, values?: Record<string, unknown>) =>
|
||||
t(key, { defaultValue: fallback, ...(values ?? {}) });
|
||||
const status = automationStatus(job, tx);
|
||||
const origin = automationOriginLabel(job, tx);
|
||||
const origin = automationOriginLabel(job, t);
|
||||
const originHref = job.origin?.channel === "websocket" && job.origin.session_key
|
||||
? `#/chat/${encodeURIComponent(job.origin.session_key)}`
|
||||
: null;
|
||||
@@ -1358,23 +1360,27 @@ function automationStatus(
|
||||
|
||||
function automationOriginLabel(
|
||||
job: SessionAutomationJob,
|
||||
tx: (key: string, fallback: string, values?: Record<string, unknown>) => string,
|
||||
t: TFunction,
|
||||
): string {
|
||||
if (job.protected) return tx("settings.automations.origin.system", "System");
|
||||
if (job.protected) return t("settings.automations.origin.system", { defaultValue: "System" });
|
||||
const origin = job.origin;
|
||||
if (!origin) return tx("settings.automations.origin.unknown", "No linked chat");
|
||||
if (origin.channel !== "websocket") return automationChannelLabel(origin.channel, tx);
|
||||
return origin.title || origin.preview || origin.session_key || automationChannelLabel(origin.channel, tx);
|
||||
if (!origin) return t("settings.automations.origin.unknown", { defaultValue: "No linked chat" });
|
||||
if (origin.channel !== "websocket") return automationChannelLabel(origin.channel, t);
|
||||
return origin.title || origin.preview || origin.session_key || automationChannelLabel(origin.channel, t);
|
||||
}
|
||||
|
||||
function automationChannelLabel(
|
||||
channel: string,
|
||||
tx: (key: string, fallback: string, values?: Record<string, unknown>) => string,
|
||||
t: TFunction,
|
||||
): string {
|
||||
const key = channel.trim().toLowerCase();
|
||||
const displayName = automationChannelDisplayName(key);
|
||||
const presentation = channelUiPresentation(key);
|
||||
if (presentation) {
|
||||
return channelTranslator(t, channelUiOwner(key))("displayName", presentation.displayName);
|
||||
}
|
||||
const displayName = HOST_AUTOMATION_CHANNEL_LABELS[key];
|
||||
return displayName
|
||||
? tx(`settings.automations.channels.${key}`, displayName)
|
||||
? t(`settings.automations.channels.${key}`, { defaultValue: displayName })
|
||||
: channel;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
import { useRef, useState } from "react";
|
||||
import { Loader2 } from "lucide-react";
|
||||
import { useCallback, useLayoutEffect, useRef, useState } from "react";
|
||||
import { Loader2, Search, X } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { SegmentedControl } from "@/components/ui/segmented-control";
|
||||
import { SETTINGS_SEARCH_INPUT_CLASS } from "@/components/settings/shared/SettingsControls";
|
||||
import type { ChannelFeatureAction } from "@/channel-plugins/types";
|
||||
import { localizedChannelDisplayName } from "@/components/settings/channels/ChannelIdentity";
|
||||
import { ChannelHelpMenu } from "@/components/settings/channels/ChannelHelpMenu";
|
||||
import { ChannelCatalogRow, ChannelSetupPanel } from "@/components/settings/channels/ChannelSetupPanel";
|
||||
import { DismissibleStatusMessage, RestartRequiredNotice, SettingsGroup } from "@/components/settings/shared/SettingsControls";
|
||||
import type { NanobotFeaturesPayload } from "@/lib/types";
|
||||
import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog";
|
||||
import { Dialog, DialogClose, DialogContent, DialogTitle } from "@/components/ui/dialog";
|
||||
|
||||
type ChannelFilter = "all" | "enabled";
|
||||
|
||||
export function ChannelsSettings({
|
||||
token, nanobotFeatures, loading, actionKey, chatAppsDocsUrl, showBrandLogos,
|
||||
@@ -21,7 +28,7 @@ export function ChannelsSettings({
|
||||
showBrandLogos: boolean;
|
||||
error: string | null;
|
||||
requiresRestartPending: boolean;
|
||||
onAction: (action: "enable" | "disable", name: string) => void;
|
||||
onAction: ChannelFeatureAction;
|
||||
onFeaturesUpdate: (payload: NanobotFeaturesPayload) => void;
|
||||
onDismissStatus: () => void;
|
||||
onRestart?: () => void;
|
||||
@@ -29,19 +36,83 @@ export function ChannelsSettings({
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const [selectedChannelName, setSelectedChannelName] = useState<string | null>(null);
|
||||
const [query, setQuery] = useState("");
|
||||
const [filter, setFilter] = useState<ChannelFilter>("enabled");
|
||||
const [connectRequestId, setConnectRequestId] = useState(0);
|
||||
const filterInitializedRef = useRef(false);
|
||||
const triggerRef = useRef<HTMLElement | 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 hasEnabledChannels = channels.some((feature) => feature.enabled);
|
||||
const restartRequired = requiresRestartPending || Boolean(nanobotFeatures?.requires_restart);
|
||||
useLayoutEffect(() => {
|
||||
if (!nanobotFeatures || filterInitializedRef.current) return;
|
||||
filterInitializedRef.current = true;
|
||||
setFilter(hasEnabledChannels ? "enabled" : "all");
|
||||
}, [hasEnabledChannels, 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; },
|
||||
[],
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="settings-stack">
|
||||
{error ? <DismissibleStatusMessage message={error} isError onDismiss={onDismissStatus} /> : null}
|
||||
{requiresRestartPending ? (
|
||||
<div className="flex flex-col gap-3 sm:flex-row sm:items-center">
|
||||
<div className="relative min-w-0 flex-1">
|
||||
<Search className="pointer-events-none absolute start-4 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" aria-hidden />
|
||||
<Input type="search" value={query} onChange={(event) => setQuery(event.target.value)}
|
||||
aria-label={t("settings.channels.search")}
|
||||
placeholder={t("settings.channels.search")}
|
||||
className={`h-12 ps-11 text-[15px] ${SETTINGS_SEARCH_INPUT_CLASS}`} />
|
||||
</div>
|
||||
<SegmentedControl value={filter} onChange={(value) => {
|
||||
filterInitializedRef.current = true;
|
||||
setFilter(value);
|
||||
}} options={[
|
||||
{ value: "all", label: t("settings.channels.filterAll") },
|
||||
{ value: "enabled", label: t("settings.channels.filterEnabled") },
|
||||
]} />
|
||||
</div>
|
||||
{error && !selectedChannel ? <DismissibleStatusMessage message={error} isError onDismiss={onDismissStatus} /> : null}
|
||||
{restartRequired ? (
|
||||
<RestartRequiredNotice message={t("settings.channels.restartRequired")}
|
||||
onRestart={onRestart} isRestarting={isRestarting} />
|
||||
) : null}
|
||||
@@ -50,34 +121,62 @@ export function ChannelsSettings({
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" aria-hidden />
|
||||
{t("settings.channels.loading")}
|
||||
</div>
|
||||
) : channels.length ? (
|
||||
<SettingsGroup>
|
||||
{channels.map((feature) => (
|
||||
) : 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} onAction={onAction}
|
||||
actionKey={actionKey} actionsDisabled={restartRequired || Boolean(isRestarting)} onAction={onAction}
|
||||
onSelect={(connect = false) => {
|
||||
triggerRef.current = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
||||
beforeCloseRef.current = null;
|
||||
setConnectRequestId(connect ? 1 : 0);
|
||||
setSelectedChannelName(feature.name);
|
||||
}} />
|
||||
))}
|
||||
</SettingsGroup>
|
||||
</div>
|
||||
</SettingsGroup>
|
||||
</section>
|
||||
))
|
||||
) : (
|
||||
<div className="px-3 py-12 text-center text-sm text-muted-foreground">
|
||||
{t("settings.channels.empty")}
|
||||
{t(channels.length ? "settings.channels.noResults" : "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"
|
||||
<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(); }}>
|
||||
<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}
|
||||
<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:bg-muted 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}
|
||||
onBeforeCloseChange={setBeforeChannelClose} /> : null}
|
||||
</div>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
|
||||
@@ -98,6 +98,7 @@ export function createSystemSettingsActions({
|
||||
mcpOAuthFlowRef,
|
||||
mcpOAuthNavigatedUrlRef,
|
||||
mcpOAuthPopupRef,
|
||||
nanobotFeatureActionRef,
|
||||
nanobotFeatures,
|
||||
setApiService,
|
||||
setApiServiceAction,
|
||||
@@ -130,12 +131,26 @@ export function createSystemSettingsActions({
|
||||
setNanobotFeaturesError,
|
||||
} = state;
|
||||
|
||||
const beginNanobotFeatureAction = (key: string) => {
|
||||
if (nanobotFeatureActionRef.current) return false;
|
||||
nanobotFeatureActionRef.current = key;
|
||||
setNanobotFeatureAction(key);
|
||||
return true;
|
||||
};
|
||||
|
||||
const endNanobotFeatureAction = (key: string) => {
|
||||
if (nanobotFeatureActionRef.current !== key) return;
|
||||
nanobotFeatureActionRef.current = null;
|
||||
setNanobotFeatureAction(null);
|
||||
};
|
||||
|
||||
const installCapabilities = async (names: string[]): Promise<boolean> => {
|
||||
const missing = names.filter(
|
||||
(name) => !featureCatalog.find((feature) => feature.name === name)?.installed,
|
||||
);
|
||||
if (!missing.length) return true;
|
||||
setNanobotFeatureAction(`enable:${names.join("+")}`);
|
||||
const key = `enable:${names.join("+")}`;
|
||||
if (!beginNanobotFeatureAction(key)) return false;
|
||||
setNanobotFeaturesError(null);
|
||||
try {
|
||||
let latest = nanobotFeatures;
|
||||
@@ -151,7 +166,7 @@ export function createSystemSettingsActions({
|
||||
setNanobotFeaturesError((err as Error).message);
|
||||
return false;
|
||||
} finally {
|
||||
setNanobotFeatureAction(null);
|
||||
endNanobotFeatureAction(key);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -204,30 +219,42 @@ export function createSystemSettingsActions({
|
||||
const handleNanobotFeatureAction = async (
|
||||
action: "enable" | "disable",
|
||||
name: string,
|
||||
confirmed = false,
|
||||
options: { confirmed?: boolean; installOnly?: boolean } = {},
|
||||
) => {
|
||||
const feature = featureCatalog.find((item) => item.name === name);
|
||||
if (action === "enable" && !confirmed && feature && !feature.installed && feature.install_supported) {
|
||||
if (
|
||||
action === "enable"
|
||||
&& !options.confirmed
|
||||
&& feature
|
||||
&& !feature.installed
|
||||
&& feature.install_supported
|
||||
) {
|
||||
setNanobotFeaturesError(null);
|
||||
setNanobotFeatureConfirm(feature);
|
||||
setNanobotFeatureConfirm({ feature, installOnly: Boolean(options.installOnly) });
|
||||
return;
|
||||
}
|
||||
const key = `${action}:${name}`;
|
||||
setNanobotFeatureAction(key);
|
||||
const key = `${action === "enable" && options.installOnly ? "install" : action}:${name}`;
|
||||
if (!beginNanobotFeatureAction(key)) return;
|
||||
setNanobotFeatureConfirm(null);
|
||||
setNanobotFeaturesError(null);
|
||||
try {
|
||||
const payload = action === "enable"
|
||||
? await enableNanobotFeature(client, name)
|
||||
? await enableNanobotFeature(client, name, { installOnly: options.installOnly })
|
||||
: await disableNanobotFeature(client, name);
|
||||
setNanobotFeatures(payload);
|
||||
if (payload.requires_restart) {
|
||||
setPendingRestartSections((prev) => ({ ...prev, runtime: true }));
|
||||
}
|
||||
} catch (err) {
|
||||
setNanobotFeaturesError((err as Error).message);
|
||||
console.error("nanobot feature action failed", {
|
||||
action,
|
||||
name,
|
||||
installOnly: Boolean(options.installOnly),
|
||||
error: err,
|
||||
});
|
||||
setNanobotFeaturesError(err instanceof Error ? err.message : String(err));
|
||||
} finally {
|
||||
setNanobotFeatureAction(null);
|
||||
endNanobotFeatureAction(key);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -20,6 +20,11 @@ import type {
|
||||
SessionAutomationJob,
|
||||
} from "@/lib/types";
|
||||
|
||||
export type NanobotFeatureInstallRequest = {
|
||||
feature: NanobotFeatureInfo;
|
||||
installOnly: boolean;
|
||||
};
|
||||
|
||||
export function useSystemSettingsState() {
|
||||
const [cliApps, setCliApps] = useState<CliAppsPayload | null>(null);
|
||||
const [nanobotFeatures, setNanobotFeatures] = useState<NanobotFeaturesPayload | null>(null);
|
||||
@@ -31,7 +36,9 @@ export function useSystemSettingsState() {
|
||||
const [automationsLoading, setAutomationsLoading] = useState(false);
|
||||
const [cliAppsAction, setCliAppsAction] = useState<string | null>(null);
|
||||
const [nanobotFeatureAction, setNanobotFeatureAction] = useState<string | null>(null);
|
||||
const [nanobotFeatureConfirm, setNanobotFeatureConfirm] = useState<NanobotFeatureInfo | null>(null);
|
||||
const nanobotFeatureActionRef = useRef<string | null>(null);
|
||||
const [nanobotFeatureConfirm, setNanobotFeatureConfirm] =
|
||||
useState<NanobotFeatureInstallRequest | null>(null);
|
||||
const [mcpPresetAction, setMcpPresetAction] = useState<string | null>(null);
|
||||
const [mcpOAuthFlow, setMcpOAuthFlow] = useState<McpOAuthFlowPayload | null>(null);
|
||||
const mcpOAuthFlowRef = useRef<McpOAuthFlowPayload | null>(null);
|
||||
@@ -105,6 +112,7 @@ export function useSystemSettingsState() {
|
||||
mcpPresets,
|
||||
mcpPresetsLoading,
|
||||
nanobotFeatureAction,
|
||||
nanobotFeatureActionRef,
|
||||
nanobotFeatureConfirm,
|
||||
nanobotFeatures,
|
||||
nanobotFeaturesError,
|
||||
|
||||
Reference in new issue
Block a user