From 71b06c5261ab53e366d0a63f4e4f0d32ff4ce90c Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Wed, 8 May 2024 00:28:40 -0700 Subject: [PATCH] Slight refactor to copy-as-curl --- src-web/components/Sidebar.tsx | 13 ++++++++----- src-web/components/core/Dropdown.tsx | 5 ++++- src-web/hooks/useCopyAsCurl.ts | 12 ++++++++---- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src-web/components/Sidebar.tsx b/src-web/components/Sidebar.tsx index 7095e741..5c25602e 100644 --- a/src-web/components/Sidebar.tsx +++ b/src-web/components/Sidebar.tsx @@ -608,7 +608,7 @@ const SidebarItem = forwardRef(function SidebarItem( const deleteRequest = useDeleteRequest(activeRequest ?? null); const duplicateHttpRequest = useDuplicateHttpRequest({ id: itemId, navigateAfter: true }); const duplicateGrpcRequest = useDuplicateGrpcRequest({ id: itemId, navigateAfter: true }); - const copyAsCurl = useCopyAsCurl(itemId); + const [isCopied, copyAsCurl] = useCopyAsCurl(itemId); const sendRequest = useSendRequest(itemId); const sendManyRequests = useSendManyRequests(); const latestHttpResponse = useLatestHttpResponse(itemId); @@ -739,10 +739,13 @@ const SidebarItem = forwardRef(function SidebarItem( { key: 'copyCurl', label: 'Copy as Curl', - leftSlot: , - onSelect: async () => { - await copyAsCurl.mutateAsync(); - }, + leftSlot: ( + + ), + onSelect: copyAsCurl, }, { type: 'separator' }, ] diff --git a/src-web/components/core/Dropdown.tsx b/src-web/components/core/Dropdown.tsx index 5069d701..9e7d200d 100644 --- a/src-web/components/core/Dropdown.tsx +++ b/src-web/components/core/Dropdown.tsx @@ -40,6 +40,7 @@ export type DropdownItemDefault = { key: string; type?: 'default'; label: ReactNode; + keepOpen?: boolean; hotKeyAction?: HotkeyAction; hotKeyLabelOnly?: boolean; variant?: 'default' | 'danger' | 'notify'; @@ -307,7 +308,9 @@ const Menu = forwardRef, MenuPro const handleSelect = useCallback( (i: DropdownItem) => { - handleClose(); + if (i.type !== 'separator' && !i.keepOpen) { + handleClose(); + } setSelectedIndex(null); if (i.type !== 'separator') { i.onSelect?.(); diff --git a/src-web/hooks/useCopyAsCurl.ts b/src-web/hooks/useCopyAsCurl.ts index f9ad6913..98fc2c03 100644 --- a/src-web/hooks/useCopyAsCurl.ts +++ b/src-web/hooks/useCopyAsCurl.ts @@ -1,13 +1,17 @@ -import { useMutation } from '@tanstack/react-query'; import { invoke } from '@tauri-apps/api/core'; import { writeText } from '@tauri-apps/plugin-clipboard-manager'; +import { useState } from 'react'; export function useCopyAsCurl(requestId: string) { - return useMutation({ - mutationFn: async () => { + const [checked, setChecked] = useState(false); + return [ + checked, + async () => { const cmd: string = await invoke('cmd_request_to_curl', { requestId }); await writeText(cmd); + setChecked(true); + setTimeout(() => setChecked(false), 800); return cmd; }, - }); + ] as const; }