From bb2d3dd5b16962f35dc9a5cc950c9d9e5cadccca Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Thu, 9 Nov 2023 20:00:19 -0800 Subject: [PATCH] Show import errors --- src-tauri/src/main.rs | 25 ++++++++----------- .../components/WorkspaceActionsDropdown.tsx | 10 +++++++- src-web/hooks/Alert.tsx | 21 ++++++++++++++++ src-web/hooks/useAlert.ts | 15 +++++++++++ 4 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 src-web/hooks/Alert.tsx create mode 100644 src-web/hooks/useAlert.ts diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index f1db3106..1c752cb5 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -269,26 +269,23 @@ async fn import_data( file_paths: Vec<&str>, ) -> Result { let pool = &*db_instance.lock().await; - let mut resources = plugin::run_plugin_import( - &window.app_handle(), - "insomnia-importer", - file_paths.first().unwrap(), - ) - .await; - println!("Resources: {:?}", resources); - - if resources.is_none() { - resources = plugin::run_plugin_import( + let mut resources: Option = None; + let plugins = vec!["yaak-importer", "insomnia-importer"]; + for plugin_name in plugins { + if let Some(r) = plugin::run_plugin_import( &window.app_handle(), - "yaak-importer", + plugin_name, file_paths.first().unwrap(), ) - .await; + .await + { + resources = Some(r); + break; + } } - println!("Resources: {:?}", resources); match resources { - None => Err("Failed to import data".to_string()), + None => Err("No importers found for the chosen file".to_string()), Some(r) => { let mut imported_resources = ImportResources::default(); diff --git a/src-web/components/WorkspaceActionsDropdown.tsx b/src-web/components/WorkspaceActionsDropdown.tsx index fd3cb266..daa23815 100644 --- a/src-web/components/WorkspaceActionsDropdown.tsx +++ b/src-web/components/WorkspaceActionsDropdown.tsx @@ -2,6 +2,7 @@ import { invoke } from '@tauri-apps/api'; import classNames from 'classnames'; import { memo, useMemo } from 'react'; import { useActiveWorkspace } from '../hooks/useActiveWorkspace'; +import { useAlert } from '../hooks/useAlert'; import { useAppRoutes } from '../hooks/useAppRoutes'; import { useCreateWorkspace } from '../hooks/useCreateWorkspace'; import { useDeleteWorkspace } from '../hooks/useDeleteWorkspace'; @@ -38,6 +39,7 @@ export const WorkspaceActionsDropdown = memo(function WorkspaceActionsDropdown({ const { appearance, toggleAppearance } = useTheme(); const dialog = useDialog(); const prompt = usePrompt(); + const alert = useAlert(); const routes = useAppRoutes(); const items: DropdownItem[] = useMemo(() => { @@ -155,7 +157,13 @@ export const WorkspaceActionsDropdown = memo(function WorkspaceActionsDropdown({ key: 'import-data', label: 'Import Data', leftSlot: , - onSelect: () => importData.mutate(), + onSelect: () => + importData.mutateAsync().catch((err) => { + alert({ + title: 'Import Failed', + body: err, + }); + }), }, { key: 'export-data', diff --git a/src-web/hooks/Alert.tsx b/src-web/hooks/Alert.tsx new file mode 100644 index 00000000..572212c7 --- /dev/null +++ b/src-web/hooks/Alert.tsx @@ -0,0 +1,21 @@ +import type { ReactNode } from 'react'; +import { Button } from '../components/core/Button'; +import { HStack, VStack } from '../components/core/Stacks'; + +export interface AlertProps { + onHide: () => void; + body: ReactNode; +} + +export function Alert({ onHide, body }: AlertProps) { + return ( + +
{body}
+ + + +
+ ); +} diff --git a/src-web/hooks/useAlert.ts b/src-web/hooks/useAlert.ts new file mode 100644 index 00000000..22aec3f5 --- /dev/null +++ b/src-web/hooks/useAlert.ts @@ -0,0 +1,15 @@ +import type { DialogProps } from '../components/core/Dialog'; +import { useDialog } from '../components/DialogContext'; +import type { AlertProps } from './Alert'; +import { Alert } from './Alert'; + +export function useAlert() { + const dialog = useDialog(); + return ({ title, body }: { title: DialogProps['title']; body: AlertProps['body'] }) => + dialog.show({ + title, + hideX: true, + size: 'sm', + render: ({ hide }) => Alert({ onHide: hide, body }), + }); +}