Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export type ChatLandingProjectSelection =
| { kind: 'none' }
| { kind: 'github'; repoFullName: string }
| { kind: 'local'; machineId: string; localProjectId: string };

export type ChatLandingProjectSelectionRequest = {
workspaceSlug: string;
selection: ChatLandingProjectSelection;
};

type ChatLandingProjectSelectionListener = (request: ChatLandingProjectSelectionRequest) => void;

const projectSelectionListeners = new Set<ChatLandingProjectSelectionListener>();

/**
* This request is deliberately transient. A mounted desktop landing consumes
* it; otherwise the accompanying route navigation applies the URL selection
* when the landing mounts. Do not persist it or mirror it into URL state.
*/
export function requestChatLandingProjectSelection(
request: ChatLandingProjectSelectionRequest
): void {
for (const listener of projectSelectionListeners) {
listener(request);
}
}

export function subscribeToChatLandingProjectSelection(
listener: ChatLandingProjectSelectionListener
): () => void {
projectSelectionListeners.add(listener);
return () => {
projectSelectionListeners.delete(listener);
};
}
62 changes: 39 additions & 23 deletions packages/components/src/components/chat/chat-landing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ import {
getChatLandingVisibleComposerStatus,
isChatLandingMachineReachable,
} from './chat-landing-derived';
import {
subscribeToChatLandingProjectSelection,
type ChatLandingProjectSelection,
} from './chat-landing-project-selection';

