@grafana/plugin-compat 0.4.0 → 0.5.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/CHANGELOG.md +6 -0
- package/dist/cjs/apps.cjs +6 -0
- package/dist/cjs/apps.cjs.map +1 -1
- package/dist/cjs/datasources/useDataSourceInstance.cjs +30 -0
- package/dist/cjs/datasources/useDataSourceInstance.cjs.map +1 -0
- package/dist/cjs/datasources.cjs +2 -0
- package/dist/cjs/datasources.cjs.map +1 -1
- package/dist/esm/apps.mjs +8 -2
- package/dist/esm/apps.mjs.map +1 -1
- package/dist/esm/datasources/useDataSourceInstance.mjs +26 -0
- package/dist/esm/datasources/useDataSourceInstance.mjs.map +1 -0
- package/dist/esm/datasources.mjs +1 -0
- package/dist/esm/datasources.mjs.map +1 -1
- package/dist/types/datasources/useDataSourceInstance.d.ts +13 -0
- package/dist/types/datasources.d.ts +1 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
# 0.5.0 (2026-10-12)
|
|
2
|
+
|
|
3
|
+
`getPluginSettings` and `updateAppPluginSettings` now use the host's stable `@grafana/runtime` exports when available, then `@grafana/runtime/unstable`, then the legacy `getBackendSrv()` plugin-settings endpoints, so they keep working once the host removes them from `unstable`.
|
|
4
|
+
|
|
5
|
+
Add `useDataSourceInstance` to `@grafana/plugin-compat/datasources`. It uses the host's stable `@grafana/runtime` hook when available, then `@grafana/runtime/unstable`, then resolves through `getDataSourceInstance`, so it works on hosts without the async data source hooks.
|
|
6
|
+
|
|
1
7
|
# 0.4.0 (2026-10-09)
|
|
2
8
|
|
|
3
9
|
The data source functions now use the host's stable `@grafana/runtime` exports when available, then `@grafana/runtime/unstable`, then the legacy `getDataSourceSrv()` APIs, so they keep working on hosts that only expose the async APIs under `unstable`.
|
package/dist/cjs/apps.cjs
CHANGED
|
@@ -7,6 +7,9 @@ var unstable = require('@grafana/runtime/unstable');
|
|
|
7
7
|
|
|
8
8
|
"use strict";
|
|
9
9
|
async function getPluginSettings(pluginId, showErrorAlert = false) {
|
|
10
|
+
if (typeof runtime.getPluginSettings === "function") {
|
|
11
|
+
return runtime.getPluginSettings(pluginId, showErrorAlert);
|
|
12
|
+
}
|
|
10
13
|
if (typeof unstable.getPluginSettings === "function") {
|
|
11
14
|
return unstable.getPluginSettings(pluginId, showErrorAlert);
|
|
12
15
|
}
|
|
@@ -19,6 +22,9 @@ async function backwardsCompatibleGetPluginSettings(pluginId, showErrorAlert = f
|
|
|
19
22
|
});
|
|
20
23
|
}
|
|
21
24
|
async function updateAppPluginSettings(pluginId, data) {
|
|
25
|
+
if (typeof runtime.updateAppPluginSettings === "function") {
|
|
26
|
+
return runtime.updateAppPluginSettings(pluginId, data);
|
|
27
|
+
}
|
|
22
28
|
if (typeof unstable.updateAppPluginSettings === "function") {
|
|
23
29
|
return unstable.updateAppPluginSettings(pluginId, data);
|
|
24
30
|
}
|
package/dist/cjs/apps.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apps.cjs","sources":["../../src/apps.ts"],"sourcesContent":["import { type PluginMeta } from '@grafana/data';\nimport {
|
|
1
|
+
{"version":3,"file":"apps.cjs","sources":["../../src/apps.ts"],"sourcesContent":["import { type PluginMeta } from '@grafana/data';\nimport {\n getBackendSrv,\n getPluginSettings as stableGetPluginSettings,\n updateAppPluginSettings as stableUpdateAppPluginSettings,\n} from '@grafana/runtime';\nimport {\n getPluginSettings as unstableGetPluginSettings,\n updateAppPluginSettings as unstableUpdateAppPluginSettings,\n} from '@grafana/runtime/unstable';\n\nexport async function getPluginSettings(pluginId: string, showErrorAlert = false): Promise<PluginMeta> {\n if (typeof stableGetPluginSettings === 'function') {\n return stableGetPluginSettings(pluginId, showErrorAlert);\n }\n\n if (typeof unstableGetPluginSettings === 'function') {\n return unstableGetPluginSettings(pluginId, showErrorAlert);\n }\n\n return backwardsCompatibleGetPluginSettings(pluginId, showErrorAlert);\n}\n\nasync function backwardsCompatibleGetPluginSettings(pluginId: string, showErrorAlert = false): Promise<PluginMeta> {\n return getBackendSrv().get(`/api/plugins/${pluginId}/settings`, undefined, undefined, {\n showErrorAlert,\n validatePath: true,\n });\n}\n\nexport async function updateAppPluginSettings(pluginId: string, data: Partial<PluginMeta>): Promise<PluginMeta> {\n if (typeof stableUpdateAppPluginSettings === 'function') {\n return stableUpdateAppPluginSettings(pluginId, data);\n }\n\n if (typeof unstableUpdateAppPluginSettings === 'function') {\n return unstableUpdateAppPluginSettings(pluginId, data);\n }\n\n return backwardsCompatibleUpdatePluginSettings(pluginId, data);\n}\n\nasync function backwardsCompatibleUpdatePluginSettings(\n pluginId: string,\n data: Partial<PluginMeta>\n): Promise<PluginMeta> {\n await getBackendSrv().post(`/api/plugins/${pluginId}/settings`, data, { validatePath: true });\n return backwardsCompatibleGetPluginSettings(pluginId);\n}\n"],"names":["stableGetPluginSettings","unstableGetPluginSettings","getBackendSrv","stableUpdateAppPluginSettings","unstableUpdateAppPluginSettings"],"mappings":";;;;;;;;AAWA,eAAsB,iBAAA,CAAkB,QAAA,EAAkB,cAAA,GAAiB,KAAA,EAA4B;AACrG,EAAA,IAAI,OAAOA,8BAA4B,UAAA,EAAY;AACjD,IAAA,OAAOA,yBAAA,CAAwB,UAAU,cAAc,CAAA;AAAA,EACzD;AAEA,EAAA,IAAI,OAAOC,+BAA8B,UAAA,EAAY;AACnD,IAAA,OAAOA,0BAAA,CAA0B,UAAU,cAAc,CAAA;AAAA,EAC3D;AAEA,EAAA,OAAO,oCAAA,CAAqC,UAAU,cAAc,CAAA;AACtE;AAEA,eAAe,oCAAA,CAAqC,QAAA,EAAkB,cAAA,GAAiB,KAAA,EAA4B;AACjH,EAAA,OAAOC,uBAAc,CAAE,GAAA,CAAI,gBAAgB,QAAQ,CAAA,SAAA,CAAA,EAAa,QAAW,KAAA,CAAA,EAAW;AAAA,IACpF,cAAA;AAAA,IACA,YAAA,EAAc;AAAA,GACf,CAAA;AACH;AAEA,eAAsB,uBAAA,CAAwB,UAAkB,IAAA,EAAgD;AAC9G,EAAA,IAAI,OAAOC,oCAAkC,UAAA,EAAY;AACvD,IAAA,OAAOA,+BAAA,CAA8B,UAAU,IAAI,CAAA;AAAA,EACrD;AAEA,EAAA,IAAI,OAAOC,qCAAoC,UAAA,EAAY;AACzD,IAAA,OAAOA,gCAAA,CAAgC,UAAU,IAAI,CAAA;AAAA,EACvD;AAEA,EAAA,OAAO,uCAAA,CAAwC,UAAU,IAAI,CAAA;AAC/D;AAEA,eAAe,uCAAA,CACb,UACA,IAAA,EACqB;AACrB,EAAA,MAAMF,qBAAA,EAAc,CAAE,IAAA,CAAK,CAAA,aAAA,EAAgB,QAAQ,aAAa,IAAA,EAAM,EAAE,YAAA,EAAc,IAAA,EAAM,CAAA;AAC5F,EAAA,OAAO,qCAAqC,QAAQ,CAAA;AACtD;;;;;"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var react = require('react');
|
|
6
|
+
var runtime = require('@grafana/runtime');
|
|
7
|
+
var unstable = require('@grafana/runtime/unstable');
|
|
8
|
+
var getDataSourceInstance = require('./getDataSourceInstance.cjs');
|
|
9
|
+
|
|
10
|
+
"use strict";
|
|
11
|
+
const useDataSourceInstance = typeof runtime.useDataSourceInstance === "function" ? runtime.useDataSourceInstance : typeof unstable.useDataSourceInstance === "function" ? unstable.useDataSourceInstance : useBackwardsCompatibleDataSourceInstance;
|
|
12
|
+
function useBackwardsCompatibleDataSourceInstance(ref) {
|
|
13
|
+
const [result, setResult] = react.useState({ isLoading: true });
|
|
14
|
+
const refKey = JSON.stringify(ref != null ? ref : null);
|
|
15
|
+
react.useEffect(() => {
|
|
16
|
+
let isCurrent = true;
|
|
17
|
+
setResult((previous) => ({ ...previous, isLoading: true }));
|
|
18
|
+
getDataSourceInstance.getDataSourceInstance(ref).then(
|
|
19
|
+
(dataSource) => isCurrent && setResult({ isLoading: false, dataSource }),
|
|
20
|
+
(error) => isCurrent && setResult({ isLoading: false, error })
|
|
21
|
+
);
|
|
22
|
+
return () => {
|
|
23
|
+
isCurrent = false;
|
|
24
|
+
};
|
|
25
|
+
}, [refKey]);
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
exports.useDataSourceInstance = useDataSourceInstance;
|
|
30
|
+
//# sourceMappingURL=useDataSourceInstance.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDataSourceInstance.cjs","sources":["../../../src/datasources/useDataSourceInstance.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nimport { type DataSourceApi } from '@grafana/data';\nimport { useDataSourceInstance as stableUseDataSourceInstance } from '@grafana/runtime';\nimport { useDataSourceInstance as unstableUseDataSourceInstance } from '@grafana/runtime/unstable';\n\nimport { getDataSourceInstance } from './getDataSourceInstance';\nimport { type Ref } from './types';\n\n/** Declared here because `@grafana/runtime` lacks it on older supported hosts. */\nexport interface UseDataSourceInstanceResult {\n isLoading: boolean;\n error?: Error;\n dataSource?: DataSourceApi;\n}\n\n/**\n * Uses the host hook when available. The fallback re-resolves only when `ref` changes, not on data source cache\n * changes. Picked once at load so the hook order stays stable.\n */\nexport const useDataSourceInstance: (ref?: Ref) => UseDataSourceInstanceResult =\n typeof stableUseDataSourceInstance === 'function'\n ? stableUseDataSourceInstance\n : typeof unstableUseDataSourceInstance === 'function'\n ? unstableUseDataSourceInstance\n : useBackwardsCompatibleDataSourceInstance;\n\nfunction useBackwardsCompatibleDataSourceInstance(ref?: Ref): UseDataSourceInstanceResult {\n const [result, setResult] = useState<UseDataSourceInstanceResult>({ isLoading: true });\n // Same key as the host hook, so inline ref objects don't refetch and `null` matches `undefined`.\n const refKey = JSON.stringify(ref ?? null);\n\n useEffect(() => {\n let isCurrent = true;\n setResult((previous) => ({ ...previous, isLoading: true }));\n\n getDataSourceInstance(ref).then(\n (dataSource) => isCurrent && setResult({ isLoading: false, dataSource }),\n (error: Error) => isCurrent && setResult({ isLoading: false, error })\n );\n\n return () => {\n isCurrent = false;\n };\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [refKey]);\n\n return result;\n}\n"],"names":["stableUseDataSourceInstance","unstableUseDataSourceInstance","useState","useEffect","getDataSourceInstance"],"mappings":";;;;;;;;;;AAoBO,MAAM,qBAAA,GACX,OAAOA,6BAAA,KAAgC,UAAA,GACnCA,gCACA,OAAOC,8BAAA,KAAkC,aACvCA,8BAAA,GACA;AAER,SAAS,yCAAyC,GAAA,EAAwC;AACxF,EAAA,MAAM,CAAC,QAAQ,SAAS,CAAA,GAAIC,eAAsC,EAAE,SAAA,EAAW,MAAM,CAAA;AAErF,EAAA,MAAM,MAAA,GAAS,IAAA,CAAK,SAAA,CAAU,GAAA,IAAA,IAAA,GAAA,GAAA,GAAO,IAAI,CAAA;AAEzC,EAAAC,eAAA,CAAU,MAAM;AACd,IAAA,IAAI,SAAA,GAAY,IAAA;AAChB,IAAA,SAAA,CAAU,CAAC,QAAA,MAAc,EAAE,GAAG,QAAA,EAAU,SAAA,EAAW,MAAK,CAAE,CAAA;AAE1D,IAAAC,2CAAA,CAAsB,GAAG,CAAA,CAAE,IAAA;AAAA,MACzB,CAAC,eAAe,SAAA,IAAa,SAAA,CAAU,EAAE,SAAA,EAAW,KAAA,EAAO,YAAY,CAAA;AAAA,MACvE,CAAC,UAAiB,SAAA,IAAa,SAAA,CAAU,EAAE,SAAA,EAAW,KAAA,EAAO,OAAO;AAAA,KACtE;AAEA,IAAA,OAAO,MAAM;AACX,MAAA,SAAA,GAAY,KAAA;AAAA,IACd,CAAA;AAAA,EAEF,CAAA,EAAG,CAAC,MAAM,CAAC,CAAA;AAEX,EAAA,OAAO,MAAA;AACT;;;;"}
|
package/dist/cjs/datasources.cjs
CHANGED
|
@@ -7,6 +7,7 @@ var getDataSourceInstanceList = require('./datasources/getDataSourceInstanceList
|
|
|
7
7
|
var getDataSourceInstanceSettings = require('./datasources/getDataSourceInstanceSettings.cjs');
|
|
8
8
|
var getDefaultDataSourceInstanceListItem = require('./datasources/getDefaultDataSourceInstanceListItem.cjs');
|
|
9
9
|
var registerRuntimeDataSourceInstance = require('./datasources/registerRuntimeDataSourceInstance.cjs');
|
|
10
|
+
var useDataSourceInstance = require('./datasources/useDataSourceInstance.cjs');
|
|
10
11
|
var useDefaultDataSourceInstanceListItem = require('./datasources/useDefaultDataSourceInstanceListItem.cjs');
|
|
11
12
|
|
|
12
13
|
"use strict";
|
|
@@ -16,5 +17,6 @@ exports.getDataSourceInstanceList = getDataSourceInstanceList.getDataSourceInsta
|
|
|
16
17
|
exports.getDataSourceInstanceSettings = getDataSourceInstanceSettings.getDataSourceInstanceSettings;
|
|
17
18
|
exports.getDefaultDataSourceInstanceListItem = getDefaultDataSourceInstanceListItem.getDefaultDataSourceInstanceListItem;
|
|
18
19
|
exports.registerRuntimeDataSourceInstance = registerRuntimeDataSourceInstance.registerRuntimeDataSourceInstance;
|
|
20
|
+
exports.useDataSourceInstance = useDataSourceInstance.useDataSourceInstance;
|
|
19
21
|
exports.useDefaultDataSourceInstanceListItem = useDefaultDataSourceInstanceListItem.useDefaultDataSourceInstanceListItem;
|
|
20
22
|
//# sourceMappingURL=datasources.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"datasources.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"datasources.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/esm/apps.mjs
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { getBackendSrv } from '@grafana/runtime';
|
|
2
|
-
import { getPluginSettings as getPluginSettings$
|
|
1
|
+
import { getPluginSettings as getPluginSettings$1, getBackendSrv, updateAppPluginSettings as updateAppPluginSettings$1 } from '@grafana/runtime';
|
|
2
|
+
import { getPluginSettings as getPluginSettings$2, updateAppPluginSettings as updateAppPluginSettings$2 } from '@grafana/runtime/unstable';
|
|
3
3
|
|
|
4
4
|
"use strict";
|
|
5
5
|
async function getPluginSettings(pluginId, showErrorAlert = false) {
|
|
6
6
|
if (typeof getPluginSettings$1 === "function") {
|
|
7
7
|
return getPluginSettings$1(pluginId, showErrorAlert);
|
|
8
8
|
}
|
|
9
|
+
if (typeof getPluginSettings$2 === "function") {
|
|
10
|
+
return getPluginSettings$2(pluginId, showErrorAlert);
|
|
11
|
+
}
|
|
9
12
|
return backwardsCompatibleGetPluginSettings(pluginId, showErrorAlert);
|
|
10
13
|
}
|
|
11
14
|
async function backwardsCompatibleGetPluginSettings(pluginId, showErrorAlert = false) {
|
|
@@ -18,6 +21,9 @@ async function updateAppPluginSettings(pluginId, data) {
|
|
|
18
21
|
if (typeof updateAppPluginSettings$1 === "function") {
|
|
19
22
|
return updateAppPluginSettings$1(pluginId, data);
|
|
20
23
|
}
|
|
24
|
+
if (typeof updateAppPluginSettings$2 === "function") {
|
|
25
|
+
return updateAppPluginSettings$2(pluginId, data);
|
|
26
|
+
}
|
|
21
27
|
return backwardsCompatibleUpdatePluginSettings(pluginId, data);
|
|
22
28
|
}
|
|
23
29
|
async function backwardsCompatibleUpdatePluginSettings(pluginId, data) {
|
package/dist/esm/apps.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apps.mjs","sources":["../../src/apps.ts"],"sourcesContent":["import { type PluginMeta } from '@grafana/data';\nimport {
|
|
1
|
+
{"version":3,"file":"apps.mjs","sources":["../../src/apps.ts"],"sourcesContent":["import { type PluginMeta } from '@grafana/data';\nimport {\n getBackendSrv,\n getPluginSettings as stableGetPluginSettings,\n updateAppPluginSettings as stableUpdateAppPluginSettings,\n} from '@grafana/runtime';\nimport {\n getPluginSettings as unstableGetPluginSettings,\n updateAppPluginSettings as unstableUpdateAppPluginSettings,\n} from '@grafana/runtime/unstable';\n\nexport async function getPluginSettings(pluginId: string, showErrorAlert = false): Promise<PluginMeta> {\n if (typeof stableGetPluginSettings === 'function') {\n return stableGetPluginSettings(pluginId, showErrorAlert);\n }\n\n if (typeof unstableGetPluginSettings === 'function') {\n return unstableGetPluginSettings(pluginId, showErrorAlert);\n }\n\n return backwardsCompatibleGetPluginSettings(pluginId, showErrorAlert);\n}\n\nasync function backwardsCompatibleGetPluginSettings(pluginId: string, showErrorAlert = false): Promise<PluginMeta> {\n return getBackendSrv().get(`/api/plugins/${pluginId}/settings`, undefined, undefined, {\n showErrorAlert,\n validatePath: true,\n });\n}\n\nexport async function updateAppPluginSettings(pluginId: string, data: Partial<PluginMeta>): Promise<PluginMeta> {\n if (typeof stableUpdateAppPluginSettings === 'function') {\n return stableUpdateAppPluginSettings(pluginId, data);\n }\n\n if (typeof unstableUpdateAppPluginSettings === 'function') {\n return unstableUpdateAppPluginSettings(pluginId, data);\n }\n\n return backwardsCompatibleUpdatePluginSettings(pluginId, data);\n}\n\nasync function backwardsCompatibleUpdatePluginSettings(\n pluginId: string,\n data: Partial<PluginMeta>\n): Promise<PluginMeta> {\n await getBackendSrv().post(`/api/plugins/${pluginId}/settings`, data, { validatePath: true });\n return backwardsCompatibleGetPluginSettings(pluginId);\n}\n"],"names":["stableGetPluginSettings","unstableGetPluginSettings","stableUpdateAppPluginSettings","unstableUpdateAppPluginSettings"],"mappings":";;;;AAWA,eAAsB,iBAAA,CAAkB,QAAA,EAAkB,cAAA,GAAiB,KAAA,EAA4B;AACrG,EAAA,IAAI,OAAOA,wBAA4B,UAAA,EAAY;AACjD,IAAA,OAAOA,mBAAA,CAAwB,UAAU,cAAc,CAAA;AAAA,EACzD;AAEA,EAAA,IAAI,OAAOC,wBAA8B,UAAA,EAAY;AACnD,IAAA,OAAOA,mBAAA,CAA0B,UAAU,cAAc,CAAA;AAAA,EAC3D;AAEA,EAAA,OAAO,oCAAA,CAAqC,UAAU,cAAc,CAAA;AACtE;AAEA,eAAe,oCAAA,CAAqC,QAAA,EAAkB,cAAA,GAAiB,KAAA,EAA4B;AACjH,EAAA,OAAO,eAAc,CAAE,GAAA,CAAI,gBAAgB,QAAQ,CAAA,SAAA,CAAA,EAAa,QAAW,KAAA,CAAA,EAAW;AAAA,IACpF,cAAA;AAAA,IACA,YAAA,EAAc;AAAA,GACf,CAAA;AACH;AAEA,eAAsB,uBAAA,CAAwB,UAAkB,IAAA,EAAgD;AAC9G,EAAA,IAAI,OAAOC,8BAAkC,UAAA,EAAY;AACvD,IAAA,OAAOA,yBAAA,CAA8B,UAAU,IAAI,CAAA;AAAA,EACrD;AAEA,EAAA,IAAI,OAAOC,8BAAoC,UAAA,EAAY;AACzD,IAAA,OAAOA,yBAAA,CAAgC,UAAU,IAAI,CAAA;AAAA,EACvD;AAEA,EAAA,OAAO,uCAAA,CAAwC,UAAU,IAAI,CAAA;AAC/D;AAEA,eAAe,uCAAA,CACb,UACA,IAAA,EACqB;AACrB,EAAA,MAAM,aAAA,EAAc,CAAE,IAAA,CAAK,CAAA,aAAA,EAAgB,QAAQ,aAAa,IAAA,EAAM,EAAE,YAAA,EAAc,IAAA,EAAM,CAAA;AAC5F,EAAA,OAAO,qCAAqC,QAAQ,CAAA;AACtD;;;;"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
import { useDataSourceInstance as useDataSourceInstance$1 } from '@grafana/runtime';
|
|
3
|
+
import { useDataSourceInstance as useDataSourceInstance$2 } from '@grafana/runtime/unstable';
|
|
4
|
+
import { getDataSourceInstance } from './getDataSourceInstance.mjs';
|
|
5
|
+
|
|
6
|
+
"use strict";
|
|
7
|
+
const useDataSourceInstance = typeof useDataSourceInstance$1 === "function" ? useDataSourceInstance$1 : typeof useDataSourceInstance$2 === "function" ? useDataSourceInstance$2 : useBackwardsCompatibleDataSourceInstance;
|
|
8
|
+
function useBackwardsCompatibleDataSourceInstance(ref) {
|
|
9
|
+
const [result, setResult] = useState({ isLoading: true });
|
|
10
|
+
const refKey = JSON.stringify(ref != null ? ref : null);
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
let isCurrent = true;
|
|
13
|
+
setResult((previous) => ({ ...previous, isLoading: true }));
|
|
14
|
+
getDataSourceInstance(ref).then(
|
|
15
|
+
(dataSource) => isCurrent && setResult({ isLoading: false, dataSource }),
|
|
16
|
+
(error) => isCurrent && setResult({ isLoading: false, error })
|
|
17
|
+
);
|
|
18
|
+
return () => {
|
|
19
|
+
isCurrent = false;
|
|
20
|
+
};
|
|
21
|
+
}, [refKey]);
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { useDataSourceInstance };
|
|
26
|
+
//# sourceMappingURL=useDataSourceInstance.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDataSourceInstance.mjs","sources":["../../../src/datasources/useDataSourceInstance.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nimport { type DataSourceApi } from '@grafana/data';\nimport { useDataSourceInstance as stableUseDataSourceInstance } from '@grafana/runtime';\nimport { useDataSourceInstance as unstableUseDataSourceInstance } from '@grafana/runtime/unstable';\n\nimport { getDataSourceInstance } from './getDataSourceInstance';\nimport { type Ref } from './types';\n\n/** Declared here because `@grafana/runtime` lacks it on older supported hosts. */\nexport interface UseDataSourceInstanceResult {\n isLoading: boolean;\n error?: Error;\n dataSource?: DataSourceApi;\n}\n\n/**\n * Uses the host hook when available. The fallback re-resolves only when `ref` changes, not on data source cache\n * changes. Picked once at load so the hook order stays stable.\n */\nexport const useDataSourceInstance: (ref?: Ref) => UseDataSourceInstanceResult =\n typeof stableUseDataSourceInstance === 'function'\n ? stableUseDataSourceInstance\n : typeof unstableUseDataSourceInstance === 'function'\n ? unstableUseDataSourceInstance\n : useBackwardsCompatibleDataSourceInstance;\n\nfunction useBackwardsCompatibleDataSourceInstance(ref?: Ref): UseDataSourceInstanceResult {\n const [result, setResult] = useState<UseDataSourceInstanceResult>({ isLoading: true });\n // Same key as the host hook, so inline ref objects don't refetch and `null` matches `undefined`.\n const refKey = JSON.stringify(ref ?? null);\n\n useEffect(() => {\n let isCurrent = true;\n setResult((previous) => ({ ...previous, isLoading: true }));\n\n getDataSourceInstance(ref).then(\n (dataSource) => isCurrent && setResult({ isLoading: false, dataSource }),\n (error: Error) => isCurrent && setResult({ isLoading: false, error })\n );\n\n return () => {\n isCurrent = false;\n };\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [refKey]);\n\n return result;\n}\n"],"names":["stableUseDataSourceInstance","unstableUseDataSourceInstance"],"mappings":";;;;;;AAoBO,MAAM,qBAAA,GACX,OAAOA,uBAAA,KAAgC,UAAA,GACnCA,0BACA,OAAOC,uBAAA,KAAkC,aACvCA,uBAAA,GACA;AAER,SAAS,yCAAyC,GAAA,EAAwC;AACxF,EAAA,MAAM,CAAC,QAAQ,SAAS,CAAA,GAAI,SAAsC,EAAE,SAAA,EAAW,MAAM,CAAA;AAErF,EAAA,MAAM,MAAA,GAAS,IAAA,CAAK,SAAA,CAAU,GAAA,IAAA,IAAA,GAAA,GAAA,GAAO,IAAI,CAAA;AAEzC,EAAA,SAAA,CAAU,MAAM;AACd,IAAA,IAAI,SAAA,GAAY,IAAA;AAChB,IAAA,SAAA,CAAU,CAAC,QAAA,MAAc,EAAE,GAAG,QAAA,EAAU,SAAA,EAAW,MAAK,CAAE,CAAA;AAE1D,IAAA,qBAAA,CAAsB,GAAG,CAAA,CAAE,IAAA;AAAA,MACzB,CAAC,eAAe,SAAA,IAAa,SAAA,CAAU,EAAE,SAAA,EAAW,KAAA,EAAO,YAAY,CAAA;AAAA,MACvE,CAAC,UAAiB,SAAA,IAAa,SAAA,CAAU,EAAE,SAAA,EAAW,KAAA,EAAO,OAAO;AAAA,KACtE;AAEA,IAAA,OAAO,MAAM;AACX,MAAA,SAAA,GAAY,KAAA;AAAA,IACd,CAAA;AAAA,EAEF,CAAA,EAAG,CAAC,MAAM,CAAC,CAAA;AAEX,EAAA,OAAO,MAAA;AACT;;;;"}
|
package/dist/esm/datasources.mjs
CHANGED
|
@@ -3,6 +3,7 @@ export { getDataSourceInstanceList } from './datasources/getDataSourceInstanceLi
|
|
|
3
3
|
export { getDataSourceInstanceSettings } from './datasources/getDataSourceInstanceSettings.mjs';
|
|
4
4
|
export { getDefaultDataSourceInstanceListItem } from './datasources/getDefaultDataSourceInstanceListItem.mjs';
|
|
5
5
|
export { registerRuntimeDataSourceInstance } from './datasources/registerRuntimeDataSourceInstance.mjs';
|
|
6
|
+
export { useDataSourceInstance } from './datasources/useDataSourceInstance.mjs';
|
|
6
7
|
export { useDefaultDataSourceInstanceListItem } from './datasources/useDefaultDataSourceInstanceListItem.mjs';
|
|
7
8
|
|
|
8
9
|
"use strict";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"datasources.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"datasources.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type DataSourceApi } from '@grafana/data';
|
|
2
|
+
import { type Ref } from './types';
|
|
3
|
+
/** Declared here because `@grafana/runtime` lacks it on older supported hosts. */
|
|
4
|
+
export interface UseDataSourceInstanceResult {
|
|
5
|
+
isLoading: boolean;
|
|
6
|
+
error?: Error;
|
|
7
|
+
dataSource?: DataSourceApi;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Uses the host hook when available. The fallback re-resolves only when `ref` changes, not on data source cache
|
|
11
|
+
* changes. Picked once at load so the hook order stays stable.
|
|
12
|
+
*/
|
|
13
|
+
export declare const useDataSourceInstance: (ref?: Ref) => UseDataSourceInstanceResult;
|
|
@@ -3,4 +3,5 @@ export { getDataSourceInstanceList } from './datasources/getDataSourceInstanceLi
|
|
|
3
3
|
export { getDataSourceInstanceSettings } from './datasources/getDataSourceInstanceSettings';
|
|
4
4
|
export { getDefaultDataSourceInstanceListItem } from './datasources/getDefaultDataSourceInstanceListItem';
|
|
5
5
|
export { registerRuntimeDataSourceInstance } from './datasources/registerRuntimeDataSourceInstance';
|
|
6
|
+
export { useDataSourceInstance, type UseDataSourceInstanceResult } from './datasources/useDataSourceInstance';
|
|
6
7
|
export { useDefaultDataSourceInstanceListItem, type UseDefaultDataSourceInstanceListItemResult, } from './datasources/useDefaultDataSourceInstanceListItem';
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"author": "Grafana Labs",
|
|
3
3
|
"license": "Apache-2.0",
|
|
4
4
|
"name": "@grafana/plugin-compat",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.5.0",
|
|
6
6
|
"description": "Compatibility shims that let Grafana plugins adopt new Grafana host APIs across a range of host versions",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"grafana",
|
|
@@ -72,5 +72,5 @@
|
|
|
72
72
|
"@grafana/schema": ">=12.0.0",
|
|
73
73
|
"react": ">=18"
|
|
74
74
|
},
|
|
75
|
-
"gitHead": "
|
|
75
|
+
"gitHead": "e664af940679e7ccfe3106ac9fb0d11a82f03ed2"
|
|
76
76
|
}
|