mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-19 07:53:54 +01:00
Better curl import
This commit is contained in:
@@ -1,43 +0,0 @@
|
||||
import { clear, readText, writeText } from '@tauri-apps/plugin-clipboard-manager';
|
||||
import { useCallback, useEffect } from 'react';
|
||||
import { createGlobalState } from 'react-use';
|
||||
import { useToast } from '../components/ToastContext';
|
||||
import { useWindowFocus } from './useWindowFocus';
|
||||
|
||||
const useClipboardTextState = createGlobalState<string>('');
|
||||
|
||||
export function useClipboardText({ disableToast }: { disableToast?: boolean } = {}) {
|
||||
const [value, setValue] = useClipboardTextState();
|
||||
const focused = useWindowFocus();
|
||||
const toast = useToast();
|
||||
|
||||
useEffect(() => {
|
||||
readText()
|
||||
.then(setValue)
|
||||
.catch(() => {
|
||||
// Clipboard probably empty
|
||||
setValue('');
|
||||
});
|
||||
}, [focused, setValue]);
|
||||
|
||||
const setText = useCallback(
|
||||
(text: string | null) => {
|
||||
if (text == null) {
|
||||
clear().catch(console.error);
|
||||
} else {
|
||||
writeText(text).catch(console.error);
|
||||
}
|
||||
if (text != '' && !disableToast) {
|
||||
toast.show({
|
||||
id: 'copied',
|
||||
variant: 'copied',
|
||||
message: 'Copied to clipboard',
|
||||
});
|
||||
}
|
||||
setValue(text || '');
|
||||
},
|
||||
[disableToast, setValue, toast],
|
||||
);
|
||||
|
||||
return [value, setText] as const;
|
||||
}
|
||||
27
src-web/hooks/useCopy.ts
Normal file
27
src-web/hooks/useCopy.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { clear, writeText } from '@tauri-apps/plugin-clipboard-manager';
|
||||
import { useCallback } from 'react';
|
||||
import { useToast } from '../components/ToastContext';
|
||||
|
||||
export function useCopy({ disableToast }: { disableToast?: boolean } = {}) {
|
||||
const toast = useToast();
|
||||
|
||||
const copy = useCallback(
|
||||
(text: string | null) => {
|
||||
if (text == null) {
|
||||
clear().catch(console.error);
|
||||
} else {
|
||||
writeText(text).catch(console.error);
|
||||
}
|
||||
if (text != '' && !disableToast) {
|
||||
toast.show({
|
||||
id: 'copied',
|
||||
variant: 'copied',
|
||||
message: 'Copied to clipboard',
|
||||
});
|
||||
}
|
||||
},
|
||||
[disableToast, toast],
|
||||
);
|
||||
|
||||
return copy;
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
import { invokeCmd } from '../lib/tauri';
|
||||
import { useActiveEnvironmentId } from './useActiveEnvironmentId';
|
||||
import { useClipboardText } from './useClipboardText';
|
||||
import { useCopy } from './useCopy';
|
||||
|
||||
export function useCopyAsCurl(requestId: string) {
|
||||
const [, copy] = useClipboardText();
|
||||
const copy = useCopy();
|
||||
const environmentId = useActiveEnvironmentId();
|
||||
return useMutation({
|
||||
mutationKey: ['copy_as_curl', requestId],
|
||||
|
||||
@@ -1,23 +1,27 @@
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
import { useToast } from '../components/ToastContext';
|
||||
import { invokeCmd } from '../lib/tauri';
|
||||
import { useActiveWorkspaceId } from './useActiveWorkspaceId';
|
||||
import { useCreateHttpRequest } from './useCreateHttpRequest';
|
||||
import { useRequestUpdateKey } from './useRequestUpdateKey';
|
||||
import { useUpdateAnyHttpRequest } from './useUpdateAnyHttpRequest';
|
||||
import { useToast } from '../components/ToastContext';
|
||||
import { useCreateHttpRequest } from './useCreateHttpRequest';
|
||||
import { useClipboardText } from './useClipboardText';
|
||||
|
||||
export function useImportCurl({ clearClipboard }: { clearClipboard?: boolean } = {}) {
|
||||
export function useImportCurl() {
|
||||
const workspaceId = useActiveWorkspaceId();
|
||||
const updateRequest = useUpdateAnyHttpRequest();
|
||||
const createRequest = useCreateHttpRequest();
|
||||
const { wasUpdatedExternally } = useRequestUpdateKey(null);
|
||||
const toast = useToast();
|
||||
const [, setClipboardText] = useClipboardText();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ['import_curl'],
|
||||
mutationFn: async ({ requestId, command }: { requestId: string | null; command: string }) => {
|
||||
mutationFn: async ({
|
||||
overwriteRequestId,
|
||||
command,
|
||||
}: {
|
||||
overwriteRequestId?: string;
|
||||
command: string;
|
||||
}) => {
|
||||
const request: Record<string, unknown> = await invokeCmd('cmd_curl_to_request', {
|
||||
command,
|
||||
workspaceId,
|
||||
@@ -25,23 +29,19 @@ export function useImportCurl({ clearClipboard }: { clearClipboard?: boolean } =
|
||||
delete request.id;
|
||||
|
||||
let verb;
|
||||
if (requestId == null) {
|
||||
if (overwriteRequestId == null) {
|
||||
verb = 'Created';
|
||||
await createRequest.mutateAsync(request);
|
||||
} else {
|
||||
verb = 'Updated';
|
||||
await updateRequest.mutateAsync({ id: requestId, update: request });
|
||||
setTimeout(() => wasUpdatedExternally(requestId), 100);
|
||||
await updateRequest.mutateAsync({ id: overwriteRequestId, update: request });
|
||||
setTimeout(() => wasUpdatedExternally(overwriteRequestId), 100);
|
||||
}
|
||||
|
||||
toast.show({
|
||||
variant: 'success',
|
||||
message: `${verb} request from Curl`,
|
||||
});
|
||||
|
||||
if (clearClipboard) {
|
||||
setClipboardText(null);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user