interface ChatLandingProps {
workspaceSlug: string;
Expand Down Expand Up @@ -1238,6 +1242,32 @@ function WorkspaceChatLanding({
},
[]
);
const applyProjectSelection = useCallback(
(selection: ChatLandingProjectSelection) => {
if (selection.kind === 'none') {
setContextType('chat');
return;
}
if (selection.kind === 'github') {
setSelectedRepo(selection.repoFullName);
setContextType('github');
return;
}
handleSelectedLocalProjectChange({
machineId: selection.machineId as MachineId,
localProjectId: selection.localProjectId as LocalProjectId,
});
setContextType('local');
},
[handleSelectedLocalProjectChange]
);
useLayoutEffect(() => {
if (isMobile) return;
return subscribeToChatLandingProjectSelection((request) => {
if (request.workspaceSlug !== workspaceSlug) return;
applyProjectSelection(request.selection);
});
}, [applyProjectSelection, isMobile, workspaceSlug]);
const getFirstVisibleLocalProjectForMachine = useCallback(
(machineId: MachineId): LocalProjectSelection | null => {
for (const entry of visibleLocalProjectMap.values()) {
Expand Down Expand Up @@ -1413,24 +1443,23 @@ function WorkspaceChatLanding({
preSelectionAppliedRef.current = preSelectionKey;

if (preSelectedContext === 'chat') {
setContextType('chat');
applyProjectSelection({ kind: 'none' });
} else if (preSelectedContext === 'local' && preSelectedMachine && preSelectedProject) {
setContextType('local');
handleSelectedLocalProjectChange({
machineId: preSelectedMachine as MachineId,
localProjectId: preSelectedProject as LocalProjectId,
applyProjectSelection({
kind: 'local',
machineId: preSelectedMachine,
localProjectId: preSelectedProject,
});
} else if (preSelectedRepo) {
setContextType('github');
setSelectedRepo(preSelectedRepo);
applyProjectSelection({ kind: 'github', repoFullName: preSelectedRepo });
}
}, [
applyProjectSelection,
preSelectionKey,
preSelectedContext,
preSelectedMachine,
preSelectedProject,
preSelectedRepo,
handleSelectedLocalProjectChange,
]);

// ── Machine-owner authorization check for local projects ──
Expand Down Expand Up @@ -3380,22 +3409,9 @@ function WorkspaceChatLanding({
}, [contextType, selectedLocalProject, selectedRepo]);
const handleDesktopProjectChange = useCallback(
(selection: UnifiedProjectSelection) => {
if (selection.kind === 'none') {
setContextType('chat');
return;
}
if (selection.kind === 'github') {
setSelectedRepo(selection.repoFullName);
setContextType('github');
return;
}
handleSelectedLocalProjectChange({
machineId: selection.machineId,
localProjectId: selection.localProjectId,
});
setContextType('local');
applyProjectSelection(selection);
},
[handleSelectedLocalProjectChange]
[applyProjectSelection]
);
const desktopAgentMachineIds = useMemo(
() => (scopedMachineId ? [scopedMachineId] : []),
Expand Down
17 changes: 15 additions & 2 deletions packages/components/src/components/loro-app-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import { lodyConnectionUiStateAtom } from '@/atoms/control-connection';
import { localMachineIdAtom } from '@/atoms/local-probe';
import { selectAndWriteLocalProject } from '@/lib/local-project-import';
import { importSidebarLocalProject } from '@/components/sidebar-local-project-import';
import { requestChatLandingProjectSelection } from '@/components/chat/chat-landing-project-selection';
import { lodyPresenceNowMsAtom, lodyPresenceStatesAtom } from '@/atoms/presence';
import {
chatScopeAtom,
Expand Down Expand Up @@ -1672,13 +1673,19 @@ export function LoroAppSidebar({ className }: LoroAppSidebarProps) {
(machineId: MachineId, localProjectId: string) => {
if (!workspaceSlug) return;
closeMobileDrawer();
if (!isMobile) {
requestChatLandingProjectSelection({
workspaceSlug,
selection: { kind: 'local', machineId, localProjectId },
});
}
void router.navigate({
to: '/$workspaceName/chat',
params: { workspaceName: workspaceSlug },
search: { context: 'local' as const, machine: machineId, project: localProjectId },
});
},
[closeMobileDrawer, router, workspaceSlug]
[closeMobileDrawer, isMobile, router, workspaceSlug]
);

const handleImportLocalProject = useCallback(async () => {
Expand Down Expand Up @@ -1826,6 +1833,12 @@ export function LoroAppSidebar({ className }: LoroAppSidebarProps) {
(repoFullName?: string) => {
if (!workspaceSlug) return;
closeMobileDrawer();
if (!isMobile) {
requestChatLandingProjectSelection({
workspaceSlug,
selection: repoFullName ? { kind: 'github', repoFullName } : { kind: 'none' },
});
}
void router.navigate({
to: '/$workspaceName/chat',
params: { workspaceName: workspaceSlug },
Expand All @@ -1834,7 +1847,7 @@ export function LoroAppSidebar({ className }: LoroAppSidebarProps) {
: { context: 'chat' as const },
});
},
[closeMobileDrawer, router, workspaceSlug]
[closeMobileDrawer, isMobile, router, workspaceSlug]
);

const handleRequestRemoval = useCallback(
Expand Down
32 changes: 32 additions & 0 deletions packages/components/tests/chat-landing-project-selection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it, vi } from 'vitest';

import {
requestChatLandingProjectSelection,
subscribeToChatLandingProjectSelection,
} from '../src/components/chat/chat-landing-project-selection';

describe('chat landing project selection requests', () => {
it('delivers repeated requests for the same project without URL state', () => {
const listener = vi.fn();
const unsubscribe = subscribeToChatLandingProjectSelection(listener);
const request = {
workspaceSlug: 'lody',
selection: {
kind: 'local' as const,
machineId: 'machine-a',
localProjectId: 'project-a',
},
};

requestChatLandingProjectSelection(request);
requestChatLandingProjectSelection(request);

expect(listener).toHaveBeenCalledTimes(2);
expect(listener).toHaveBeenNthCalledWith(1, request);
expect(listener).toHaveBeenNthCalledWith(2, request);

unsubscribe();
requestChatLandingProjectSelection(request);
expect(listener).toHaveBeenCalledTimes(2);
});
});
Loading