@forgeportal/plugin-grafana 1.3.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.
@@ -0,0 +1,4 @@
1
+
2
+ > @forgeportal/plugin-grafana@1.3.0 build /home/runner/work/forgeportal/forgeportal/packages/plugin-grafana
3
+ > tsc
4
+
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 bendaamerahmed
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+ import type { Entity } from '@forgeportal/plugin-sdk';
3
+ interface GrafanaTabProps {
4
+ entity: Entity;
5
+ }
6
+ export declare function GrafanaTab({ entity }: GrafanaTabProps): React.ReactElement;
7
+ export {};
8
+ //# sourceMappingURL=GrafanaTab.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GrafanaTab.d.ts","sourceRoot":"","sources":["../src/GrafanaTab.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA4B,MAAM,OAAO,CAAC;AACjD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AA8EtD,UAAU,eAAe;IAAG,MAAM,EAAE,MAAM,CAAA;CAAE;AAE5C,wBAAgB,UAAU,CAAC,EAAE,MAAM,EAAE,EAAE,eAAe,GAAG,KAAK,CAAC,YAAY,CAqI1E"}
@@ -0,0 +1,90 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useState, useMemo } from 'react';
3
+ const TIME_RANGES = {
4
+ '1h': { label: '1h', from: 'now-1h', to: 'now' },
5
+ '6h': { label: '6h', from: 'now-6h', to: 'now' },
6
+ '24h': { label: '24h', from: 'now-24h', to: 'now' },
7
+ '7d': { label: '7d', from: 'now-7d', to: 'now' },
8
+ };
9
+ // ─── URL builder ─────────────────────────────────────────────────────────────
10
+ function buildEmbedUrl(dashboardUrl, opts) {
11
+ try {
12
+ const url = new URL(dashboardUrl);
13
+ url.searchParams.set('from', opts.from);
14
+ url.searchParams.set('to', opts.to);
15
+ url.searchParams.set('kiosk', '1');
16
+ url.searchParams.set('theme', 'light');
17
+ if (opts.varName && opts.varValue) {
18
+ url.searchParams.set(`var-${opts.varName}`, opts.varValue);
19
+ }
20
+ return url.toString();
21
+ }
22
+ catch {
23
+ return dashboardUrl;
24
+ }
25
+ }
26
+ function buildOpenUrl(dashboardUrl) {
27
+ try {
28
+ const url = new URL(dashboardUrl);
29
+ // Remove kiosk param for the "open in Grafana" link
30
+ url.searchParams.delete('kiosk');
31
+ return url.toString();
32
+ }
33
+ catch {
34
+ return dashboardUrl;
35
+ }
36
+ }
37
+ /**
38
+ * Parses a comma-separated (or newline-separated) list of dashboard URLs
39
+ * from the annotation value.
40
+ */
41
+ function parseDashboardUrls(raw) {
42
+ return raw
43
+ .split(/[\n,]+/)
44
+ .map((s) => s.trim())
45
+ .filter((s) => {
46
+ try {
47
+ new URL(s);
48
+ return true;
49
+ }
50
+ catch {
51
+ return false;
52
+ }
53
+ });
54
+ }
55
+ function dashboardLabel(url, index) {
56
+ try {
57
+ const u = new URL(url);
58
+ // Extract the dashboard slug from the path: /d/<uid>/<slug>
59
+ const parts = u.pathname.split('/').filter(Boolean);
60
+ const slugIdx = parts.findIndex((p) => p === 'd') + 2;
61
+ const slug = parts[slugIdx];
62
+ if (slug)
63
+ return slug.replace(/-/g, ' ');
64
+ }
65
+ catch { /* fall through */ }
66
+ return `Dashboard ${index + 1}`;
67
+ }
68
+ export function GrafanaTab({ entity }) {
69
+ const [timeRange, setTimeRange] = useState('6h');
70
+ const [activeDashId, setActiveDashId] = useState(0);
71
+ const annotations = entity.annotations ?? {};
72
+ const rawUrls = annotations['forgeportal.dev/grafana-dashboard-url'] ?? '';
73
+ const varName = annotations['forgeportal.dev/grafana-variable-name'];
74
+ const varValue = entity.name;
75
+ const dashboards = useMemo(() => parseDashboardUrls(rawUrls), [rawUrls]);
76
+ // Not configured state
77
+ if (dashboards.length === 0) {
78
+ return (_jsxs("div", { className: "rounded-lg border border-dashed border-gray-200 bg-gray-50 p-8 text-center", children: [_jsx("p", { className: "text-sm font-medium text-gray-700 mb-1", children: "No Grafana dashboard configured" }), _jsxs("p", { className: "text-xs text-gray-500 mb-4", children: ["Add the annotation", ' ', _jsx("code", { className: "rounded bg-gray-100 px-1 py-0.5", children: "forgeportal.dev/grafana-dashboard-url" }), ' ', "to your ", _jsx("code", { className: "rounded bg-gray-100 px-1 py-0.5", children: "entity.yaml" }), "."] }), _jsx("pre", { className: "mx-auto max-w-lg rounded bg-gray-800 p-3 text-left text-xs text-green-300", children: `metadata:\n annotations:\n forgeportal.dev/grafana-dashboard-url: https://grafana.internal/d/abc123/service-overview\n forgeportal.dev/grafana-variable-name: service # optional` })] }));
79
+ }
80
+ const activeDash = dashboards[activeDashId] ?? dashboards[0];
81
+ const { from, to } = TIME_RANGES[timeRange];
82
+ const embedUrl = buildEmbedUrl(activeDash, { from, to, varName, varValue });
83
+ const openUrl = buildOpenUrl(activeDash);
84
+ return (_jsxs("div", { className: "space-y-3", children: [_jsxs("div", { className: "flex items-center justify-between flex-wrap gap-2", children: [dashboards.length > 1 && (_jsx("div", { className: "flex gap-1 border border-gray-200 rounded-md p-0.5 bg-gray-50", children: dashboards.map((url, i) => (_jsx("button", { onClick: () => setActiveDashId(i), className: `px-3 py-1 text-xs font-medium rounded transition-colors ${activeDashId === i
85
+ ? 'bg-white shadow-sm text-gray-800'
86
+ : 'text-gray-500 hover:text-gray-700'}`, children: dashboardLabel(url, i) }, i))) })), _jsxs("div", { className: "flex items-center gap-2 ml-auto", children: [_jsx("div", { className: "flex items-center gap-0.5 rounded-md border border-gray-200 bg-gray-50 p-0.5", children: Object.keys(TIME_RANGES).map((tr) => (_jsx("button", { onClick: () => setTimeRange(tr), className: `px-2.5 py-1 text-xs font-medium rounded transition-colors ${timeRange === tr
87
+ ? 'bg-white shadow-sm text-indigo-600'
88
+ : 'text-gray-500 hover:text-gray-700'}`, children: TIME_RANGES[tr].label }, tr))) }), _jsxs("a", { href: openUrl, target: "_blank", rel: "noreferrer", className: "inline-flex items-center gap-1.5 rounded-md border border-gray-200 bg-white px-3 py-1.5 text-xs font-medium text-gray-700 hover:bg-gray-50 transition-colors", children: [_jsx("svg", { className: "h-3.5 w-3.5", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: _jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: 2, d: "M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14" }) }), "Open in Grafana"] })] })] }), varName && (_jsxs("p", { className: "text-xs text-gray-400", children: ["Variable injection: ", _jsxs("code", { className: "rounded bg-gray-100 px-1", children: ["var-", varName, "=", varValue] })] })), _jsxs("div", { className: "relative w-full overflow-hidden rounded-lg border border-gray-200 bg-gray-50", style: { paddingBottom: '56.25%' /* 16:9 */ }, children: [_jsx("iframe", { src: embedUrl, className: "absolute inset-0 h-full w-full", title: `Grafana — ${dashboardLabel(activeDash, activeDashId)}`, frameBorder: "0", allowFullScreen: true }, `${embedUrl}`), _jsx("noscript", { children: _jsxs("div", { className: "absolute inset-0 flex flex-col items-center justify-center bg-gray-50 text-center p-6", children: [_jsx("p", { className: "text-sm font-medium text-gray-700 mb-2", children: "Dashboard cannot be embedded" }), _jsx("p", { className: "text-xs text-gray-500 mb-4", children: "Your Grafana instance may require authentication or have CSP restrictions." }), _jsx("a", { href: openUrl, target: "_blank", rel: "noreferrer", className: "inline-flex items-center gap-1.5 rounded-md bg-indigo-600 px-3 py-1.5 text-xs font-medium text-white hover:bg-indigo-700", children: "Open in Grafana" })] }) })] }), _jsxs("p", { className: "text-xs text-gray-400 text-right", children: ["Grafana \u00B7 ", dashboardLabel(activeDash, activeDashId), " \u00B7 ", from, " \u2192 ", to] })] }));
89
+ }
90
+ //# sourceMappingURL=GrafanaTab.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GrafanaTab.js","sourceRoot":"","sources":["../src/GrafanaTab.tsx"],"names":[],"mappings":";AAAA,OAAc,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAajD,MAAM,WAAW,GAAuC;IACtD,IAAI,EAAG,EAAE,KAAK,EAAE,IAAI,EAAG,IAAI,EAAE,QAAQ,EAAG,EAAE,EAAE,KAAK,EAAE;IACnD,IAAI,EAAG,EAAE,KAAK,EAAE,IAAI,EAAG,IAAI,EAAE,QAAQ,EAAG,EAAE,EAAE,KAAK,EAAE;IACnD,KAAK,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,KAAK,EAAE;IACnD,IAAI,EAAG,EAAE,KAAK,EAAE,IAAI,EAAG,IAAI,EAAE,QAAQ,EAAG,EAAE,EAAE,KAAK,EAAE;CACpD,CAAC;AAEF,gFAAgF;AAEhF,SAAS,aAAa,CACpB,YAAoB,EACpB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AE,CAMlB,EAGF,6BACE,eAAK,SAAS,EAAC,uFAAuF,aACpG,YAAG,SAAS,EAAC,wCAAwC,6CAAiC,EACtF,YAAG,SAAS,EAAC,4BAA4B,2FAErC,EACJ,YACE,IAAI,EAAE,OAAO,EACb,MAAM,EAAC,QAAQ,EACf,GAAG,EAAC,YAAY,EAChB,SAAS,EAAC,0HAA0H,gCAGlI,IACA,GACG,IACP,EAEN,aAAG,SAAS,EAAC,kCAAkC,gCAClC,cAAc,CAAC,UAAU,EAAE,YAAY,CAAC,cAAK,IAAI,cAAK,EAAE,IACjE,IACA,CACP,CAAC;AACJ,CAAC"}
package/dist/ui.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { ForgePluginSDK } from '@forgeportal/plugin-sdk';
2
+ /**
3
+ * UI entry point for the Grafana plugin.
4
+ * Called by the ForgePortal UI shell at startup.
5
+ *
6
+ * Registration in apps/ui/src/plugins/index.ts:
7
+ * import { registerPlugin as registerGrafana } from '@forgeportal/plugin-grafana/ui';
8
+ * registerPluginById('grafana', registerGrafana);
9
+ */
10
+ export declare function registerPlugin(sdk: ForgePluginSDK): void;
11
+ //# sourceMappingURL=ui.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ui.d.ts","sourceRoot":"","sources":["../src/ui.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAG9D;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,cAAc,GAAG,IAAI,CAOxD"}
package/dist/ui.js ADDED
@@ -0,0 +1,18 @@
1
+ import { GrafanaTab } from './GrafanaTab.js';
2
+ /**
3
+ * UI entry point for the Grafana plugin.
4
+ * Called by the ForgePortal UI shell at startup.
5
+ *
6
+ * Registration in apps/ui/src/plugins/index.ts:
7
+ * import { registerPlugin as registerGrafana } from '@forgeportal/plugin-grafana/ui';
8
+ * registerPluginById('grafana', registerGrafana);
9
+ */
10
+ export function registerPlugin(sdk) {
11
+ sdk.registerEntityTab({
12
+ id: 'grafana-tab',
13
+ title: 'Grafana',
14
+ component: GrafanaTab,
15
+ // No appliesTo — the tab shows a helpful config message when annotation is missing
16
+ });
17
+ }
18
+ //# sourceMappingURL=ui.js.map
package/dist/ui.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ui.js","sourceRoot":"","sources":["../src/ui.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAe,iBAAiB,CAAC;AAEtD;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAAC,GAAmB;IAChD,GAAG,CAAC,iBAAiB,CAAC;QACpB,EAAE,EAAS,aAAa;QACxB,KAAK,EAAM,SAAS;QACpB,SAAS,EAAE,UAAU;QACrB,mFAAmF;KACpF,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@forgeportal/plugin-grafana",
3
+ "version": "1.0.0",
4
+ "forgeportal": {
5
+ "engineVersion": "^1.0.0",
6
+ "type": "ui",
7
+ "capabilities": {
8
+ "ui": {
9
+ "entityTabs": ["grafana-tab"]
10
+ }
11
+ },
12
+ "config": {
13
+ "grafanaUrl": {
14
+ "type": "string",
15
+ "description": "Base Grafana URL, e.g. https://grafana.internal. Used to validate annotations.",
16
+ "required": false
17
+ },
18
+ "defaultTimeRange": {
19
+ "type": "string",
20
+ "description": "Default time range: 1h | 6h | 24h | 7d (default: 6h).",
21
+ "required": false
22
+ }
23
+ }
24
+ }
25
+ }
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@forgeportal/plugin-grafana",
3
+ "version": "1.3.0",
4
+ "type": "module",
5
+ "exports": {
6
+ "./ui": {
7
+ "types": "./dist/ui.d.ts",
8
+ "import": "./dist/ui.js"
9
+ },
10
+ "./package.json": "./package.json",
11
+ "./forgeportal-plugin.json": "./forgeportal-plugin.json"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "dependencies": {
17
+ "@forgeportal/plugin-sdk": "1.3.0"
18
+ },
19
+ "devDependencies": {
20
+ "@types/react": "*",
21
+ "react": "*"
22
+ },
23
+ "peerDependencies": {
24
+ "react": ">=19"
25
+ },
26
+ "scripts": {
27
+ "build": "tsc",
28
+ "lint": "eslint src/",
29
+ "clean": "rm -rf dist"
30
+ }
31
+ }
@@ -0,0 +1,215 @@
1
+ import React, { useState, useMemo } from 'react';
2
+ import type { Entity } from '@forgeportal/plugin-sdk';
3
+
4
+ // ─── Types ────────────────────────────────────────────────────────────────────
5
+
6
+ type TimeRange = '1h' | '6h' | '24h' | '7d';
7
+
8
+ interface TimeRangeOption {
9
+ label: string;
10
+ from: string;
11
+ to: string;
12
+ }
13
+
14
+ const TIME_RANGES: Record<TimeRange, TimeRangeOption> = {
15
+ '1h': { label: '1h', from: 'now-1h', to: 'now' },
16
+ '6h': { label: '6h', from: 'now-6h', to: 'now' },
17
+ '24h': { label: '24h', from: 'now-24h', to: 'now' },
18
+ '7d': { label: '7d', from: 'now-7d', to: 'now' },
19
+ };
20
+
21
+ // ─── URL builder ─────────────────────────────────────────────────────────────
22
+
23
+ function buildEmbedUrl(
24
+ dashboardUrl: string,
25
+ opts: { from: string; to: string; varName?: string; varValue?: string },
26
+ ): string {
27
+ try {
28
+ const url = new URL(dashboardUrl);
29
+ url.searchParams.set('from', opts.from);
30
+ url.searchParams.set('to', opts.to);
31
+ url.searchParams.set('kiosk', '1');
32
+ url.searchParams.set('theme', 'light');
33
+ if (opts.varName && opts.varValue) {
34
+ url.searchParams.set(`var-${opts.varName}`, opts.varValue);
35
+ }
36
+ return url.toString();
37
+ } catch {
38
+ return dashboardUrl;
39
+ }
40
+ }
41
+
42
+ function buildOpenUrl(dashboardUrl: string): string {
43
+ try {
44
+ const url = new URL(dashboardUrl);
45
+ // Remove kiosk param for the "open in Grafana" link
46
+ url.searchParams.delete('kiosk');
47
+ return url.toString();
48
+ } catch {
49
+ return dashboardUrl;
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Parses a comma-separated (or newline-separated) list of dashboard URLs
55
+ * from the annotation value.
56
+ */
57
+ function parseDashboardUrls(raw: string): string[] {
58
+ return raw
59
+ .split(/[\n,]+/)
60
+ .map((s) => s.trim())
61
+ .filter((s) => {
62
+ try { new URL(s); return true; } catch { return false; }
63
+ });
64
+ }
65
+
66
+ function dashboardLabel(url: string, index: number): string {
67
+ try {
68
+ const u = new URL(url);
69
+ // Extract the dashboard slug from the path: /d/<uid>/<slug>
70
+ const parts = u.pathname.split('/').filter(Boolean);
71
+ const slugIdx = parts.findIndex((p) => p === 'd') + 2;
72
+ const slug = parts[slugIdx];
73
+ if (slug) return slug.replace(/-/g, ' ');
74
+ } catch { /* fall through */ }
75
+ return `Dashboard ${index + 1}`;
76
+ }
77
+
78
+ // ─── Main Tab ────────────────────────────────────────────────────────────────
79
+
80
+ interface GrafanaTabProps { entity: Entity }
81
+
82
+ export function GrafanaTab({ entity }: GrafanaTabProps): React.ReactElement {
83
+ const [timeRange, setTimeRange] = useState<TimeRange>('6h');
84
+ const [activeDashId, setActiveDashId] = useState(0);
85
+
86
+ const annotations = entity.annotations ?? {};
87
+ const rawUrls = annotations['forgeportal.dev/grafana-dashboard-url'] ?? '';
88
+ const varName = annotations['forgeportal.dev/grafana-variable-name'];
89
+ const varValue = entity.name;
90
+
91
+ const dashboards = useMemo(() => parseDashboardUrls(rawUrls), [rawUrls]);
92
+
93
+ // Not configured state
94
+ if (dashboards.length === 0) {
95
+ return (
96
+ <div className="rounded-lg border border-dashed border-gray-200 bg-gray-50 p-8 text-center">
97
+ <p className="text-sm font-medium text-gray-700 mb-1">No Grafana dashboard configured</p>
98
+ <p className="text-xs text-gray-500 mb-4">
99
+ Add the annotation{' '}
100
+ <code className="rounded bg-gray-100 px-1 py-0.5">forgeportal.dev/grafana-dashboard-url</code>{' '}
101
+ to your <code className="rounded bg-gray-100 px-1 py-0.5">entity.yaml</code>.
102
+ </p>
103
+ <pre className="mx-auto max-w-lg rounded bg-gray-800 p-3 text-left text-xs text-green-300">
104
+ {`metadata:\n annotations:\n forgeportal.dev/grafana-dashboard-url: https://grafana.internal/d/abc123/service-overview\n forgeportal.dev/grafana-variable-name: service # optional`}
105
+ </pre>
106
+ </div>
107
+ );
108
+ }
109
+
110
+ const activeDash = dashboards[activeDashId] ?? dashboards[0]!;
111
+ const { from, to } = TIME_RANGES[timeRange];
112
+ const embedUrl = buildEmbedUrl(activeDash, { from, to, varName, varValue });
113
+ const openUrl = buildOpenUrl(activeDash);
114
+
115
+ return (
116
+ <div className="space-y-3">
117
+ {/* Toolbar */}
118
+ <div className="flex items-center justify-between flex-wrap gap-2">
119
+ {/* Multi-dashboard tab strip */}
120
+ {dashboards.length > 1 && (
121
+ <div className="flex gap-1 border border-gray-200 rounded-md p-0.5 bg-gray-50">
122
+ {dashboards.map((url, i) => (
123
+ <button
124
+ key={i}
125
+ onClick={() => setActiveDashId(i)}
126
+ className={`px-3 py-1 text-xs font-medium rounded transition-colors ${
127
+ activeDashId === i
128
+ ? 'bg-white shadow-sm text-gray-800'
129
+ : 'text-gray-500 hover:text-gray-700'
130
+ }`}
131
+ >
132
+ {dashboardLabel(url, i)}
133
+ </button>
134
+ ))}
135
+ </div>
136
+ )}
137
+
138
+ <div className="flex items-center gap-2 ml-auto">
139
+ {/* Time range selector */}
140
+ <div className="flex items-center gap-0.5 rounded-md border border-gray-200 bg-gray-50 p-0.5">
141
+ {(Object.keys(TIME_RANGES) as TimeRange[]).map((tr) => (
142
+ <button
143
+ key={tr}
144
+ onClick={() => setTimeRange(tr)}
145
+ className={`px-2.5 py-1 text-xs font-medium rounded transition-colors ${
146
+ timeRange === tr
147
+ ? 'bg-white shadow-sm text-indigo-600'
148
+ : 'text-gray-500 hover:text-gray-700'
149
+ }`}
150
+ >
151
+ {TIME_RANGES[tr].label}
152
+ </button>
153
+ ))}
154
+ </div>
155
+
156
+ {/* Open in Grafana */}
157
+ <a
158
+ href={openUrl}
159
+ target="_blank"
160
+ rel="noreferrer"
161
+ className="inline-flex items-center gap-1.5 rounded-md border border-gray-200 bg-white px-3 py-1.5 text-xs font-medium text-gray-700 hover:bg-gray-50 transition-colors"
162
+ >
163
+ <svg className="h-3.5 w-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
164
+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2}
165
+ d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14" />
166
+ </svg>
167
+ Open in Grafana
168
+ </a>
169
+ </div>
170
+ </div>
171
+
172
+ {/* Variable info badge */}
173
+ {varName && (
174
+ <p className="text-xs text-gray-400">
175
+ Variable injection: <code className="rounded bg-gray-100 px-1">var-{varName}={varValue}</code>
176
+ </p>
177
+ )}
178
+
179
+ {/* Embedded dashboard iframe */}
180
+ <div className="relative w-full overflow-hidden rounded-lg border border-gray-200 bg-gray-50"
181
+ style={{ paddingBottom: '56.25%' /* 16:9 */ }}>
182
+ <iframe
183
+ key={`${embedUrl}`}
184
+ src={embedUrl}
185
+ className="absolute inset-0 h-full w-full"
186
+ title={`Grafana — ${dashboardLabel(activeDash, activeDashId)}`}
187
+ frameBorder="0"
188
+ allowFullScreen
189
+ />
190
+
191
+ {/* Overlay shown while iframe may be blocked by CSP */}
192
+ <noscript>
193
+ <div className="absolute inset-0 flex flex-col items-center justify-center bg-gray-50 text-center p-6">
194
+ <p className="text-sm font-medium text-gray-700 mb-2">Dashboard cannot be embedded</p>
195
+ <p className="text-xs text-gray-500 mb-4">
196
+ Your Grafana instance may require authentication or have CSP restrictions.
197
+ </p>
198
+ <a
199
+ href={openUrl}
200
+ target="_blank"
201
+ rel="noreferrer"
202
+ className="inline-flex items-center gap-1.5 rounded-md bg-indigo-600 px-3 py-1.5 text-xs font-medium text-white hover:bg-indigo-700"
203
+ >
204
+ Open in Grafana
205
+ </a>
206
+ </div>
207
+ </noscript>
208
+ </div>
209
+
210
+ <p className="text-xs text-gray-400 text-right">
211
+ Grafana · {dashboardLabel(activeDash, activeDashId)} · {from} → {to}
212
+ </p>
213
+ </div>
214
+ );
215
+ }
package/src/ui.ts ADDED
@@ -0,0 +1,19 @@
1
+ import type { ForgePluginSDK } from '@forgeportal/plugin-sdk';
2
+ import { GrafanaTab } from './GrafanaTab.js';
3
+
4
+ /**
5
+ * UI entry point for the Grafana plugin.
6
+ * Called by the ForgePortal UI shell at startup.
7
+ *
8
+ * Registration in apps/ui/src/plugins/index.ts:
9
+ * import { registerPlugin as registerGrafana } from '@forgeportal/plugin-grafana/ui';
10
+ * registerPluginById('grafana', registerGrafana);
11
+ */
12
+ export function registerPlugin(sdk: ForgePluginSDK): void {
13
+ sdk.registerEntityTab({
14
+ id: 'grafana-tab',
15
+ title: 'Grafana',
16
+ component: GrafanaTab,
17
+ // No appliesTo — the tab shows a helpful config message when annotation is missing
18
+ });
19
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "rootDir": "src",
6
+ "jsx": "react-jsx",
7
+ "declaration": true,
8
+ "declarationMap": true
9
+ },
10
+ "include": ["src"]
11
+ }