123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- import { css } from '@emotion/css';
- import React, { ReactElement, useState } from 'react';
- import { GrafanaTheme2 } from '@grafana/data';
- import { config } from '@grafana/runtime';
- import { Icon, Link, useStyles2 } from '@grafana/ui';
- import { getPanelPluginNotFound } from 'app/features/panel/components/PanelPluginError';
- import { PanelTypeCard } from 'app/features/panel/components/VizTypePicker/PanelTypeCard';
- import { LibraryElementDTO } from '../../types';
- import { DeleteLibraryPanelModal } from '../DeleteLibraryPanelModal/DeleteLibraryPanelModal';
- export interface LibraryPanelCardProps {
- libraryPanel: LibraryElementDTO;
- onClick: (panel: LibraryElementDTO) => void;
- onDelete?: (panel: LibraryElementDTO) => void;
- showSecondaryActions?: boolean;
- }
- export const LibraryPanelCard: React.FC<LibraryPanelCardProps & { children?: JSX.Element | JSX.Element[] }> = ({
- libraryPanel,
- onClick,
- onDelete,
- showSecondaryActions,
- }) => {
- const [showDeletionModal, setShowDeletionModal] = useState(false);
- const onDeletePanel = () => {
- onDelete?.(libraryPanel);
- setShowDeletionModal(false);
- };
- const panelPlugin = config.panels[libraryPanel.model.type] ?? getPanelPluginNotFound(libraryPanel.model.type).meta;
- return (
- <>
- <PanelTypeCard
- isCurrent={false}
- title={libraryPanel.name}
- description={libraryPanel.description}
- plugin={panelPlugin}
- onClick={() => onClick?.(libraryPanel)}
- onDelete={showSecondaryActions ? () => setShowDeletionModal(true) : undefined}
- >
- <FolderLink libraryPanel={libraryPanel} />
- </PanelTypeCard>
- {showDeletionModal && (
- <DeleteLibraryPanelModal
- libraryPanel={libraryPanel}
- onConfirm={onDeletePanel}
- onDismiss={() => setShowDeletionModal(false)}
- />
- )}
- </>
- );
- };
- interface FolderLinkProps {
- libraryPanel: LibraryElementDTO;
- }
- function FolderLink({ libraryPanel }: FolderLinkProps): ReactElement | null {
- const styles = useStyles2(getStyles);
- if (!libraryPanel.meta.folderUid && !libraryPanel.meta.folderName) {
- return null;
- }
- if (!libraryPanel.meta.folderUid) {
- return (
- <span className={styles.metaContainer}>
- <Icon name={'folder'} size="sm" />
- <span>{libraryPanel.meta.folderName}</span>
- </span>
- );
- }
- return (
- <span className={styles.metaContainer}>
- <Link href={`/dashboards/f/${libraryPanel.meta.folderUid}`}>
- <Icon name={'folder-upload'} size="sm" />
- <span>{libraryPanel.meta.folderName}</span>
- </Link>
- </span>
- );
- }
- function getStyles(theme: GrafanaTheme2) {
- return {
- metaContainer: css`
- display: flex;
- align-items: center;
- color: ${theme.colors.text.secondary};
- font-size: ${theme.typography.bodySmall.fontSize};
- padding-top: ${theme.spacing(0.5)};
- svg {
- margin-right: ${theme.spacing(0.5)};
- margin-bottom: 3px;
- }
- `,
- };
- }
|