import { codeWidget, editor, events } from "$sb/syscalls.ts"; import { parseQuery } from "$sb/lib/parse-query.ts"; import { loadPageObject, replaceTemplateVars } from "../template/page.ts"; import { resolvePath } from "$sb/lib/resolve.ts"; import { CodeWidgetContent, PageMeta, Query } from "$sb/types.ts"; import { jsonToMDTable, renderQueryTemplate } from "../template/util.ts"; export async function widget( bodyText: string, pageName: string, ): Promise { const pageObject = await loadPageObject(pageName); try { let resultMarkdown = ""; const parsedQuery = await parseQuery( await replaceTemplateVars(bodyText, pageObject), ); const results = await performQuery( parsedQuery, pageObject, ); if (results.length === 0 && !parsedQuery.renderAll) { resultMarkdown = "No results"; } else { if (parsedQuery.render) { // Configured a custom rendering template, let's use it! const templatePage = resolvePath(pageName, parsedQuery.render); const rendered = await renderQueryTemplate( pageObject, templatePage, results, parsedQuery.renderAll!, ); resultMarkdown = rendered.trim(); } else { // TODO: At this point it's a bit pointless to first render a markdown table, and then convert that to HTML // We should just render the HTML table directly resultMarkdown = jsonToMDTable(results); } } return { markdown: resultMarkdown, buttons: [ { description: "Edit", svg: ``, invokeFunction: "query.editButton", }, { description: "Reload", svg: ``, invokeFunction: "query.refreshAllWidgets", }, ], }; } catch (e: any) { return { markdown: `**Error:** ${e.message}` }; } } export async function performQuery(parsedQuery: Query, pageObject: PageMeta) { if (!parsedQuery.limit) { parsedQuery.limit = ["number", 1000]; } const eventName = `query:${parsedQuery.querySource}`; // console.log("Parsed query", parsedQuery); // Let's dispatch an event and see what happens const results = await events.dispatchEvent( eventName, { query: parsedQuery, pageName: pageObject.name }, 30 * 1000, ); if (results.length === 0) { throw new Error(`Unsupported query source '${parsedQuery.querySource}'`); } return results.flat(); } export function refreshAllWidgets() { codeWidget.refreshAll(); } export async function editButton(pos: number) { await editor.moveCursor(pos); }