refactor(desktop): harden workspace shell foundation

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Senior Frontend Engineer
2026-03-31 04:03:38 +00:00
parent 7c1abf2fd1
commit 291f744e0f
10 changed files with 805 additions and 82 deletions

View File

@@ -0,0 +1,76 @@
export type WorkspaceKeyIdentity = {
id: string;
};
export type WorkspaceSearchableKey = WorkspaceKeyIdentity & {
name: string;
};
export function filterWorkspaceKeys<T extends WorkspaceSearchableKey>(
keys: T[],
search: string,
) {
const query = search.trim().toLowerCase();
if (!query) {
return keys;
}
return keys.filter((item) => item.name.toLowerCase().includes(query));
}
export function resolveSelectedWorkspaceKey<T extends WorkspaceKeyIdentity>(
keys: T[],
selectedKeyId: string | null,
) {
if (keys.length === 0) {
return null;
}
return keys.find((item) => item.id === selectedKeyId) ?? keys[0];
}
export function resolveSelectedWorkspaceKeyId<T extends WorkspaceKeyIdentity>(
keys: T[],
selectedKeyId: string | null,
) {
return resolveSelectedWorkspaceKey(keys, selectedKeyId)?.id ?? null;
}
export function replaceWorkspaceKeyRecord<T extends WorkspaceKeyIdentity>(
current: T[],
next: T,
) {
let replaced = false;
const updated = current.map((item) => {
if (item.id !== next.id) {
return item;
}
replaced = true;
return {
...item,
...next,
};
});
return replaced ? updated : [next, ...current];
}
export function appendWorkspaceKeyRecords<T extends WorkspaceKeyIdentity>(
current: T[],
next: T[],
) {
const seen = new Set(current.map((item) => item.id));
const appended = [...current];
for (const item of next) {
if (seen.has(item.id)) {
continue;
}
seen.add(item.id);
appended.push(item);
}
return appended;
}