mirror of
https://github.com/grafana/grafana.git
synced 2024-11-29 12:14:08 -06:00
8232b6ebbc
* Fixes under public/app/plugins * Fixes under public/app/plugins/datasource * Fixes under public/app/features * Fixes under public/app/features * Fixes under public/app/features * Fixes under public/app/components * Fix PanelNotSupported test * Fix one more warning * Fix warning in usePanelSave * Fix traceview empty response * Azure monitor fixes * More fixes * Fix tests for azure monitor * Fixes after merging master * Add comment for disabled rules * Fixes after merging master * Fixes after merging master * Adress review comments * Fix azure tests * Address review feedbacks
83 lines
2.0 KiB
TypeScript
83 lines
2.0 KiB
TypeScript
import { useCallback, useState } from 'react';
|
|
|
|
import { PlaylistItem } from './types';
|
|
import { DashboardPickerItem } from '../../core/components/Select/DashboardPicker';
|
|
|
|
export function usePlaylistItems(playlistItems?: PlaylistItem[]) {
|
|
const [items, setItems] = useState<PlaylistItem[]>(playlistItems ?? []);
|
|
|
|
const addById = useCallback(
|
|
(dashboard: DashboardPickerItem) => {
|
|
if (items.find((item) => item.id === dashboard.id)) {
|
|
return;
|
|
}
|
|
|
|
const newItem: PlaylistItem = {
|
|
id: dashboard.id,
|
|
title: dashboard.label,
|
|
type: 'dashboard_by_id',
|
|
value: dashboard.id.toString(10),
|
|
order: items.length + 1,
|
|
};
|
|
setItems([...items, newItem]);
|
|
},
|
|
[items]
|
|
);
|
|
|
|
const addByTag = useCallback(
|
|
(tags: string[]) => {
|
|
const tag = tags[0];
|
|
if (!tag || items.find((item) => item.value === tag)) {
|
|
return;
|
|
}
|
|
|
|
const newItem: PlaylistItem = {
|
|
title: tag,
|
|
type: 'dashboard_by_tag',
|
|
value: tag,
|
|
order: items.length + 1,
|
|
};
|
|
setItems([...items, newItem]);
|
|
},
|
|
[items]
|
|
);
|
|
|
|
const movePlaylistItem = useCallback(
|
|
(item: PlaylistItem, offset: number) => {
|
|
const newItems = [...items];
|
|
const currentPosition = newItems.indexOf(item);
|
|
const newPosition = currentPosition + offset;
|
|
|
|
if (newPosition >= 0 && newPosition < newItems.length) {
|
|
newItems.splice(currentPosition, 1);
|
|
newItems.splice(newPosition, 0, item);
|
|
}
|
|
setItems(newItems);
|
|
},
|
|
[items]
|
|
);
|
|
|
|
const moveUp = useCallback(
|
|
(item: PlaylistItem) => {
|
|
movePlaylistItem(item, -1);
|
|
},
|
|
[movePlaylistItem]
|
|
);
|
|
|
|
const moveDown = useCallback(
|
|
(item: PlaylistItem) => {
|
|
movePlaylistItem(item, 1);
|
|
},
|
|
[movePlaylistItem]
|
|
);
|
|
|
|
const deleteItem = useCallback(
|
|
(item: PlaylistItem) => {
|
|
setItems(items.filter((i) => i !== item));
|
|
},
|
|
[items]
|
|
);
|
|
|
|
return { items, addById, addByTag, deleteItem, moveDown, moveUp };
|
|
}
|