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.

✨ feat(canvas): make the share trigger a direct action or a menu (#21610)

Inneia61c1ae0eb02
diff --git a/src/features/Conversation/WorkingSidebar/Canvas/CanvasPane.tsx b/src/features/Conversation/WorkingSidebar/Canvas/CanvasPane.tsx
index 137146e2928..31e0d4b65c9 100644
--- a/src/features/Conversation/WorkingSidebar/Canvas/CanvasPane.tsx
+++ b/src/features/Conversation/WorkingSidebar/Canvas/CanvasPane.tsx
@@ -104,8 +104,12 @@ const CanvasPane = memo<CanvasPaneProps>(({ canvasId }) => {
<Text ellipsis fontSize={12} style={{ flex: 1, minWidth: 0 }} type={'secondary'}>
{data.title}
</Text>
- <CanvasShareButton data={data} onShared={() => mutate()} />
- <PortalMoreMenu config={moreMenu} outdent={'end'} />
+ {/* Two ghost icon buttons: their own padding is the separation, so the
+ flex gap only needs to keep the hover washes from touching. */}
+ <Flexbox horizontal align={'center'} flex={'none'} gap={2}>
+ <CanvasShareButton data={data} onShared={() => mutate()} />
+ <PortalMoreMenu config={moreMenu} outdent={'end'} />
+ </Flexbox>
</Flexbox>
{showPending && (
<Flexbox horizontal align={'center'} gap={8} {...stylex.props(styles.pill)}>
diff --git a/src/features/Conversation/WorkingSidebar/Canvas/CanvasShareButton.tsx b/src/features/Conversation/WorkingSidebar/Canvas/CanvasShareButton.tsx
index f1ce3f76418..633904a909c 100644
--- a/src/features/Conversation/WorkingSidebar/Canvas/CanvasShareButton.tsx
+++ b/src/features/Conversation/WorkingSidebar/Canvas/CanvasShareButton.tsx
@@ -1,34 +1,65 @@
'use client';
-import { Button, DropdownMenu } from '@lobehub/ui';
-import { Share2 } from 'lucide-react';
+import { Button, cssVar, DropdownMenu, Flexbox, Icon } from '@lobehub/ui';
+import { ChevronDownIcon, Share2 } from 'lucide-react';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';
import { type CanvasVersionDetail } from '@/services/canvas';
-import { useCanvasShareItems } from './useCanvasShareItems';
+import { useCanvasShareTrigger } from './useCanvasShareItems';
interface CanvasShareButtonProps {
data: CanvasVersionDetail | null | undefined;
onShared: () => unknown;
}
+const triggerStyle = { flex: 'none', paddingInline: 5 } as const;
+
/**
* The canvas toolbar's explicit share affordance. Publishing a canvas and
* opening its public link both live here, never in the `…` menu — sharing is
- * the primary thing this toolbar offers, so it is a visible button.
+ * the primary thing this toolbar offers, so it keeps a glyph of its own.
+ *
+ * The ghost glyph only becomes a menu when there is something to choose
+ * between: with a single action the click goes straight to it (a fresh canvas
+ * opens the publish confirm modal) and an arrow would promise a list holding
+ * one row. Once sharing and opening the public link both apply, the menu is
+ * real and says so with a chevron, the way the version switcher does.
*/
const CanvasShareButton = memo<CanvasShareButtonProps>(({ data, onShared }) => {
const { t } = useTranslation('portal');
- const items = useCanvasShareItems(data, onShared);
+ const trigger = useCanvasShareTrigger(data, onShared);
+
+ if (trigger.kind === 'none') return null;
+
+ if (trigger.kind === 'action') {
+ const { action } = trigger;
+
+ return (
+ <Button
+ aria-label={action.label}
+ disabled={action.disabled}
+ size={'small'}
+ style={triggerStyle}
+ title={action.label}
+ type={'text'}
+ onClick={action.onClick}
+ >
+ <Icon color={cssVar.colorIcon} icon={Share2} size={'small'} />
+ </Button>
+ );
+ }
- if (items.length === 0) return null;
+ const label = t('canvas.share.trigger');
return (
- <DropdownMenu items={items} placement={'bottomRight'}>
- <Button icon={Share2} size={'small'} style={{ flex: 'none' }}>
- {t('canvas.share.trigger')}
+ <DropdownMenu items={trigger.items} placement={'bottomRight'}>
+ <Button aria-label={label} size={'small'} style={triggerStyle} title={label} type={'text'}>
+ <Flexbox horizontal align={'center'} gap={2}>
+ <Icon color={cssVar.colorIcon} icon={Share2} size={'small'} />
+ <Icon color={cssVar.colorIcon} icon={ChevronDownIcon} size={12} />
+ </Flexbox>
</Button>
</DropdownMenu>
);
diff --git a/src/features/Conversation/WorkingSidebar/Canvas/useCanvasShareItems.test.ts b/src/features/Conversation/WorkingSidebar/Canvas/useCanvasShareItems.test.ts
index fcd81ab1586..a2c61ac3f70 100644
--- a/src/features/Conversation/WorkingSidebar/Canvas/useCanvasShareItems.test.ts
+++ b/src/features/Conversation/WorkingSidebar/Canvas/useCanvasShareItems.test.ts
@@ -3,7 +3,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { CanvasVersionDetail } from '@/services/canvas';
-import { useCanvasShareItems } from './useCanvasShareItems';
+import { useCanvasShareItems, useCanvasShareTrigger } from './useCanvasShareItems';
const mocks = vi.hoisted(() => ({
openPublishConfirm: vi.fn(),
@@ -99,3 +99,47 @@ describe('useCanvasShareItems', () => {
expect(result.current).toHaveLength(0);
});
});
+
+describe('useCanvasShareTrigger', () => {
+ it('runs a fresh canvas’s only action directly', () => {
+ const { result } = renderHook(() =>
+ useCanvasShareTrigger(detail({ latestVersion: 1 }), vi.fn()),
+ );
+
+ expect(result.current).toMatchObject({ action: { key: 'share' }, kind: 'action' });
+ });
+
+ it('runs the public link directly when sharing is already up to date', () => {
+ const { result } = renderHook(() =>
+ useCanvasShareTrigger(
+ detail({ publishedUrl: 'https://cost.lobehub.sites', publishedVersion: 2 }),
+ vi.fn(),
+ ),
+ );
+
+ expect(result.current).toMatchObject({ action: { key: 'open-public' }, kind: 'action' });
+ });
+
+ it('opens a menu once updating the share and opening the link both apply', () => {
+ const { result } = renderHook(() =>
+ useCanvasShareTrigger(
+ detail({ publishedUrl: 'https://cost.lobehub.sites', publishedVersion: 1 }),
+ vi.fn(),
+ ),
+ );
+
+ const trigger = result.current;
+ expect(trigger.kind).toBe('menu');
+ if (trigger.kind !== 'menu') throw new Error('the trigger should be a menu');
+
+ expect(trigger.items.map((item) => item.key)).toEqual(['share', 'open-public']);
+ });
+
+ it('renders nothing when there is no action to offer', () => {
+ const { result } = renderHook(() =>
+ useCanvasShareTrigger(detail({ canPublish: false }), vi.fn()),
+ );
+
+ expect(result.current.kind).toBe('none');
+ });
+});
diff --git a/src/features/Conversation/WorkingSidebar/Canvas/useCanvasShareItems.ts b/src/features/Conversation/WorkingSidebar/Canvas/useCanvasShareItems.ts
index 7e3d181245f..417747fe365 100644
--- a/src/features/Conversation/WorkingSidebar/Canvas/useCanvasShareItems.ts
+++ b/src/features/Conversation/WorkingSidebar/Canvas/useCanvasShareItems.ts
@@ -1,4 +1,4 @@
-import { type DropdownItem, toast } from '@lobehub/ui';
+import { toast } from '@lobehub/ui';
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
@@ -6,6 +6,19 @@ import { openPublishConfirm } from '@/components/PublishConfirmModal';
import { canvasService, type CanvasVersionDetail } from '@/services/canvas';
import { workService } from '@/services/work';
+/**
+ * One thing the toolbar's share affordance can do. Deliberately concrete, not
+ * `DropdownItem`: with a single action the trigger runs it directly instead of
+ * opening a menu, so the caller needs `label`/`onClick` without narrowing a
+ * menu-item union.
+ */
+export interface CanvasShareAction {
+ disabled?: boolean;
+ key: string;
+ label: string;
+ onClick: () => void;
+}
+
/**
* The canvas share actions. They live behind the toolbar's explicit `分享`
* button rather than the `…` menu: sharing a canvas is the one thing everyone
@@ -14,13 +27,13 @@ import { workService } from '@/services/work';
export const useCanvasShareItems = (
data: CanvasVersionDetail | null | undefined,
onShared: () => unknown,
-): DropdownItem[] => {
+): CanvasShareAction[] => {
const { t } = useTranslation('portal');
const [sharing, setSharing] = useState(false);
if (!data) return [];
- const items: DropdownItem[] = [];
+ const items: CanvasShareAction[] = [];
const publishedUrl = data.publishedUrl;
if (data.canPublish && (sharing || data.publishedVersion !== data.latestVersion)) {
@@ -68,3 +81,27 @@ export const useCanvasShareItems = (
return items;
};
+
+/**
+ * What the toolbar's share affordance should render. A single available
+ * action is a plain button that runs it — a one-row menu would make the user
+ * click twice to reach the only thing on offer (a fresh canvas: the publish
+ * confirm modal). Only a real choice turns the trigger into a menu.
+ */
+export type CanvasShareTrigger =
+ | { kind: 'none' }
+ | { action: CanvasShareAction; kind: 'action' }
+ | { items: CanvasShareAction[]; kind: 'menu' };
+
+export const useCanvasShareTrigger = (
+ data: CanvasVersionDetail | null | undefined,
+ onShared: () => unknown,
+): CanvasShareTrigger => {
+ const items = useCanvasShareItems(data, onShared);
+ const [only, ...rest] = items;
+
+ if (!only) return { kind: 'none' };
+ if (rest.length === 0) return { action: only, kind: 'action' };
+
+ return { items, kind: 'menu' };
+};