Add prompt() plugin API (#121)

This commit is contained in:
Gregory Schier
2024-10-01 08:32:42 -07:00
committed by GitHub
parent be60e4648a
commit 7e4f807f75
26 changed files with 220 additions and 82 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@yaakapp/api",
"version": "0.2.7",
"version": "0.2.11",
"main": "lib/index.js",
"typings": "./lib/index.d.ts",
"files": [

View File

@@ -1,4 +1,9 @@
import { TemplateRenderRequest, TemplateRenderResponse } from '@yaakapp-internal/plugin';
import {
ShowPromptRequest,
ShowPromptResponse,
TemplateRenderRequest,
TemplateRenderResponse,
} from '@yaakapp-internal/plugin';
import {
FindHttpResponsesRequest,
FindHttpResponsesResponse,
@@ -18,6 +23,9 @@ export type Context = {
toast: {
show(args: ShowToastRequest): void;
};
prompt: {
show(args: ShowPromptRequest): Promise<ShowPromptResponse['value']>;
};
httpRequest: {
send(args: SendHttpRequestRequest): Promise<SendHttpRequestResponse['httpResponse']>;
getById(args: GetHttpRequestByIdRequest): Promise<GetHttpRequestByIdResponse['httpRequest']>;

View File

@@ -13,6 +13,7 @@ import {
InternalEvent,
InternalEventPayload,
SendHttpRequestResponse,
ShowPromptResponse,
TemplateFunction,
} from '@yaakapp/api';
import { HttpRequestActionPlugin } from '@yaakapp/api/lib/plugins/HttpRequestActionPlugin';
@@ -138,6 +139,15 @@ async function initialize() {
await sendAndWaitForReply(event.windowContext, { type: 'show_toast_request', ...args });
},
},
prompt: {
async show(args) {
const reply: ShowPromptResponse = await sendAndWaitForReply(event.windowContext, {
type: 'show_prompt_request',
...args,
});
return reply.value;
},
},
httpResponse: {
async find(args) {
const payload = { type: 'find_http_responses_request', ...args } as const;

View File

@@ -17,6 +17,7 @@ use fern::colors::ColoredLevelConfig;
use log::{debug, error, info, warn};
use rand::random;
use regex::Regex;
use serde::Serialize;
use serde_json::{json, Value};
#[cfg(target_os = "macos")]
use tauri::TitleBarStyle;
@@ -62,7 +63,8 @@ use yaak_plugin_runtime::events::{
BootResponse, CallHttpRequestActionRequest, FilterResponse, FindHttpResponsesResponse,
GetHttpRequestActionsResponse, GetHttpRequestByIdResponse, GetTemplateFunctionsResponse, Icon,
InternalEvent, InternalEventPayload, RenderHttpRequestResponse, RenderPurpose,
SendHttpRequestResponse, ShowToastRequest, TemplateRenderResponse, WindowContext,
SendHttpRequestResponse, ShowPromptResponse, ShowToastRequest, TemplateRenderResponse,
WindowContext,
};
use yaak_plugin_runtime::plugin_handle::PluginHandle;
use yaak_templates::{Parser, Tokens};
@@ -2158,6 +2160,40 @@ fn monitor_plugin_events<R: Runtime>(app_handle: &AppHandle<R>) {
});
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct FrontendCall<T: Serialize + Clone> {
args: T,
reply_id: String,
}
async fn call_frontend<T: Serialize + Clone, R: Runtime>(
window: WebviewWindow<R>,
event_name: &str,
args: T,
) -> ShowPromptResponse {
let reply_id = format!("{event_name}_reply");
let payload = FrontendCall {
args,
reply_id: reply_id.clone(),
};
window.emit_to(window.label(), event_name, payload).unwrap();
let (tx, mut rx) = tokio::sync::watch::channel(ShowPromptResponse::default());
let event_id = window.clone().listen(reply_id, move |ev| {
println!("GOT REPLY {ev:?}");
let resp: ShowPromptResponse = serde_json::from_str(ev.payload()).unwrap();
_ = tx.send(resp);
});
// When reply shows up, unlisten to events and return
_ = rx.changed().await;
window.unlisten(event_id);
let foo = rx.borrow();
foo.clone()
}
async fn handle_plugin_event<R: Runtime>(
app_handle: &AppHandle<R>,
event: &InternalEvent,
@@ -2184,6 +2220,12 @@ async fn handle_plugin_event<R: Runtime>(
};
None
}
InternalEventPayload::ShowPromptRequest(req) => {
let window = get_window_from_window_context(app_handle, &window_context)
.expect("Failed to find window for render");
let resp = call_frontend(window, "show_prompt", req).await;
Some(InternalEventPayload::ShowPromptResponse(resp))
}
InternalEventPayload::FindHttpResponsesRequest(req) => {
let http_responses = list_http_responses(
app_handle,

View File

@@ -55,6 +55,7 @@ impl TemplateCallback for PluginTemplateCallback {
TemplateFunctionArg::Text(a) => a.base,
TemplateFunctionArg::Select(a) => a.base,
TemplateFunctionArg::Checkbox(a) => a.base,
TemplateFunctionArg::File(a) => a.base,
TemplateFunctionArg::HttpRequest(a) => a.base,
};
if let None = args_with_defaults.get(base.name.as_str()) {

View File

@@ -33,7 +33,7 @@ export type FilterRequest = { content: string, filter: string, };
export type FilterResponse = { content: string, };
export type FindHttpResponsesRequest = { requestId: string, limit: number | null, };
export type FindHttpResponsesRequest = { requestId: string, limit?: number, };
export type FindHttpResponsesResponse = { httpResponses: Array<HttpResponse>, };
@@ -47,9 +47,9 @@ export type GetHttpRequestByIdResponse = { httpRequest: HttpRequest | null, };
export type GetTemplateFunctionsResponse = { functions: Array<TemplateFunction>, pluginRefId: string, };
export type HttpRequestAction = { key: string, label: string, icon: string | null, };
export type HttpRequestAction = { key: string, label: string, icon?: Icon, };
export type Icon = "copy" | "info" | "check_circle" | "alert_triangle";
export type Icon = "copy" | "info" | "check_circle" | "alert_triangle" | "_unknown";
export type ImportRequest = { content: string, };
@@ -59,7 +59,9 @@ export type ImportResponse = { resources: ImportResources, };
export type InternalEvent = { id: string, pluginRefId: string, replyId: string | null, payload: InternalEventPayload, windowContext: WindowContext, };
export type InternalEventPayload = { "type": "boot_request" } & BootRequest | { "type": "boot_response" } & BootResponse | { "type": "reload_request" } | { "type": "reload_response" } | { "type": "terminate_request" } | { "type": "terminate_response" } | { "type": "import_request" } & ImportRequest | { "type": "import_response" } & ImportResponse | { "type": "filter_request" } & FilterRequest | { "type": "filter_response" } & FilterResponse | { "type": "export_http_request_request" } & ExportHttpRequestRequest | { "type": "export_http_request_response" } & ExportHttpRequestResponse | { "type": "send_http_request_request" } & SendHttpRequestRequest | { "type": "send_http_request_response" } & SendHttpRequestResponse | { "type": "get_http_request_actions_request" } & GetHttpRequestActionsRequest | { "type": "get_http_request_actions_response" } & GetHttpRequestActionsResponse | { "type": "call_http_request_action_request" } & CallHttpRequestActionRequest | { "type": "get_template_functions_request" } | { "type": "get_template_functions_response" } & GetTemplateFunctionsResponse | { "type": "call_template_function_request" } & CallTemplateFunctionRequest | { "type": "call_template_function_response" } & CallTemplateFunctionResponse | { "type": "copy_text_request" } & CopyTextRequest | { "type": "render_http_request_request" } & RenderHttpRequestRequest | { "type": "render_http_request_response" } & RenderHttpRequestResponse | { "type": "template_render_request" } & TemplateRenderRequest | { "type": "template_render_response" } & TemplateRenderResponse | { "type": "show_toast_request" } & ShowToastRequest | { "type": "get_http_request_by_id_request" } & GetHttpRequestByIdRequest | { "type": "get_http_request_by_id_response" } & GetHttpRequestByIdResponse | { "type": "find_http_responses_request" } & FindHttpResponsesRequest | { "type": "find_http_responses_response" } & FindHttpResponsesResponse | { "type": "empty_response" };
export type InternalEventPayload = { "type": "boot_request" } & BootRequest | { "type": "boot_response" } & BootResponse | { "type": "reload_request" } | { "type": "reload_response" } | { "type": "terminate_request" } | { "type": "terminate_response" } | { "type": "import_request" } & ImportRequest | { "type": "import_response" } & ImportResponse | { "type": "filter_request" } & FilterRequest | { "type": "filter_response" } & FilterResponse | { "type": "export_http_request_request" } & ExportHttpRequestRequest | { "type": "export_http_request_response" } & ExportHttpRequestResponse | { "type": "send_http_request_request" } & SendHttpRequestRequest | { "type": "send_http_request_response" } & SendHttpRequestResponse | { "type": "get_http_request_actions_request" } & GetHttpRequestActionsRequest | { "type": "get_http_request_actions_response" } & GetHttpRequestActionsResponse | { "type": "call_http_request_action_request" } & CallHttpRequestActionRequest | { "type": "get_template_functions_request" } | { "type": "get_template_functions_response" } & GetTemplateFunctionsResponse | { "type": "call_template_function_request" } & CallTemplateFunctionRequest | { "type": "call_template_function_response" } & CallTemplateFunctionResponse | { "type": "copy_text_request" } & CopyTextRequest | { "type": "render_http_request_request" } & RenderHttpRequestRequest | { "type": "render_http_request_response" } & RenderHttpRequestResponse | { "type": "template_render_request" } & TemplateRenderRequest | { "type": "template_render_response" } & TemplateRenderResponse | { "type": "show_toast_request" } & ShowToastRequest | { "type": "show_prompt_request" } & ShowPromptRequest | { "type": "show_prompt_response" } & ShowPromptResponse | { "type": "get_http_request_by_id_request" } & GetHttpRequestByIdRequest | { "type": "get_http_request_by_id_response" } & GetHttpRequestByIdResponse | { "type": "find_http_responses_request" } & FindHttpResponsesRequest | { "type": "find_http_responses_response" } & FindHttpResponsesResponse | { "type": "empty_response" };
export type OpenFileFilter = { name: string, extensions: Array<string>, };
export type RenderHttpRequestRequest = { httpRequest: HttpRequest, purpose: RenderPurpose, };
@@ -71,23 +73,41 @@ export type SendHttpRequestRequest = { httpRequest: HttpRequest, };
export type SendHttpRequestResponse = { httpResponse: HttpResponse, };
export type ShowToastRequest = { message: string, color?: Color | null, icon?: Icon | null, };
export type ShowPromptRequest = { id: string, title: string, label: string, description?: string, defaultValue?: string, placeholder?: string,
/**
* Text to add to the confirmation button
*/
confirmText?: string,
/**
* Text to add to the cancel button
*/
cancelText?: string,
/**
* Require the user to enter a non-empty value
*/
require?: boolean, };
export type TemplateFunction = { name: string, aliases: Array<string>, args: Array<TemplateFunctionArg>, };
export type ShowPromptResponse = { value: string, };
export type TemplateFunctionArg = { "type": "text" } & TemplateFunctionTextArg | { "type": "select" } & TemplateFunctionSelectArg | { "type": "checkbox" } & TemplateFunctionCheckboxArg | { "type": "http_request" } & TemplateFunctionHttpRequestArg;
export type ShowToastRequest = { message: string, color?: Color, icon?: Icon, };
export type TemplateFunctionBaseArg = { name: string, optional?: boolean | null, label?: string | null, defaultValue?: string | null, };
export type TemplateFunction = { name: string, aliases?: Array<string>, args: Array<TemplateFunctionArg>, };
export type TemplateFunctionCheckboxArg = { name: string, optional?: boolean | null, label?: string | null, defaultValue?: string | null, };
export type TemplateFunctionArg = { "type": "text" } & TemplateFunctionTextArg | { "type": "select" } & TemplateFunctionSelectArg | { "type": "checkbox" } & TemplateFunctionCheckboxArg | { "type": "http_request" } & TemplateFunctionHttpRequestArg | { "type": "file" } & TemplateFunctionFileArg;
export type TemplateFunctionHttpRequestArg = { name: string, optional?: boolean | null, label?: string | null, defaultValue?: string | null, };
export type TemplateFunctionBaseArg = { name: string, optional?: boolean, label?: string, defaultValue?: string, };
export type TemplateFunctionSelectArg = { options: Array<TemplateFunctionSelectOption>, name: string, optional?: boolean | null, label?: string | null, defaultValue?: string | null, };
export type TemplateFunctionCheckboxArg = { name: string, optional?: boolean, label?: string, defaultValue?: string, };
export type TemplateFunctionFileArg = { title: string, multiple?: boolean, directory?: boolean, defaultPath?: string, filters?: Array<OpenFileFilter>, name: string, optional?: boolean, label?: string, defaultValue?: string, };
export type TemplateFunctionHttpRequestArg = { name: string, optional?: boolean, label?: string, defaultValue?: string, };
export type TemplateFunctionSelectArg = { options: Array<TemplateFunctionSelectOption>, name: string, optional?: boolean, label?: string, defaultValue?: string, };
export type TemplateFunctionSelectOption = { name: string, value: string, };
export type TemplateFunctionTextArg = { placeholder?: string | null, name: string, optional?: boolean | null, label?: string | null, defaultValue?: string | null, };
export type TemplateFunctionTextArg = { placeholder?: string, name: string, optional?: boolean, label?: string, defaultValue?: string, };
export type TemplateRenderRequest = { data: JsonValue, purpose: RenderPurpose, };

View File

@@ -76,6 +76,9 @@ pub enum InternalEventPayload {
ShowToastRequest(ShowToastRequest),
ShowPromptRequest(ShowPromptRequest),
ShowPromptResponse(ShowPromptResponse),
GetHttpRequestByIdRequest(GetHttpRequestByIdRequest),
GetHttpRequestByIdResponse(GetHttpRequestByIdResponse),
@@ -203,12 +206,46 @@ pub struct TemplateRenderResponse {
#[ts(export, export_to = "events.ts")]
pub struct ShowToastRequest {
pub message: String,
#[ts(optional = nullable)]
#[ts(optional)]
pub color: Option<Color>,
#[ts(optional = nullable)]
#[ts(optional)]
pub icon: Option<Icon>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
pub struct ShowPromptRequest {
// A unique ID to identify the prompt (eg. "enter-password")
pub id: String,
// Title to show on the prompt dialog
pub title: String,
// Text to show on the label above the input
pub label: String,
#[ts(optional)]
pub description: Option<String>,
#[ts(optional)]
pub default_value: Option<String>,
#[ts(optional)]
pub placeholder: Option<String>,
/// Text to add to the confirmation button
#[ts(optional)]
pub confirm_text: Option<String>,
/// Text to add to the cancel button
#[ts(optional)]
pub cancel_text: Option<String>,
/// Require the user to enter a non-empty value
#[ts(optional)]
pub require: Option<bool>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
pub struct ShowPromptResponse {
pub value: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[serde(rename_all = "snake_case")]
#[ts(export, export_to = "events.ts")]
@@ -238,6 +275,10 @@ pub enum Icon {
Info,
CheckCircle,
AlertTriangle,
#[serde(untagged)]
#[ts(type = "\"_unknown\"")]
_Unknown(String),
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
@@ -253,7 +294,8 @@ pub struct GetTemplateFunctionsResponse {
#[ts(export, export_to = "events.ts")]
pub struct TemplateFunction {
pub name: String,
pub aliases: Vec<String>,
#[ts(optional)]
pub aliases: Option<Vec<String>>,
pub args: Vec<TemplateFunctionArg>,
}
@@ -265,6 +307,7 @@ pub enum TemplateFunctionArg {
Select(TemplateFunctionSelectArg),
Checkbox(TemplateFunctionCheckboxArg),
HttpRequest(TemplateFunctionHttpRequestArg),
File(TemplateFunctionFileArg),
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
@@ -272,11 +315,11 @@ pub enum TemplateFunctionArg {
#[ts(export, export_to = "events.ts")]
pub struct TemplateFunctionBaseArg {
pub name: String,
#[ts(optional = nullable)]
#[ts(optional)]
pub optional: Option<bool>,
#[ts(optional = nullable)]
#[ts(optional)]
pub label: Option<String>,
#[ts(optional = nullable)]
#[ts(optional)]
pub default_value: Option<String>,
}
@@ -286,7 +329,7 @@ pub struct TemplateFunctionBaseArg {
pub struct TemplateFunctionTextArg {
#[serde(flatten)]
pub base: TemplateFunctionBaseArg,
#[ts(optional = nullable)]
#[ts(optional)]
pub placeholder: Option<String>,
}
@@ -298,6 +341,31 @@ pub struct TemplateFunctionHttpRequestArg {
pub base: TemplateFunctionBaseArg,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
pub struct TemplateFunctionFileArg {
#[serde(flatten)]
pub base: TemplateFunctionBaseArg,
pub title: String,
#[ts(optional)]
pub multiple: Option<bool>,
#[ts(optional)]
pub directory: Option<bool>,
#[ts(optional)]
pub default_path: Option<String>,
#[ts(optional)]
pub filters: Option<Vec<OpenFileFilter>>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
pub struct OpenFileFilter {
pub name: String,
pub extensions: Vec<String>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
@@ -379,7 +447,8 @@ pub struct GetHttpRequestActionsResponse {
pub struct HttpRequestAction {
pub key: String,
pub label: String,
pub icon: Option<String>,
#[ts(optional)]
pub icon: Option<Icon>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
@@ -417,6 +486,7 @@ pub struct GetHttpRequestByIdResponse {
#[ts(export, export_to = "events.ts")]
pub struct FindHttpResponsesRequest {
pub request_id: String,
#[ts(optional)]
pub limit: Option<i32>,
}

View File

@@ -212,7 +212,7 @@ impl PluginManager {
};
let plugin_handle = PluginHandle::new(dir, tx.clone());
// Add the new plugin
// Add the new plugin
self.plugins.lock().await.push(plugin_handle.clone());
// Boot the plugin

View File

@@ -59,8 +59,8 @@ export function CookieDropdown() {
Enter a new name for <InlineCode>{activeCookieJar?.name}</InlineCode>
</>
),
name: 'name',
label: 'Name',
confirmText: 'Save',
placeholder: 'New name',
defaultValue: activeCookieJar?.name,
});

View File

@@ -274,8 +274,8 @@ function SidebarButton({
Enter a new name for <InlineCode>{environment.name}</InlineCode>
</>
),
name: 'name',
label: 'Name',
confirmText: 'Save',
placeholder: 'New Name',
defaultValue: environment.name,
});

View File

@@ -1,6 +1,8 @@
import { useQueryClient } from '@tanstack/react-query';
import { emit } from '@tauri-apps/api/event';
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow';
import type { AnyModel } from '@yaakapp-internal/models';
import type { ShowPromptRequest, ShowPromptResponse } from '@yaakapp-internal/plugin';
import { useSetAtom } from 'jotai';
import { useEffect } from 'react';
import { useEnsureActiveCookieJar, useMigrateActiveCookieJarId } from '../hooks/useActiveCookieJar';
@@ -19,6 +21,7 @@ import { keyValueQueryKey } from '../hooks/useKeyValue';
import { useListenToTauriEvent } from '../hooks/useListenToTauriEvent';
import { useNotificationToast } from '../hooks/useNotificationToast';
import { pluginsAtom } from '../hooks/usePlugins';
import { usePrompt } from '../hooks/usePrompt';
import { useRecentCookieJars } from '../hooks/useRecentCookieJars';
import { useRecentEnvironments } from '../hooks/useRecentEnvironments';
import { useRecentRequests } from '../hooks/useRecentRequests';
@@ -176,6 +179,16 @@ export function GlobalHooks() {
useHotKey('app.zoom_reset', zoom.zoomReset);
useListenToTauriEvent('zoom_reset', zoom.zoomReset);
const prompt = usePrompt();
useListenToTauriEvent<{ replyId: string; args: ShowPromptRequest }>(
'show_prompt',
async (event) => {
const value = await prompt(event.payload.args);
const result: ShowPromptResponse = { value };
await emit(event.payload.replyId, result);
},
);
const copy = useCopy();
useListenToTauriEvent('generate_theme_css', () => {
const themesCss = [

View File

@@ -43,9 +43,9 @@ export const RequestMethodDropdown = memo(function RequestMethodDropdown({
const newMethod = await prompt({
id: 'custom-method',
label: 'Http Method',
name: 'httpMethod',
defaultValue: '',
title: 'Custom Method',
confirmText: 'Save',
description: 'Enter a custom method name',
placeholder: 'CUSTOM',
});

View File

@@ -762,7 +762,7 @@ function SidebarItem({
Enter a new name for <InlineCode>{itemName}</InlineCode>
</>
),
name: 'name',
confirmText: 'Save',
label: 'Name',
placeholder: 'New Name',
defaultValue: itemName,

View File

@@ -93,6 +93,7 @@ export function TemplateFunctionDialog({ templateFunction, hide, initialTokens,
return (
<VStack className="pb-3" space={4}>
<h1 className="font-mono !text-base">{templateFunction.name}()</h1>
<VStack space={2}>
{templateFunction.args.map((a: TemplateFunctionArg, i: number) => {
switch (a.type) {

View File

@@ -62,7 +62,6 @@ export const WorkspaceActionsDropdown = memo(function WorkspaceActionsDropdown({
Enter a new name for <InlineCode>{activeWorkspace?.name}</InlineCode>
</>
),
name: 'name',
label: 'Name',
placeholder: 'New Name',
defaultValue: activeWorkspace?.name,

View File

@@ -13,7 +13,7 @@ export type TwigCompletionOptionNamespace = {
export type TwigCompletionOptionFunction = {
args: { name: string }[];
aliases: string[];
aliases?: string[];
type: 'function';
};
@@ -65,7 +65,7 @@ export function twigCompletion({ options }: TwigCompletionConfig) {
if (matchSegments.length < optionSegments.length) {
return [
{
label: optionSegments.slice(0, matchSegments.length).join('.'),
label: optionSegments.slice(0, matchSegments.length).join('.') + '…',
apply: optionSegments.slice(0, matchSegments.length).join('.'),
type: 'namespace',
},

View File

@@ -24,7 +24,7 @@ export type InputProps = Omit<
| 'onKeyDown'
| 'readOnly'
> & {
name: string;
name?: string;
type?: 'text' | 'password';
label: string;
hideLabel?: boolean;
@@ -41,7 +41,7 @@ export type InputProps = Omit<
rightSlot?: ReactNode;
size?: 'xs' | 'sm' | 'md' | 'auto';
className?: string;
placeholder: string;
placeholder?: string;
validate?: boolean | ((v: string) => boolean);
require?: boolean;
wrapLines?: boolean;

View File

@@ -519,8 +519,7 @@ function PairEditorRow({
label: 'Content-Type',
placeholder: 'text/plain',
defaultValue: pairContainer.pair.contentType ?? '',
name: 'content-type',
confirmLabel: 'Set',
confirmText: 'Set',
description: 'Leave blank to auto-detect',
});
handleChangeValueContentType(v);

View File

@@ -1,30 +1,25 @@
import type { FormEvent } from 'react';
import type { ShowPromptRequest } from '@yaakapp-internal/plugin';
import type { FormEvent, ReactNode } from 'react';
import { useCallback, useState } from 'react';
import { Button } from '../components/core/Button';
import type { InputProps } from '../components/core/Input';
import { PlainInput } from '../components/core/PlainInput';
import { HStack } from '../components/core/Stacks';
export interface PromptProps {
export type PromptProps = Omit<ShowPromptRequest, 'id' | 'title' | 'description'> & {
description?: ReactNode;
onHide: () => void;
onResult: (value: string) => void;
label: InputProps['label'];
name: InputProps['name'];
defaultValue: InputProps['defaultValue'];
placeholder: InputProps['placeholder'];
require?: InputProps['require'];
confirmLabel?: string;
}
};
export function Prompt({
onHide,
label,
name,
defaultValue,
placeholder,
onResult,
require = true,
confirmLabel = 'Save',
require,
confirmText,
cancelText,
}: PromptProps) {
const [value, setValue] = useState<string>(defaultValue ?? '');
const handleSubmit = useCallback(
@@ -45,18 +40,17 @@ export function Prompt({
hideLabel
autoSelect
require={require}
placeholder={placeholder}
placeholder={placeholder ?? 'Enter text'}
label={label}
name={name}
defaultValue={defaultValue}
onChange={setValue}
/>
<HStack space={2} justifyContent="end">
<Button onClick={onHide} variant="border" color="secondary">
Cancel
{cancelText || 'Cancel'}
</Button>
<Button type="submit" color="primary">
{confirmLabel}
{confirmText || 'Done'}
</Button>
</HStack>
</form>

View File

@@ -17,9 +17,9 @@ export function useCreateCookieJar() {
}
const name = await prompt({
id: 'new-cookie-jar',
name: 'name',
title: 'New CookieJar',
placeholder: 'My Jar',
confirmText: 'Create',
label: 'Name',
defaultValue: 'My Jar',
});

View File

@@ -16,12 +16,12 @@ export function useCreateEnvironment() {
mutationFn: async () => {
const name = await prompt({
id: 'new-environment',
name: 'name',
title: 'New Environment',
description: 'Create multiple environments with different sets of variables',
label: 'Name',
placeholder: 'My Environment',
defaultValue: 'My Environment',
confirmText: 'Create',
});
return invokeCmd('cmd_create_environment', {
name,

View File

@@ -19,11 +19,10 @@ export function useCreateFolder() {
patch.name ||
(await prompt({
id: 'new-folder',
name: 'name',
label: 'Name',
defaultValue: 'Folder',
title: 'New Folder',
confirmLabel: 'Create',
confirmText: 'Create',
placeholder: 'Name',
}));
patch.sortPriority = patch.sortPriority || -Date.now();

View File

@@ -12,12 +12,11 @@ export function useCreateWorkspace() {
mutationFn: async () => {
const name = await prompt({
id: 'new-workspace',
name: 'name',
label: 'Name',
defaultValue: 'My Workspace',
title: 'New Workspace',
confirmLabel: 'Create',
placeholder: 'My Workspace',
confirmText: 'Create',
});
return invokeCmd('cmd_create_workspace', { name });
},

View File

@@ -16,6 +16,7 @@ export function useHttpRequestActions() {
const responses = (await invokeCmd(
'cmd_http_request_actions',
)) as GetHttpRequestActionsResponse[];
console.log('REQUEST ACTIONS', responses);
return responses;
},
});

View File

@@ -3,20 +3,12 @@ import { useDialog } from '../components/DialogContext';
import type { PromptProps } from './Prompt';
import { Prompt } from './Prompt';
type Props = Pick<DialogProps, 'title' | 'description'> &
Omit<PromptProps, 'onResult' | 'onHide'> & { id: string };
export function usePrompt() {
const dialog = useDialog();
return ({
id,
title,
description,
name,
label,
defaultValue,
placeholder,
confirmLabel,
require,
}: Pick<DialogProps, 'title' | 'description'> &
Omit<PromptProps, 'onResult' | 'onHide'> & { id: string }) =>
return ({ id, title, description, ...props }: Props) =>
new Promise((onResult: PromptProps['onResult']) => {
dialog.show({
id,
@@ -24,17 +16,7 @@ export function usePrompt() {
description,
hideX: true,
size: 'sm',
render: ({ hide }) =>
Prompt({
onHide: hide,
onResult,
name,
label,
defaultValue,
placeholder,
confirmLabel,
require,
}),
render: ({ hide: onHide }) => Prompt({ onHide, onResult, ...props }),
});
});
}

View File

@@ -28,10 +28,10 @@ export function useRenameRequest(requestId: string | null) {
Enter a new name for <InlineCode>{request.name}</InlineCode>
</>
),
name: 'name',
label: 'Name',
placeholder: 'New Name',
defaultValue: request.name,
confirmText: 'Save',
});
if (request.model === 'http_request') {
updateHttpRequest.mutate({ id: request.id, update: (r: HttpRequest) => ({ ...r, name }) });