Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

💄 style(connector): move connection route and OAuth client into advanced settings (#23199)

Lobehub Coder31c0c8482ba5
diff --git a/src/features/PluginDevModal/MCPManifestForm/CollapsibleSection.test.tsx b/src/features/PluginDevModal/MCPManifestForm/CollapsibleSection.test.tsx
new file mode 100644
index 00000000000..bb4f3feb7e7
--- /dev/null
+++ b/src/features/PluginDevModal/MCPManifestForm/CollapsibleSection.test.tsx
@@ -0,0 +1,60 @@
+import { render, screen } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+import { describe, expect, it } from 'vitest';
+
+import CollapsibleSection from './CollapsibleSection';
+
+describe('CollapsibleSection', () => {
+ it('is reachable and toggled from the keyboard, announcing its state', async () => {
+ const user = userEvent.setup();
+ render(
+ <CollapsibleSection title="Advanced Settings">
+ <input aria-label="Client ID" />
+ </CollapsibleSection>,
+ );
+
+ const trigger = screen.getByRole('button', { name: 'Advanced Settings' });
+ expect(trigger).toHaveAttribute('aria-expanded', 'false');
+ expect(screen.queryByLabelText('Client ID')).toBeNull();
+
+ await user.tab();
+ expect(trigger).toHaveFocus();
+
+ await user.keyboard('{Enter}');
+ expect(trigger).toHaveAttribute('aria-expanded', 'true');
+ const panel = screen.getByRole('region', { name: 'Advanced Settings' });
+ expect(trigger).toHaveAttribute('aria-controls', panel.id);
+
+ await user.tab();
+ expect(screen.getByLabelText('Client ID')).toHaveFocus();
+
+ await user.tab({ shift: true });
+ expect(trigger).toHaveFocus();
+
+ await user.keyboard(' ');
+ expect(trigger).toHaveAttribute('aria-expanded', 'false');
+ expect(screen.queryByLabelText('Client ID')).toBeNull();
+ });
+
+ it('does not submit the surrounding form when toggled', async () => {
+ const user = userEvent.setup();
+ let submitted = false;
+ render(
+ <form
+ onSubmit={(event) => {
+ event.preventDefault();
+ submitted = true;
+ }}
+ >
+ <CollapsibleSection title="Advanced Settings">
+ <span>content</span>
+ </CollapsibleSection>
+ </form>,
+ );
+
+ await user.click(screen.getByRole('button', { name: 'Advanced Settings' }));
+
+ expect(screen.getByText('content')).toBeInTheDocument();
+ expect(submitted).toBe(false);
+ });
+});
diff --git a/src/features/PluginDevModal/MCPManifestForm/CollapsibleSection.tsx b/src/features/PluginDevModal/MCPManifestForm/CollapsibleSection.tsx
index 6ed796b6aa0..c4c2ca1a7f0 100644
--- a/src/features/PluginDevModal/MCPManifestForm/CollapsibleSection.tsx
+++ b/src/features/PluginDevModal/MCPManifestForm/CollapsibleSection.tsx
@@ -1,28 +1,38 @@
import { createStaticStyles, cx } from '@lobehub/ui';
import { ChevronDown, ChevronRight } from 'lucide-react';
import { type ReactNode } from 'react';
-import { useState } from 'react';
+import { useId, useState } from 'react';
const styles = createStaticStyles(({ css, cssVar }) => ({
- container: css`
- margin-block-end: ${cssVar.marginLG};
- `,
-
header: css`
cursor: pointer;
display: flex;
align-items: center;
+ width: 100%;
+ /* Same block padding as a Form.Field, so the row sits one field-gap from its neighbours */
+ padding-block: var(--lobe-form-item-padding-block, ${cssVar.padding});
+ padding-inline: 0;
+ border: none;
border-radius: ${cssVar.borderRadius};
+ font: inherit;
color: ${cssVar.colorTextTertiary};
+ text-align: start;
+
+ background: transparent;
transition: all ${cssVar.motionDurationMid} ease;
&:hover {
color: ${cssVar.colorText};
}
+
+ &:focus-visible {
+ outline: 2px solid ${cssVar.colorPrimaryBorder};
+ outline-offset: 1px;
+ }
`,
title: css`
@@ -37,6 +47,9 @@ interface CollapsibleSectionProps {
children: ReactNode;
/** Whether expanded by default */
defaultExpanded?: boolean;
+ /** Controlled expanded state; pair with `onExpandedChange` */
+ expanded?: boolean;
+ onExpandedChange?: (expanded: boolean) => void;
/** Title text */
title: string;
}
@@ -45,16 +58,42 @@ const CollapsibleSection = ({
title,
children,
defaultExpanded = false,
+ expanded,
+ onExpandedChange,
}: CollapsibleSectionProps) => {
- const [isExpanded, setIsExpanded] = useState(defaultExpanded);
+ const [innerExpanded, setInnerExpanded] = useState(defaultExpanded);
+ const isExpanded = expanded ?? innerExpanded;
+ const triggerId = useId();
+ const panelId = useId();
+
+ const toggle = () => {
+ setInnerExpanded(!isExpanded);
+ onExpandedChange?.(!isExpanded);
+ };
return (
- <div className={styles.container}>
- <div className={cx(styles.header)} onClick={() => setIsExpanded(!isExpanded)}>
- {isExpanded ? <ChevronDown size={16} /> : <ChevronRight size={16} />}
+ <div>
+ {/* A real button so keyboard users can reach the fields kept collapsed here */}
+ <button
+ aria-controls={isExpanded ? panelId : undefined}
+ aria-expanded={isExpanded}
+ className={cx(styles.header)}
+ id={triggerId}
+ type="button"
+ onClick={toggle}
+ >
+ {isExpanded ? (
+ <ChevronDown aria-hidden size={16} />
+ ) : (
+ <ChevronRight aria-hidden size={16} />
+ )}
<span className={styles.title}>{title}</span>
- </div>
- {isExpanded && <div>{children}</div>}
+ </button>
+ {isExpanded && (
+ <div aria-labelledby={triggerId} id={panelId} role="region">
+ {children}
+ </div>
+ )}
</div>
);
};
diff --git a/src/features/PluginDevModal/MCPManifestForm/index.tsx b/src/features/PluginDevModal/MCPManifestForm/index.tsx
index afcf4f75dec..c36258bf6b2 100644
--- a/src/features/PluginDevModal/MCPManifestForm/index.tsx
+++ b/src/features/PluginDevModal/MCPManifestForm/index.tsx
@@ -28,6 +28,7 @@ import BoundDeviceSelect from './BoundDeviceSelect';
import CollapsibleSection from './CollapsibleSection';
import MCPTypeSelect from './MCPTypeSelect';
import QuickImportSection from './QuickImportSection';
+import { hasAdvancedValue, useAdvancedSectionExpanded } from './useAdvancedSection';
interface MCPManifestFormProps {
/**
@@ -118,6 +119,13 @@ const MCPManifestForm = ({
// means running the authorize flow instead.
const isOAuth = enableOAuth && mcpType === 'http' && authType === 'oauth2';
+ const clientId = useWatch(form, AUTH_CLIENT_ID);
+ const clientSecret = useWatch(form, AUTH_CLIENT_SECRET);
+ const headers = useWatch(form, HEADERS);
+ const [advancedExpanded, setAdvancedExpanded] = useAdvancedSectionExpanded(
+ hasAdvancedValue({ clientId, clientSecret, connectVia, headers, isOAuth }),
+ );
+
// The redirect URI the server will use at authorize time (APP_URL-based), shown
// so the user registers a matching URI on their OAuth app. Fetched lazily once
// the OAuth auth type is in play.
@@ -274,22 +282,6 @@ const MCPManifestForm = ({
>
<Input placeholder="https://mcp.higress.ai/mcp-github/xxxxx" />
</Form.Field>
- <Form.Field
- label={t('dev.mcp.connectVia.label')}
- name={CONNECT_VIA}
- desc={
- connectVia === 'device'
- ? t('dev.mcp.connectVia.deviceDesc')
- : t('dev.mcp.connectVia.autoDesc')
- }
- >
- <ConnectViaRadio
- options={[
- { label: t('dev.mcp.connectVia.auto'), value: 'auto' },
- { label: t('dev.mcp.connectVia.device'), value: 'device' },
- ]}
- />
- </Form.Field>
<Form.Field
desc={t('dev.mcp.auth.desc')}
label={t('dev.mcp.auth.label')}
@@ -330,39 +322,59 @@ const MCPManifestForm = ({
/>
</Form.Field>
)}
- {enableOAuth && authType === 'oauth2' && (
- <>
- <Form.Field
- desc={t('dev.mcp.auth.oauth.clientId.desc')}
- label={t('dev.mcp.auth.oauth.clientId.label')}
- name={AUTH_CLIENT_ID}
- >
- <Input placeholder={t('dev.mcp.auth.oauth.clientId.placeholder')} />
- </Form.Field>
- <Form.Field
- desc={t('dev.mcp.auth.oauth.clientSecret.desc')}
- label={t('dev.mcp.auth.oauth.clientSecret.label')}
- name={AUTH_CLIENT_SECRET}
- >
- <InputPassword
- autoComplete="new-password"
- placeholder={t('dev.mcp.auth.oauth.clientSecret.placeholder')}
- />
- </Form.Field>
- <div
- style={{
- color: 'var(--lobe-colors-textDescription)',
- fontSize: 12,
- marginBottom: 8,
- }}
- >
- {t('dev.mcp.auth.oauth.redirectHint')}
- <br />
- <code style={{ wordBreak: 'break-all' }}>{redirectUri}</code>
- </div>
- </>
- )}
- <CollapsibleSection title={t('dev.mcp.advanced.title')}>
+ <CollapsibleSection
+ expanded={advancedExpanded}
+ title={t('dev.mcp.advanced.title')}
+ onExpandedChange={setAdvancedExpanded}
+ >
+ <Form.Field
+ label={t('dev.mcp.connectVia.label')}
+ name={CONNECT_VIA}
+ desc={
+ connectVia === 'device'
+ ? t('dev.mcp.connectVia.deviceDesc')
+ : t('dev.mcp.connectVia.autoDesc')
+ }
+ >
+ <ConnectViaRadio
+ options={[
+ { label: t('dev.mcp.connectVia.auto'), value: 'auto' },
+ { label: t('dev.mcp.connectVia.device'), value: 'device' },
+ ]}
+ />
+ </Form.Field>
+ {enableOAuth && authType === 'oauth2' && (
+ <>
+ <Form.Field
+ desc={t('dev.mcp.auth.oauth.clientId.desc')}
+ label={t('dev.mcp.auth.oauth.clientId.label')}
+ name={AUTH_CLIENT_ID}
+ >
+ <Input placeholder={t('dev.mcp.auth.oauth.clientId.placeholder')} />
+ </Form.Field>
+ <Form.Field
+ desc={t('dev.mcp.auth.oauth.clientSecret.desc')}
+ label={t('dev.mcp.auth.oauth.clientSecret.label')}
+ name={AUTH_CLIENT_SECRET}
+ >
+ <InputPassword
+ autoComplete="new-password"
+ placeholder={t('dev.mcp.auth.oauth.clientSecret.placeholder')}
+ />
+ </Form.Field>
+ <div
+ style={{
+ color: 'var(--lobe-colors-textDescription)',
+ fontSize: 12,
+ marginBottom: 8,
+ }}
+ >
+ {t('dev.mcp.auth.oauth.redirectHint')}
+ <br />
+ <code style={{ wordBreak: 'break-all' }}>{redirectUri}</code>
+ </div>
+ </>
+ )}
<Form.Field
desc={t('dev.mcp.headers.desc')}
label={t('dev.mcp.headers.label')}
diff --git a/src/features/PluginDevModal/MCPManifestForm/useAdvancedSection.test.ts b/src/features/PluginDevModal/MCPManifestForm/useAdvancedSection.test.ts
new file mode 100644
index 00000000000..31cb3272a43
--- /dev/null
+++ b/src/features/PluginDevModal/MCPManifestForm/useAdvancedSection.test.ts
@@ -0,0 +1,73 @@
+import { act, renderHook } from '@testing-library/react';
+import { describe, expect, it } from 'vitest';
+
+import { hasAdvancedValue, useAdvancedSectionExpanded } from './useAdvancedSection';
+
+describe('hasAdvancedValue', () => {
+ it('is false for the defaults, including OAuth with an empty client (auto-detected)', () => {
+ expect(hasAdvancedValue({})).toBe(false);
+ expect(hasAdvancedValue({ connectVia: 'auto', headers: {} })).toBe(false);
+ expect(hasAdvancedValue({ clientId: '', clientSecret: '', isOAuth: true })).toBe(false);
+ });
+
+ it('is true for a device route, an OAuth client or headers', () => {
+ expect(hasAdvancedValue({ connectVia: 'device' })).toBe(true);
+ expect(hasAdvancedValue({ clientId: 'client-1', isOAuth: true })).toBe(true);
+ expect(hasAdvancedValue({ clientSecret: 'secret', isOAuth: true })).toBe(true);
+ expect(hasAdvancedValue({ headers: { 'X-Team': 'a' } })).toBe(true);
+ });
+
+ it('ignores an OAuth client left over from another auth type, which saving drops', () => {
+ expect(hasAdvancedValue({ clientId: 'client-1', isOAuth: false })).toBe(false);
+ });
+});
+
+describe('useAdvancedSectionExpanded', () => {
+ it('starts collapsed without a saved value', () => {
+ const { result } = renderHook(() => useAdvancedSectionExpanded(false));
+
+ expect(result.current[0]).toBe(false);
+ });
+
+ it('starts expanded with a saved value', () => {
+ const { result } = renderHook(() => useAdvancedSectionExpanded(true));
+
+ expect(result.current[0]).toBe(true);
+ });
+
+ it('opens when a value arrives after mount, e.g. edit-mode seeding', () => {
+ const { result, rerender } = renderHook(({ holds }) => useAdvancedSectionExpanded(holds), {
+ initialProps: { holds: false },
+ });
+
+ rerender({ holds: true });
+
+ expect(result.current[0]).toBe(true);
+ });
+
+ it('lets the user collapse it and does not reopen until a value appears again', () => {
+ const { result, rerender } = renderHook(({ holds }) => useAdvancedSectionExpanded(holds), {
+ initialProps: { holds: true },
+ });
+
+ act(() => result.current[1](false));
+ rerender({ holds: true });
+ expect(result.current[0]).toBe(false);
+
+ rerender({ holds: false });
+ expect(result.current[0]).toBe(false);
+
+ rerender({ holds: true });
+ expect(result.current[0]).toBe(true);
+ });
+
+ it('does not collapse when the value is cleared while open', () => {
+ const { result, rerender } = renderHook(({ holds }) => useAdvancedSectionExpanded(holds), {
+ initialProps: { holds: true },
+ });
+
+ rerender({ holds: false });
+
+ expect(result.current[0]).toBe(true);
+ });
+});
diff --git a/src/features/PluginDevModal/MCPManifestForm/useAdvancedSection.ts b/src/features/PluginDevModal/MCPManifestForm/useAdvancedSection.ts
new file mode 100644
index 00000000000..5b29291c7e6
--- /dev/null
+++ b/src/features/PluginDevModal/MCPManifestForm/useAdvancedSection.ts
@@ -0,0 +1,44 @@
+import { type McpConnectVia } from '@lobechat/types';
+import { useState } from 'react';
+
+export interface AdvancedSectionFields {
+ clientId?: string;
+ clientSecret?: string;
+ connectVia?: McpConnectVia;
+ headers?: Record<string, string>;
+ /** OAuth is the chosen auth type (and offered by this form). */
+ isOAuth?: boolean;
+}
+
+/**
+ * Whether 高级设置 holds a value that saving keeps. The OAuth client counts only
+ * while OAuth is chosen: saving drops it for any other auth type.
+ */
+export const hasAdvancedValue = ({
+ clientId,
+ clientSecret,
+ connectVia,
+ headers,
+ isOAuth,
+}: AdvancedSectionFields): boolean =>
+ connectVia === 'device' ||
+ (Boolean(isOAuth) && Boolean(clientId || clientSecret)) ||
+ Object.keys(headers ?? {}).length > 0;
+
+/**
+ * Expanded state of 高级设置. It starts collapsed but opens whenever the section
+ * comes to hold a saved value (an edited connector, a JSON import), so a device
+ * route, an OAuth client or headers never take effect unseen. The user can still
+ * collapse it afterwards.
+ */
+export const useAdvancedSectionExpanded = (holdsValue: boolean) => {
+ const [expanded, setExpanded] = useState(holdsValue);
+ const [seenValue, setSeenValue] = useState(holdsValue);
+
+ if (holdsValue !== seenValue) {
+ setSeenValue(holdsValue);
+ if (holdsValue) setExpanded(true);
+ }
+
+ return [expanded, setExpanded] as const;
+};