import { Button, Typography } from 'antd'; import createDashboard from 'api/dashboard/create'; import { useGetAllDashboard } from 'hooks/dashboard/useGetAllDashboard'; import useAxiosError from 'hooks/useAxiosError'; import { useCallback, useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useMutation } from 'react-query'; import { ExportPanelProps } from '.'; import { DashboardSelect, NewDashboardButton, SelectWrapper, Title, Wrapper, } from './styles'; import { filterOptions, getSelectOptions } from './utils'; function ExportPanelContainer({ isLoading, onExport, }: ExportPanelProps): JSX.Element { const { t } = useTranslation(['dashboard']); const [selectedDashboardId, setSelectedDashboardId] = useState( null, ); const { data, isLoading: isAllDashboardsLoading, refetch, } = useGetAllDashboard(); const handleError = useAxiosError(); const { mutate: createNewDashboard, isLoading: createDashboardLoading, } = useMutation(createDashboard, { onSuccess: (data) => { if (data.payload) { onExport(data?.payload); } refetch(); }, onError: handleError, }); const options = useMemo(() => getSelectOptions(data || []), [data]); const handleExportClick = useCallback((): void => { const currentSelectedDashboard = data?.find( ({ uuid }) => uuid === selectedDashboardId, ); onExport(currentSelectedDashboard || null); }, [data, selectedDashboardId, onExport]); const handleSelect = useCallback( (selectedDashboardValue: string): void => { setSelectedDashboardId(selectedDashboardValue); }, [setSelectedDashboardId], ); const handleNewDashboard = useCallback(async () => { createNewDashboard({ title: t('new_dashboard_title', { ns: 'dashboard', }), uploadedGrafana: false, }); }, [t, createNewDashboard]); const isDashboardLoading = isAllDashboardsLoading || createDashboardLoading; const isDisabled = isAllDashboardsLoading || !options?.length || !selectedDashboardId || isLoading; return ( Export Panel Or create dashboard with this panel - New Dashboard ); } export default ExportPanelContainer;