mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-20 16:43:53 +01:00
Make prompt() to return null on cancel
This commit is contained in:
@@ -8,7 +8,7 @@ import { HStack } from '../components/core/Stacks';
|
||||
export type PromptProps = Omit<ShowPromptRequest, 'id' | 'title' | 'description'> & {
|
||||
description?: ReactNode;
|
||||
onHide: () => void;
|
||||
onResult: (value: string) => void;
|
||||
onResult: (value: string | null) => void;
|
||||
};
|
||||
|
||||
export function Prompt({
|
||||
|
||||
@@ -9,24 +9,30 @@ export function useCreateFolder() {
|
||||
const workspace = useActiveWorkspace();
|
||||
const prompt = usePrompt();
|
||||
|
||||
return useMutation<Folder, unknown, Partial<Pick<Folder, 'name' | 'sortPriority' | 'folderId'>>>({
|
||||
return useMutation<void, unknown, Partial<Pick<Folder, 'name' | 'sortPriority' | 'folderId'>>>({
|
||||
mutationKey: ['create_folder'],
|
||||
mutationFn: async (patch) => {
|
||||
if (workspace === null) {
|
||||
throw new Error("Cannot create folder when there's no active workspace");
|
||||
}
|
||||
patch.name =
|
||||
patch.name ||
|
||||
(await prompt({
|
||||
|
||||
if (!patch.name) {
|
||||
const name = await prompt({
|
||||
id: 'new-folder',
|
||||
label: 'Name',
|
||||
defaultValue: 'Folder',
|
||||
title: 'New Folder',
|
||||
confirmText: 'Create',
|
||||
placeholder: 'Name',
|
||||
}));
|
||||
});
|
||||
if (name == null) {
|
||||
return;
|
||||
}
|
||||
patch.name = name;
|
||||
}
|
||||
|
||||
patch.sortPriority = patch.sortPriority || -Date.now();
|
||||
return invokeCmd('cmd_create_folder', { workspaceId: workspace.id, ...patch });
|
||||
await invokeCmd('cmd_create_folder', { workspaceId: workspace.id, ...patch });
|
||||
},
|
||||
onSettled: () => trackEvent('folder', 'create'),
|
||||
});
|
||||
|
||||
@@ -4,19 +4,27 @@ import type { PromptProps } from './Prompt';
|
||||
import { Prompt } from './Prompt';
|
||||
|
||||
type Props = Pick<DialogProps, 'title' | 'description'> &
|
||||
Omit<PromptProps, 'onResult' | 'onHide'> & { id: string };
|
||||
Omit<PromptProps, 'onClose' | 'onHide' | 'onResult'> & { id: string };
|
||||
|
||||
export function usePrompt() {
|
||||
const dialog = useDialog();
|
||||
return ({ id, title, description, ...props }: Props) =>
|
||||
new Promise((onResult: PromptProps['onResult']) => {
|
||||
new Promise((resolve: PromptProps['onResult']) => {
|
||||
dialog.show({
|
||||
id,
|
||||
title,
|
||||
description,
|
||||
hideX: true,
|
||||
size: 'sm',
|
||||
render: ({ hide: onHide }) => Prompt({ onHide, onResult, ...props }),
|
||||
render: ({ hide }) =>
|
||||
Prompt({
|
||||
onHide: () => {
|
||||
hide();
|
||||
resolve(null);
|
||||
},
|
||||
onResult: resolve,
|
||||
...props,
|
||||
}),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -33,6 +33,9 @@ export function useRenameRequest(requestId: string | null) {
|
||||
defaultValue: request.name,
|
||||
confirmText: 'Save',
|
||||
});
|
||||
|
||||
if (name == null) return;
|
||||
|
||||
if (request.model === 'http_request') {
|
||||
updateHttpRequest.mutate({ id: request.id, update: (r: HttpRequest) => ({ ...r, name }) });
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user