import { css } from '@emotion/css'; import { cloneDeep } from 'lodash'; import React, { useState } from 'react'; import { DataFrame, DataLink, GrafanaTheme2, VariableSuggestion } from '@grafana/data'; import { useStyles2 } from '../../../themes'; import { Button } from '../../Button/Button'; import { Modal } from '../../Modal/Modal'; import { DataLinkEditorModalContent } from './DataLinkEditorModalContent'; import { DataLinksListItem } from './DataLinksListItem'; interface DataLinksInlineEditorProps { links?: DataLink[]; onChange: (links: DataLink[]) => void; getSuggestions: () => VariableSuggestion[]; data: DataFrame[]; } export const DataLinksInlineEditor = ({ links, onChange, getSuggestions, data }: DataLinksInlineEditorProps) => { const [editIndex, setEditIndex] = useState(null); const [isNew, setIsNew] = useState(false); const styles = useStyles2(getDataLinksInlineEditorStyles); const linksSafe: DataLink[] = links ?? []; const isEditing = editIndex !== null; const onDataLinkChange = (index: number, link: DataLink) => { if (isNew) { if (link.title.trim() === '' && link.url.trim() === '') { setIsNew(false); setEditIndex(null); return; } else { setEditIndex(null); setIsNew(false); } } const update = cloneDeep(linksSafe); update[index] = link; onChange(update); setEditIndex(null); }; const onDataLinkAdd = () => { let update = cloneDeep(linksSafe); setEditIndex(update.length); setIsNew(true); }; const onDataLinkCancel = (index: number) => { if (isNew) { setIsNew(false); } setEditIndex(null); }; const onDataLinkRemove = (index: number) => { const update = cloneDeep(linksSafe); update.splice(index, 1); onChange(update); }; return ( <> {linksSafe.length > 0 && (
{linksSafe.map((l, i) => { return ( setEditIndex(i)} onRemove={() => onDataLinkRemove(i)} data={data} /> ); })}
)} {isEditing && editIndex !== null && ( { onDataLinkCancel(editIndex); }} > )} ); }; const getDataLinksInlineEditorStyles = (theme: GrafanaTheme2) => ({ wrapper: css({ marginBottom: theme.spacing(2), }), });