GetStartedWithApp.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import React from 'react';
  2. import { PluginMeta } from '@grafana/data';
  3. import { Button } from '@grafana/ui';
  4. import { updatePluginSettings } from '../../api';
  5. import { usePluginConfig } from '../../hooks/usePluginConfig';
  6. import { CatalogPlugin } from '../../types';
  7. type Props = {
  8. plugin: CatalogPlugin;
  9. };
  10. export function GetStartedWithApp({ plugin }: Props): React.ReactElement | null {
  11. const { value: pluginConfig } = usePluginConfig(plugin);
  12. if (!pluginConfig) {
  13. return null;
  14. }
  15. const { enabled, jsonData } = pluginConfig?.meta;
  16. const enable = () =>
  17. updatePluginSettingsAndReload(plugin.id, {
  18. enabled: true,
  19. pinned: true,
  20. jsonData,
  21. });
  22. const disable = () => {
  23. updatePluginSettingsAndReload(plugin.id, {
  24. enabled: false,
  25. pinned: false,
  26. jsonData,
  27. });
  28. };
  29. return (
  30. <>
  31. {!enabled && (
  32. <Button variant="primary" onClick={enable}>
  33. Enable
  34. </Button>
  35. )}
  36. {enabled && (
  37. <Button variant="destructive" onClick={disable}>
  38. Disable
  39. </Button>
  40. )}
  41. </>
  42. );
  43. }
  44. const updatePluginSettingsAndReload = async (id: string, data: Partial<PluginMeta>) => {
  45. try {
  46. await updatePluginSettings(id, data);
  47. // Reloading the page as the plugin meta changes made here wouldn't be propagated throughout the app.
  48. window.location.reload();
  49. } catch (e) {
  50. console.error('Error while updating the plugin', e);
  51. }
  52. };