mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-19 07:53:54 +01:00
Fixed asset:// loading and tweak curl stuff
This commit is contained in:
@@ -7,10 +7,10 @@ import { motion } from 'framer-motion';
|
||||
|
||||
export function ImportCurlButton() {
|
||||
const [clipboardText] = useClipboardText();
|
||||
const [lastImportedCmd, setLastImportedCmd] = useState<string>('');
|
||||
const importCurl = useImportCurl();
|
||||
const importCurl = useImportCurl({ clearClipboard: true });
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
if (!clipboardText?.trim().startsWith('curl ') || lastImportedCmd === clipboardText) {
|
||||
if (!clipboardText?.trim().startsWith('curl ')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -25,13 +25,15 @@ export function ImportCurlButton() {
|
||||
variant="border"
|
||||
color="secondary"
|
||||
leftSlot={<Icon icon="paste" size="sm" />}
|
||||
isLoading={isLoading}
|
||||
onClick={() => {
|
||||
importCurl.mutate({
|
||||
requestId: null, // Create request
|
||||
command: clipboardText,
|
||||
});
|
||||
// setClipboardText('');
|
||||
setLastImportedCmd(clipboardText);
|
||||
setIsLoading(true);
|
||||
importCurl
|
||||
.mutateAsync({
|
||||
requestId: null, // Create request
|
||||
command: clipboardText,
|
||||
})
|
||||
.finally(() => setIsLoading(false));
|
||||
}}
|
||||
>
|
||||
Import Curl
|
||||
|
||||
@@ -230,7 +230,7 @@ export const RequestPane = memo(function RequestPane({
|
||||
|
||||
const isLoading = useIsResponseLoading(activeRequestId ?? null);
|
||||
const { updateKey } = useRequestUpdateKey(activeRequestId ?? null);
|
||||
const importCurl = useImportCurl();
|
||||
const importCurl = useImportCurl({ clearClipboard: true });
|
||||
|
||||
return (
|
||||
<div
|
||||
|
||||
@@ -1,18 +1,25 @@
|
||||
import { readText, writeText } from '@tauri-apps/plugin-clipboard-manager';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { useCallback, useEffect } from 'react';
|
||||
import { useWindowFocus } from './useWindowFocus';
|
||||
import { createGlobalState } from 'react-use';
|
||||
|
||||
const useClipboardTextState = createGlobalState<string>('');
|
||||
|
||||
export function useClipboardText() {
|
||||
const [value, setValue] = useState<string>('');
|
||||
const [value, setValue] = useClipboardTextState();
|
||||
const focused = useWindowFocus();
|
||||
|
||||
useEffect(() => {
|
||||
readText().then(setValue);
|
||||
}, [focused]);
|
||||
}, [focused, setValue]);
|
||||
|
||||
const setText = useCallback((text: string) => {
|
||||
writeText(text).catch(console.error);
|
||||
}, []);
|
||||
const setText = useCallback(
|
||||
(text: string) => {
|
||||
writeText(text).catch(console.error);
|
||||
setValue(text);
|
||||
},
|
||||
[setValue],
|
||||
);
|
||||
|
||||
return [value, setText] as const;
|
||||
}
|
||||
|
||||
@@ -13,13 +13,7 @@ export function useCreateHttpRequest() {
|
||||
const activeRequest = useActiveRequest();
|
||||
const routes = useAppRoutes();
|
||||
|
||||
return useMutation<
|
||||
HttpRequest,
|
||||
unknown,
|
||||
Partial<
|
||||
Pick<HttpRequest, 'name' | 'sortPriority' | 'folderId' | 'bodyType' | 'method' | 'headers'>
|
||||
>
|
||||
>({
|
||||
return useMutation<HttpRequest, unknown, Partial<HttpRequest>>({
|
||||
mutationFn: (patch) => {
|
||||
if (workspaceId === null) {
|
||||
throw new Error("Cannot create request when there's no active workspace");
|
||||
@@ -34,7 +28,8 @@ export function useCreateHttpRequest() {
|
||||
}
|
||||
}
|
||||
patch.folderId = patch.folderId || activeRequest?.folderId;
|
||||
return invoke('cmd_create_http_request', { workspaceId, name: '', ...patch });
|
||||
console.log('PATCH', patch);
|
||||
return invoke('cmd_create_http_request', { request: { workspaceId, ...patch } });
|
||||
},
|
||||
onSettled: () => trackEvent('http_request', 'create'),
|
||||
onSuccess: async (request) => {
|
||||
|
||||
@@ -5,13 +5,15 @@ import { useRequestUpdateKey } from './useRequestUpdateKey';
|
||||
import { useUpdateAnyHttpRequest } from './useUpdateAnyHttpRequest';
|
||||
import { useToast } from '../components/ToastContext';
|
||||
import { useCreateHttpRequest } from './useCreateHttpRequest';
|
||||
import { useClipboardText } from './useClipboardText';
|
||||
|
||||
export function useImportCurl() {
|
||||
export function useImportCurl({ clearClipboard }: { clearClipboard?: boolean } = {}) {
|
||||
const workspaceId = useActiveWorkspaceId();
|
||||
const updateRequest = useUpdateAnyHttpRequest();
|
||||
const createRequest = useCreateHttpRequest();
|
||||
const { wasUpdatedExternally } = useRequestUpdateKey(null);
|
||||
const toast = useToast();
|
||||
const [, setClipboardText] = useClipboardText();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ requestId, command }: { requestId: string | null; command: string }) => {
|
||||
@@ -21,16 +23,24 @@ export function useImportCurl() {
|
||||
});
|
||||
delete request.id;
|
||||
|
||||
const id = requestId ?? (await createRequest.mutateAsync({})).id;
|
||||
await updateRequest.mutateAsync({ id, update: request });
|
||||
let verb;
|
||||
if (requestId == null) {
|
||||
verb = 'Created';
|
||||
await createRequest.mutateAsync(request);
|
||||
} else {
|
||||
verb = 'Updated';
|
||||
await updateRequest.mutateAsync({ id: requestId, update: request });
|
||||
setTimeout(() => wasUpdatedExternally(requestId), 100);
|
||||
}
|
||||
|
||||
const verb = requestId ? 'updated' : 'created';
|
||||
toast.show({
|
||||
variant: 'success',
|
||||
message: `Request ${verb} from Curl`,
|
||||
message: `${verb} request from Curl`,
|
||||
});
|
||||
|
||||
wasUpdatedExternally(id);
|
||||
if (clearClipboard) {
|
||||
setClipboardText('');
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user