Skip to content

Commit

Permalink
refact: Separate concerns in window dedupe logic #69
Browse files Browse the repository at this point in the history
  • Loading branch information
Lev Z Király authored and Lev Z Király committed Nov 30, 2023
1 parent 1154247 commit ff8b174
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions stores/use-windows-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,11 @@ export const useWindowsStore = defineStore("windows", () => {
const id = params.id ?? `window-${nanoid()}`;
const { title, kind } = params;

if (isWindowContentAlreadyOpen(params.kind, params.params)) return;
const w = windowWithContentId(params.kind, params.params);
if (w != null) {
w.winbox.focus();
return;
}

const winbox = new WinBox({
id,
Expand Down Expand Up @@ -241,25 +245,26 @@ export const useWindowsStore = defineStore("windows", () => {
} as WindowItem);
}

function isWindowContentAlreadyOpen(kind: Kind, params: WindowItemMap[Kind]["params"]) {
let hasFoundIdentical = false;
function windowWithContentId(
kind: WindowItemKind,
params: WindowItemMap[WindowItemKind]["params"],
): WindowItem | null {
let foundWindow: WindowItem | null = null;
if (typeof params === "object" && params !== null && "id" in params) {
registry.value.forEach((w) => {
if (
!hasFoundIdentical &&
foundWindow === null &&
w.kind === kind &&
typeof w.params === "object" &&
w.params !== null &&
"id" in w.params &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
w.params.id === params.id
) {
w.winbox.focus();
hasFoundIdentical = true;
foundWindow = w;
}
});
}
return hasFoundIdentical;
return foundWindow;
}

function removeWindow(id: WindowItem["id"]) {
Expand Down

0 comments on commit ff8b174

Please sign in to comment.