@scalar/api-client
Version:
the open source API testing client
53 lines (52 loc) • 1.91 kB
JavaScript
import { ref as w, watch as d } from "vue";
import { useRoute as v, useRouter as x } from "vue-router";
import { DEFAULT_WORKSPACE as g } from "./use-workspace-selector.js";
import { workspaceStorage as k } from "../../../helpers/storage.js";
const F = ({
workspaceSelectorState: e,
tabsState: u,
eventBus: l
}) => {
const c = v(), f = x(), o = w(!1), b = () => {
const a = e.store.value;
if (!a)
return;
const t = a.workspace["x-scalar-tabs"], r = a.workspace["x-scalar-active-tab"] ?? 0;
t?.[r] && (t[r] = u.createTabFromCurrentRoute());
}, m = async (a) => {
const t = e.store.value;
if (!t)
return;
const r = t.workspace["x-scalar-tabs"] ?? [], n = t.workspace["x-scalar-active-tab"] ?? 0, s = r[n];
s && s.path !== c.path && await a?.({ tabPath: s.path });
}, h = async (a) => {
e.store.value = null, o.value = !0;
const t = await e.loadWorkspace(a);
if (t.success) {
const { workspace: n } = t, s = n.workspace["x-scalar-active-tab"] ?? 0, i = n.workspace["x-scalar-tabs"], p = i?.[s];
p && await f.replace(p.path), i && s >= i.length && l.emit("tabs:update:tabs", {
"x-scalar-active-tab": 0
}), i || l.emit("tabs:update:tabs", {
"x-scalar-tabs": [u.createTabFromCurrentRoute()],
"x-scalar-active-tab": 0
}), o.value = !1;
return;
}
const r = await e.createWorkspace(g);
if (o.value = !1, !r)
return console.error("Failed to create the default workspace, something went wrong, can not load the workspace");
};
return d(
[() => c.path],
async () => {
const a = c.params.workspaceSlug;
if (typeof a == "string" && k.setCurrentPath(c.path), typeof a == "string" && a !== e.activeWorkspace.value?.id)
return await h(a);
await m(b), o.value = !1;
},
{ immediate: !0 }
), { isLoading: o };
};
export {
F as useSyncPath
};