@makoai/app-sdk 2.18.0 → 2.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -667,8 +667,18 @@ at once. Once it is older than `revalidateMs` (five minutes), it is also
667
667
  rebuilt in the background for the next load. `?refresh` waits for a
668
668
  build. A copy from other text is never served: after an edit the cache is never served, however long
669
669
  `revalidateMs` is, and while the API is unreachable a stale copy is served
670
- only if it was built from the same text. It is `apply: "serve"` only —
671
- production builds never load it.
670
+ only if it was built from the same text. The data middleware runs in
671
+ `vite dev` only; production builds load the plugin for the favicon alone.
672
+
673
+ ### Favicon
674
+
675
+ `makoData()` also links the app's favicon in `index.html`, in `vite dev` and
676
+ in `vite build`: the app's `icon.svg` (or `icon.png`) at its root, inlined
677
+ as a data URI so it works under any base path. An app without one gets a
678
+ generated letter mark: its `mako.json` title's first letter, white on a
679
+ rounded square whose hue comes from its id, as Mako's Apps explorer draws
680
+ it. An `index.html` that already has a `<link rel="icon">` is left alone;
681
+ `makoData({ favicon: false })` turns it off.
672
682
 
673
683
  ### dbt models you are still building
674
684
 
package/index.js CHANGED
@@ -37,7 +37,7 @@ import {
37
37
  * in a page (or this export) says which SDK a deployed app actually runs.
38
38
  * Kept in step with package.json by snapshot.test.mjs.
39
39
  */
40
- export const SDK_VERSION = "2.18.0";
40
+ export const SDK_VERSION = "2.19.0";
41
41
  if (typeof window !== "undefined") {
42
42
  try {
43
43
  window.__MAKO_APP_SDK__ = SDK_VERSION;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@makoai/app-sdk",
3
- "version": "2.18.0",
3
+ "version": "2.19.0",
4
4
  "description": "Mako app SDK: data bindings (useQuery/useDuckDB), URL state, theme, and the house dashboard kit (/ui).",
5
5
  "license": "MIT",
6
6
  "repository": {
package/ui.js CHANGED
@@ -186,12 +186,19 @@ function usePopover(open, setOpen, wrapRef, align) {
186
186
  * like a MultiSelect option row, runs the label's activation behaviour: the
187
187
  * browser clicks the labelled control, the popover's trigger, which closed
188
188
  * the menu. Cancelling the click stops that. Clicks on links, buttons and
189
- * form fields keep their default, and the label ignores those anyway.
189
+ * form fields keep their default, and the label ignores those anyway. A label
190
+ * the app renders inside the popover (a FreshnessBadge action) keeps working.
190
191
  */
191
192
  const LABEL_SAFE = "a[href],button,input,select,textarea,summary";
192
193
  function cancelLabelActivation(e) {
193
194
  const t = e.target;
194
- if (!(t instanceof Element) || !t.closest(LABEL_SAFE)) e.preventDefault();
195
+ if (t instanceof Element) {
196
+ if (t.closest(LABEL_SAFE)) return;
197
+ // A label inside the popover is the app's own: let it click its control.
198
+ const own = t.closest("label");
199
+ if (own && e.currentTarget.contains(own)) return;
200
+ }
201
+ e.preventDefault();
195
202
  }
196
203
 
197
204
  // ---------------------------------------------------------------------------
package/vite.d.ts CHANGED
@@ -45,6 +45,12 @@ export interface MakoDataOptions {
45
45
  * as Mako would for them. Default: MAKO_VIEWER_AS, else yourself.
46
46
  */
47
47
  viewAs?: string;
48
+ /**
49
+ * Link the app's favicon in index.html (dev and build): its `icon.svg` or
50
+ * `icon.png`, inlined, else a generated letter mark. Skipped when
51
+ * index.html already has a `<link rel="icon">`. Default: true.
52
+ */
53
+ favicon?: boolean;
48
54
  }
49
55
 
50
56
  export interface MakoContext {
@@ -76,9 +82,26 @@ export function normalizeBindingSource(source: string): string;
76
82
 
77
83
  export function bindingFingerprint(source: string, dbtEnvironment?: string): string;
78
84
 
85
+ /**
86
+ * The generated favicon of an app without an icon: the title's first letter
87
+ * on a rounded square, hue from the app id (as Mako's explorer draws it).
88
+ */
89
+ export function defaultAppIconSvg(app?: { id?: string; title?: string }): string;
90
+
91
+ /** The app's favicon: its icon.svg / icon.png inlined, else the letter mark. */
92
+ export function appFavicon(appDir: string): {
93
+ file: "icon.svg" | "icon.png" | null;
94
+ type: string;
95
+ href: string;
96
+ };
97
+
98
+ /** index.html with the favicon linked, unless it already links one. */
99
+ export function injectFavicon(html: string, appDir: string): string;
100
+
79
101
  /**
80
102
  * Serve the app's data bindings (`__data/index.json`, `__data/<name>.parquet`)
81
- * during a local `vite dev`, built by Mako from the local binding files.
103
+ * during a local `vite dev`, built by Mako from the local binding files, and
104
+ * link the app's favicon in index.html (dev and build).
82
105
  */
83
106
  export function makoData(options?: MakoDataOptions): Plugin;
84
107
  export default makoData;
package/vite.js CHANGED
@@ -14,6 +14,10 @@
14
14
  // cache is keyed by that text, so an edit never serves a parquet built from
15
15
  // the old query.
16
16
  //
17
+ // It also gives the app its favicon, in dev and in the build: the app's own
18
+ // `icon.svg` (or `icon.png`) inlined as a data URI, else a generated letter
19
+ // mark — unless index.html already links one.
20
+ //
17
21
  // Plain ESM, Node built-ins only — like the rest of this package.
18
22
  import fs from "node:fs";
19
23
  import path from "node:path";
@@ -137,6 +141,89 @@ function readManifestId(appDir) {
137
141
  }
138
142
  }
139
143
 
144
+ function readManifest(appDir) {
145
+ try {
146
+ const raw = JSON.parse(fs.readFileSync(path.join(appDir, "mako.json"), "utf8"));
147
+ return raw && typeof raw === "object" ? raw : {};
148
+ } catch {
149
+ return {};
150
+ }
151
+ }
152
+
153
+ // ---------------------------------------------------------------------------
154
+ // Favicon
155
+ // ---------------------------------------------------------------------------
156
+
157
+ /** The icon convention: first match at the app's root wins (as Mako indexes it). */
158
+ const ICON_FILES = [
159
+ ["icon.svg", "image/svg+xml"],
160
+ ["icon.png", "image/png"],
161
+ ];
162
+
163
+ /** index.html already links a favicon: leave it alone. */
164
+ const LINKS_ICON = /<link\b[^>]*\brel\s*=\s*["']?(?:shortcut\s+)?icon\b/i;
165
+
166
+ const escapeXml = (s) =>
167
+ s.replace(/[<>&"']/g, (c) => ({ "<": "&lt;", ">": "&gt;", "&": "&amp;", '"': "&quot;", "'": "&apos;" })[c]);
168
+
169
+ /**
170
+ * The favicon of an app that ships no icon: its title's first letter, white
171
+ * on a rounded square whose hue comes from the app id — the same hash and
172
+ * hue Mako's thumbnail placeholder and Apps explorer use, so the tab and the
173
+ * explorer row match.
174
+ */
175
+ export function defaultAppIconSvg({ id = "", title = "" } = {}) {
176
+ let hash = 0;
177
+ for (const ch of String(id)) hash = (hash * 31 + ch.charCodeAt(0)) >>> 0;
178
+ const hue = hash % 360;
179
+ const letter = (/[\p{L}\p{N}]/u.exec(String(title))?.[0] ?? "?").toUpperCase();
180
+ return (
181
+ `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">` +
182
+ `<rect width="32" height="32" rx="7" fill="hsl(${hue} 55% 42%)"/>` +
183
+ `<text x="16" y="22.5" text-anchor="middle" font-family="system-ui, -apple-system, Segoe UI, sans-serif" font-size="19" font-weight="700" fill="#fff">${escapeXml(letter)}</text>` +
184
+ `</svg>`
185
+ );
186
+ }
187
+
188
+ const svgDataUri = (svg) => `data:image/svg+xml,${encodeURIComponent(svg.trim())}`;
189
+
190
+ /**
191
+ * The app's favicon as a `<link rel="icon">` href: its `icon.svg`/`icon.png`
192
+ * inlined (a data URI needs no asset pipeline and works under any base path),
193
+ * otherwise defaultAppIconSvg for its manifest id and title.
194
+ */
195
+ export function appFavicon(appDir) {
196
+ for (const [file, type] of ICON_FILES) {
197
+ let bytes;
198
+ try {
199
+ bytes = fs.readFileSync(path.join(appDir, file));
200
+ } catch {
201
+ continue;
202
+ }
203
+ return {
204
+ file,
205
+ type,
206
+ href:
207
+ type === "image/svg+xml"
208
+ ? svgDataUri(bytes.toString("utf8"))
209
+ : `data:${type};base64,${bytes.toString("base64")}`,
210
+ };
211
+ }
212
+ const manifest = readManifest(appDir);
213
+ const folder = path.basename(path.resolve(appDir));
214
+ const id = typeof manifest.id === "string" && manifest.id ? manifest.id.toLowerCase() : folder;
215
+ const title = typeof manifest.title === "string" && manifest.title ? manifest.title : folder;
216
+ return { file: null, type: "image/svg+xml", href: svgDataUri(defaultAppIconSvg({ id, title })) };
217
+ }
218
+
219
+ /** index.html with the app's favicon linked, unless it links one already. */
220
+ export function injectFavicon(html, appDir) {
221
+ if (LINKS_ICON.test(html)) return html;
222
+ const { type, href } = appFavicon(appDir);
223
+ const link = `<link rel="icon" type="${type}" href="${href.replace(/"/g, "%22")}" />`;
224
+ return /<\/head>/i.test(html) ? html.replace(/<\/head>/i, ` ${link}\n </head>`) : `${link}\n${html}`;
225
+ }
226
+
140
227
  function listBindings(bindingsDir) {
141
228
  try {
142
229
  return fs
@@ -205,9 +292,17 @@ function phaseHub(first) {
205
292
  }
206
293
 
207
294
  export function makoData(options = {}) {
295
+ let root = process.cwd();
208
296
  return {
209
297
  name: "mako-data",
210
- apply: "serve",
298
+ // Serve and build: the data middleware only exists in configureServer
299
+ // (dev), the favicon goes into both.
300
+ configResolved(config) {
301
+ root = config.root;
302
+ },
303
+ transformIndexHtml(html) {
304
+ return options.favicon === false ? html : injectFavicon(html, root);
305
+ },
211
306
  configureServer(server) {
212
307
  const appDir = server.config.root;
213
308
  const ctx = resolveMakoContext(appDir, options);