import React, { FC, useState, useCallback } from 'react'; import useAsync from 'react-use/lib/useAsync'; import { getBackendSrv, locationService } from '@grafana/runtime'; import { ConfirmModal, Button, LinkButton } from '@grafana/ui'; import { Snapshot } from '../types'; export function getSnapshots() { return getBackendSrv() .get('/api/dashboard/snapshots') .then((result: Snapshot[]) => { return result.map((snapshot) => ({ ...snapshot, url: `/dashboard/snapshot/${snapshot.key}`, })); }); } export const SnapshotListTable: FC = () => { const [snapshots, setSnapshots] = useState([]); const [removeSnapshot, setRemoveSnapshot] = useState(); const currentPath = locationService.getLocation().pathname; const fullUrl = window.location.href; const baseUrl = fullUrl.substring(0, fullUrl.indexOf(currentPath)); useAsync(async () => { const response = await getSnapshots(); setSnapshots(response); }, [setSnapshots]); const doRemoveSnapshot = useCallback( async (snapshot: Snapshot) => { const filteredSnapshots = snapshots.filter((ss) => ss.key !== snapshot.key); setSnapshots(filteredSnapshots); await getBackendSrv() .delete(`/api/snapshots/${snapshot.key}`) .catch(() => { setSnapshots(snapshots); }); }, [snapshots] ); return (
{snapshots.map((snapshot) => { const url = snapshot.externalUrl || snapshot.url; const fullUrl = snapshot.externalUrl || `${baseUrl}${snapshot.url}`; return ( ); })}
Name Snapshot url
{snapshot.name} {fullUrl} {snapshot.external && External} View
setRemoveSnapshot(undefined)} onConfirm={() => { doRemoveSnapshot(removeSnapshot!); setRemoveSnapshot(undefined); }} />
); };