Preserve thread selection during background updates
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
export type SidebarSelection = { kind: "chat" | "search"; id: string };
|
||||
|
||||
type WorkspaceSelectionItem = { type: SidebarSelection["kind"]; id: string };
|
||||
|
||||
type ResolveSidebarSelectionOptions = {
|
||||
initialSelection?: SidebarSelection;
|
||||
selectFallback?: boolean;
|
||||
};
|
||||
|
||||
export function resolveSidebarSelectionAfterRefresh(
|
||||
current: SidebarSelection | null,
|
||||
workspaceItems: WorkspaceSelectionItem[],
|
||||
{ initialSelection, selectFallback = false }: ResolveSidebarSelectionOptions = {}
|
||||
): SidebarSelection | null {
|
||||
const hasItem = (candidate: SidebarSelection | null | undefined) => {
|
||||
if (!candidate) return false;
|
||||
return workspaceItems.some((item) => item.type === candidate.kind && item.id === candidate.id);
|
||||
};
|
||||
|
||||
if (hasItem(current)) {
|
||||
return current;
|
||||
}
|
||||
if (hasItem(initialSelection)) {
|
||||
return initialSelection ?? null;
|
||||
}
|
||||
if (!selectFallback) {
|
||||
return null;
|
||||
}
|
||||
const first = workspaceItems[0];
|
||||
return first ? { kind: first.type, id: first.id } : null;
|
||||
}
|
||||
Reference in New Issue
Block a user