github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/ui/dashboard/src/components/OpenSnapshotButton/index.tsx (about)

     1  import { DashboardActions, DashboardDataModeCLISnapshot } from "../../types";
     2  import { SnapshotDataToExecutionCompleteSchemaMigrator } from "../../utils/schema";
     3  import { useDashboard } from "../../hooks/useDashboard";
     4  import { useNavigate } from "react-router-dom";
     5  import { useRef } from "react";
     6  
     7  const OpenSnapshotButton = () => {
     8    const { dispatch } = useDashboard();
     9    const fileInputRef = useRef<HTMLInputElement | null>(null);
    10    const navigate = useNavigate();
    11  
    12    return (
    13      <>
    14        <span
    15          className="text-base text-foreground-lighter hover:text-foreground cursor-pointer"
    16          onClick={() => {
    17            fileInputRef.current?.click();
    18          }}
    19        >
    20          Open snapshot…
    21        </span>
    22        <input
    23          ref={fileInputRef}
    24          accept=".sps"
    25          className="hidden"
    26          id="open-snapshot"
    27          name="open-snapshot"
    28          type="file"
    29          onChange={(e) => {
    30            const files = e.target.files;
    31            if (!files || files.length === 0) {
    32              return;
    33            }
    34            const fileName = files[0].name;
    35            const fr = new FileReader();
    36            fr.onload = () => {
    37              if (!fr.result) {
    38                return;
    39              }
    40  
    41              e.target.value = "";
    42              try {
    43                const data = JSON.parse(fr.result.toString());
    44                const eventMigrator =
    45                  new SnapshotDataToExecutionCompleteSchemaMigrator();
    46                const migratedEvent = eventMigrator.toLatest(data);
    47                dispatch({
    48                  type: DashboardActions.CLEAR_DASHBOARD_INPUTS,
    49                  recordInputsHistory: false,
    50                });
    51                dispatch({
    52                  type: DashboardActions.SELECT_DASHBOARD,
    53                  dashboard: null,
    54                  recordInputsHistory: false,
    55                });
    56                navigate(`/snapshot/${fileName}`);
    57                dispatch({
    58                  type: DashboardActions.SET_DATA_MODE,
    59                  dataMode: DashboardDataModeCLISnapshot,
    60                  snapshotFileName: fileName,
    61                });
    62                dispatch({
    63                  type: DashboardActions.EXECUTION_COMPLETE,
    64                  ...migratedEvent,
    65                });
    66                dispatch({
    67                  type: DashboardActions.SET_DASHBOARD_INPUTS,
    68                  value: migratedEvent.snapshot.inputs,
    69                  recordInputsHistory: false,
    70                });
    71              } catch (err: any) {
    72                dispatch({
    73                  type: DashboardActions.WORKSPACE_ERROR,
    74                  error: "Unable to load snapshot:" + err.message,
    75                });
    76              }
    77            };
    78            fr.readAsText(files[0]);
    79          }}
    80        />
    81      </>
    82    );
    83  };
    84  
    85  export default OpenSnapshotButton;