@scalar/api-client
Version:
the open source API testing client
129 lines (128 loc) • 3.35 kB
JavaScript
import { isDefined as l } from "@scalar/helpers/array/is-defined";
import { createSidebarState as w, generateReverseIndex as b, getParentEntry as a } from "@scalar/sidebar";
import { computed as h, toValue as c, watch as x } from "vue";
import { useRouter as R } from "vue-router";
const O = ({
workspaceStore: d,
documentSlug: f,
path: y,
method: E,
exampleName: S
}) => {
const m = R(), u = h(() => {
const e = c(d);
return e ? (e.workspace["x-scalar-order"] ?? Object.keys(e.workspace.documents)).map((r) => e.workspace.documents[r]?.["x-scalar-navigation"]).filter(l) : [];
}), n = w(u);
x(
() => c(d),
() => {
n.reset();
},
{
immediate: !0
}
);
const p = ({
document: e,
path: t,
method: r,
example: o
}) => JSON.stringify([e, t, r, o].filter(l)), i = h(
() => b({
items: u.value,
nestedKey: "children",
filter: (e) => e.type === "document" || e.type === "operation" || e.type === "example",
getId: (e) => {
const t = a("document", e), r = a("operation", e);
return p({
document: t?.name ?? "",
path: r?.path,
method: r?.method,
example: e.type === "example" ? e.name : void 0
});
}
})
), g = (e) => {
const t = i.value.get(p(e));
return t || i.value.get(
p({
document: e.document,
path: e.path,
method: e.method
})
);
}, v = (e) => {
const t = n.getEntryById(e);
if (!t) {
console.warn(`Could not find sidebar entry with id ${e} to select`);
return;
}
if (t.type === "document")
return n.setSelected(e), n.setExpanded(e, !n.isExpanded(e)), m.push({
name: "document.overview",
params: { documentSlug: t.name }
});
if (t.type === "operation") {
if (n.isSelected(e)) {
n.setExpanded(e, !n.isExpanded(e));
return;
}
const r = t.children?.find((o) => o.type === "example");
return r ? (n.setSelected(r.id), n.setExpanded(r.id, !0)) : n.setSelected(e), m.push({
name: "example",
params: {
documentSlug: a("document", t)?.name,
pathEncoded: encodeURIComponent(t.path),
method: t.method,
exampleName: r?.name ?? "default"
}
});
}
if (t.type === "example") {
n.setSelected(e), n.setExpanded(e, !0);
const r = a("operation", t);
return m.push({
name: "example",
params: {
documentSlug: a("document", t)?.name,
pathEncoded: encodeURIComponent(r?.path ?? ""),
method: r?.method,
exampleName: t.name
}
});
}
if (t.type === "text")
return m.push({
name: "document.overview",
params: {
documentSlug: a("document", t)?.name
}
});
n.setExpanded(e, !n.isExpanded(e));
};
return x(
[() => c(d), f, y, E, S],
([e, t, r, o, I]) => {
if (!t) {
n.setSelected(null);
return;
}
const s = g({
document: t,
path: r,
method: o,
example: I
});
s && (n.setSelected(s.id), n.setExpanded(s.id, !0));
},
{
immediate: !0
}
), {
handleSelectItem: v,
sidebarState: n
};
};
export {
O as useSidebarState
};