UNPKG

@scalar/api-client

Version:

the open source API testing client

94 lines (93 loc) 2.34 kB
import { createSidebarState as S, generateReverseIndex as v, getChildEntry as I } from "@scalar/sidebar"; import { getParentEntry as l } from "@scalar/workspace-store/navigation"; import { computed as h, toValue as p, watch as b } from "vue"; import { generateLocationId as m } from "../../../helpers/generate-location-id.js"; const k = ({ workspaceStore: u, documentSlug: o, path: x, method: y, exampleName: f, route: E }) => { const s = h( () => u?.workspace.documents[p(o) ?? ""]?.["x-scalar-navigation"]?.children ?? [] ), t = S(s), i = h( () => v({ items: s.value, nestedKey: "children", filter: (e) => e.type === "operation" || e.type === "example", getId: (e) => { const r = l("operation", e); return m({ document: p(o) ?? "", path: r?.path, method: r?.method, example: e.type === "example" ? e.name : void 0 }); } }) ), c = (e) => { const r = i.value.get( m({ document: e.document, path: e.path, method: e.method, example: e.example }) ); return r || i.value.get( m({ document: e.document, path: e.path, method: e.method }) ); }, g = (e) => { const r = t.getEntryById(e); if (!r) { console.warn(`Could not find sidebar entry with id ${e} to select`); return; } if (r.type === "operation" || r.type === "example") { if (t.isSelected(e)) { t.setExpanded(e, !t.isExpanded(e)); return; } const a = l("operation", r), n = I("example", r); return n ? (t.setSelected(n.id), t.setExpanded(n.id, !0)) : t.setSelected(e), a ? E({ documentSlug: p(o), path: a.path, method: a.method, example: n?.name ?? "default" }) : void 0; } t.setExpanded(e, !t.isExpanded(e)); }; return b( [o, x, y, f], ([e, r, a, n]) => { if (!e) { t.setSelected(null); return; } const d = c({ document: e, path: r, method: a, example: n }); d && (t.setSelected(d.id), t.setExpanded(d.id, !0)); }, { immediate: !0 } ), { handleSelectItem: g, state: t, getEntryByLocation: c }; }; export { k as useModalSidebar };