43 lines
1.4 KiB
JavaScript
43 lines
1.4 KiB
JavaScript
import { get } from "./api.js";
|
|
import { h, replace } from "./dom.js";
|
|
import { onEach, state } from "./state.js";
|
|
export function previewURL(port) {
|
|
return `https://${location.hostname}:${port}/`;
|
|
}
|
|
export function previewLinks() {
|
|
const root = h("nav", { class: "preview-links" });
|
|
let target = 0;
|
|
const load = async () => {
|
|
const taskId = state.selectedTaskId;
|
|
const started = ++target;
|
|
if (!taskId) {
|
|
replace(root);
|
|
return;
|
|
}
|
|
try {
|
|
const previews = await get("/api/tasks/{taskId}/previews", { path: { taskId } });
|
|
if (started !== target) {
|
|
return;
|
|
}
|
|
replace(root, ...previews.map((p) => h("a", { class: "preview-link", href: previewURL(p.port), target: "_blank", rel: "noopener", title: `Open ${p.name} (port ${p.port}) in a new window` }, p.name, " ↗")));
|
|
}
|
|
catch (err) {
|
|
if (started === target) {
|
|
replace(root);
|
|
}
|
|
console.error("list previews", err);
|
|
}
|
|
};
|
|
onEach(["selection"], () => {
|
|
replace(root);
|
|
void load();
|
|
});
|
|
onEach(["transcript"], (fresh) => {
|
|
if (fresh?.some((ev) => ev.kind === "preview.exposed")) {
|
|
void load();
|
|
}
|
|
});
|
|
void load();
|
|
return root;
|
|
}
|
|
//# sourceMappingURL=preview.js.map
|