import { css } from '@emotion/css';
import pluralize from 'pluralize';
import React, { ReactNode } from 'react';
import { Draggable } from 'react-beautiful-dnd';
import { GrafanaTheme2 } from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors';
import { Icon, IconButton, useStyles2, Spinner, IconName } from '@grafana/ui';
import { TagBadge } from 'app/core/components/TagFilter/TagBadge';
import { PlaylistItem } from './types';
interface Props {
items: PlaylistItem[];
onDelete: (idx: number) => void;
}
export const PlaylistTableRows = ({ items, onDelete }: Props) => {
const styles = useStyles2(getStyles);
if (!items?.length) {
return (
Playlist is empty. Add dashboards below.
);
}
const renderItem = (item: PlaylistItem) => {
let icon: IconName = item.type === 'dashboard_by_tag' ? 'apps' : 'tag-alt';
const info: ReactNode[] = [];
const first = item.dashboards?.[0];
if (!item.dashboards) {
info.push();
} else if (item.type === 'dashboard_by_tag') {
info.push();
if (!first) {
icon = 'exclamation-triangle';
info.push( No dashboards found);
} else {
info.push( {pluralize('dashboard', item.dashboards.length, true)});
}
} else if (first) {
info.push(
item.dashboards.length > 1 ? (
Multiple items found: ${item.value}
) : (
{first.name ?? item.value}
)
);
} else {
icon = 'exclamation-triangle';
info.push( Not found: {item.value});
}
return (
<>
{info}
>
);
};
return (
<>
{items.map((item, index) => (
{(provided) => (
{renderItem(item)}
onDelete(index)}
aria-label={selectors.pages.PlaylistForm.itemDelete}
type="button"
/>
)}
))}
>
);
};
function getStyles(theme: GrafanaTheme2) {
return {
row: css`
padding: 6px;
background: ${theme.colors.background.secondary};
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 3px;
border: 1px solid ${theme.colors.border.medium};
&:hover {
border: 1px solid ${theme.colors.border.strong};
}
`,
rightMargin: css`
margin-right: 5px;
`,
actions: css`
align-items: center;
justify-content: center;
display: flex;
`,
settings: css`
label: settings;
text-align: right;
`,
};
}