Make prompt() to return null on cancel

This commit is contained in:
Gregory Schier
2024-10-02 05:54:44 -07:00
parent 89ff25cd54
commit 4160e5b1c4
26 changed files with 436 additions and 108 deletions

View File

@@ -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({

View File

@@ -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'),
});

View File

@@ -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,
}),
});
});
}

View File

@@ -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 {