refactor(desktop): harden workspace shell foundation
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
76
apps/desktop/src/lib/key-workspace-state.ts
Normal file
76
apps/desktop/src/lib/key-workspace-state.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user