2023-05-24 04:41:03 -05:00
|
|
|
import React, { useMemo, useState } from 'react';
|
|
|
|
|
|
|
|
import { Page } from 'app/core/components/Page/Page';
|
|
|
|
|
|
|
|
import { GrafanaRouteComponentProps } from '../../core/navigation/types';
|
|
|
|
import { FolderActionsButton } from '../browse-dashboards/components/FolderActionsButton';
|
|
|
|
import { buildNavModel, getLibraryPanelsTabID } from '../folders/state/navModel';
|
|
|
|
import { LibraryPanelsSearch } from '../library-panels/components/LibraryPanelsSearch/LibraryPanelsSearch';
|
|
|
|
import { OpenLibraryPanelModal } from '../library-panels/components/OpenLibraryPanelModal/OpenLibraryPanelModal';
|
|
|
|
import { LibraryElementDTO } from '../library-panels/types';
|
|
|
|
|
2023-05-31 11:03:54 -05:00
|
|
|
import { useGetFolderQuery, useSaveFolderMutation } from './api/browseDashboardsAPI';
|
2023-05-24 04:41:03 -05:00
|
|
|
|
|
|
|
export interface OwnProps extends GrafanaRouteComponentProps<{ uid: string }> {}
|
|
|
|
|
|
|
|
export function BrowseFolderLibraryPanelsPage({ match }: OwnProps) {
|
|
|
|
const { uid: folderUID } = match.params;
|
2023-05-31 11:03:54 -05:00
|
|
|
const { data: folderDTO } = useGetFolderQuery(folderUID);
|
2023-05-24 04:41:03 -05:00
|
|
|
const [selected, setSelected] = useState<LibraryElementDTO | undefined>(undefined);
|
2023-05-31 11:03:54 -05:00
|
|
|
const [saveFolder] = useSaveFolderMutation();
|
2023-05-24 04:41:03 -05:00
|
|
|
|
|
|
|
const navModel = useMemo(() => {
|
|
|
|
if (!folderDTO) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const model = buildNavModel(folderDTO);
|
|
|
|
|
|
|
|
// Set the "Library panels" tab to active
|
|
|
|
const libraryPanelsTabID = getLibraryPanelsTabID(folderDTO.uid);
|
|
|
|
const libraryPanelsTab = model.children?.find((child) => child.id === libraryPanelsTabID);
|
|
|
|
if (libraryPanelsTab) {
|
|
|
|
libraryPanelsTab.active = true;
|
|
|
|
}
|
|
|
|
return model;
|
|
|
|
}, [folderDTO]);
|
|
|
|
|
2023-05-31 11:03:54 -05:00
|
|
|
const onEditTitle = folderUID
|
|
|
|
? async (newValue: string) => {
|
|
|
|
if (folderDTO) {
|
|
|
|
const result = await saveFolder({
|
|
|
|
...folderDTO,
|
|
|
|
title: newValue,
|
|
|
|
});
|
|
|
|
if ('error' in result) {
|
|
|
|
throw result.error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
: undefined;
|
|
|
|
|
2023-05-24 04:41:03 -05:00
|
|
|
return (
|
|
|
|
<Page
|
|
|
|
navId="dashboards/browse"
|
|
|
|
pageNav={navModel}
|
2023-05-31 11:03:54 -05:00
|
|
|
onEditTitle={onEditTitle}
|
2023-05-24 04:41:03 -05:00
|
|
|
actions={<>{folderDTO && <FolderActionsButton folder={folderDTO} />}</>}
|
|
|
|
>
|
2023-05-31 11:03:54 -05:00
|
|
|
<Page.Contents>
|
2023-05-24 04:41:03 -05:00
|
|
|
<LibraryPanelsSearch
|
|
|
|
onClick={setSelected}
|
|
|
|
currentFolderUID={folderUID}
|
|
|
|
showSecondaryActions
|
|
|
|
showSort
|
|
|
|
showPanelFilter
|
|
|
|
/>
|
|
|
|
{selected ? <OpenLibraryPanelModal onDismiss={() => setSelected(undefined)} libraryPanel={selected} /> : null}
|
|
|
|
</Page.Contents>
|
|
|
|
</Page>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default BrowseFolderLibraryPanelsPage;
|