import { save } from '@tauri-apps/plugin-dialog'; import { useCallback, useMemo, useState } from 'react'; import slugify from 'slugify'; import type { Workspace } from '../lib/models'; import { count } from '../lib/pluralize'; import { invokeCmd } from '../lib/tauri'; import { Button } from './core/Button'; import { Checkbox } from './core/Checkbox'; import { HStack, VStack } from './core/Stacks'; interface Props { onHide: () => void; onSuccess: (path: string) => void; activeWorkspace: Workspace; workspaces: Workspace[]; } export function ExportDataDialog({ onHide, onSuccess, activeWorkspace, workspaces: allWorkspaces, }: Props) { const [selectedWorkspaces, setSelectedWorkspaces] = useState>({ [activeWorkspace.id]: true, }); // Put active workspace first const workspaces = useMemo( () => [activeWorkspace, ...allWorkspaces.filter((w) => w.id !== activeWorkspace.id)], [activeWorkspace, allWorkspaces], ); const handleToggleAll = () => { setSelectedWorkspaces( allSelected ? {} : workspaces.reduce((acc, w) => ({ ...acc, [w.id]: true }), {}), ); }; const handleExport = useCallback(async () => { const ids = Object.keys(selectedWorkspaces).filter((k) => selectedWorkspaces[k]); const workspace = ids.length === 1 ? workspaces.find((w) => w.id === ids[0]) : undefined; const slug = workspace ? slugify(workspace.name, { lower: true }) : 'workspaces'; const exportPath = await save({ title: 'Export Data', defaultPath: `yaak.${slug}.json`, }); if (exportPath == null) { return; } await invokeCmd('cmd_export_data', { workspaceIds: ids, exportPath }); onHide(); onSuccess(exportPath); }, [onHide, onSuccess, selectedWorkspaces, workspaces]); const allSelected = workspaces.every((w) => selectedWorkspaces[w.id]); const numSelected = Object.values(selectedWorkspaces).filter(Boolean).length; const noneSelected = numSelected === 0; return ( {workspaces.map((w) => ( ))}
Workspace
setSelectedWorkspaces((prev) => ({ ...prev, [w.id]: !prev[w.id] })) } /> setSelectedWorkspaces((prev) => ({ ...prev, [w.id]: !prev[w.id] }))} > {w.name} {w.id === activeWorkspace.id ? '(current workspace)' : ''}
); }