Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,7 @@ function MainApp() {
useWorkspaceRestore({
workspaces,
hasLoaded,
enabled: !appSettingsLoading,
connectWorkspace,
listThreadsForWorkspace
});
Expand Down
40 changes: 40 additions & 0 deletions src/features/workspaces/hooks/useWorkspaceRestore.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,46 @@ describe("useWorkspaceRestore", () => {
expect(listThreadsForWorkspace).toHaveBeenCalledTimes(1);
expect(listThreadsForWorkspace).toHaveBeenCalledWith(workspace);
});

it("waits until restore is enabled before reconnecting workspaces", async () => {
const workspace = createWorkspace();
const connectWorkspace = vi
.fn<WorkspaceRestoreOptions["connectWorkspace"]>()
.mockResolvedValue(undefined);
const listThreadsForWorkspace = vi
.fn<WorkspaceRestoreOptions["listThreadsForWorkspace"]>()
.mockResolvedValue(undefined);

const { rerender } = renderHook(
({ enabled }) =>
useWorkspaceRestore({
workspaces: [workspace],
hasLoaded: true,
enabled,
connectWorkspace,
listThreadsForWorkspace,
}),
{
initialProps: { enabled: false },
},
);

await act(async () => {
await flushMicrotasks();
});

expect(connectWorkspace).not.toHaveBeenCalled();
expect(listThreadsForWorkspace).not.toHaveBeenCalled();

rerender({ enabled: true });

await act(async () => {
await flushMicrotasks();
});

expect(connectWorkspace).toHaveBeenCalledTimes(1);
expect(listThreadsForWorkspace).toHaveBeenCalledTimes(1);
});
});

type WorkspaceRestoreOptions = {
Expand Down
11 changes: 8 additions & 3 deletions src/features/workspaces/hooks/useWorkspaceRestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const MAX_RESTORE_RETRIES = 5;
type WorkspaceRestoreOptions = {
workspaces: WorkspaceInfo[];
hasLoaded: boolean;
enabled?: boolean;
connectWorkspace: (workspace: WorkspaceInfo) => Promise<void>;
listThreadsForWorkspace: (
workspace: WorkspaceInfo,
Expand All @@ -17,6 +18,7 @@ type WorkspaceRestoreOptions = {
export function useWorkspaceRestore({
workspaces,
hasLoaded,
enabled = true,
connectWorkspace,
listThreadsForWorkspace,
}: WorkspaceRestoreOptions) {
Expand All @@ -27,6 +29,7 @@ export function useWorkspaceRestore({
const optionsRef = useRef({
workspaces,
hasLoaded,
enabled,
connectWorkspace,
listThreadsForWorkspace,
});
Expand All @@ -35,6 +38,7 @@ export function useWorkspaceRestore({
optionsRef.current = {
workspaces,
hasLoaded,
enabled,
connectWorkspace,
listThreadsForWorkspace,
};
Expand All @@ -51,7 +55,7 @@ export function useWorkspaceRestore({
);

useEffect(() => {
if (!hasLoaded) {
if (!hasLoaded || !enabled) {
return;
}

Expand Down Expand Up @@ -83,10 +87,11 @@ export function useWorkspaceRestore({
const {
workspaces: latestWorkspaces,
hasLoaded: latestHasLoaded,
enabled: latestEnabled,
connectWorkspace: latestConnectWorkspace,
listThreadsForWorkspace: latestListThreadsForWorkspace,
} = optionsRef.current;
if (!latestHasLoaded) {
if (!latestHasLoaded || !latestEnabled) {
return;
}
const workspace = latestWorkspaces.find((entry) => entry.id === workspaceId);
Expand Down Expand Up @@ -136,5 +141,5 @@ export function useWorkspaceRestore({
workspaces.forEach((workspace) => {
restoreWorkspace(workspace.id);
});
}, [connectWorkspace, hasLoaded, listThreadsForWorkspace, workspaces]);
}, [connectWorkspace, enabled, hasLoaded, listThreadsForWorkspace, workspaces]);
}
Loading