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(work): open the Working Sidebar PR tab from a matching PR card (#22297)

AmAzing-2bee93bb3819
diff --git a/src/features/Conversation/WorkingSidebar/PullRequest/SidebarTab.tsx b/src/features/Conversation/WorkingSidebar/PullRequest/SidebarTab.tsx
new file mode 100644
index 00000000000..fe85b0543a1
--- /dev/null
+++ b/src/features/Conversation/WorkingSidebar/PullRequest/SidebarTab.tsx
@@ -0,0 +1,51 @@
+import type { DeviceGitLinkedPullRequest } from '@lobechat/types';
+import { Flexbox } from '@lobehub/ui';
+import { Activity, memo } from 'react';
+
+import PullRequest from '.';
+import { usePublishPrUrl } from './usePublishPrUrl';
+
+interface PullRequestSidebarTabProps {
+ active: boolean;
+ /** The sidebar has room for this tab, so a Work card may open it. */
+ canShow: boolean;
+ className?: string;
+ confirmed: boolean;
+ confirmError?: unknown;
+ deviceId?: string;
+ onOpenTab: (tab: string) => void;
+ onRetryConfirm?: () => void;
+ pullRequest: DeviceGitLinkedPullRequest;
+ workingDirectory: string;
+}
+
+/**
+ * The Working Sidebar's PR tab. Mounted (hidden) whenever the working directory
+ * has a linked PR, so it also publishes that PR for Work cards while collapsed.
+ */
+const PullRequestSidebarTab = memo<PullRequestSidebarTabProps>(
+ ({ active, canShow, className, deviceId, pullRequest, workingDirectory, ...rest }) => {
+ usePublishPrUrl(canShow ? pullRequest.url : undefined);
+
+ return (
+ <Activity mode={active ? 'visible' : 'hidden'}>
+ <Flexbox className={className}>
+ <PullRequest
+ {...rest}
+ active={active}
+ deviceId={deviceId}
+ key={JSON.stringify([deviceId, workingDirectory, pullRequest.number])}
+ number={pullRequest.number}
+ summary={pullRequest}
+ url={pullRequest.url}
+ workingDirectory={workingDirectory}
+ />
+ </Flexbox>
+ </Activity>
+ );
+ },
+);
+
+PullRequestSidebarTab.displayName = 'PullRequestSidebarTab';
+
+export default PullRequestSidebarTab;
diff --git a/src/features/Conversation/WorkingSidebar/PullRequest/__tests__/usePublishPrUrl.test.ts b/src/features/Conversation/WorkingSidebar/PullRequest/__tests__/usePublishPrUrl.test.ts
new file mode 100644
index 00000000000..b49a1176990
--- /dev/null
+++ b/src/features/Conversation/WorkingSidebar/PullRequest/__tests__/usePublishPrUrl.test.ts
@@ -0,0 +1,28 @@
+import { renderHook } from '@testing-library/react';
+import { describe, expect, it, vi } from 'vitest';
+
+import { usePublishPrUrl } from '../usePublishPrUrl';
+
+const setPullRequestUrl = vi.hoisted(() => vi.fn());
+
+vi.mock('@/store/global', () => ({
+ useGlobalStore: (selector: (s: unknown) => unknown) =>
+ selector({ setWorkingSidebarPullRequestUrl: setPullRequestUrl }),
+}));
+
+describe('usePublishPrUrl', () => {
+ // Work cards open the PR tab only while it can show; otherwise they keep GitHub.
+ it('follows the URL and clears it on unmount', () => {
+ const { rerender, unmount } = renderHook((url?: string) => usePublishPrUrl(url), {
+ initialProps: 'https://github.com/o/r/pull/1',
+ });
+ expect(setPullRequestUrl).toHaveBeenLastCalledWith('https://github.com/o/r/pull/1');
+
+ rerender(undefined);
+ expect(setPullRequestUrl).toHaveBeenLastCalledWith(undefined);
+
+ rerender('https://github.com/o/r/pull/1');
+ unmount();
+ expect(setPullRequestUrl).toHaveBeenLastCalledWith(undefined);
+ });
+});
diff --git a/src/features/Conversation/WorkingSidebar/PullRequest/usePublishPrUrl.ts b/src/features/Conversation/WorkingSidebar/PullRequest/usePublishPrUrl.ts
new file mode 100644
index 00000000000..af5cb7d7129
--- /dev/null
+++ b/src/features/Conversation/WorkingSidebar/PullRequest/usePublishPrUrl.ts
@@ -0,0 +1,18 @@
+import { useEffect } from 'react';
+
+import { useGlobalStore } from '@/store/global';
+
+/**
+ * Tell Work cards which PR the sidebar's PR tab can show, so clicking the same
+ * PR opens the tab instead of GitHub. Pass `undefined` while the tab could not
+ * become visible (no room beside an open portal): the card then keeps its
+ * GitHub link. Cleared on unmount, when the tab disappears.
+ */
+export const usePublishPrUrl = (url: string | undefined) => {
+ const setPullRequestUrl = useGlobalStore((s) => s.setWorkingSidebarPullRequestUrl);
+
+ useEffect(() => {
+ setPullRequestUrl(url);
+ return () => setPullRequestUrl(undefined);
+ }, [url, setPullRequestUrl]);
+};
diff --git a/src/features/Conversation/WorkingSidebar/__tests__/harness.tsx b/src/features/Conversation/WorkingSidebar/__tests__/harness.tsx
index 08c5c4f4e13..6c9dfb29a25 100644
--- a/src/features/Conversation/WorkingSidebar/__tests__/harness.tsx
+++ b/src/features/Conversation/WorkingSidebar/__tests__/harness.tsx
@@ -144,6 +144,7 @@ const globalStore = vi.hoisted(() => ({
updateSystemStatus: vi.fn(),
toggleRightPanel: vi.fn(),
toggleTerminalPanel: vi.fn(),
+ setWorkingSidebarPullRequestUrl: vi.fn(),
setWorkingSidebarTab: vi.fn(),
status: {
portalWidth: 400 as number | undefined,
diff --git a/src/features/Conversation/WorkingSidebar/index.tsx b/src/features/Conversation/WorkingSidebar/index.tsx
index 39b66ed803d..cc48660ee33 100644
--- a/src/features/Conversation/WorkingSidebar/index.tsx
+++ b/src/features/Conversation/WorkingSidebar/index.tsx
@@ -93,7 +93,7 @@ import { CANVAS_MIN_WIDTH, MAX_PANEL_WIDTH, MIN_PANEL_WIDTH, OVERVIEW_PANEL_WIDT
import Overview from './Overview';
import { getStatusDeviceId } from './Overview/overviewData';
import OverviewSlot from './OverviewSlot';
-import PullRequest from './PullRequest';
+import PullRequestSidebarTab from './PullRequest/SidebarTab';
import { useLinkedPullRequest } from './PullRequest/useLinkedPullRequest';
import ResourcesSection from './ResourcesSection';
import Review from './Review';
@@ -1138,21 +1138,17 @@ const AgentWorkingSidebar = memo<AgentWorkingSidebarProps>(({ availableWidth })
</Activity>
)}
{pullRequest && workingDirectory && (
- <Activity mode={showRightPanel && activeTab === 'pr' ? 'visible' : 'hidden'}>
- <Flexbox className={styles.pane}>
- <PullRequest
- active={!!showRightPanel && activeTab === 'pr'}
- {...pullRequestLink}
- deviceId={remoteDeviceId}
- key={JSON.stringify([remoteDeviceId, workingDirectory, pullRequest.number])}
- number={pullRequest.number}
- summary={pullRequest}
- url={pullRequest.url}
- workingDirectory={workingDirectory}
- onOpenTab={openTab}
- />
- </Flexbox>
- </Activity>
+ // The PR tab renders at MIN_PANEL_WIDTH: it fits exactly when Overview does.
+ <PullRequestSidebarTab
+ {...pullRequestLink}
+ active={!!showRightPanel && activeTab === 'pr'}
+ canShow={overviewFits}
+ className={styles.pane}
+ deviceId={remoteDeviceId}
+ pullRequest={pullRequest}
+ workingDirectory={workingDirectory}
+ onOpenTab={openTab}
+ />
)}
<DynamicTabPanes
activeTab={activeTab}
diff --git a/src/features/Work/WorkSummaryCard.tsx b/src/features/Work/WorkSummaryCard.tsx
index f2130f3601c..5fc871f66f5 100644
--- a/src/features/Work/WorkSummaryCard.tsx
+++ b/src/features/Work/WorkSummaryCard.tsx
@@ -12,12 +12,8 @@ import { getWorkVersionTotalTokens } from '@/utils/workCumulativeUsage';
import { formatWorkVersionCost } from '@/utils/workVersionCost';
import CanvasWorkCard from './CanvasWorkCard';
-import {
- getWorkTypeDescriptor,
- isSafeExternalUrl,
- type WorkBranch,
- type WorkIcon,
-} from './descriptors';
+import { getWorkTypeDescriptor, type WorkBranch, type WorkIcon } from './descriptors';
+import { openExternalWork } from './openExternalWork';
import { useResourceDeletedPrompt } from './useResourceDeletedPrompt';
const styles = createStaticStyles(({ css, cssVar }) => ({
@@ -42,6 +38,7 @@ const styles = createStaticStyles(({ css, cssVar }) => ({
branch: css`
flex: 1 1 0;
min-width: 0;
+ font-size: 12px;
color: ${cssVar.colorTextTertiary};
`,
cost: css`
@@ -323,9 +320,7 @@ const WorkSummaryCard = memo<WorkSummaryCardProps>(
return;
}
case 'external': {
- // Defense in depth: only ever hand http(s) to shell.openExternal.
- if (isSafeExternalUrl(openTarget.url))
- window.open(openTarget.url, '_blank', 'noopener,noreferrer');
+ openExternalWork(openTarget.url);
return;
}
case 'filePreview': {
diff --git a/src/features/Work/openExternalWork.test.ts b/src/features/Work/openExternalWork.test.ts
new file mode 100644
index 00000000000..f209c8a9828
--- /dev/null
+++ b/src/features/Work/openExternalWork.test.ts
@@ -0,0 +1,79 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { isSamePullRequestUrl, openExternalWork } from './openExternalWork';
+
+const globalState = vi.hoisted(() => ({
+ openWorkingSidebar: vi.fn(),
+ workingSidebarPullRequestUrl: undefined as string | undefined,
+}));
+
+vi.mock('@/store/global', () => ({
+ useGlobalStore: { getState: () => globalState },
+}));
+
+const PR_URL = 'https://github.com/lobehub/lobehub/pull/22293';
+
+describe('isSamePullRequestUrl', () => {
+ it('matches the same PR across case, sub-pages, query and fragment', () => {
+ expect(isSamePullRequestUrl(PR_URL, PR_URL)).toBe(true);
+ expect(isSamePullRequestUrl(PR_URL, 'https://github.com/LobeHub/LobeHub/pull/22293')).toBe(
+ true,
+ );
+ expect(isSamePullRequestUrl(PR_URL, `${PR_URL}/files`)).toBe(true);
+ expect(isSamePullRequestUrl(PR_URL, `${PR_URL}?w=1#discussion`)).toBe(true);
+ });
+
+ it('rejects another PR, repo, issue, or a missing URL', () => {
+ expect(isSamePullRequestUrl(PR_URL, 'https://github.com/lobehub/lobehub/pull/2229')).toBe(
+ false,
+ );
+ expect(isSamePullRequestUrl(PR_URL, 'https://github.com/lobehub/lobe-ui/pull/22293')).toBe(
+ false,
+ );
+ expect(isSamePullRequestUrl(PR_URL, 'https://github.com/lobehub/lobehub/issues/22293')).toBe(
+ false,
+ );
+ expect(isSamePullRequestUrl(PR_URL, undefined)).toBe(false);
+ expect(isSamePullRequestUrl(undefined, undefined)).toBe(false);
+ });
+});
+
+describe('openExternalWork', () => {
+ const windowOpen = vi.fn();
+
+ beforeEach(() => {
+ vi.stubGlobal('open', windowOpen);
+ });
+
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.clearAllMocks();
+ globalState.workingSidebarPullRequestUrl = undefined;
+ });
+
+ it('opens the Working Sidebar PR tab when it shows the same PR', () => {
+ globalState.workingSidebarPullRequestUrl = PR_URL;
+
+ openExternalWork(PR_URL);
+
+ expect(globalState.openWorkingSidebar).toHaveBeenCalledWith('pr');
+ expect(windowOpen).not.toHaveBeenCalled();
+ });
+
+ it('falls back to the URL when the sidebar shows another PR or none', () => {
+ globalState.workingSidebarPullRequestUrl = 'https://github.com/lobehub/lobehub/pull/1';
+ openExternalWork(PR_URL);
+ globalState.workingSidebarPullRequestUrl = undefined;
+ openExternalWork(PR_URL);
+
+ expect(globalState.openWorkingSidebar).not.toHaveBeenCalled();
+ expect(windowOpen).toHaveBeenCalledTimes(2);
+ expect(windowOpen).toHaveBeenCalledWith(PR_URL, '_blank', 'noopener,noreferrer');
+ });
+
+ it('never opens a non-http(s) URL', () => {
+ openExternalWork('javascript:alert(1)');
+
+ expect(windowOpen).not.toHaveBeenCalled();
+ });
+});
diff --git a/src/features/Work/openExternalWork.ts b/src/features/Work/openExternalWork.ts
new file mode 100644
index 00000000000..8e78ae6c347
--- /dev/null
+++ b/src/features/Work/openExternalWork.ts
@@ -0,0 +1,33 @@
+import { useGlobalStore } from '@/store/global';
+
+import { isSafeExternalUrl } from './descriptors';
+
+const GITHUB_PULL_REQUEST_URL =
+ /^https?:\/\/github\.com\/([^/]+)\/([^/]+)\/pull\/(\d+)(?:[#/?]|$)/i;
+
+/** `owner/repo#n` of a GitHub PR URL (GitHub names are case-insensitive), or `null`. */
+const getPullRequestKey = (url?: string | null): string | null => {
+ const match = url?.match(GITHUB_PULL_REQUEST_URL);
+ return match ? `${match[1]}/${match[2]}#${match[3]}`.toLowerCase() : null;
+};
+
+/** Both URLs point at the same GitHub PR, ignoring sub-pages, query and fragment. */
+export const isSamePullRequestUrl = (a?: string | null, b?: string | null): boolean => {
+ const key = getPullRequestKey(a);
+ return !!key && key === getPullRequestKey(b);
+};
+
+/**
+ * Open an external Work. A PR the Working Sidebar already shows (live state,
+ * checks, merge) opens that tab rather than leaving the app; anything else goes
+ * to its URL.
+ */
+export const openExternalWork = (url: string): void => {
+ const global = useGlobalStore.getState();
+ if (isSamePullRequestUrl(url, global.workingSidebarPullRequestUrl)) {
+ global.openWorkingSidebar('pr');
+ return;
+ }
+ // Defense in depth: only ever hand http(s) to shell.openExternal.
+ if (isSafeExternalUrl(url)) window.open(url, '_blank', 'noopener,noreferrer');
+};
diff --git a/src/store/global/actions/workspacePane.ts b/src/store/global/actions/workspacePane.ts
index 9764180b991..7e9bb129ad3 100644
--- a/src/store/global/actions/workspacePane.ts
+++ b/src/store/global/actions/workspacePane.ts
@@ -19,11 +19,12 @@ export const globalWorkspaceSlice = (set: Setter, get: () => GlobalStore, _api?:
export class GlobalWorkspacePaneActionImpl {
readonly #get: () => GlobalStore;
+ readonly #set: Setter;
constructor(set: Setter, get: () => GlobalStore, _api?: unknown) {
void _api;
- void set;
this.#get = get;
+ this.#set = set;
}
switchBackToChat = (sessionId?: string): void => {
@@ -189,6 +190,12 @@ export class GlobalWorkspacePaneActionImpl {
);
};
+ /** Published by the Working Sidebar's PR tab; `undefined` when it shows no PR. */
+ setWorkingSidebarPullRequestUrl = (url?: string): void => {
+ if (this.#get().workingSidebarPullRequestUrl === url) return;
+ this.#set({ workingSidebarPullRequestUrl: url }, false, n('setWorkingSidebarPullRequestUrl'));
+ };
+
revealInFilesTab = (relativePath: string): void => {
this.#get().openWorkingSidebar('files');
this.#get().updateSystemStatus(
diff --git a/src/store/global/initialState.ts b/src/store/global/initialState.ts
index 576624c4ecd..7bb0f51de92 100644
--- a/src/store/global/initialState.ts
+++ b/src/store/global/initialState.ts
@@ -45,6 +45,7 @@ export type WorkingSidebarTab =
| 'files'
| 'overview'
| 'params'
+ | 'pr'
| 'review'
| 'search'
| 'skills'
@@ -596,6 +597,12 @@ export interface GlobalState {
sidebarKey: SidebarTabKey;
status: SystemStatus;
statusStorage: AsyncLocalStorage<SystemStatus>;
+ /**
+ * URL of the PR the mounted Working Sidebar's PR tab shows, so a Work card for
+ * the same PR can open that tab instead of leaving the app. Session state:
+ * deliberately outside the persisted `status`.
+ */
+ workingSidebarPullRequestUrl?: string;
}
export const INITIAL_STATUS = {