@grafana/runtime
Version:
Grafana Runtime Library
1 lines • 2.33 kB
Source Map (JSON)
{"version":3,"file":"usePluginInteractionReporter.mjs","sources":["../../../../src/analytics/plugins/usePluginInteractionReporter.ts"],"sourcesContent":["import { useMemo } from 'react';\n\nimport { isDataSourcePluginContext, usePluginContext } from '@grafana/data';\n\nimport { reportInteraction } from '../utils';\n\nimport { createDataSourcePluginEventProperties, createPluginEventProperties } from './eventProperties';\n\nconst namePrefix = 'grafana_plugin_';\n\nexport function usePluginInteractionReporter(): typeof reportInteraction {\n const context = usePluginContext();\n\n return useMemo(() => {\n // Happens when the hook is not used inside a plugin (e.g. in core Grafana)\n if (!context) {\n throw new Error(\n `No PluginContext found. The usePluginInteractionReporter() hook can only be used from a plugin.`\n );\n }\n\n const info = isDataSourcePluginContext(context)\n ? createDataSourcePluginEventProperties(context.instanceSettings)\n : createPluginEventProperties(context.meta);\n\n return (interactionName: string, properties?: Record<string, unknown>) => {\n if (!validInteractionName(interactionName)) {\n throw new Error(`Interactions reported in plugins should start with: \"${namePrefix}\".`);\n }\n return reportInteraction(interactionName, { ...properties, ...info });\n };\n }, [context]);\n}\n\nfunction validInteractionName(interactionName: string): boolean {\n return interactionName.startsWith(namePrefix) && interactionName.length > namePrefix.length;\n}\n"],"names":[],"mappings":";;;;;;AAQA,MAAM,UAAA,GAAa,iBAAA;AAEZ,SAAS,4BAAA,GAAyD;AACvE,EAAA,MAAM,UAAU,gBAAA,EAAiB;AAEjC,EAAA,OAAO,QAAQ,MAAM;AAEnB,IAAA,IAAI,CAAC,OAAA,EAAS;AACZ,MAAA,MAAM,IAAI,KAAA;AAAA,QACR,CAAA,+FAAA;AAAA,OACF;AAAA,IACF;AAEA,IAAA,MAAM,IAAA,GAAO,yBAAA,CAA0B,OAAO,CAAA,GAC1C,qCAAA,CAAsC,QAAQ,gBAAgB,CAAA,GAC9D,2BAAA,CAA4B,OAAA,CAAQ,IAAI,CAAA;AAE5C,IAAA,OAAO,CAAC,iBAAyB,UAAA,KAAyC;AACxE,MAAA,IAAI,CAAC,oBAAA,CAAqB,eAAe,CAAA,EAAG;AAC1C,QAAA,MAAM,IAAI,KAAA,CAAM,CAAA,qDAAA,EAAwD,UAAU,CAAA,EAAA,CAAI,CAAA;AAAA,MACxF;AACA,MAAA,OAAO,kBAAkB,eAAA,EAAiB,EAAE,GAAG,UAAA,EAAY,GAAG,MAAM,CAAA;AAAA,IACtE,CAAA;AAAA,EACF,CAAA,EAAG,CAAC,OAAO,CAAC,CAAA;AACd;AAEA,SAAS,qBAAqB,eAAA,EAAkC;AAC9D,EAAA,OAAO,gBAAgB,UAAA,CAAW,UAAU,CAAA,IAAK,eAAA,CAAgB,SAAS,UAAA,CAAW,MAAA;AACvF;;;;"}