import { useState } from 'react'; import useAsyncFn from 'react-use/lib/useAsyncFn'; import { selectors as e2eSelectors } from '@grafana/e2e-selectors'; import { SceneComponentProps } from '@grafana/scenes'; import { Alert, Button, ClipboardButton, Spinner, Stack, TextLink } from '@grafana/ui'; import { t, Trans } from 'app/core/internationalization'; import { contextSrv } from 'app/core/services/context_srv'; import { AccessControlAction } from 'app/types'; import { SnapshotSharingOptions } from '../../../../dashboard/services/SnapshotSrv'; import { ShareDrawerConfirmAction } from '../../ShareDrawer/ShareDrawerConfirmAction'; import { ShareSnapshotTab } from '../../ShareSnapshotTab'; import { ShareView } from '../../types'; import { UpsertSnapshot } from './UpsertSnapshot'; const selectors = e2eSelectors.pages.ShareDashboardDrawer.ShareSnapshot; export class ShareSnapshot extends ShareSnapshotTab implements ShareView { static Component = ShareSnapshotRenderer; public getTabLabel() { return t('share-dashboard.menu.share-snapshot-title', 'Share snapshot'); } } function ShareSnapshotRenderer({ model }: SceneComponentProps) { const [showDeleteConfirmation, setShowDeleteConfirmation] = useState(false); const [showDeletedAlert, setShowDeletedAlert] = useState(false); const [step, setStep] = useState(1); const { snapshotName, snapshotSharingOptions, selectedExpireOption, panelRef, onDismiss, dashboardRef } = model.useState(); const [snapshotResult, createSnapshot] = useAsyncFn(async (external = false) => { const response = await model.onSnapshotCreate(external); setStep(2); return response; }); const [deleteSnapshotResult, deleteSnapshot] = useAsyncFn(async (url: string) => { const response = await model.onSnapshotDelete(url); setStep(1); setShowDeleteConfirmation(false); setShowDeletedAlert(true); return response; }); const onCancelClick = () => { onDismiss?.(); }; const reset = () => { model.onSnasphotNameChange(dashboardRef.resolve().state.title); setStep(1); }; const onDeleteSnapshotClick = async () => { await deleteSnapshot(snapshotResult.value?.deleteUrl!); reset(); }; if (showDeleteConfirmation) { return ( setShowDeleteConfirmation(false)} description={t('snapshot.share.delete-description', 'Are you sure you want to delete this snapshot?')} isActionLoading={deleteSnapshotResult.loading} /> ); } return (
<> {step === 1 && showDeletedAlert && ( setShowDeletedAlert(false)}> Snapshot deleted. It could take an hour to be cleared from CDN caches. )} {step === 1 ? ( ) : ( step === 2 && snapshotResult.value && ( setShowDeleteConfirmation(true)} onNewSnapshotClick={reset} /> ) )} {t('snapshot.share.view-all-button', 'View all snapshots')}
); } const CreateSnapshotActions = ({ isLoading, onCreateClick, onCancelClick, sharingOptions, }: { isLoading: boolean; sharingOptions?: SnapshotSharingOptions; onCancelClick: () => void; onCreateClick: (isExternal?: boolean) => void; }) => ( {sharingOptions?.externalEnabled && ( )} {isLoading && } ); const UpsertSnapshotActions = ({ url, onDeleteClick, onNewSnapshotClick, }: { url: string; onDeleteClick: () => void; onNewSnapshotClick: () => void; }) => { const hasDeletePermission = contextSrv.hasPermission(AccessControlAction.SnapshotsDelete); const deleteTooltip = hasDeletePermission ? '' : t('snapshot.share.delete-permission-tooltip', "You don't have permission to delete snapshots"); return ( url} data-testid={selectors.copyUrlButton} > Copy link ); };