@colixsystems/widget-sdk 0.13.0 → 0.15.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/dist/toast.js ADDED
@@ -0,0 +1,73 @@
1
+ // REQ-WSDK-PLATFORM §6 — `useToast()` hook (web implementation).
2
+ //
3
+ // Surfaces a short auto-dismissing notification. Widget API:
4
+ //
5
+ // const { showToast } = useToast();
6
+ // showToast({ kind: "success" | "error" | "info" | "warning", message: "Saved" });
7
+ //
8
+ // Renderer resolution order:
9
+ // 1. If the host provided `ctx.toast.showToast`, call that. The host
10
+ // owns positioning + theming.
11
+ // 2. Otherwise: dispatch a CustomEvent named "appstudio:widget-toast"
12
+ // on the window with detail `{ kind, message }`. Hosts that want a
13
+ // central toast renderer install a `window.addEventListener` for it.
14
+ // 3. As a last resort, `console.log` the toast so dev work surfaces
15
+ // something even before either of the above is wired.
16
+ //
17
+ // The SDK does NOT render any DOM itself — that responsibility belongs
18
+ // to the host so a single workspace-themed toast component shows the
19
+ // notifications across all widgets.
20
+
21
+ import { useCallback } from "react";
22
+ import { useWidgetContextOrThrow } from "./hooks.js";
23
+
24
+ function _normalizeKind(kind) {
25
+ return kind === "success" || kind === "error" || kind === "warning"
26
+ ? kind
27
+ : "info";
28
+ }
29
+
30
+ export function useToast() {
31
+ const ctx = useWidgetContextOrThrow("useToast");
32
+ const hostToast =
33
+ ctx && ctx.toast && typeof ctx.toast.showToast === "function"
34
+ ? ctx.toast.showToast
35
+ : null;
36
+ const showToast = useCallback(
37
+ (payload) => {
38
+ const opts = payload && typeof payload === "object" ? payload : {};
39
+ const kind = _normalizeKind(opts.kind);
40
+ const message = typeof opts.message === "string" ? opts.message : "";
41
+ if (!message) return;
42
+ if (hostToast) {
43
+ try {
44
+ hostToast({ kind, message });
45
+ return;
46
+ } catch (err) {
47
+ // Fall through to the event + console fallback rather than
48
+ // crashing the widget.
49
+ if (typeof console !== "undefined" && console.warn) {
50
+ console.warn("[useToast] host toast handler threw:", err);
51
+ }
52
+ }
53
+ }
54
+ if (
55
+ typeof window !== "undefined" &&
56
+ typeof window.dispatchEvent === "function" &&
57
+ typeof CustomEvent === "function"
58
+ ) {
59
+ window.dispatchEvent(
60
+ new CustomEvent("appstudio:widget-toast", {
61
+ detail: { kind, message },
62
+ }),
63
+ );
64
+ return;
65
+ }
66
+ if (typeof console !== "undefined" && console.log) {
67
+ console.log(`[toast:${kind}] ${message}`);
68
+ }
69
+ },
70
+ [hostToast],
71
+ );
72
+ return { showToast };
73
+ }
@@ -0,0 +1,46 @@
1
+ // REQ-WSDK-PLATFORM §6 — `useToast()` hook (native implementation).
2
+ //
3
+ // Same API as the web counterpart (./toast.js). Native has no
4
+ // `window.dispatchEvent` fallback — if the host doesn't provide
5
+ // `ctx.toast.showToast`, the hook falls back to `console.log` so the
6
+ // widget at least surfaces something during dev.
7
+
8
+ import { useCallback } from "react";
9
+ import { useWidgetContextOrThrow } from "./hooks.js";
10
+
11
+ function _normalizeKind(kind) {
12
+ return kind === "success" || kind === "error" || kind === "warning"
13
+ ? kind
14
+ : "info";
15
+ }
16
+
17
+ export function useToast() {
18
+ const ctx = useWidgetContextOrThrow("useToast");
19
+ const hostToast =
20
+ ctx && ctx.toast && typeof ctx.toast.showToast === "function"
21
+ ? ctx.toast.showToast
22
+ : null;
23
+ const showToast = useCallback(
24
+ (payload) => {
25
+ const opts = payload && typeof payload === "object" ? payload : {};
26
+ const kind = _normalizeKind(opts.kind);
27
+ const message = typeof opts.message === "string" ? opts.message : "";
28
+ if (!message) return;
29
+ if (hostToast) {
30
+ try {
31
+ hostToast({ kind, message });
32
+ return;
33
+ } catch (err) {
34
+ if (typeof console !== "undefined" && console.warn) {
35
+ console.warn("[useToast] host toast handler threw:", err);
36
+ }
37
+ }
38
+ }
39
+ if (typeof console !== "undefined" && console.log) {
40
+ console.log(`[toast:${kind}] ${message}`);
41
+ }
42
+ },
43
+ [hostToast],
44
+ );
45
+ return { showToast };
46
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colixsystems/widget-sdk",
3
- "version": "0.13.0",
3
+ "version": "0.15.0",
4
4
  "description": "Common widget interface for AppStudio. Implements WidgetManifest, WidgetContext, property schema, and helper hooks.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -35,7 +35,7 @@
35
35
  ],
36
36
  "scripts": {
37
37
  "build": "node scripts/build.js",
38
- "test": "node --test src/__tests__/contract.test.js"
38
+ "test": "node --test src/__tests__/contract.test.js src/__tests__/hooks-users.test.js src/__tests__/hooks-groups.test.js src/__tests__/linter-users-scope.test.js"
39
39
  },
40
40
  "engines": {
41
41
  "node": ">=18"