mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-18 07:24:07 +01:00
Improve copy-as-curl
This commit is contained in:
@@ -1,9 +1,18 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { readText } from '@tauri-apps/plugin-clipboard-manager';
|
||||
import { readText, writeText } from '@tauri-apps/plugin-clipboard-manager';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { useWindowFocus } from './useWindowFocus';
|
||||
|
||||
export function useClipboardText() {
|
||||
return useQuery({
|
||||
queryKey: [],
|
||||
queryFn: () => readText(),
|
||||
}).data;
|
||||
const [value, setValue] = useState<string>('');
|
||||
const focused = useWindowFocus();
|
||||
|
||||
useEffect(() => {
|
||||
readText().then(setValue);
|
||||
}, [focused]);
|
||||
|
||||
const setText = useCallback((text: string) => {
|
||||
writeText(text).catch(console.error);
|
||||
}, []);
|
||||
|
||||
return [value, setText] as const;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import { writeText } from '@tauri-apps/plugin-clipboard-manager';
|
||||
import React, { useState } from 'react';
|
||||
import { useState } from 'react';
|
||||
import { useToast } from '../components/ToastContext';
|
||||
import { Icon } from '../components/core/Icon';
|
||||
|
||||
export function useCopyAsCurl(requestId: string) {
|
||||
const [checked, setChecked] = useState<boolean>(false);
|
||||
@@ -15,12 +14,8 @@ export function useCopyAsCurl(requestId: string) {
|
||||
setChecked(true);
|
||||
setTimeout(() => setChecked(false), 800);
|
||||
toast.show({
|
||||
render: () => [
|
||||
<>
|
||||
<Icon icon="copyCheck" />
|
||||
<span>Command copied to clipboard</span>
|
||||
</>,
|
||||
],
|
||||
variant: 'copied',
|
||||
message: 'Curl copied to clipboard',
|
||||
});
|
||||
return cmd;
|
||||
},
|
||||
|
||||
@@ -3,22 +3,34 @@ import { useMutation } from '@tanstack/react-query';
|
||||
import { useActiveWorkspaceId } from './useActiveWorkspaceId';
|
||||
import { useRequestUpdateKey } from './useRequestUpdateKey';
|
||||
import { useUpdateAnyHttpRequest } from './useUpdateAnyHttpRequest';
|
||||
import { useToast } from '../components/ToastContext';
|
||||
import { useCreateHttpRequest } from './useCreateHttpRequest';
|
||||
|
||||
export function useCurlToRequest() {
|
||||
export function useImportCurl() {
|
||||
const workspaceId = useActiveWorkspaceId();
|
||||
const updateRequest = useUpdateAnyHttpRequest();
|
||||
const createRequest = useCreateHttpRequest();
|
||||
const { wasUpdatedExternally } = useRequestUpdateKey(null);
|
||||
const toast = useToast();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ requestId, command }: { requestId: string; command: string }) => {
|
||||
mutationFn: async ({ requestId, command }: { requestId: string | null; command: string }) => {
|
||||
const request: Record<string, unknown> = await invoke('cmd_curl_to_request', {
|
||||
command,
|
||||
workspaceId,
|
||||
});
|
||||
delete request.id;
|
||||
await updateRequest.mutateAsync({ id: requestId, update: request });
|
||||
wasUpdatedExternally(requestId);
|
||||
console.log('FOO', request);
|
||||
|
||||
const id = requestId ?? (await createRequest.mutateAsync({})).id;
|
||||
await updateRequest.mutateAsync({ id, update: request });
|
||||
|
||||
const verb = requestId ? 'updated' : 'created';
|
||||
toast.show({
|
||||
variant: 'success',
|
||||
message: `Request ${verb} from Curl`,
|
||||
});
|
||||
|
||||
wasUpdatedExternally(id);
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user