Share Yaak's request and response panes (#815)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Gregory SchierandClaude Opus 5.5 authored and GitHub committed 2026-11-05 09:23:38 -07:00
1 parent 4ad0533901
commit 08a98d5f82
85 files changed
+5567 -2872

No files matched your search

+194
View File
@@ -0,0 +1,194 @@
import type { Completion } from "@codemirror/autocomplete";
import type { Extension } from "@codemirror/state";
import type { GenericCompletionOption } from "@yaakapp-internal/plugins";
import type { ComponentType, ReactNode } from "react";
import { createContext, useContext, useMemo } from "react";
import type { SetStoredValue, StoredValue, UseStoredValue } from "../hooks/useStoredValue";
import { useInMemoryStoredValue } from "../hooks/useStoredValue";
import type { HttpBodyLoader } from "./http/types";
import type { MultipartViewerProps } from "./viewers/MultipartViewer";
import { MultipartViewer } from "./viewers/MultipartViewer";
import type { TextViewerProps } from "./viewers/TextViewer";
import { TextViewer } from "./viewers/TextViewer";
import type { ButtonProps } from "./Button";
import type { DialogProps } from "./Dialog";
import type { CodeEditorProps } from "./editor/CodeEditor";
import { ErrorBoundary } from "./ErrorBoundary";
import { LazyCodeEditor } from "./editor/LazyCodeEditor";
import type { InputProps } from "./Input";
import { Input } from "./Input";
export type GenericCompletion = GenericCompletionOption & {
apply?: Completion["apply"];
};
export interface GenericCompletionConfig {
minMatch?: number;
options: GenericCompletion[];
}
/** A template variable, as far as shared components need to know one */
export interface TemplateVariableRef {
variable: { name: string };
}
/**
* What a host's editor adds on top of `CodeEditor`, like the client's template tags and
* environment variables. Shared components pass these through; a host without templating
* ignores them.
*/
export interface TemplatingProps {
autocomplete?: GenericCompletionConfig;
autocompleteFunctions?: boolean;
autocompleteVariables?: boolean | ((v: TemplateVariableRef) => boolean);
forcedEnvironmentId?: string;
extraExtensions?: Extension[] | Extension;
}
/** `CodeEditor`'s languages, plus the host-only ones it may know (the client's URL and pairs) */
export type HostEditorLanguage = CodeEditorProps["language"] | "pairs" | "url";
export type HostEditorProps = Omit<CodeEditorProps, "language" | "extensions" | "keymap"> &
TemplatingProps & { language?: HostEditorLanguage };
export type HostInputProps = Omit<
InputProps,
"language" | "extensions" | "renderEditor" | "renderOptionsPicker"
> &
TemplatingProps & {
language?: HostEditorLanguage;
/** Show a chevron that opens the `autocomplete` options as a menu */
showOptionsPicker?: boolean;
};
export type SelectFileProps = Omit<ButtonProps, "type" | "onChange"> & {
onChange: (value: { filePath: string | null; contentType: string | null }) => void;
filePath: string | null;
nameOverride?: string | null;
inline?: boolean;
};
export interface HostPromptArgs {
id: string;
title: ReactNode;
label: string;
description?: ReactNode;
placeholder?: string;
defaultValue?: string;
confirmText?: string;
required?: boolean;
}
export type HostDialogArgs = {
id: string;
render: ComponentType<{ hide: () => void }>;
} & Omit<DialogProps, "open" | "children" | "onClose">;
export type StoredValueNamespace = "global" | "no_sync";
/** Like `UseStoredValue`, but told whether the value is a synced preference or local state */
export type UseHostStoredValue = <T extends StoredValue>(
key: string | string[] | null,
fallback: T,
namespace?: StoredValueNamespace,
) => readonly [T, SetStoredValue<T>];
/**
* What shared editing components get from the app they run in. The client provides its
* templated editor, file picker, dialogs and key-value store; anything else (the proxy) gets
* plain `CodeEditor`s, in-memory state, and no dialogs.
*/
export interface EditorHost {
Editor: ComponentType<HostEditorProps>;
Input: ComponentType<HostInputProps>;
SelectFile: ComponentType<SelectFileProps> | null;
showDialog: ((args: HostDialogArgs) => void) | null;
showPrompt: ((args: HostPromptArgs) => Promise<string | null>) | null;
basename: (path: string) => Promise<string | null>;
useStoredValue: UseHostStoredValue;
/** Where a `DetailsBanner` with a `storageKey` keeps whether it's open */
useDetailsBannerStoredValue: UseStoredValue;
ErrorBoundary: ComponentType<{ name: string; children: ReactNode }>;
/** The text viewer, with whatever the app adds to it (formatting, links) */
TextViewer: ComponentType<TextViewerProps>;
/** The multipart viewer, with whatever the app adds to it (a PDF viewer) */
MultipartViewer: ComponentType<MultipartViewerProps>;
/** Viewers only the app has, like the client's PDF viewer; null to use the shared ones */
renderBodyViewer?: (args: HostBodyViewerArgs) => ReactNode | null;
/** Open a URL outside the app */
openUrl: ((url: string) => void) | null;
/** Copy text to the clipboard, quietly, as rows of values do */
copy: (text: string) => void;
}
export interface HostBodyViewerArgs {
/** Which body: a response's, or the request body it was sent with */
source: "response" | "request";
/** The media type, without parameters */
mimeType: string | null;
viewMode: "pretty" | "raw";
body: HttpBodyLoader;
/** Whether the whole body has arrived */
complete: boolean;
}
const defaultEditorHost: EditorHost = {
ErrorBoundary,
useDetailsBannerStoredValue: useInMemoryStoredValue,
TextViewer,
MultipartViewer,
openUrl: null,
copy: (text) => {
navigator.clipboard.writeText(text).catch(console.error);
},
Editor: PlainEditor,
Input: PlainInputEditor,
SelectFile: null,
showDialog: null,
showPrompt: null,
basename: async (path) => path.split(/[\\/]/).pop() ?? null,
useStoredValue: useInMemoryStoredValue,
};
const EditorHostContext = createContext<EditorHost>(defaultEditorHost);
export function EditorHostProvider({
value,
children,
}: {
value: Partial<EditorHost>;
children: ReactNode;
}) {
const parent = useContext(EditorHostContext);
const merged = useMemo(() => ({ ...parent, ...value }), [parent, value]);
return <EditorHostContext.Provider value={merged}>{children}</EditorHostContext.Provider>;
}
export function useEditorHost(): EditorHost {
return useContext(EditorHostContext);
}
/** Drops what only a host's editor understands, leaving what `CodeEditor` does */
function stripTemplating<T extends TemplatingProps & { language?: HostEditorLanguage }>({
autocomplete: _autocomplete,
autocompleteFunctions: _autocompleteFunctions,
autocompleteVariables: _autocompleteVariables,
forcedEnvironmentId: _forcedEnvironmentId,
extraExtensions,
language,
...props
}: T) {
return {
...props,
extensions: extraExtensions,
language: language === "pairs" || language === "url" ? null : language,
};
}
function PlainEditor(props: HostEditorProps) {
return <LazyCodeEditor {...stripTemplating(props)} />;
}
function PlainInputEditor({ showOptionsPicker: _showOptionsPicker, ...props }: HostInputProps) {
return <Input {...stripTemplating(props)} />;
}
@@ -0,0 +1,24 @@
import classNames from "classnames";
import type { ReactNode } from "react";
interface Props {
children: ReactNode;
className?: string;
wrapperClassName?: string;
}
export function EmptyStateText({ children, className, wrapperClassName }: Props) {
return (
<div className={classNames("w-full h-full pb-2", wrapperClassName)}>
<div
className={classNames(
className,
"rounded-lg border border-dashed border-border-subtle",
"h-full py-2 text-text-subtlest flex items-center justify-center italic",
)}
>
{children}
</div>
</div>
);
}
@@ -0,0 +1,15 @@
import classNames from "classnames";
import type { HotkeyAction } from "../hooks/useHotKey";
import { useHotkeyLabel } from "../hooks/useHotKey";
interface Props {
action: HotkeyAction;
className?: string;
}
export function HotkeyLabel({ action, className }: Props) {
const label = useHotkeyLabel(action);
return (
<span className={classNames(className, "text-text-subtle whitespace-nowrap")}>{label}</span>
);
}
@@ -0,0 +1,25 @@
import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, test, vi } from "vite-plus/test";
import type { HotkeyAction } from "../hooks/useHotKey";
import { HotkeyList } from "./HotkeyList";
vi.mock("./Hotkey", () => ({
Hotkey: ({ action }: { action: HotkeyAction }) =>
action === "sidebar.selected.move" ? null : <span>{action}</span>,
}));
vi.mock("./HotkeyLabel", () => ({
HotkeyLabel: ({ action }: { action: HotkeyAction }) => <span>{action}</span>,
}));
describe("HotkeyList", () => {
test("keeps a grid cell for actions without a shortcut", () => {
const markup = renderToStaticMarkup(
<HotkeyList hotkeys={["sidebar.selected.move", "request.send"]} />,
);
expect(markup).toContain(
'<span>sidebar.selected.move</span><div class="ml-4"></div><span>request.send</span>',
);
});
});
+31
View File
@@ -0,0 +1,31 @@
import classNames from "classnames";
import type { ReactNode } from "react";
import { Fragment } from "react";
import type { HotkeyAction } from "../hooks/useHotKey";
import { Hotkey } from "./Hotkey";
import { HotkeyLabel } from "./HotkeyLabel";
interface Props {
hotkeys: HotkeyAction[];
bottomSlot?: ReactNode;
className?: string;
}
export const HotkeyList = ({ hotkeys, bottomSlot, className }: Props) => {
return (
<div className={classNames(className, "h-full flex items-center justify-center")}>
<div className="grid gap-2 grid-cols-[auto_auto]">
{hotkeys.map((hotkey) => (
<Fragment key={hotkey}>
<HotkeyLabel className="truncate" action={hotkey} />
{/* Keep this grid cell when Hotkey renders nothing so later rows stay aligned. */}
<div className="ml-4">
<Hotkey action={hotkey} />
</div>
</Fragment>
))}
{bottomSlot}
</div>
</div>
);
};
+14
View File
@@ -0,0 +1,14 @@
import classNames from "classnames";
import type { ButtonProps } from "./Button";
import { Button } from "./Button";
export function PillButton({ className, ...props }: ButtonProps) {
return (
<Button
size="2xs"
variant="border"
className={classNames(className, "rounded-full! mx-1 px-3!")}
{...props}
/>
);
}
@@ -0,0 +1,69 @@
import type { ReactNode } from "react";
import { useMemo } from "react";
import type { DropdownItem, DropdownItemSeparator, DropdownProps } from "./Dropdown";
import { Dropdown } from "./Dropdown";
import { Icon } from "./Icon";
export type RadioDropdownItem<T = string | null> =
| {
type?: "default";
label: ReactNode;
shortLabel?: ReactNode;
value: T;
rightSlot?: ReactNode;
}
| DropdownItemSeparator;
export interface RadioDropdownProps<T = string | null> {
value: T;
onChange: (value: T) => void;
itemsBefore?: DropdownItem[];
items: RadioDropdownItem<T>[];
itemsAfter?: DropdownItem[];
children: DropdownProps["children"];
}
export function RadioDropdown<T = string | null>({
value,
items,
itemsAfter,
itemsBefore,
onChange,
children,
}: RadioDropdownProps<T>) {
const dropdownItems = useMemo(
() => [
...((itemsBefore
? [
...itemsBefore,
{
type: "separator",
hidden: itemsBefore[itemsBefore.length - 1]?.type === "separator",
},
]
: []) as DropdownItem[]),
...items.map((item) => {
if (item.type === "separator") {
return item;
}
return {
key: item.value,
label: item.label,
rightSlot: item.rightSlot,
onSelect: () => onChange(item.value),
leftSlot: <Icon icon={value === item.value ? "check" : "empty"} />,
} as DropdownItem;
}),
...((itemsAfter
? [{ type: "separator", hidden: itemsAfter[0]?.type === "separator" }, ...itemsAfter]
: []) as DropdownItem[]),
],
[itemsBefore, items, itemsAfter, value, onChange],
);
return (
<Dropdown fullWidth items={dropdownItems}>
{children}
</Dropdown>
);
}
@@ -0,0 +1,70 @@
import { type ReactNode, useMemo } from "react";
import { useToggle } from "../../hooks/useToggle";
import { isProbablyTextContentType } from "../../lib/mimeType";
import { Banner } from "../Banner";
import { Button } from "../Button";
import { InlineCode } from "../InlineCode";
import { HStack } from "../Stacks";
import { SizeTag } from "../tags/SizeTag";
interface Props {
children: ReactNode;
/** Bytes of the body being guarded */
contentLength: number | null;
contentType: string | null;
/** What's being shown, in the notice: "responses" or "content" */
noun?: string;
/** The reveal button's label */
revealLabel?: string;
/** Save the body to a file, when the app can */
onSave?: () => void;
/** Read the body as text for copying, when the app can */
getText?: () => Promise<string | null>;
/** The copy button, rendered by the app so it reports the copy its own way */
renderCopyButton?: (getText: () => Promise<string | null>) => ReactNode;
}
const LARGE_BYTES = 2 * 1000 * 1000;
/** Holds back a body big enough to slow the window down, until asked to show it */
export function ConfirmLargeResponse({
children,
contentLength,
contentType,
noun = "responses",
revealLabel = "Reveal Response",
onSave,
getText,
renderCopyButton,
}: Props) {
const [showLargeResponse, toggleShowLargeResponse] = useToggle();
const isProbablyText = useMemo(() => isProbablyTextContentType(contentType), [contentType]);
const isLarge = (contentLength ?? 0) > LARGE_BYTES;
if (!showLargeResponse && isLarge) {
return (
<Banner color="primary" className="flex flex-col gap-3">
<p>
Showing {noun} over{" "}
<InlineCode>
<SizeTag contentLength={LARGE_BYTES} />
</InlineCode>{" "}
may impact performance
</p>
<HStack wrap space={2}>
<Button color="primary" size="xs" onClick={toggleShowLargeResponse}>
{revealLabel}
</Button>
{onSave != null && (
<Button color="secondary" variant="border" size="xs" onClick={onSave}>
Save to File
</Button>
)}
{isProbablyText && getText != null && renderCopyButton?.(getText)}
</HStack>
</Banner>
);
}
return <>{children}</>;
}
@@ -0,0 +1,68 @@
import { useCallback, useMemo } from "react";
import type { Pair, PairEditorProps } from "../pairs/PairEditor";
import { PairEditor } from "../pairs/PairEditor";
import type { HttpFormFieldView, HttpRequestBodyView } from "./types";
type Props = {
forceUpdateKey: string;
/** The request's id, keeping each request's editor state apart */
requestId: string;
form: HttpFormFieldView[] | undefined;
onChange?: (body: HttpRequestBodyView) => void;
/** Show the fields without a way to change them */
readOnly?: boolean;
};
export function FormMultipartEditor({
requestId,
form,
forceUpdateKey,
onChange,
readOnly,
}: Props) {
const pairs = useMemo<Pair[]>(
() =>
(Array.isArray(form) ? form : []).map((p) => ({
enabled: p.enabled,
name: p.name,
value: (p.file ?? p.value) as string,
contentType: p.contentType,
filename: p.filename,
isFile: !!p.file,
id: p.id,
})),
[form],
);
const handleChange = useCallback<PairEditorProps["onChange"]>(
(pairs) =>
onChange?.({
form: pairs.map((p) => ({
enabled: p.enabled,
name: p.name,
contentType: p.contentType,
filename: p.filename,
file: p.isFile ? p.value : undefined,
value: p.isFile ? undefined : p.value,
id: p.id,
})),
}),
[onChange],
);
return (
<PairEditor
valueAutocompleteFunctions
valueAutocompleteVariables
nameAutocompleteVariables
nameAutocompleteFunctions
allowFileValues
allowMultilineValues
pairs={pairs}
onChange={handleChange}
readOnly={readOnly}
forceUpdateKey={forceUpdateKey}
stateKey={`multipart.${requestId}`}
/>
);
}
@@ -0,0 +1,59 @@
import { useCallback, useMemo } from "react";
import type { Pair, PairEditorProps } from "../pairs/PairEditor";
import { PairOrBulkEditor } from "../pairs/PairOrBulkEditor";
import type { HttpFormFieldView, HttpRequestBodyView } from "./types";
type Props = {
forceUpdateKey: string;
/** The request's id, keeping each request's editor state apart */
requestId: string;
form: HttpFormFieldView[] | undefined;
onChange?: (body: HttpRequestBodyView) => void;
/** Show the fields without a way to change them */
readOnly?: boolean;
};
export function FormUrlencodedEditor({
requestId,
form,
forceUpdateKey,
onChange,
readOnly,
}: Props) {
const pairs = useMemo<Pair[]>(
() =>
(Array.isArray(form) ? form : []).map((p) => ({
enabled: !!p.enabled,
name: p.name || "",
value: p.value || "",
id: p.id,
})),
[form],
);
const handleChange = useCallback<PairEditorProps["onChange"]>(
(pairs) =>
onChange?.({
form: pairs.map((p) => ({ enabled: p.enabled, name: p.name, value: p.value })),
}),
[onChange],
);
return (
<PairOrBulkEditor
allowMultilineValues
preferenceName="form_urlencoded"
valueAutocompleteFunctions
valueAutocompleteVariables
nameAutocompleteFunctions
nameAutocompleteVariables
namePlaceholder="entry_name"
valuePlaceholder="Value"
pairs={pairs}
onChange={handleChange}
readOnly={readOnly}
forceUpdateKey={forceUpdateKey}
stateKey={`urlencoded.${requestId}`}
/>
);
}
@@ -0,0 +1,189 @@
import { acceptLanguages } from "../../lib/data/acceptLanguages";
import { cacheControlDirectives } from "../../lib/data/cacheControl";
import { charsets } from "../../lib/data/charsets";
import { connections } from "../../lib/data/connections";
import { encodings } from "../../lib/data/encodings";
import type { HeaderValuePreset } from "../../lib/data/headerValuePresets";
import { headerNames } from "../../lib/data/headerNames";
import { mimeTypes } from "../../lib/data/mimetypes";
import { userAgents } from "../../lib/data/userAgents";
import { validateHttpHeader } from "../../lib/validateHttpHeader";
import { CountBadge } from "../CountBadge";
import { DetailsBanner } from "../DetailsBanner";
import type { GenericCompletion, GenericCompletionConfig, HostInputProps } from "../EditorHost";
import { useEditorHost } from "../EditorHost";
import type { Pair, PairEditorProps } from "../pairs/PairEditor";
import { PairEditorRow } from "../pairs/PairEditor";
import { ensurePairId } from "../pairs/PairEditor.util";
import { PairOrBulkEditor } from "../pairs/PairOrBulkEditor";
import { HStack } from "../Stacks";
import type { HttpPairView } from "./types";
type Props = {
forceUpdateKey: string;
headers: HttpPairView[];
inheritedHeaders?: HttpPairView[];
inheritedHeadersLabel?: string;
stateKey: string;
onChange?: PairEditorProps["onChange"];
/** Show the headers without a way to change them */
readOnly?: boolean;
label?: string;
};
export function HeadersEditor({
stateKey,
headers,
inheritedHeaders,
inheritedHeadersLabel = "Inherited",
onChange = ignoreChange,
readOnly,
forceUpdateKey,
}: Props) {
const { useDetailsBannerStoredValue } = useEditorHost();
// Get header names defined at current level (case-insensitive)
const currentHeaderNames = new Set(
headers.filter((h) => h.name).map((h) => h.name.toLowerCase()),
);
// Filter inherited headers: must be enabled, have content, and not be overridden by current level
const validInheritedHeaders =
inheritedHeaders?.filter(
(pair) =>
pair.enabled &&
(pair.name || pair.value) &&
!currentHeaderNames.has(pair.name.toLowerCase()),
) ?? [];
const hasInheritedHeaders = validInheritedHeaders.length > 0;
return (
<div
className={
hasInheritedHeaders
? "@container w-full h-full grid grid-rows-[auto_minmax(0,1fr)] gap-y-1.5"
: "@container w-full h-full"
}
>
{hasInheritedHeaders && (
<DetailsBanner
useStoredValue={useDetailsBannerStoredValue}
color="secondary"
className="text-sm"
summary={
<HStack>
{inheritedHeadersLabel} <CountBadge count={validInheritedHeaders.length} />
</HStack>
}
>
<div className="pb-2">
{validInheritedHeaders?.map((pair, i) => (
<PairEditorRow
key={`${pair.id}.${i}`}
index={i}
disabled
disableDrag
className="py-1"
pair={ensurePairId(pair)}
stateKey={null}
nameAutocompleteFunctions
nameAutocompleteVariables
valueAutocompleteFunctions
valueAutocompleteVariables
/>
))}
</div>
</DetailsBanner>
)}
<PairOrBulkEditor
forceUpdateKey={forceUpdateKey}
nameAutocomplete={nameAutocomplete}
nameAutocompleteFunctions
nameAutocompleteVariables
namePlaceholder="Header-Name"
nameValidate={validateHttpHeader}
onChange={onChange}
pairs={headers}
readOnly={readOnly}
preferenceName="headers"
stateKey={stateKey}
valueType={valueType}
valueAutocomplete={valueAutocomplete}
valueAutocompleteFunctions
valueAutocompleteVariables
/>
</div>
);
}
const MIN_MATCH = 3;
const headerOptionsMap: Record<string, HeaderValuePreset[]> = {
"content-type": mimeTypes,
accept: ["*/*", ...mimeTypes],
"accept-encoding": encodings,
"content-encoding": encodings,
connection: connections,
"accept-charset": charsets,
"accept-language": acceptLanguages,
"cache-control": cacheControlDirectives,
"user-agent": userAgents,
pragma: ["no-cache"],
te: ["trailers", "compress", "deflate", "gzip"],
dnt: ["1", "0"],
"upgrade-insecure-requests": ["1"],
"x-requested-with": ["XMLHttpRequest"],
};
function ignoreChange() {}
const valueType = (pair: Pair): HostInputProps["type"] => {
const name = pair.name.toLowerCase().trim();
if (
name.includes("authorization") ||
name.includes("api-key") ||
name.includes("access-token") ||
name.includes("auth") ||
name.includes("secret") ||
name.includes("token") ||
name === "cookie" ||
name === "set-cookie"
) {
return "password";
}
return "text";
};
const valueAutocomplete = (headerName: string): GenericCompletionConfig | undefined => {
const name = headerName.toLowerCase().trim();
const options: GenericCompletion[] =
headerOptionsMap[name]?.map((o) =>
typeof o === "string"
? {
label: o,
type: "constant",
boost: 1, // Put above other completions
}
: {
// Show the short label but insert the full value (e.g. User-Agent)
label: o.label,
apply: o.value,
type: "constant",
boost: 1,
},
) ?? [];
return { minMatch: MIN_MATCH, options };
};
const nameAutocomplete: PairEditorProps["nameAutocomplete"] = {
minMatch: MIN_MATCH,
options: headerNames.map((t) =>
typeof t === "string"
? {
label: t,
type: "constant",
boost: 1, // Put above other completions
}
: {
...t,
boost: 1, // Put above other completions
},
),
};
@@ -0,0 +1,458 @@
import classNames from "classnames";
import type { CSSProperties, ReactNode, Ref } from "react";
import { useMemo } from "react";
import { languageFromContentType } from "../../lib/contentType";
import { CountBadge } from "../CountBadge";
import { EmptyStateText } from "../EmptyStateText";
import type { HostInputProps } from "../EditorHost";
import { useEditorHost } from "../EditorHost";
import { KeyValueRows } from "../KeyValueRow";
import type { HotkeyAction } from "../../hooks/useHotKey";
import type { EditablePair, PairEditorHandle, PairEditorProps } from "../pairs/PairEditor";
import type { TabItem, TabsRef } from "../tabs/Tabs";
import { TabContent, Tabs } from "../tabs/Tabs";
import { FormMultipartEditor } from "./FormMultipartEditor";
import { FormUrlencodedEditor } from "./FormUrlencodedEditor";
import { HeadersEditor } from "./HeadersEditor";
import type { ExtraPaneTab } from "./HttpResponsePaneView";
import { JsonBodyEditor } from "./JsonBodyEditor";
import { RequestBodyViewer } from "./RequestBodyViewer";
import { RequestMethodDropdown } from "./RequestMethodDropdown";
import { KeyValueRow } from "./ResponseHeaders";
import {
BODY_TYPE_BINARY,
BODY_TYPE_FORM_MULTIPART,
BODY_TYPE_FORM_URLENCODED,
BODY_TYPE_GRAPHQL,
BODY_TYPE_JSON,
BODY_TYPE_OTHER,
BODY_TYPE_XML,
type HttpBodyLoader,
type HttpRequestBodyView,
type HttpRequestView,
} from "./types";
import { UrlBar } from "./UrlBar";
import { UrlParametersEditor } from "./UrlParametersEditor";
export const REQUEST_TAB_BODY = "body";
export const REQUEST_TAB_PARAMS = "params";
export const REQUEST_TAB_HEADERS = "headers";
export const REQUEST_TAB_AUTH = "auth";
export interface HttpRequestPaneViewProps {
request: HttpRequestView;
/** Lets the request be changed and sent; without it the pane only shows it */
editable?: boolean;
style?: CSSProperties;
className?: string;
fullHeight: boolean;
/** Where the tab order and the active tab are remembered */
tabsStorageKey?: string;
tabsRef?: Ref<TabsRef>;
/** Re-seeds the editors when the request changes from elsewhere */
forceUpdateKey: string;
/** The URL bar's own reset key, when the app resets it apart from the rest */
urlBarKey?: string;
/** Re-seeds the URL bar's text without remounting it */
urlForceUpdateKey?: string;
urlAutocomplete?: HostInputProps["autocomplete"];
urlPlaceholder?: string;
onUrlChange?: (url: string) => void;
onUrlPasteOverwrite?: HostInputProps["onPasteOverwrite"];
onSend?: () => void;
onCancel?: () => void;
isSending?: boolean;
/** The action whose keys send the request */
sendHotkeyAction?: HotkeyAction;
/** The action whose keys select the URL */
focusHotkeyAction?: HotkeyAction;
/** Colour the method by what it does */
methodColored?: boolean;
onMethodChange?: (method: string) => void | Promise<void>;
/** Replaces the method shown at the start of the URL bar */
methodSlot?: ReactNode;
/** The body tab, when the app offers a menu of body types; otherwise a label names it */
bodyTab?: TabItem;
/** Renders the body tab's content, given the shared editor for the body type */
renderBody?: (sharedEditor: ReactNode) => ReactNode;
/** Reads a body that isn't text, like a captured binary or multipart one */
bodyLoader?: HttpBodyLoader;
onBodyChange?: (body: HttpRequestBodyView) => void;
/** A text body changed; JSON writes at once, the rest may debounce */
onBodyTextChange?: (text: string, bodyType: string | null) => void;
onSendJsonCommentsChange?: (sendJsonComments: boolean) => void;
/** Where dismissing the JSON comments notice is remembered */
jsonNoticeStorageKey?: string;
/** Parameters to edit, when the app derives more than the request's own (path placeholders) */
urlParameterPairs?: EditablePair[];
urlParametersForceUpdateKey?: string;
onUrlParametersChange?: PairEditorProps["onChange"];
onUrlParametersRef?: (handle: PairEditorHandle) => void;
/** Overrides the parameters editor, like the client's one wired to its focus events */
renderUrlParameters?: (pairs: EditablePair[]) => ReactNode;
/** The headers tab, when the app counts inherited headers into it */
headersTabs?: TabItem[];
inheritedHeaders?: HeadersEditorInherited;
headersForceUpdateKey?: string;
onHeadersChange?: PairEditorProps["onChange"];
/** The auth tab and what it shows, when the app edits authentication itself */
authTabs?: TabItem[];
authContent?: ReactNode;
/** Tabs only the app has, after the shared ones */
extraTabs?: ExtraPaneTab[];
}
type HeadersEditorInherited = { name: string; value: string; enabled?: boolean; id?: string }[];
/** Yaak's request pane: the URL bar over Body, Params, Headers and Auth tabs */
export function HttpRequestPaneView({
request,
editable = false,
style,
className,
fullHeight,
tabsStorageKey = "http_request_tabs",
tabsRef,
forceUpdateKey,
urlBarKey,
urlForceUpdateKey,
urlAutocomplete,
urlPlaceholder = "https://example.com",
onUrlChange,
onUrlPasteOverwrite,
onSend,
onCancel,
isSending,
sendHotkeyAction,
focusHotkeyAction,
methodColored,
onMethodChange,
methodSlot,
bodyTab,
renderBody,
bodyLoader,
onBodyChange,
onBodyTextChange,
onSendJsonCommentsChange,
jsonNoticeStorageKey,
urlParameterPairs,
urlParametersForceUpdateKey,
onUrlParametersChange,
onUrlParametersRef,
renderUrlParameters,
headersTabs,
inheritedHeaders,
headersForceUpdateKey,
onHeadersChange,
authTabs,
authContent,
extraTabs,
}: HttpRequestPaneViewProps) {
const { ErrorBoundary } = useEditorHost();
const readOnly = !editable;
const paramPairs = urlParameterPairs ?? request.urlParameters;
let numParams = 0;
if (
request.bodyType === BODY_TYPE_FORM_URLENCODED ||
request.bodyType === BODY_TYPE_FORM_MULTIPART
) {
numParams = Array.isArray(request.body?.form)
? request.body.form.filter((p) => p.name).length
: 0;
}
const tabs = useMemo<TabItem[]>(
() => [
bodyTab ?? {
value: REQUEST_TAB_BODY,
label: bodyTypeLabel(request.bodyType, request.contentType),
rightSlot: numParams > 0 ? <CountBadge count={numParams} /> : null,
},
{
value: REQUEST_TAB_PARAMS,
rightSlot: <CountBadge count={paramPairs.length} />,
label: "Params",
},
...(headersTabs ?? [
{
value: REQUEST_TAB_HEADERS,
label: "Headers",
rightSlot: (
<CountBadge
count={request.headers.filter((h) => h.name && h.enabled !== false).length}
/>
),
},
]),
...(authTabs ??
(request.auth == null
? []
: [
{
value: REQUEST_TAB_AUTH,
label: request.auth.label,
},
])),
...(extraTabs?.map((t) => t.tab) ?? []),
],
[
authTabs,
bodyTab,
extraTabs,
headersTabs,
numParams,
paramPairs.length,
request.auth,
request.bodyType,
request.contentType,
request.headers,
],
);
const sharedBody = (
<RequestBodyEditor
request={request}
readOnly={readOnly}
fullHeight={fullHeight}
forceUpdateKey={forceUpdateKey}
bodyLoader={bodyLoader}
onBodyChange={onBodyChange}
onBodyTextChange={onBodyTextChange}
onSendJsonCommentsChange={onSendJsonCommentsChange}
jsonNoticeStorageKey={jsonNoticeStorageKey ?? request.id}
/>
);
return (
<div
style={style}
className={classNames(className, "h-full grid grid-rows-[auto_minmax(0,1fr)] grid-cols-1")}
>
<UrlBar
stateKey={`url.${request.id}`}
key={urlBarKey}
url={request.url}
placeholder={urlPlaceholder}
onPasteOverwrite={onUrlPasteOverwrite}
autocomplete={urlAutocomplete}
onSend={onSend}
onCancel={onCancel}
onUrlChange={onUrlChange}
readOnly={readOnly}
sendHotkeyAction={readOnly ? undefined : sendHotkeyAction}
focusHotkeyAction={readOnly ? undefined : focusHotkeyAction}
leftSlot={
<div className="py-0.5">
{methodSlot ?? (
<RequestMethodDropdown
method={request.method}
colored={methodColored}
onChange={onMethodChange}
readOnly={readOnly}
className="ml-0.5 h-full!"
/>
)}
</div>
}
forceUpdateKey={urlForceUpdateKey ?? forceUpdateKey}
isLoading={isSending}
/>
<Tabs
ref={tabsRef}
label="Request"
tabs={tabs}
tabListClassName="mt-1 -mb-1.5"
storageKey={tabsStorageKey}
activeTabKey={request.id}
>
<TabContent value={REQUEST_TAB_AUTH} ErrorBoundary={ErrorBoundary}>
{authContent ?? (request.auth != null && <AuthDisplay auth={request.auth} />)}
</TabContent>
<TabContent value={REQUEST_TAB_HEADERS} ErrorBoundary={ErrorBoundary}>
<HeadersEditor
inheritedHeaders={inheritedHeaders}
forceUpdateKey={headersForceUpdateKey ?? forceUpdateKey}
headers={request.headers}
stateKey={`headers.${request.id}`}
onChange={onHeadersChange}
readOnly={readOnly}
/>
</TabContent>
<TabContent value={REQUEST_TAB_PARAMS} ErrorBoundary={ErrorBoundary}>
{renderUrlParameters?.(paramPairs) ?? (
<UrlParametersEditor
stateKey={`params.${request.id}`}
forceUpdateKey={urlParametersForceUpdateKey ?? forceUpdateKey}
pairs={paramPairs}
onChange={onUrlParametersChange}
setRef={onUrlParametersRef}
readOnly={readOnly}
/>
)}
</TabContent>
<TabContent value={REQUEST_TAB_BODY} ErrorBoundary={ErrorBoundary}>
{renderBody ? renderBody(sharedBody) : sharedBody}
</TabContent>
{extraTabs?.map(({ tab, content }) => (
<TabContent key={tab.value} value={tab.value} ErrorBoundary={ErrorBoundary}>
{content}
</TabContent>
))}
</Tabs>
</div>
);
}
interface RequestBodyEditorProps {
request: HttpRequestView;
readOnly: boolean;
fullHeight: boolean;
forceUpdateKey: string;
bodyLoader?: HttpBodyLoader;
onBodyChange?: (body: HttpRequestBodyView) => void;
onBodyTextChange?: (text: string, bodyType: string | null) => void;
onSendJsonCommentsChange?: (sendJsonComments: boolean) => void;
jsonNoticeStorageKey: string;
}
/** The editor for a request body's type: JSON, XML, forms, or any other text */
export function RequestBodyEditor({
request,
readOnly,
fullHeight,
forceUpdateKey,
bodyLoader,
onBodyChange,
onBodyTextChange,
onSendJsonCommentsChange,
jsonNoticeStorageKey,
}: RequestBodyEditorProps) {
const { Editor } = useEditorHost();
const { bodyType } = request;
const handleTextChange = (text: string) => onBodyTextChange?.(text, bodyType);
// A captured body that isn't text shows in the viewer for its content type
if (
bodyLoader != null &&
(bodyType === BODY_TYPE_BINARY || bodyType === BODY_TYPE_FORM_MULTIPART) &&
readOnly
) {
return (
<RequestBodyViewer id={request.id} contentType={request.contentType} body={bodyLoader} />
);
}
if (bodyType === BODY_TYPE_JSON) {
return (
<JsonBodyEditor
forceUpdateKey={forceUpdateKey}
heightMode={fullHeight ? "full" : "auto"}
requestId={request.id}
text={request.body?.text ?? ""}
sendJsonComments={request.body?.sendJsonComments === true}
onChange={handleTextChange}
onChangeSendJsonComments={onSendJsonCommentsChange}
noticeStorageKey={jsonNoticeStorageKey}
readOnly={readOnly}
/>
);
}
if (bodyType === BODY_TYPE_XML) {
return (
<Editor
forceUpdateKey={forceUpdateKey}
autocompleteFunctions
autocompleteVariables
placeholder="..."
heightMode={fullHeight ? "full" : "auto"}
defaultValue={`${request.body?.text ?? ""}`}
language="xml"
onChange={handleTextChange}
readOnly={readOnly}
stateKey={`xml.${request.id}`}
/>
);
}
if (bodyType === BODY_TYPE_FORM_URLENCODED) {
return (
<FormUrlencodedEditor
forceUpdateKey={forceUpdateKey}
requestId={request.id}
form={request.body.form}
onChange={onBodyChange}
readOnly={readOnly}
/>
);
}
if (bodyType === BODY_TYPE_FORM_MULTIPART) {
return (
<FormMultipartEditor
forceUpdateKey={forceUpdateKey}
requestId={request.id}
form={request.body.form}
onChange={onBodyChange}
readOnly={readOnly}
/>
);
}
if (typeof bodyType === "string" && bodyType !== BODY_TYPE_BINARY) {
return (
<Editor
forceUpdateKey={forceUpdateKey}
autocompleteFunctions
autocompleteVariables
language={languageFromContentType(request.contentType)}
placeholder="..."
heightMode={fullHeight ? "full" : "auto"}
defaultValue={`${request.body?.text ?? ""}`}
onChange={handleTextChange}
readOnly={readOnly}
stateKey={`other.${request.id}`}
/>
);
}
return <EmptyStateText>No Body</EmptyStateText>;
}
function AuthDisplay({ auth }: { auth: NonNullable<HttpRequestView["auth"]> }) {
return (
<KeyValueRows>
{auth.values.map((v) => (
<KeyValueRow key={v.name} labelColor="primary" label={v.name}>
{v.value}
</KeyValueRow>
))}
</KeyValueRows>
);
}
/** What the body tab is called when there's no menu of body types to name it */
export function bodyTypeLabel(bodyType: string | null, contentType: string | null): string {
switch (bodyType) {
case BODY_TYPE_JSON:
return "JSON";
case BODY_TYPE_XML:
return "XML";
case BODY_TYPE_GRAPHQL:
return "GraphQL";
case BODY_TYPE_FORM_URLENCODED:
return "Url Encoded";
case BODY_TYPE_FORM_MULTIPART:
return "Multi-Part";
case BODY_TYPE_BINARY:
return "Binary";
case BODY_TYPE_OTHER:
return languageFromContentType(contentType) === "markdown" ? "Markdown" : "Other";
case null:
return "Body";
default:
return "Body";
}
}
@@ -0,0 +1,350 @@
import classNames from "classnames";
import type { CSSProperties, ReactElement, ReactNode } from "react";
import { Suspense, useMemo } from "react";
import { getMimeTypeFromContentType } from "../../lib/mimeType";
import { Banner } from "../Banner";
import { CountBadge } from "../CountBadge";
import type { DropdownItem } from "../Dropdown";
import { type EditorHost, useEditorHost } from "../EditorHost";
import { Icon } from "../Icon";
import { LoadingIcon } from "../LoadingIcon";
import { HStack } from "../Stacks";
import type { TabItem } from "../tabs/Tabs";
import { TabContent, Tabs } from "../tabs/Tabs";
import { HttpResponseDurationTagRaw } from "../tags/HttpResponseDurationTag";
import { HttpStatusTagRaw } from "../tags/HttpStatusTag";
import { SizeTag } from "../tags/SizeTag";
import { ConfirmLargeResponse } from "./ConfirmLargeResponse";
import { RequestBodyViewer } from "./RequestBodyViewer";
import type { ResponseTextViewerExtras, ResponseViewMode } from "./ResponseBodyViewer";
import { ResponseBodyViewer } from "./ResponseBodyViewer";
import { parseCookieHeaders, ResponseCookies } from "./ResponseCookies";
import { ResponseHeaders } from "./ResponseHeaders";
import type { HttpBodyLoader, HttpResponseView } from "./types";
export const RESPONSE_TAB_BODY = "body";
export const RESPONSE_TAB_REQUEST = "request";
export const RESPONSE_TAB_HEADERS = "headers";
export const RESPONSE_TAB_COOKIES = "cookies";
/** A tab only one app has, like the client's timeline, with what it shows */
export interface ExtraPaneTab {
tab: TabItem;
content: ReactNode;
}
export interface HttpResponsePaneViewProps {
response: HttpResponseView | null;
/** Reads the response body */
body: HttpBodyLoader | null;
/** Reads the request body the response answered */
requestBody: HttpBodyLoader | null;
style?: CSSProperties;
className?: string;
/** Shown before there's a response */
emptyState: ReactNode;
viewMode: ResponseViewMode;
onViewModeChange: (viewMode: ResponseViewMode) => void;
/** Where the tab order and the active tab are remembered */
tabsStorageKey?: string;
/** Remembers the active tab per request or flow */
activeTabKey: string;
/** Between the status and the right of the status line, like a warning */
statusLineCenter?: ReactNode;
/** At the right of the status line, like a menu of earlier responses */
statusLineRight?: ReactNode;
onSaveBody?: () => void;
onCopyBody?: () => void;
onCopyAsCurl?: () => void;
/** Stop a request that's still being sent */
onCancel?: () => void;
/** Overrides the cookie counts the tab shows, when the app counts them its own way */
cookieCounts?: { sent: number; received: number };
/** Tabs only the app has, after the shared ones */
extraTabs?: ExtraPaneTab[];
textViewer?: ResponseTextViewerExtras;
/** Viewers only this app has for a response body, like the client's event stream viewer */
renderBodyViewer?: EditorHost["renderBodyViewer"];
/** More rows for the Headers tab's Info section */
infoRows?: ReactElement[];
/** The copy button for a body held back for its size */
renderCopyButton?: (getText: () => Promise<string | null>) => ReactNode;
}
/** Yaak's response pane: a status line over Response, Request, Headers and Cookies tabs */
export function HttpResponsePaneView({
response,
body,
requestBody,
style,
className,
emptyState,
viewMode,
onViewModeChange,
tabsStorageKey = "http_response_tabs",
activeTabKey,
statusLineCenter,
statusLineRight,
onSaveBody,
onCopyBody,
onCopyAsCurl,
onCancel,
cookieCounts: cookieCountsOverride,
extraTabs,
textViewer,
renderBodyViewer,
infoRows,
renderCopyButton,
}: HttpResponsePaneViewProps) {
const { ErrorBoundary } = useEditorHost();
const mimeType =
response?.contentType == null ? null : getMimeTypeFromContentType(response.contentType).essence;
const cookieCounts = useMemo(() => {
if (cookieCountsOverride != null) return cookieCountsOverride;
if (response == null) return { sent: 0, received: 0 };
const { sentCookies, receivedCookies } = parseCookieHeaders(response.cookieHeaders);
return { sent: sentCookies.length, received: receivedCookies.length };
}, [cookieCountsOverride, response]);
const tabs = useMemo<TabItem[]>(() => {
const bodyActionsDisabled = response?.state !== "closed" && (response?.status ?? 0) >= 100;
const bodyActionsHidden = response == null || !!response.error;
const itemsAfter: DropdownItem[] = [
...(onSaveBody != null
? [
{
label: "Save to File",
onSelect: onSaveBody,
leftSlot: <Icon icon="save" />,
hidden: bodyActionsHidden,
disabled: bodyActionsDisabled,
},
]
: []),
...(onCopyBody != null
? [
{
label: "Copy Body",
onSelect: onCopyBody,
leftSlot: <Icon icon="copy" />,
hidden: bodyActionsHidden,
disabled: bodyActionsDisabled,
},
]
: []),
...(onCopyAsCurl != null
? [{ label: "Copy as cURL", onSelect: onCopyAsCurl, leftSlot: <Icon icon="copy" /> }]
: []),
];
return [
{
value: RESPONSE_TAB_BODY,
options: {
value: viewMode,
onChange: (v) => onViewModeChange(v === "raw" ? "raw" : "pretty"),
items: [
{ label: "Response", value: "pretty" },
...(mimeType?.startsWith("image")
? []
: [{ label: "Response (Raw)", shortLabel: "Raw", value: "raw" }]),
],
itemsAfter: itemsAfter.length > 0 ? itemsAfter : undefined,
},
},
{
value: RESPONSE_TAB_REQUEST,
label: "Request",
rightSlot: (response?.requestContentLength ?? 0) > 0 ? <CountBadge count={true} /> : null,
},
{
value: RESPONSE_TAB_HEADERS,
label: "Headers",
rightSlot: (
<CountBadge
count={response?.requestHeaders.length ?? 0}
count2={response?.headers.length ?? 0}
showZero
/>
),
},
{
value: RESPONSE_TAB_COOKIES,
label: "Cookies",
rightSlot:
cookieCounts.sent > 0 || cookieCounts.received > 0 ? (
<CountBadge count={cookieCounts.sent} count2={cookieCounts.received} showZero />
) : null,
},
...(extraTabs?.map((t) => t.tab) ?? []),
];
}, [
cookieCounts,
extraTabs,
mimeType,
onCopyAsCurl,
onCopyBody,
onSaveBody,
onViewModeChange,
response,
viewMode,
]);
return (
<div
style={style}
className={classNames(
className,
"x-theme-responsePane",
"max-h-full h-full",
"bg-surface rounded-md border border-border-subtle overflow-hidden",
"relative",
)}
>
{response == null ? (
emptyState
) : (
<div className="h-full w-full grid grid-rows-[auto_minmax(0,1fr)] grid-cols-1">
<HStack
className={classNames(
"text-text-subtle w-full shrink-0",
// Remove a bit of space because the tabs have lots too
"-mb-1.5",
)}
>
<ResponseStatusLine
response={response}
center={statusLineCenter}
right={statusLineRight}
/>
</HStack>
<div className="overflow-hidden flex flex-col min-h-0">
{response.error && (
<Banner color="danger" className="mx-3 mt-1 shrink-0">
{response.error}
</Banner>
)}
{/* Show tabs if we have any data (headers, body, etc.) even if there's an error */}
<Tabs
tabs={tabs}
label="Response"
className="ml-3 mr-3 mb-3 min-h-0 flex-1"
tabListClassName="mt-0.5 -mb-1.5"
storageKey={tabsStorageKey}
activeTabKey={activeTabKey}
>
<TabContent value={RESPONSE_TAB_BODY} ErrorBoundary={ErrorBoundary}>
<ErrorBoundary name="Http Response Viewer">
<Suspense>
<ConfirmLargeResponse
contentLength={response.contentLength}
contentType={response.contentType}
onSave={onSaveBody}
getText={body?.getText}
renderCopyButton={renderCopyButton}
>
{body != null && (
<ResponseBodyViewer
response={response}
body={body}
viewMode={viewMode}
textViewer={textViewer}
onCancel={onCancel}
renderBodyViewer={renderBodyViewer}
/>
)}
</ConfirmLargeResponse>
</Suspense>
</ErrorBoundary>
</TabContent>
<TabContent value={RESPONSE_TAB_REQUEST} ErrorBoundary={ErrorBoundary}>
<ConfirmLargeResponse
contentLength={response.requestContentLength}
contentType={response.contentType}
noun="content"
revealLabel="Reveal Request Body"
getText={requestBody?.getText}
renderCopyButton={renderCopyButton}
>
{requestBody != null && (
<RequestBodyViewer
id={response.id}
contentType={findHeader(response.requestHeaders, "content-type")}
body={requestBody}
/>
)}
</ConfirmLargeResponse>
</TabContent>
<TabContent value={RESPONSE_TAB_HEADERS} ErrorBoundary={ErrorBoundary}>
<ResponseHeaders response={response} infoRows={infoRows} />
</TabContent>
<TabContent value={RESPONSE_TAB_COOKIES} ErrorBoundary={ErrorBoundary}>
<ResponseCookies response={response} />
</TabContent>
{extraTabs?.map(({ tab, content }) => (
<TabContent key={tab.value} value={tab.value} ErrorBoundary={ErrorBoundary}>
{content}
</TabContent>
))}
</Tabs>
</div>
</div>
)}
</div>
);
}
/** The status, time and size of a response, with room for the app's own pieces */
export function ResponseStatusLine({
response,
center,
right,
}: {
response: HttpResponseView;
center?: ReactNode;
right?: ReactNode;
}) {
return (
<div
className={classNames(
"grid grid-cols-[auto_minmax(4rem,1fr)_auto]",
// As tall as an `h-sm` button with `m-0.5` (the client's response history menu), so the
// status sits the same distance from the card's edge with or without one on the right
"min-h-[calc(var(--height-sm)+0.25rem)]",
"cursor-default select-none",
"whitespace-nowrap w-full pl-3 overflow-x-auto font-mono text-sm hide-scrollbars",
)}
>
<HStack space={2} className="w-full shrink-0">
{response.state !== "closed" && <LoadingIcon size="sm" />}
<HttpStatusTagRaw
showReason
status={response.status}
state={response.state}
statusReason={response.statusReason}
/>
<span>&bull;</span>
<HttpResponseDurationTagRaw
closed={response.state === "closed"}
startedAt={response.startedAt}
elapsedMs={response.elapsedMs}
elapsedHeadersMs={response.elapsedHeadersMs}
elapsedDnsMs={response.elapsedDnsMs}
/>
<span>&bull;</span>
<SizeTag
contentLength={response.contentLength ?? 0}
contentLengthCompressed={response.contentLengthCompressed}
/>
</HStack>
{center ?? <span />}
<div className="justify-self-end shrink-0">{right}</div>
</div>
);
}
function findHeader(headers: { name: string; value: string }[], name: string): string | null {
return headers.find((h) => h.name.toLowerCase() === name)?.value ?? null;
}
@@ -0,0 +1,141 @@
import { linter } from "@codemirror/lint";
import { useCallback, useMemo } from "react";
import { textLikelyContainsJsonComments } from "../../lib/jsonComments";
import { replaceTemplateTags } from "../../lib/templateTags";
import { Banner } from "../Banner";
import type { DropdownItem } from "../Dropdown";
import { Dropdown } from "../Dropdown";
import type { HostEditorProps } from "../EditorHost";
import { useEditorHost } from "../EditorHost";
import { jsonParseLinter } from "../editor/json-lint";
import { Icon } from "../Icon";
import { IconButton } from "../IconButton";
import { IconTooltip } from "../IconTooltip";
interface Props {
forceUpdateKey: string;
heightMode: HostEditorProps["heightMode"];
/** The request's id, keeping each request's editor state apart */
requestId: string;
text: string;
/** JSON is sent as written, comments and all, instead of cleaned up first */
sendJsonComments: boolean;
onChange?: (text: string) => void;
onChangeSendJsonComments?: (sendJsonComments: boolean) => void;
/** Where dismissing the comments notice is remembered, like the workspace it applies to */
noticeStorageKey: string;
/** Show the body without a way to change it */
readOnly?: boolean;
}
export function JsonBodyEditor({
forceUpdateKey,
heightMode,
requestId,
text,
sendJsonComments,
onChange,
onChangeSendJsonComments,
noticeStorageKey,
readOnly,
}: Props) {
const { Editor, useStoredValue } = useEditorHost();
const autoFix = sendJsonComments !== true;
const lintExtension = useMemo(
() =>
linter(
jsonParseLinter(
autoFix || readOnly
? { allowComments: true, allowTrailingCommas: true, escape: escapeTemplateTags }
: { allowComments: false, allowTrailingCommas: false, escape: escapeTemplateTags },
),
),
[autoFix, readOnly],
);
const hasComments = useMemo(() => textLikelyContainsJsonComments(text), [text]);
const [bannerDismissed, setBannerDismissed] = useStoredValue<boolean>(
["json-fix-3", noticeStorageKey],
false,
"no_sync",
);
const handleToggleAutoFix = useCallback(
() => onChangeSendJsonComments?.(autoFix),
[autoFix, onChangeSendJsonComments],
);
const handleDropdownOpen = useCallback(() => {
if (!bannerDismissed) {
Promise.resolve(setBannerDismissed(true)).catch(console.error);
}
}, [bannerDismissed, setBannerDismissed]);
const showBanner = hasComments && autoFix && !bannerDismissed;
const stripMessage = "Automatically strip comments and trailing commas before sending";
const actions = useMemo<HostEditorProps["actions"]>(
() =>
readOnly
? undefined
: [
showBanner && (
<Banner
color="notice"
className="opacity-100! h-sm py-0! px-2! flex items-center text-xs"
>
<p className="inline-flex items-center gap-1 min-w-0">
<span className="truncate">Auto-fix enabled</span>
<Icon icon="arrow_right" size="sm" className="opacity-disabled" />
</p>
</Banner>
),
<div key="settings" className="opacity-100! shadow!">
<Dropdown
onOpen={handleDropdownOpen}
items={
[
{
label: "Automatically Fix JSON",
keepOpenOnSelect: true,
onSelect: handleToggleAutoFix,
rightSlot: <IconTooltip content={stripMessage} />,
leftSlot: (
<Icon icon={autoFix ? "check_square_checked" : "check_square_unchecked"} />
),
},
] satisfies DropdownItem[]
}
>
<IconButton size="sm" variant="border" icon="settings" title="JSON Settings" />
</Dropdown>
</div>,
],
[handleDropdownOpen, handleToggleAutoFix, autoFix, readOnly, showBanner],
);
return (
<Editor
forceUpdateKey={forceUpdateKey}
autocompleteFunctions
autocompleteVariables
placeholder="..."
heightMode={heightMode}
defaultValue={text}
language="json"
onChange={onChange}
readOnly={readOnly}
stateKey={`json.${requestId}`}
actions={actions}
lintExtension={lintExtension}
/>
);
}
// We need lint to not break on stuff like {"foo:" ${[ ... ]}} so we'll replace all template
// syntax with repeating `1` characters, so it's valid JSON and the position is still correct.
function escapeTemplateTags(doc: string): string {
return replaceTemplateTags(doc, (m) => "1".repeat(m.end - m.start));
}
@@ -0,0 +1,102 @@
import { formatSize } from "@yaakapp-internal/lib/formatSize";
import { languageFromContentType } from "../../lib/contentType";
import { getMimeTypeFromContentType } from "../../lib/mimeType";
import { EmptyStateText } from "../EmptyStateText";
import { useEditorHost } from "../EditorHost";
import { LoadingIcon } from "../LoadingIcon";
import { AudioViewer } from "../viewers/AudioViewer";
import { CsvViewer } from "../viewers/CsvViewer";
import { ImageViewer } from "../viewers/ImageViewer";
import { SvgViewer } from "../viewers/SvgViewer";
import { VideoViewer } from "../viewers/VideoViewer";
import { WebPageViewer } from "../viewers/WebPageViewer";
import type { HttpBodyLoader } from "./types";
interface Props {
/** Identifies the body, for viewer state */
id: string;
/** The Content-Type the request was sent with */
contentType: string | null;
body: HttpBodyLoader;
}
/** The body a request was sent with, in the viewer its content type calls for */
export function RequestBodyViewer({ id, contentType, body }: Props) {
return <RequestBodyViewerInner key={id} id={id} contentType={contentType} body={body} />;
}
function RequestBodyViewerInner({ id, contentType, body }: Props) {
const { MultipartViewer, TextViewer, renderBodyViewer } = useEditorHost();
const text = body.useText();
const bytes = body.useBytes();
if (text.isLoading || bytes.isLoading) {
return (
<EmptyStateText>
<LoadingIcon />
</EmptyStateText>
);
}
const error = text.error ?? bytes.error;
if (error) {
return <EmptyStateText>Error loading request body: {error.message}</EmptyStateText>;
}
// Whether there's a body is the bytes' to say: a binary body has no text
const bodyBytes = bytes.data;
if (bodyBytes == null || bodyBytes.length === 0) {
return <EmptyStateText>No request body</EmptyStateText>;
}
const mimeType = contentType ? getMimeTypeFromContentType(contentType).essence : null;
// Route to appropriate viewer based on content type
if (mimeType?.match(/^multipart/i)) {
const boundary = contentType?.split("boundary=")[1] ?? "unknown";
// Create a copy because parseMultipart may detach the buffer
const bodyCopy = new Uint8Array(bodyBytes);
return <MultipartViewer data={bodyCopy} boundary={boundary} idPrefix={`request.${id}`} />;
}
if (mimeType?.match(/^image/i) && !mimeType.match(/^image\/svg/i)) {
return <ImageViewer data={bodyBytes.buffer as ArrayBuffer} />;
}
if (mimeType?.match(/^audio/i)) {
return <AudioViewer data={bodyBytes} />;
}
if (mimeType?.match(/^video/i)) {
return <VideoViewer data={bodyBytes} />;
}
const appViewer = mimeType?.match(/pdf/i)
? renderBodyViewer?.({ source: "request", mimeType, viewMode: "pretty", body, complete: true })
: null;
// The rest show text, which a body that isn't valid UTF-8 doesn't have
const bodyText = text.data;
if (bodyText == null || bodyText.length === 0) {
return (
appViewer ?? <EmptyStateText>Binary body, {formatSize(bodyBytes.length)}</EmptyStateText>
);
}
if (mimeType?.match(/^image\/svg/i)) {
return <SvgViewer text={bodyText} />;
}
if (mimeType?.match(/csv|tab-separated/i)) {
return <CsvViewer text={bodyText} />;
}
if (mimeType?.match(/^text\/html/i)) {
return <WebPageViewer html={bodyText} />;
}
if (appViewer != null) return appViewer;
const language = languageFromContentType(contentType, bodyText);
return <TextViewer text={bodyText} language={language} stateKey={`request.body.${id}`} />;
}
@@ -0,0 +1,87 @@
import classNames from "classnames";
import { memo, useCallback, useMemo } from "react";
import { Button } from "../Button";
import type { DropdownItem } from "../Dropdown";
import { useEditorHost } from "../EditorHost";
import { Icon } from "../Icon";
import type { RadioDropdownItem } from "../RadioDropdown";
import { RadioDropdown } from "../RadioDropdown";
import { HttpMethodTagRaw } from "../tags/HttpMethodTag";
type Props = {
method: string;
/** Colour the method by what it does, as the client's setting chooses */
colored?: boolean;
onChange?: (method: string) => void | Promise<void>;
/** Show the method without a menu to change it */
readOnly?: boolean;
className?: string;
};
const METHODS = ["GET", "PUT", "POST", "PATCH", "DELETE", "OPTIONS", "QUERY", "HEAD"];
export const RequestMethodDropdown = memo(function RequestMethodDropdown({
method,
colored,
onChange,
readOnly,
className,
}: Props) {
const { showPrompt } = useEditorHost();
const radioItems = useMemo<RadioDropdownItem<string>[]>(
() =>
METHODS.map((m) => ({ value: m, label: <HttpMethodTagRaw method={m} colored={colored} /> })),
[colored],
);
const handleChange = useCallback(
async (method: string) => {
await onChange?.(method);
},
[onChange],
);
const itemsAfter = useMemo<DropdownItem[]>(
() => [
{
key: "custom",
label: "CUSTOM",
leftSlot: <Icon icon="sparkles" />,
hidden: showPrompt == null,
onSelect: async () => {
const newMethod = await showPrompt?.({
id: "custom-method",
label: "Http Method",
title: "Custom Method",
confirmText: "Save",
description: "Enter a custom method name",
placeholder: "CUSTOM",
});
if (newMethod == null) return;
await handleChange(newMethod);
},
},
],
[handleChange, showPrompt],
);
const button = (
<Button size="xs" className={classNames(className, "text-text-subtle hover:text")}>
<HttpMethodTagRaw method={method} colored={colored} />
</Button>
);
if (readOnly) return button;
return (
<RadioDropdown
value={method}
items={radioItems}
itemsAfter={itemsAfter}
onChange={handleChange}
>
{button}
</RadioDropdown>
);
});
@@ -0,0 +1,224 @@
import { formatSize } from "@yaakapp-internal/lib/formatSize";
import type { ComponentType } from "react";
import { languageFromContentType } from "../../lib/contentType";
import { getMimeTypeFromContentType } from "../../lib/mimeType";
import { Banner } from "../Banner";
import { Button } from "../Button";
import { EmptyStateText } from "../EmptyStateText";
import { type EditorHost, useEditorHost } from "../EditorHost";
import { LoadingIcon } from "../LoadingIcon";
import { HStack, VStack } from "../Stacks";
import { AudioViewer } from "../viewers/AudioViewer";
import { CsvViewer } from "../viewers/CsvViewer";
import { ImageViewer } from "../viewers/ImageViewer";
import { SvgViewer } from "../viewers/SvgViewer";
import type { TextViewerProps } from "../viewers/TextViewer";
import { VideoViewer } from "../viewers/VideoViewer";
import { WebPageViewer } from "../viewers/WebPageViewer";
import type { HttpBodyLoader, HttpResponseView } from "./types";
export type ResponseViewMode = "pretty" | "raw";
/** What the text viewer adds for the app, like its filter history and save and copy buttons */
export type ResponseTextViewerExtras = Pick<
TextViewerProps,
"footerActions" | "filterStateKey" | "onFilterApplied" | "filterLeftSlot"
>;
interface Props {
response: HttpResponseView;
body: HttpBodyLoader;
viewMode: ResponseViewMode;
textViewer?: ResponseTextViewerExtras;
/** Stop a request that's still being sent */
onCancel?: () => void;
/** Viewers only this pane's app has, tried before the host's and the shared ones */
renderBodyViewer?: EditorHost["renderBodyViewer"];
}
/** Picks the viewer for a response body from its content type, as the client's response pane does */
export function ResponseBodyViewer({
response,
body,
viewMode,
textViewer,
onCancel,
renderBodyViewer: renderPaneBodyViewer,
}: Props) {
const { renderBodyViewer: renderHostBodyViewer } = useEditorHost();
const mimeType =
response.contentType == null ? null : getMimeTypeFromContentType(response.contentType).essence;
if (response.state === "initialized") {
return (
<EmptyStateText>
<VStack space={3}>
<HStack space={3}>
<LoadingIcon className="text-text-subtlest" />
Sending Request
</HStack>
{onCancel != null && (
<Button size="sm" variant="border" onClick={onCancel}>
Cancel
</Button>
)}
</VStack>
</EmptyStateText>
);
}
if (response.state === "closed" && (response.contentLength ?? 0) === 0) {
return <EmptyStateText>Empty</EmptyStateText>;
}
const viewerArgs = {
source: "response" as const,
mimeType,
viewMode,
body,
complete: response.state === "closed",
};
const appViewer = renderPaneBodyViewer?.(viewerArgs) ?? renderHostBodyViewer?.(viewerArgs);
if (appViewer != null) return appViewer;
if (mimeType?.match(/^image\/svg/)) {
return <BodySvgViewer body={body} />;
}
if (mimeType?.match(/^image/i)) {
return <EnsureCompleteResponse response={response} body={body} Component={ImageViewer} />;
}
if (mimeType?.match(/^audio/i)) {
return <EnsureCompleteResponse response={response} body={body} Component={AudioViewer} />;
}
if (mimeType?.match(/^video/i)) {
return <EnsureCompleteResponse response={response} body={body} Component={VideoViewer} />;
}
if (mimeType?.match(/^multipart/i) && viewMode === "pretty") {
return <BodyMultipartViewer response={response} body={body} />;
}
if (mimeType?.match(/csv|tab-separated/i) && viewMode === "pretty") {
return <BodyCsvViewer className="pb-2" body={body} />;
}
return (
<HTMLOrTextViewer
textViewerClassName="-mr-2 bg-surface" // Pull to the right
response={response}
body={body}
pretty={viewMode === "pretty"}
textViewer={textViewer}
/>
);
}
/** A body with no text is either empty or not valid UTF-8, which only its bytes can tell apart */
function BinaryOrEmptyBody({ body }: { body: HttpBodyLoader }) {
const bytes = body.useBytes();
if (bytes.isLoading) return null;
const length = bytes.data?.length ?? 0;
return (
<EmptyStateText>
{length > 0 ? `Binary body, ${formatSize(length)}` : "Empty response"}
</EmptyStateText>
);
}
function EnsureCompleteResponse({
response,
body,
Component,
}: {
response: HttpResponseView;
body: HttpBodyLoader;
Component: ComponentType<{ bodyUrl: string }>;
}) {
// Wait until the response has been fully-downloaded before asking for it
const complete = response.state === "closed";
const bodyUrl = body.useUrl(complete);
if (!complete || bodyUrl.isLoading) {
return (
<EmptyStateText>
<LoadingIcon />
</EmptyStateText>
);
}
if (bodyUrl.error) {
return <Banner color="danger">{String(bodyUrl.error)}</Banner>;
}
if (bodyUrl.data == null) {
return <div>Empty response body</div>;
}
return <Component bodyUrl={bodyUrl.data} />;
}
function BodySvgViewer({ body }: { body: HttpBodyLoader }) {
const text = body.useText();
if (!text.data) return null;
return <SvgViewer text={text.data} />;
}
function BodyCsvViewer({ body, className }: { body: HttpBodyLoader; className?: string }) {
const text = body.useText();
return <CsvViewer text={text.data ?? null} className={className} />;
}
function BodyMultipartViewer({
response,
body,
}: {
response: HttpResponseView;
body: HttpBodyLoader;
}) {
const { MultipartViewer } = useEditorHost();
const bytes = body.useBytes();
if (bytes.data == null) return null;
const boundary = response.contentType?.split("boundary=")[1] ?? "unknown";
return <MultipartViewer data={bytes.data} boundary={boundary} idPrefix={response.id} />;
}
interface HTMLOrTextViewerProps {
response: HttpResponseView;
body: HttpBodyLoader;
pretty: boolean;
textViewerClassName?: string;
textViewer?: ResponseTextViewerExtras;
}
export function HTMLOrTextViewer({
response,
body,
pretty,
textViewerClassName,
textViewer,
}: HTMLOrTextViewerProps) {
const { TextViewer } = useEditorHost();
const rawTextBody = body.useText();
const language = languageFromContentType(response.contentType, rawTextBody.data ?? "");
if (rawTextBody.isLoading || response.state === "initialized") {
return null;
}
if (language === "html" && pretty) {
return <WebPageViewer html={rawTextBody.data ?? ""} baseUrl={response.url} />;
}
if (rawTextBody.data == null) {
return <BinaryOrEmptyBody body={body} />;
}
return (
<TextViewer
text={rawTextBody.data}
language={language}
stateKey={`response.body.${response.id}`}
pretty={pretty}
className={textViewerClassName}
filterText={body.filterText}
filterStateKey={`response.body.${response.ownerId}`}
{...textViewer}
/>
);
}
@@ -0,0 +1,222 @@
import classNames from "classnames";
import { useMemo } from "react";
import type { JSX } from "react/jsx-runtime";
import { CountBadge } from "../CountBadge";
import { DetailsBanner } from "../DetailsBanner";
import { useEditorHost } from "../EditorHost";
import { KeyValueRows } from "../KeyValueRow";
import { KeyValueRow } from "./ResponseHeaders";
import type { HttpResponseView } from "./types";
interface Props {
response: HttpResponseView;
}
interface ParsedCookie {
name: string;
value: string;
domain?: string;
path?: string;
expires?: string;
maxAge?: string;
secure?: boolean;
httpOnly?: boolean;
sameSite?: string;
isDeleted?: boolean;
}
function parseCookieHeader(cookieHeader: string): Array<{ name: string; value: string }> {
// Parse "Cookie: name=value; name2=value2" format
return cookieHeader.split(";").map((pair) => {
const [name = "", ...valueParts] = pair.split("=");
return {
name: name.trim(),
value: valueParts.join("=").trim(),
};
});
}
function parseSetCookieHeader(setCookieHeader: string): ParsedCookie {
// Parse "Set-Cookie: name=value; Domain=...; Path=..." format
const parts = setCookieHeader.split(";").map((p) => p.trim());
const [nameValue = "", ...attributes] = parts;
const [name = "", ...valueParts] = nameValue.split("=");
const cookie: ParsedCookie = {
name: name.trim(),
value: valueParts.join("=").trim(),
};
for (const attr of attributes) {
const [key = "", val] = attr.split("=").map((s) => s.trim());
const lowerKey = key.toLowerCase();
if (lowerKey === "domain") cookie.domain = val;
else if (lowerKey === "path") cookie.path = val;
else if (lowerKey === "expires") cookie.expires = val;
else if (lowerKey === "max-age") cookie.maxAge = val;
else if (lowerKey === "secure") cookie.secure = true;
else if (lowerKey === "httponly") cookie.httpOnly = true;
else if (lowerKey === "samesite") cookie.sameSite = val;
}
// Detect if cookie is being deleted
if (cookie.maxAge !== undefined) {
const maxAgeNum = Number.parseInt(cookie.maxAge, 10);
if (!Number.isNaN(maxAgeNum) && maxAgeNum <= 0) {
cookie.isDeleted = true;
}
} else if (cookie.expires !== undefined) {
// Check if expires date is in the past
try {
const expiresDate = new Date(cookie.expires);
if (expiresDate.getTime() < Date.now()) {
cookie.isDeleted = true;
}
} catch {
// Invalid date, ignore
}
}
return cookie;
}
/** The cookies a response's headers sent and received, the latest value of each name winning */
export function parseCookieHeaders({ sent, received }: HttpResponseView["cookieHeaders"]) {
const sentMap = new Map<string, { name: string; value: string }>();
const receivedMap = new Map<string, ParsedCookie>();
for (const header of sent) {
for (const cookie of parseCookieHeader(header)) {
sentMap.set(cookie.name, cookie);
}
}
for (const header of received) {
const cookie = parseSetCookieHeader(header);
receivedMap.set(cookie.name, cookie);
}
return {
sentCookies: Array.from(sentMap.values()),
receivedCookies: Array.from(receivedMap.values()),
};
}
export function ResponseCookies({ response }: Props) {
const { useDetailsBannerStoredValue } = useEditorHost();
const { sentCookies, receivedCookies } = useMemo(
() => parseCookieHeaders(response.cookieHeaders),
[response.cookieHeaders],
);
return (
<div className="overflow-auto h-full pb-4 gap-y-3 flex flex-col pr-0.5">
<DetailsBanner
defaultOpen
useStoredValue={useDetailsBannerStoredValue}
storageKey={`${response.ownerId}.sent_cookies`}
summary={
<h2 className="flex items-center">
Sent Cookies <CountBadge showZero count={sentCookies.length} />
</h2>
}
>
{sentCookies.length === 0 ? (
<NoCookies />
) : (
<KeyValueRows>
{sentCookies.map((cookie, i) => (
// oxlint-disable-next-line react/no-array-index-key
<KeyValueRow labelColor="primary" key={i} label={cookie.name}>
{cookie.value}
</KeyValueRow>
))}
</KeyValueRows>
)}
</DetailsBanner>
<DetailsBanner
defaultOpen
useStoredValue={useDetailsBannerStoredValue}
storageKey={`${response.ownerId}.received_cookies`}
summary={
<h2 className="flex items-center">
Received Cookies <CountBadge showZero count={receivedCookies.length} />
</h2>
}
>
{receivedCookies.length === 0 ? (
<NoCookies />
) : (
<div className="flex flex-col gap-4">
{receivedCookies.map((cookie, i) => (
// oxlint-disable-next-line react/no-array-index-key
<div key={i} className="flex flex-col gap-1">
<div className="flex items-center gap-2 my-1">
<span
className={classNames(
"font-mono text-editor select-auto cursor-auto",
cookie.isDeleted ? "line-through opacity-60 text-text-subtle" : "text-text",
)}
>
{cookie.name}
<span className="text-text-subtlest select-auto cursor-auto mx-0.5">=</span>
{cookie.value}
</span>
{cookie.isDeleted && (
<span className="text-xs font-sans text-danger bg-danger/10 px-1.5 py-0.5 rounded-sm">
Deleted
</span>
)}
</div>
<KeyValueRows>
{[
cookie.domain && (
<KeyValueRow labelColor="info" label="Domain" key="domain">
{cookie.domain}
</KeyValueRow>
),
cookie.path && (
<KeyValueRow labelColor="info" label="Path" key="path">
{cookie.path}
</KeyValueRow>
),
cookie.expires && (
<KeyValueRow labelColor="info" label="Expires" key="expires">
{cookie.expires}
</KeyValueRow>
),
cookie.maxAge && (
<KeyValueRow labelColor="info" label="Max-Age" key="maxAge">
{cookie.maxAge}
</KeyValueRow>
),
cookie.secure && (
<KeyValueRow labelColor="info" label="Secure" key="secure">
true
</KeyValueRow>
),
cookie.httpOnly && (
<KeyValueRow labelColor="info" label="HttpOnly" key="httpOnly">
true
</KeyValueRow>
),
cookie.sameSite && (
<KeyValueRow labelColor="info" label="SameSite" key="sameSite">
{cookie.sameSite}
</KeyValueRow>
),
].filter((item): item is JSX.Element => Boolean(item))}
</KeyValueRows>
</div>
))}
</div>
)}
</DetailsBanner>
</div>
);
}
function NoCookies() {
return <span className="text-text-subtlest text-sm italic">No Cookies</span>;
}
@@ -0,0 +1,83 @@
import { format, formatDistanceToNowStrict } from "date-fns";
import type { ReactElement } from "react";
import { DetailsBanner } from "../DetailsBanner";
import { useEditorHost } from "../EditorHost";
import { HeadersTable } from "../HeadersTable";
import { IconButton } from "../IconButton";
import type { KeyValueRowProps } from "../KeyValueRow";
import { KeyValueRow as BaseKeyValueRow, KeyValueRows } from "../KeyValueRow";
import type { HttpResponseView } from "./types";
interface Props {
response: HttpResponseView;
/** More rows for the Info section, after the standard ones, each keyed */
infoRows?: ReactElement[];
}
export function ResponseHeaders({ response, infoRows }: Props) {
const { openUrl, useDetailsBannerStoredValue } = useEditorHost();
return (
<div className="overflow-auto h-full pb-4 gap-y-3 flex flex-col pr-0.5">
<DetailsBanner
storageKey={`${response.ownerId}.general`}
summary={<h2>Info</h2>}
useStoredValue={useDetailsBannerStoredValue}
>
<KeyValueRows>
{[
<KeyValueRow key="sent" labelColor="secondary" label="Sent">
<time
dateTime={new Date(response.startedAt).toISOString()}
title={formatDistanceToNowStrict(response.startedAt, { addSuffix: true })}
>
{format(response.startedAt, "MMM d, yyyy, h:mm:ss a O")}
</time>
</KeyValueRow>,
<KeyValueRow key="url" labelColor="secondary" label="Request URL">
<div className="flex items-center gap-1">
<span className="select-text cursor-text">{response.url}</span>
{openUrl != null && (
<IconButton
iconSize="sm"
className="inline-block w-auto h-auto! opacity-50 hover:opacity-100"
icon="external_link"
onClick={() => openUrl(response.url)}
title="Open in browser"
/>
)}
</div>
</KeyValueRow>,
<KeyValueRow key="remote" labelColor="secondary" label="Remote Address">
{response.remoteAddr ?? <span className="text-text-subtlest">--</span>}
</KeyValueRow>,
<KeyValueRow key="version" labelColor="secondary" label="Version">
{response.version ?? <span className="text-text-subtlest">--</span>}
</KeyValueRow>,
...(infoRows ?? []),
]}
</KeyValueRows>
</DetailsBanner>
<HeadersTable
title="Request Headers"
headers={response.requestHeaders}
labelColor="primary"
storageKey={`${response.ownerId}.request_headers`}
useStoredValue={useDetailsBannerStoredValue}
/>
<HeadersTable
defaultOpen
title="Response Headers"
headers={response.headers}
labelColor="info"
storageKey={`${response.ownerId}.response_headers`}
useStoredValue={useDetailsBannerStoredValue}
/>
</div>
);
}
/** A row that copies its value with the host's clipboard */
export function KeyValueRow(props: Omit<KeyValueRowProps, "copy">) {
const { copy } = useEditorHost();
return <BaseKeyValueRow {...props} copy={copy} />;
}
+146
View File
@@ -0,0 +1,146 @@
import classNames from "classnames";
import type { FormEvent, ReactNode } from "react";
import { memo, useCallback, useRef, useState } from "react";
import type { HotkeyAction } from "../../hooks/useHotKey";
import { useFormattedHotkey, useHotKey } from "../../hooks/useHotKey";
import type { HostInputProps } from "../EditorHost";
import { useEditorHost } from "../EditorHost";
import type { IconProps } from "../Icon";
import { IconButton } from "../IconButton";
import type { InputHandle } from "../Input";
import { HStack } from "../Stacks";
export type UrlBarProps = {
url: string;
className?: string;
placeholder: string;
onSend?: () => void;
onUrlChange?: (url: string) => void;
onPaste?: (v: string) => void;
onPasteOverwrite?: HostInputProps["onPasteOverwrite"];
onCancel?: () => void;
submitIcon?: IconProps["icon"] | null;
isLoading?: boolean;
forceUpdateKey: string;
rightSlot?: ReactNode;
leftSlot?: ReactNode;
autocomplete?: HostInputProps["autocomplete"];
stateKey: HostInputProps["stateKey"];
/** Show the URL without a way to change or send it */
readOnly?: boolean;
/** The action whose keys send, shown on the send button */
sendHotkeyAction?: HotkeyAction;
/** The action whose keys select the whole URL */
focusHotkeyAction?: HotkeyAction;
};
export const UrlBar = memo(function UrlBar({
forceUpdateKey,
onUrlChange,
url,
placeholder,
className,
onSend,
onCancel,
onPaste,
onPasteOverwrite,
submitIcon = "send_horizontal",
autocomplete,
leftSlot,
rightSlot,
isLoading = false,
stateKey,
readOnly,
sendHotkeyAction,
focusHotkeyAction,
}: UrlBarProps) {
const { Input } = useEditorHost();
const inputRef = useRef<InputHandle>(null);
const [isFocused, setIsFocused] = useState<boolean>(false);
const handleInitInputRef = useCallback((h: InputHandle | null) => {
inputRef.current = h;
}, []);
useHotKey(focusHotkeyAction ?? null, () => {
inputRef.current?.selectAll();
});
const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (readOnly) return;
if (isLoading) onCancel?.();
else onSend?.();
};
return (
<form onSubmit={handleSubmit} className={classNames("x-theme-urlBar", className)}>
<Input
setRef={handleInitInputRef}
autocompleteFunctions
autocompleteVariables
stateKey={stateKey}
size="sm"
wrapLines={isFocused}
hideLabel
language="url"
className="px-1.5 py-0.5"
label="Enter URL"
name="url"
autocomplete={autocomplete}
forceUpdateKey={forceUpdateKey}
onFocus={() => setIsFocused(true)}
onBlur={() => setIsFocused(false)}
onPaste={onPaste}
onPasteOverwrite={onPasteOverwrite}
onChange={onUrlChange}
readOnly={readOnly}
defaultValue={url}
placeholder={placeholder}
leftSlot={leftSlot}
rightSlot={
<HStack space={0.5}>
{rightSlot && <div className="py-0.5 h-full">{rightSlot}</div>}
{submitIcon !== null && !readOnly && (
<div className="py-0.5 h-full">
<SendButton
icon={isLoading ? "x" : submitIcon}
hotkeyAction={sendHotkeyAction ?? null}
/>
</div>
)}
</HStack>
}
/>
</form>
);
});
function SendButton({
icon,
hotkeyAction,
}: {
icon: IconProps["icon"];
hotkeyAction: HotkeyAction | null;
}) {
const buttonRef = useRef<HTMLButtonElement>(null);
const hotkeyTrigger = useFormattedHotkey(hotkeyAction)?.join("");
useHotKey(hotkeyAction, () => buttonRef.current?.click());
return (
<IconButton
ref={buttonRef}
size="xs"
iconSize="md"
title={hotkeyTrigger ? `Send Request ${hotkeyTrigger}` : "Send Request"}
type="submit"
className="w-8 mr-0.5 h-full!"
iconColor="secondary"
icon={icon}
onMouseDown={(e) => {
// Prevent the button from taking focus
e.preventDefault();
}}
/>
);
}
@@ -0,0 +1,45 @@
import type { EditablePair, PairEditorHandle, PairEditorProps } from "../pairs/PairEditor";
import { PairOrBulkEditor } from "../pairs/PairOrBulkEditor";
import { VStack } from "../Stacks";
type Props = {
forceUpdateKey: string;
pairs: EditablePair[];
stateKey: PairEditorProps["stateKey"];
onChange?: PairEditorProps["onChange"];
/** Show the parameters without a way to change them */
readOnly?: boolean;
setRef?: (handle: PairEditorHandle) => void;
};
export function UrlParametersEditor({
pairs,
forceUpdateKey,
onChange = ignoreChange,
readOnly,
setRef,
stateKey,
}: Props) {
return (
<VStack className="h-full">
<PairOrBulkEditor
setRef={setRef}
allowMultilineValues
forceUpdateKey={forceUpdateKey}
nameAutocompleteFunctions
nameAutocompleteVariables
namePlaceholder="param_name"
onChange={onChange}
pairs={pairs}
preferenceName="url_parameters"
readOnly={readOnly}
stateKey={stateKey}
valueAutocompleteFunctions
valueAutocompleteVariables
valuePlaceholder="Value"
/>
</VStack>
);
}
function ignoreChange() {}
@@ -0,0 +1,247 @@
import type { ReactNode } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, test } from "vite-plus/test";
import type { HostEditorProps, HostInputProps, SelectFileProps } from "../EditorHost";
import { EditorHostProvider } from "../EditorHost";
import { PairEditorRow } from "../pairs/PairEditor";
import { bodyTypeLabel } from "./HttpRequestPaneView";
import { ResponseStatusLine } from "./HttpResponsePaneView";
import { JsonBodyEditor } from "./JsonBodyEditor";
import { RequestBodyViewer } from "./RequestBodyViewer";
import { HTMLOrTextViewer } from "./ResponseBodyViewer";
import { RequestMethodDropdown } from "./RequestMethodDropdown";
import { parseCookieHeaders } from "./ResponseCookies";
import {
BODY_TYPE_FORM_URLENCODED,
BODY_TYPE_JSON,
BODY_TYPE_OTHER,
type HttpBodyLoader,
type HttpResponseView,
} from "./types";
import { UrlBar } from "./UrlBar";
// Stand-ins for the host's editors that show what they were given
function StubInput({ name, defaultValue, readOnly, rightSlot, leftSlot }: HostInputProps) {
return (
<div data-input={name} data-readonly={String(!!readOnly)}>
{leftSlot}
{defaultValue}
{rightSlot}
</div>
);
}
function StubEditor({ defaultValue, readOnly, actions }: HostEditorProps) {
return (
<div data-editor data-readonly={String(!!readOnly)}>
{defaultValue}
{actions}
</div>
);
}
function StubSelectFile({ filePath }: SelectFileProps) {
return (
<button type="button" data-picker>
{filePath}
</button>
);
}
function render(node: ReactNode) {
return renderToStaticMarkup(
<EditorHostProvider value={{ Input: StubInput, Editor: StubEditor }}>
{node}
</EditorHostProvider>,
);
}
describe("read-only modes", () => {
test("a pair row shows its fields read-only, with no drag handle or row menu", () => {
const markup = render(
<PairEditorRow
readOnly
index={0}
stateKey="test"
pair={{ id: "p1", enabled: true, name: "Accept", value: "*/*" }}
/>,
);
expect(markup).toContain('data-input="name[0]" data-readonly="true"');
expect(markup).toContain('data-input="value[0]" data-readonly="true"');
expect(markup).not.toContain("More actions");
expect(markup).toMatch(/<input[^>]*disabled/);
});
test("a read-only file value shows as text, not a file picker", () => {
const pair = {
id: "p1",
enabled: true,
name: "avatar",
value: "/Users/me/avatar.png",
isFile: true,
};
const host = { Input: StubInput, Editor: StubEditor, SelectFile: StubSelectFile };
const readOnly = renderToStaticMarkup(
<EditorHostProvider value={host}>
<PairEditorRow readOnly allowFileValues index={0} stateKey="test" pair={pair} />
</EditorHostProvider>,
);
expect(readOnly).not.toContain("data-picker");
expect(readOnly).toContain("data-file-value");
expect(readOnly).toContain("/Users/me/avatar.png");
const editable = renderToStaticMarkup(
<EditorHostProvider value={host}>
<PairEditorRow allowFileValues index={0} stateKey="test" pair={pair} />
</EditorHostProvider>,
);
expect(editable).toContain("data-picker");
});
test("an editable pair row keeps its row menu", () => {
const markup = render(
<PairEditorRow
index={0}
stateKey="test"
pair={{ id: "p1", enabled: true, name: "Accept", value: "*/*" }}
/>,
);
expect(markup).toContain('data-input="name[0]" data-readonly="false"');
expect(markup).toContain("More actions");
});
test("the URL bar has no send button when read-only", () => {
const readOnly = render(
<UrlBar url="https://yaak.app" placeholder="" forceUpdateKey="" stateKey={null} readOnly />,
);
expect(readOnly).toContain('data-input="url" data-readonly="true"');
expect(readOnly).not.toContain("Send Request");
const editable = render(
<UrlBar url="https://yaak.app" placeholder="" forceUpdateKey="" stateKey={null} />,
);
expect(editable).toContain("Send Request");
});
test("the method shows without a menu when read-only", () => {
const readOnly = render(<RequestMethodDropdown method="POST" readOnly />);
const editable = render(<RequestMethodDropdown method="POST" onChange={() => {}} />);
expect(readOnly).toContain("POST");
expect(readOnly).not.toEqual(editable);
});
test("a read-only JSON body has no settings menu", () => {
const props = {
forceUpdateKey: "",
heightMode: "auto" as const,
requestId: "r1",
text: '{"a":1}',
sendJsonComments: false,
noticeStorageKey: "w1",
};
const readOnly = render(<JsonBodyEditor {...props} readOnly />);
expect(readOnly).toContain('data-readonly="true"');
expect(readOnly).not.toContain("JSON Settings");
const editable = render(<JsonBodyEditor {...props} onChange={() => {}} />);
expect(editable).toContain("JSON Settings");
});
});
describe("RequestBodyViewer", () => {
// A captured body that isn't valid UTF-8 has bytes but no text
const binary: HttpBodyLoader = {
useText: () => ({ data: null, isLoading: false, error: null }),
useBytes: () => ({ data: new Uint8Array([0x89, 0x50, 0xff]), isLoading: false, error: null }),
useUrl: () => ({ data: null, isLoading: false, error: null }),
};
test("shows a binary body instead of calling it empty", () => {
const markup = render(
<RequestBodyViewer id="b1" contentType="application/octet-stream" body={binary} />,
);
expect(markup).not.toContain("No request body");
expect(markup).toContain("Binary body, 3");
});
test("hands a binary image to the image viewer", () => {
const markup = render(<RequestBodyViewer id="b2" contentType="image/png" body={binary} />);
expect(markup).not.toContain("No request body");
expect(markup).not.toContain("Binary body");
});
});
describe("HTMLOrTextViewer", () => {
const response = {
id: "rs_1",
ownerId: "rq_1",
state: "closed",
status: 200,
statusReason: "OK",
url: "https://yaak.app",
headers: [],
requestHeaders: [],
contentType: "application/octet-stream",
contentLength: 3,
contentLengthCompressed: null,
requestContentLength: 0,
startedAt: "2026-11-04T22:10:00Z",
elapsedMs: 1,
elapsedHeadersMs: 1,
elapsedDnsMs: 0,
error: null,
remoteAddr: null,
version: null,
cookieHeaders: { sent: [], received: [] },
} satisfies HttpResponseView;
function loader(bytes: Uint8Array): HttpBodyLoader {
return {
useText: () => ({ data: null, isLoading: false, error: null }),
useBytes: () => ({ data: bytes, isLoading: false, error: null }),
useUrl: () => ({ data: null, isLoading: false, error: null }),
};
}
test("tells a binary response from an empty one", () => {
const binary = render(
<HTMLOrTextViewer response={response} body={loader(new Uint8Array([0xff, 0, 1]))} pretty />,
);
expect(binary).toContain("Binary body, 3");
const empty = render(
<HTMLOrTextViewer response={response} body={loader(new Uint8Array())} pretty />,
);
expect(empty).toContain("Empty response");
});
test("the status line keeps its height without the client's history button", () => {
// The client's button sets the line's height; without it, the line would hug the card's top
const markup = render(<ResponseStatusLine response={response} />);
expect(markup).toContain("min-h-[calc(var(--height-sm)+0.25rem)]");
});
});
describe("bodyTypeLabel", () => {
test("names a body by its type", () => {
expect(bodyTypeLabel(BODY_TYPE_JSON, "application/json")).toBe("JSON");
expect(bodyTypeLabel(BODY_TYPE_FORM_URLENCODED, null)).toBe("Url Encoded");
expect(bodyTypeLabel(BODY_TYPE_OTHER, "text/markdown")).toBe("Markdown");
expect(bodyTypeLabel(null, null)).toBe("Body");
});
});
describe("parseCookieHeaders", () => {
test("splits sent cookies and parses received ones, the latest of a name winning", () => {
const { sentCookies, receivedCookies } = parseCookieHeaders({
sent: ["a=1; b=2", "a=3"],
received: ["s=x; Path=/; HttpOnly", "gone=; Max-Age=0"],
});
expect(sentCookies).toEqual([
{ name: "a", value: "3" },
{ name: "b", value: "2" },
]);
expect(receivedCookies[0]).toMatchObject({ name: "s", value: "x", path: "/", httpOnly: true });
expect(receivedCookies[1]).toMatchObject({ name: "gone", isDeleted: true });
});
});
+127
View File
@@ -0,0 +1,127 @@
/**
* What the shared request and response panes render. Each app maps its own models onto these
* (the client its `HttpRequest` and `HttpResponse`, the proxy a captured `Flow`), so the panes
* never import either app's model types.
*/
export const BODY_TYPE_NONE = null;
export const BODY_TYPE_GRAPHQL = "graphql";
export const BODY_TYPE_JSON = "application/json";
export const BODY_TYPE_BINARY = "binary";
export const BODY_TYPE_OTHER = "other";
export const BODY_TYPE_FORM_URLENCODED = "application/x-www-form-urlencoded";
export const BODY_TYPE_FORM_MULTIPART = "multipart/form-data";
export const BODY_TYPE_XML = "text/xml";
/** A header, URL parameter or form field */
export interface HttpPairView {
id?: string;
name: string;
value: string;
enabled?: boolean;
}
/** A form field. A multipart file field has the file's path in `file` instead of a `value` */
export interface HttpFormFieldView {
id?: string;
name: string;
value?: string;
enabled?: boolean;
file?: string;
contentType?: string;
filename?: string;
}
/** A request body, in the shape its body type edits it */
export interface HttpRequestBodyView {
/** Text bodies: JSON, XML, GraphQL's query, anything else typed */
text?: string;
/** Form bodies, url-encoded or multipart */
form?: HttpFormFieldView[];
/** A binary body read from a file when sent */
filePath?: string;
/** JSON sent as written, comments and all, instead of cleaned up first */
sendJsonComments?: boolean;
}
/** Authentication, for display only; editing it stays with the app that knows its plugins */
export interface HttpAuthView {
/** The scheme's name, like `Bearer` or `Basic` */
label: string;
/** What it sends, like an `Authorization` header */
values: HttpPairView[];
}
export interface HttpRequestView {
/** Identifies the request across renders, for remembered tabs and editor state */
id: string;
method: string;
url: string;
headers: HttpPairView[];
urlParameters: HttpPairView[];
/** One of the `BODY_TYPE_*` values, or a content type the body was typed as */
bodyType: string | null;
body: HttpRequestBodyView;
/** The request's Content-Type header */
contentType: string | null;
auth: HttpAuthView | null;
}
export type HttpResponseViewState = "initialized" | "connected" | "closed";
export interface HttpResponseView {
/** Identifies this response, for viewer state */
id: string;
/** Identifies what it answers (the request or flow), for state kept across responses */
ownerId: string;
state: HttpResponseViewState;
/** 0 before a status arrives or when it never did */
status: number;
statusReason: string | null;
url: string;
headers: HttpPairView[];
requestHeaders: HttpPairView[];
/** The response's Content-Type header */
contentType: string | null;
/** Decoded body bytes */
contentLength: number | null;
/** Body bytes on the wire, when it was compressed */
contentLengthCompressed: number | null;
/** Request body bytes sent */
requestContentLength: number | null;
/** When the request started, as an ISO 8601 string with its zone */
startedAt: string;
elapsedMs: number;
elapsedHeadersMs: number;
elapsedDnsMs: number;
error: string | null;
remoteAddr: string | null;
version: string | null;
/** Raw `Cookie` header values sent and `Set-Cookie` values received */
cookieHeaders: { sent: string[]; received: string[] };
}
/** What a viewer gets back from loading part of a body */
export interface HttpBodyResult<T> {
data: T | undefined;
isLoading: boolean;
error: Error | null;
}
/**
* How a pane reads a body, which each app stores its own way: the client in files read
* through its host, the proxy through the engine's body RPC. Each `use*` is a hook, called
* unconditionally by the viewer that needs that form of the body.
*/
export interface HttpBodyLoader {
/** The body as text, null when there's none */
useText: () => HttpBodyResult<string | null>;
/** The body's bytes, null when there's none */
useBytes: () => HttpBodyResult<Uint8Array | null>;
/** A URL a media element can load the body from, null when there's none or not `enabled` */
useUrl: (enabled: boolean) => HttpBodyResult<string | null>;
/** Run a JSONPath or XPath filter over the body */
filterText?: (expression: string) => Promise<string | null>;
/** The body's text, for copying, without rendering it */
getText?: () => Promise<string | null>;
}
@@ -0,0 +1,167 @@
import { describe, expect, test } from "vite-plus/test";
import {
formatBulkPairLine,
formatBulkPairs,
parseBulkPairLine,
parseBulkPairs,
} from "./BulkPairEditor";
describe("parseBulkPairLine", () => {
test("parses colon-space pairs as name and value", () => {
expect(parseBulkPairLine("foo: bar")).toMatchObject({
enabled: true,
name: "foo",
value: "bar",
});
});
test("preserves colon-without-space lines as a name with an empty value", () => {
expect(parseBulkPairLine("foo:bar")).toMatchObject({
enabled: true,
name: "foo:bar",
value: "",
});
});
test("preserves malformed lines instead of dropping their contents", () => {
expect(parseBulkPairLine("not a pair")).toMatchObject({
enabled: true,
name: "not a pair",
value: "",
});
});
test("unescapes newlines in parsed values", () => {
expect(parseBulkPairLine("foo: bar\\nbaz")).toMatchObject({
enabled: true,
name: "foo",
value: "bar\nbaz",
});
});
test("parses commented-out pairs as disabled", () => {
expect(parseBulkPairLine("# foo: bar")).toMatchObject({
enabled: false,
name: "foo",
value: "bar",
});
});
test("allows any whitespace after the comment marker", () => {
expect(parseBulkPairLine("# foo: bar")).toMatchObject({
enabled: false,
name: "foo",
value: "bar",
});
expect(parseBulkPairLine("#\tfoo: bar")).toMatchObject({
enabled: false,
name: "foo",
value: "bar",
});
});
test("treats a # without whitespace after it as part of the name", () => {
expect(parseBulkPairLine("#foo: bar")).toMatchObject({
enabled: true,
name: "#foo",
value: "bar",
});
});
test("parses commented-out pairs with an empty value as disabled", () => {
expect(parseBulkPairLine("# token:")).toMatchObject({
enabled: false,
name: "token",
value: "",
});
expect(parseBulkPairLine("# token: ")).toMatchObject({
enabled: false,
name: "token",
value: "",
});
});
test("drops comments that are not pairs", () => {
expect(parseBulkPairLine("# just a comment")).toBeNull();
expect(parseBulkPairLine("# see http://example.com")).toBeNull();
expect(parseBulkPairLine("#")).toBeNull();
});
test("leaves values that start with # alone", () => {
expect(parseBulkPairLine("color: #fff")).toMatchObject({
enabled: true,
name: "color",
value: "#fff",
});
});
});
describe("formatBulkPairLine", () => {
test("formats enabled pairs as name and value", () => {
expect(formatBulkPairLine({ enabled: true, name: "foo", value: "bar" })).toBe("foo: bar");
});
test("treats pairs without an enabled flag as enabled", () => {
expect(formatBulkPairLine({ name: "foo", value: "bar" })).toBe("foo: bar");
});
test("comments out disabled pairs", () => {
expect(formatBulkPairLine({ enabled: false, name: "foo", value: "bar" })).toBe("# foo: bar");
});
test("escapes newlines in values", () => {
expect(formatBulkPairLine({ enabled: false, name: "foo", value: "a\nb" })).toBe("# foo: a\\nb");
});
});
describe("bulk pair round trip", () => {
test("preserves order and enabled state", () => {
const pairs = [
{ enabled: true, name: "host", value: "example.com" },
{ enabled: false, name: "token", value: "old" },
{ enabled: true, name: "color", value: "#fff" },
{ enabled: false, name: "multi", value: "a\nb" },
{ enabled: true, name: "token", value: "new" },
];
const text = formatBulkPairs(pairs);
expect(text).toBe(
["host: example.com", "# token: old", "color: #fff", "# multi: a\\nb", "token: new"].join(
"\n",
),
);
const parsed = parseBulkPairs(text);
expect(parsed.map(({ enabled, name, value }) => ({ enabled, name, value }))).toEqual(pairs);
});
test("preserves enabled pairs whose name starts with #", () => {
const pairs = [{ enabled: true, name: "#foo", value: "bar" }];
const text = formatBulkPairs(pairs);
expect(text).toBe("#foo: bar");
const parsed = parseBulkPairs(text);
expect(parsed.map(({ enabled, name, value }) => ({ enabled, name, value }))).toEqual(pairs);
});
test("preserves disabled pairs whose name starts with #", () => {
const pairs = [{ enabled: false, name: "#foo", value: "bar" }];
const text = formatBulkPairs(pairs);
expect(text).toBe("# #foo: bar");
const parsed = parseBulkPairs(text);
expect(parsed.map(({ enabled, name, value }) => ({ enabled, name, value }))).toEqual(pairs);
});
test("preserves disabled pairs with empty values", () => {
const pairs = [{ enabled: false, name: "token", value: "" }];
const parsed = parseBulkPairs(formatBulkPairs(pairs));
expect(parsed.map(({ enabled, name, value }) => ({ enabled, name, value }))).toEqual(pairs);
});
test("drops free-text comments and blank lines between pairs", () => {
const parsed = parseBulkPairs("# Auth\nfoo: bar\n\n# baz: qux\n# see the docs");
expect(parsed.map(({ enabled, name, value }) => ({ enabled, name, value }))).toEqual([
{ enabled: true, name: "foo", value: "bar" },
{ enabled: false, name: "baz", value: "qux" },
]);
});
});
@@ -0,0 +1,101 @@
import { generateId } from "@yaakapp-internal/lib/generateId";
import { useCallback, useMemo } from "react";
import { useEditorHost } from "../EditorHost";
import type { Pair, PairEditorProps, PairWithId } from "./PairEditor";
type Props = PairEditorProps;
export function BulkPairEditor({
pairs,
onChange,
namePlaceholder,
valuePlaceholder,
forceUpdateKey,
forcedEnvironmentId,
readOnly,
stateKey,
}: Props) {
const { Editor } = useEditorHost();
const pairsText = useMemo(() => formatBulkPairs(pairs), [pairs]);
const handleChange = useCallback(
(text: string) => {
onChange(parseBulkPairs(text));
},
[onChange],
);
return (
<Editor
autocompleteFunctions
autocompleteVariables
stateKey={`bulk_pair.${stateKey}`}
forcedEnvironmentId={forcedEnvironmentId}
forceUpdateKey={forceUpdateKey}
placeholder={`${namePlaceholder ?? "name"}: ${valuePlaceholder ?? "value"}`}
defaultValue={pairsText}
language="pairs"
readOnly={readOnly}
onChange={handleChange}
/>
);
}
export function formatBulkPairs(pairs: Pair[]): string {
return pairs
.filter((p) => !(p.name.trim() === "" && p.value.trim() === ""))
.map(formatBulkPairLine)
.join("\n");
}
export function parseBulkPairs(text: string): PairWithId[] {
return text
.split("\n")
.filter((l: string) => l.trim())
.map(parseBulkPairLine)
.filter((p) => p != null);
}
/**
* Format a pair as a `name: value` line. Disabled pairs are commented out dotenv-style, as
* `# name: value`, so the enabled state survives a round trip through {@link parseBulkPairLine}.
*/
export function formatBulkPairLine(pair: Pair) {
const value = pair.value.replaceAll("\n", "\\n");
const line = `${pair.name}: ${value}`;
return pair.enabled === false ? `# ${line}` : line;
}
const PAIR_REGEX = /^([^:]+):\s+(.*)$/;
// Commented-out pairs may also end at the colon, so a hand-typed `# name:` isn't dropped
const COMMENTED_PAIR_REGEX = /^([^:]+):(?:\s+(.*))?$/;
// A # only marks a comment when followed by whitespace (or nothing), so `#foo: bar` stays an
// enabled pair named `#foo`
const COMMENT_PREFIX_REGEX = /^\s*#(?:\s+|$)/;
/**
* Parse a `name: value` line into an enabled pair. A line starting with `# ` is a disabled pair
* if the rest of it parses as `name: value`, otherwise it's a free-text comment and `null` is
* returned so it can be dropped.
*/
export function parseBulkPairLine(line: string): PairWithId | null {
const commentPrefix = line.match(COMMENT_PREFIX_REGEX);
if (commentPrefix != null) {
const uncommented = line.slice(commentPrefix[0].length);
const match = uncommented.match(COMMENTED_PAIR_REGEX);
if (match == null) return null;
return { ...pairFromMatch(uncommented, match), enabled: false };
}
return pairFromMatch(line, line.match(PAIR_REGEX));
}
function pairFromMatch(line: string, match: RegExpMatchArray | null): PairWithId {
const [, name, value] = match ?? [];
return {
enabled: true,
name: (name ?? line).trim(),
value: (value ?? "").replaceAll("\\n", "\n").trim(),
id: generateId(),
};
}
@@ -0,0 +1,994 @@
import type { DragEndEvent, DragMoveEvent, DragStartEvent } from "@dnd-kit/core";
import {
DndContext,
DragOverlay,
PointerSensor,
pointerWithin,
useDraggable,
useDroppable,
useSensor,
useSensors,
} from "@dnd-kit/core";
import classNames from "classnames";
import { Fragment, useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useRandomKey } from "../../hooks/useRandomKey";
import { useToggle } from "../../hooks/useToggle";
import { computeSideForDragMove } from "../../lib/dnd";
import { languageFromContentType } from "../../lib/contentType";
import { Button } from "../Button";
import { Checkbox } from "../Checkbox";
import { DropMarker } from "../DropMarker";
import type { DropdownItem } from "../Dropdown";
import { Dropdown } from "../Dropdown";
import type { GenericCompletionConfig, HostInputProps, TemplatingProps } from "../EditorHost";
import { useEditorHost } from "../EditorHost";
import { Icon } from "../Icon";
import { IconButton } from "../IconButton";
import type { InputHandle } from "../Input";
import type { RadioDropdownItem } from "../RadioDropdown";
import { RadioDropdown } from "../RadioDropdown";
import { ensurePairId } from "./PairEditor.util";
export interface PairEditorHandle {
/**
* Focus a row's name field once it's able to take focus. Focus can't land immediately when the
* row isn't mounted yet or the editor is hidden — eg. sitting in a tab that's still becoming
* active — so this retries for up to ~1s. A newer focus request cancels a pending one.
*/
focusName(id: string): void;
/** Focus a row's value field. See {@link PairEditorHandle.focusName} for timing. */
focusValue(id: string): void;
}
/** ~1s at 60fps, plenty for a tab switch to land without spinning forever if it never does */
const MAX_FOCUS_ATTEMPTS = 60;
export type PairEditorProps = {
allowFileValues?: boolean;
allowMultilineValues?: boolean;
className?: string;
forcedEnvironmentId?: string;
forceUpdateKey?: string;
nameAutocomplete?: GenericCompletionConfig;
nameAutocompleteFunctions?: boolean;
nameAutocompleteVariables?: boolean;
namePlaceholder?: string;
nameValidate?: HostInputProps["validate"];
noScroll?: boolean;
onChange: (pairs: PairWithId[]) => void;
pairs: EditablePair[];
/** Show the pairs without a way to change them: no new row, no drag, no row menu */
readOnly?: boolean;
stateKey: HostInputProps["stateKey"];
setRef?: (n: PairEditorHandle) => void;
valueAutocomplete?: (name: string) => GenericCompletionConfig | undefined;
valueAutocompleteFunctions?: boolean;
valueAutocompleteVariables?: boolean | "environment";
valuePlaceholder?: string;
valueType?: HostInputProps["type"] | ((pair: Pair) => HostInputProps["type"]);
valueValidate?: HostInputProps["validate"];
};
export type Pair = {
id?: string;
enabled?: boolean;
name: string;
value: string;
contentType?: string;
filename?: string;
isFile?: boolean;
};
export type PairWithId = Pair & {
id: string;
};
/**
* A pair as handed to the editor. Adds behaviour that only the editor cares about, so the plain
* `Pair` stays the shape that gets written to models.
*/
export type EditablePair = Pair & {
/**
* When set, name edits are held until the field blurs and then committed through this, instead
* of calling `onChange` on every keystroke. Return false to reject the new name, which reverts
* the field. For names that can't be written directly, like a URL path placeholder that lives
* in the URL itself.
*/
commitName?: (name: string) => boolean;
};
type EditablePairWithId = EditablePair & {
id: string;
};
/** Strip the editor-only fields, so they can never reach a model write */
function toPairData({ commitName: _commitName, ...pair }: EditablePairWithId): PairWithId {
return pair;
}
/** Max number of pairs to show before prompting the user to reveal the rest */
const MAX_INITIAL_PAIRS = 30;
// Keyed on `stateKey` so no state survives a change of owner. Row ids alone can't tell owners
// apart — two pair sets can share ids (eg. one duplicated from the other), and the same-rows
// fast path below would swap in the new data without rebuilding the row editors, leaving any
// still-focused input showing the old owner's text.
export function PairEditor(props: PairEditorProps) {
return <PairEditorInner key={props.stateKey} {...props} />;
}
function PairEditorInner({
allowFileValues,
allowMultilineValues,
className,
forcedEnvironmentId,
forceUpdateKey,
nameAutocomplete,
nameAutocompleteFunctions,
nameAutocompleteVariables,
namePlaceholder,
nameValidate,
noScroll,
onChange,
pairs: originalPairs,
readOnly,
stateKey,
valueAutocomplete,
valueAutocompleteFunctions,
valueAutocompleteVariables,
valuePlaceholder,
valueType,
valueValidate,
setRef,
}: PairEditorProps) {
const [hoveredIndex, setHoveredIndex] = useState<number | null>(null);
const [isDragging, setIsDragging] = useState<EditablePairWithId | null>(null);
const [pairs, setPairs] = useState<EditablePairWithId[]>([]);
const [showAll, toggleShowAll] = useToggle(false);
// NOTE: Use local force update key because we trigger an effect on forceUpdateKey change. If
// we simply pass forceUpdateKey to the editor, the data set by useEffect will be stale.
const [localForceUpdateKey, regenerateLocalForceUpdateKey] = useRandomKey();
const rowsRef = useRef<Record<string, RowHandle | null>>({});
const pendingFocusFrame = useRef<number | null>(null);
useEffect(
() => () => {
if (pendingFocusFrame.current != null) cancelAnimationFrame(pendingFocusFrame.current);
},
[],
);
const focusWhenReady = useCallback((id: string, field: "name" | "value") => {
if (pendingFocusFrame.current != null) cancelAnimationFrame(pendingFocusFrame.current);
let attemptsLeft = MAX_FOCUS_ATTEMPTS;
const attempt = () => {
pendingFocusFrame.current = null;
const row = rowsRef.current[id];
const landed = field === "name" ? row?.focusName() : row?.focusValue();
if (landed || --attemptsLeft <= 0) return;
pendingFocusFrame.current = requestAnimationFrame(attempt);
};
attempt();
}, []);
const handle = useMemo<PairEditorHandle>(
() => ({
focusName(id: string) {
focusWhenReady(id, "name");
},
focusValue(id: string) {
focusWhenReady(id, "value");
},
}),
[focusWhenReady],
);
const initPairEditorRow = useCallback(
(id: string, n: RowHandle | null) => {
const isLast = id === pairs[pairs.length - 1]?.id;
if (isLast && !readOnly) return; // Never add the last pair
rowsRef.current[id] = n;
const validHandles = Object.values(rowsRef.current).filter((v) => v != null);
// Use >= because more might be added if an ID of one changes (eg. editing placeholder in URL regenerates fresh pairs every keystroke)
const ready = validHandles.length >= pairs.length - (readOnly ? 0 : 1);
if (ready) {
setRef?.(handle);
}
},
[handle, pairs, readOnly, setRef],
);
// oxlint-disable-next-line react-hooks/exhaustive-deps -- Only care about forceUpdateKey
useEffect(() => {
// Remove empty headers on initial render and ensure they all have valid ids (pairs didn't use to have IDs)
const newPairs: EditablePairWithId[] = [];
for (let i = 0; i < originalPairs.length; i++) {
const p = originalPairs[i];
if (!p) continue; // Make TS happy
if (isPairEmpty(p)) continue;
newPairs.push(ensurePairId(p));
}
// Nothing can be added, so there's no trailing row to start a new pair with
if (readOnly) {
setPairs(newPairs);
regenerateLocalForceUpdateKey();
return;
}
// When the reset holds the exact same rows (eg. renaming a URL path placeholder, which keeps
// every row id), swap the data in without rebuilding the row editors. Unfocused inputs re-seed
// themselves when `defaultValue` changes, and a rebuild would drop the user's focus and
// selection — like tabbing from a placeholder's name into its value.
const trailingPair = pairs[pairs.length - 1];
const sameRows =
trailingPair != null &&
isPairEmpty(trailingPair) &&
pairs.length === newPairs.length + 1 &&
newPairs.every((p, i) => p.id === pairs[i]?.id);
if (sameRows) {
setPairs([...newPairs, trailingPair]);
return;
}
// Add empty last pair if there is none
const lastPair = newPairs[newPairs.length - 1];
if (lastPair == null || !isPairEmpty(lastPair)) {
newPairs.push(emptyPair());
}
setPairs(newPairs);
regenerateLocalForceUpdateKey();
}, [forceUpdateKey]);
const setPairsAndSave = useCallback(
(fn: (pairs: EditablePairWithId[]) => EditablePairWithId[]) => {
setPairs((oldPairs) => {
const pairs = fn(oldPairs);
onChange(pairs.map(toPairData));
return pairs;
});
},
[onChange],
);
const handleChange = useCallback(
(pair: EditablePairWithId) =>
setPairsAndSave((pairs) => pairs.map((p) => (pair.id !== p.id ? p : pair))),
[setPairsAndSave],
);
const handleDelete = useCallback(
(pair: Pair, focusPrevious: boolean) => {
if (focusPrevious) {
const index = pairs.findIndex((p) => p.id === pair.id);
const id = pairs[index - 1]?.id ?? null;
rowsRef.current[id ?? "n/a"]?.focusName();
}
return setPairsAndSave((oldPairs) => oldPairs.filter((p) => p.id !== pair.id));
},
[setPairsAndSave, pairs],
);
const handleFocusName = useCallback(
(pair: Pair) => {
const isLast = pair.id === pairs[pairs.length - 1]?.id;
if (isLast) setPairs([...pairs, emptyPair()]);
},
[pairs],
);
const handleFocusValue = useCallback(
(pair: Pair) => {
const isLast = pair.id === pairs[pairs.length - 1]?.id;
if (isLast) setPairs([...pairs, emptyPair()]);
},
[pairs],
);
const sensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 6 } }));
// dnd-kit: show the “between rows” marker while hovering
const onDragMove = useCallback(
(e: DragMoveEvent) => {
const overId = e.over?.id as string | undefined;
if (!overId) return setHoveredIndex(null);
const overPair = pairs.find((p) => p.id === overId);
if (overPair == null) return setHoveredIndex(null);
const side = computeSideForDragMove(overPair.id, e);
const overIndex = pairs.findIndex((p) => p.id === overId);
const hoveredIndex = overIndex + (side === "before" ? 0 : 1);
setHoveredIndex(hoveredIndex);
},
[pairs],
);
const onDragStart = useCallback(
(e: DragStartEvent) => {
const pair = pairs.find((p) => p.id === e.active.id);
setIsDragging(pair ?? null);
},
[pairs],
);
const onDragCancel = useCallback(() => setIsDragging(null), []);
const onDragEnd = useCallback(
(e: DragEndEvent) => {
setIsDragging(null);
setHoveredIndex(null);
const activeId = e.active.id as string | undefined;
const overId = e.over?.id as string | undefined;
if (!activeId || !overId) return;
const from = pairs.findIndex((p) => p.id === activeId);
const baseTo = pairs.findIndex((p) => p.id === overId);
const to = hoveredIndex ?? (baseTo === -1 ? from : baseTo);
if (from !== -1 && to !== -1 && from !== to) {
setPairsAndSave((ps) => {
const next = [...ps];
const [moved] = next.splice(from, 1);
if (moved === undefined) return ps; // Make TS happy
next.splice(to > from ? to - 1 : to, 0, moved);
return next;
});
}
},
[pairs, hoveredIndex, setPairsAndSave],
);
return (
<div
className={classNames(
className,
"@container relative",
"pb-2 mb-auto h-full",
!noScroll && "overflow-y-auto max-h-full",
// Move over the width of the drag handle
"-mr-2 pr-2",
// Pad to make room for the drag divider
"pt-0.5",
"grid grid-rows-[auto_1fr]",
)}
>
<div>
<DndContext
autoScroll
sensors={sensors}
onDragMove={onDragMove}
onDragEnd={onDragEnd}
onDragStart={onDragStart}
onDragCancel={onDragCancel}
collisionDetection={pointerWithin}
>
{pairs.map((p, i) => {
if (!showAll && i > MAX_INITIAL_PAIRS) return null;
const isLast = !readOnly && i === pairs.length - 1;
return (
<Fragment key={p.id}>
{hoveredIndex === i && <DropMarker />}
<PairEditorRow
readOnly={readOnly}
setRef={initPairEditorRow}
allowFileValues={allowFileValues}
allowMultilineValues={allowMultilineValues}
className="py-1"
forcedEnvironmentId={forcedEnvironmentId}
forceUpdateKey={localForceUpdateKey}
index={i}
isLast={isLast}
isDraggingGlobal={!!isDragging}
nameAutocomplete={nameAutocomplete}
nameAutocompleteFunctions={nameAutocompleteFunctions}
nameAutocompleteVariables={nameAutocompleteVariables}
namePlaceholder={namePlaceholder}
nameValidate={nameValidate}
onChange={handleChange}
onDelete={handleDelete}
onFocusName={handleFocusName}
onFocusValue={handleFocusValue}
pair={p}
stateKey={stateKey}
valueAutocomplete={valueAutocomplete}
valueAutocompleteFunctions={valueAutocompleteFunctions}
valueAutocompleteVariables={valueAutocompleteVariables}
valuePlaceholder={valuePlaceholder}
valueType={valueType}
valueValidate={valueValidate}
/>
</Fragment>
);
})}
{!showAll && pairs.length > MAX_INITIAL_PAIRS && (
<Button onClick={toggleShowAll} variant="border" className="m-2" size="xs">
Show {pairs.length - MAX_INITIAL_PAIRS} More
</Button>
)}
<DragOverlay dropAnimation={null}>
{isDragging && (
<PairEditorRow
namePlaceholder={namePlaceholder}
valuePlaceholder={valuePlaceholder}
className="opacity-80"
pair={isDragging}
index={0}
stateKey={null}
/>
)}
</DragOverlay>
</DndContext>
</div>
<div
// There's a weird bug where clicking below one of the above Codemirror inputs will cause
// it to focus. Putting this element here prevents that
aria-hidden
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
}}
/>
</div>
);
}
type PairEditorRowProps = {
className?: string;
pair: EditablePairWithId;
forceFocusNamePairId?: string | null;
forceFocusValuePairId?: string | null;
onChange?: (pair: EditablePairWithId) => void;
onDelete?: (pair: EditablePairWithId, focusPrevious: boolean) => void;
onFocusName?: (pair: EditablePairWithId) => void;
onFocusValue?: (pair: EditablePairWithId) => void;
onSubmit?: (pair: EditablePairWithId) => void;
isLast?: boolean;
disabled?: boolean;
disableDrag?: boolean;
readOnly?: boolean;
index: number;
isDraggingGlobal?: boolean;
setRef?: (id: string, n: RowHandle | null) => void;
} & Pick<
PairEditorProps,
| "allowFileValues"
| "allowMultilineValues"
| "forcedEnvironmentId"
| "forceUpdateKey"
| "nameAutocomplete"
| "nameAutocompleteVariables"
| "namePlaceholder"
| "nameValidate"
| "nameAutocompleteFunctions"
| "stateKey"
| "valueAutocomplete"
| "valueAutocompleteFunctions"
| "valueAutocompleteVariables"
| "valuePlaceholder"
| "valueType"
| "valueValidate"
>;
interface RowHandle {
focusName(): boolean;
focusValue(): boolean;
}
export function PairEditorRow({
allowFileValues,
allowMultilineValues,
className,
disableDrag,
disabled,
forceUpdateKey,
forcedEnvironmentId,
index,
isLast,
nameAutocomplete,
nameAutocompleteFunctions,
nameAutocompleteVariables,
namePlaceholder,
nameValidate,
isDraggingGlobal,
onChange,
onDelete,
onFocusName,
onFocusValue,
pair,
readOnly,
stateKey,
valueAutocomplete,
valueAutocompleteFunctions,
valueAutocompleteVariables,
valuePlaceholder,
valueType,
valueValidate,
setRef,
}: PairEditorRowProps) {
const { Input, SelectFile, showDialog } = useEditorHost();
const nameInputRef = useRef<InputHandle>(null);
const valueInputRef = useRef<InputHandle>(null);
const handle = useRef<RowHandle>({
focusName() {
nameInputRef.current?.focus();
return nameInputRef.current?.isFocused() ?? false;
},
focusValue() {
valueInputRef.current?.focus();
return valueInputRef.current?.isFocused() ?? false;
},
});
const initNameInputRef = useCallback(
(n: InputHandle | null) => {
nameInputRef.current = n;
if (nameInputRef.current && valueInputRef.current) {
setRef?.(pair.id, handle.current);
}
},
[pair.id, setRef],
);
const initValueInputRef = useCallback(
(n: InputHandle | null) => {
valueInputRef.current = n;
if (nameInputRef.current && valueInputRef.current) {
setRef?.(pair.id, handle.current);
}
},
[pair.id, setRef],
);
const handleFocusName = useCallback(() => onFocusName?.(pair), [onFocusName, pair]);
const handleFocusValue = useCallback(() => onFocusValue?.(pair), [onFocusValue, pair]);
const handleDelete = useCallback(() => onDelete?.(pair, false), [onDelete, pair]);
const handleChangeEnabled = useMemo(
() => (enabled: boolean) => onChange?.({ ...pair, enabled }),
[onChange, pair],
);
// The name being typed into a deferred-commit field, before it's committed or reverted
const pendingName = useRef<string | null>(null);
const handleChangeName = useMemo(
() => (name: string) => {
// Keep the edit local until commit. Writing on every keystroke would reset the editor from
// beneath the cursor, since the pairs are derived from the name being edited.
if (pair.commitName != null) pendingName.current = name;
else onChange?.({ ...pair, name });
},
[onChange, pair],
);
const revertName = useCallback(() => {
const nameInput = nameInputRef.current;
if (nameInput != null) {
const changes = { from: 0, to: nameInput.value().length, insert: pair.name };
nameInput.dispatch({ changes });
}
pendingName.current = null;
}, [pair.name]);
const handleBlurName = useCallback(() => {
if (pair.commitName == null) return;
const name = pendingName.current;
pendingName.current = null;
if (name == null || name === pair.name) return;
if (!pair.commitName(name)) revertName();
}, [pair, revertName]);
const handleChangeValueText = useMemo(
() => (value: string) => onChange?.({ ...pair, value, isFile: false }),
[onChange, pair],
);
const handleChangeValueFile = useMemo(
() =>
({ filePath }: { filePath: string | null }) =>
onChange?.({ ...pair, value: filePath ?? "", isFile: true }),
[onChange, pair],
);
const handleChangeValueContentType = useMemo(
() => (contentType: string) => onChange?.({ ...pair, contentType }),
[onChange, pair],
);
const handleChangeValueFilename = useMemo(
() => (filename: string) => onChange?.({ ...pair, filename }),
[onChange, pair],
);
const handleEditMultiLineValue = useCallback(
() =>
showDialog?.({
id: "pair-edit-multiline",
size: "dynamic",
title: <>Edit {pair.name}</>,
render: ({ hide }) => (
<MultilineEditDialog
hide={hide}
onChange={handleChangeValueText}
defaultValue={pair.value}
contentType={pair.contentType ?? null}
/>
),
}),
[handleChangeValueText, pair.contentType, pair.name, pair.value, showDialog],
);
const defaultItems = useMemo(
(): DropdownItem[] => [
{
label: "Edit Multi-line",
onSelect: handleEditMultiLineValue,
hidden: !allowMultilineValues,
},
{
label: "Delete",
onSelect: handleDelete,
color: "danger",
},
],
[allowMultilineValues, handleDelete, handleEditMultiLineValue],
);
const { attributes, listeners, setNodeRef: setDraggableRef } = useDraggable({ id: pair.id });
const { setNodeRef: setDroppableRef } = useDroppable({ id: pair.id });
// Filter out the current pair name
const valueAutocompleteVariablesFiltered = useMemo<
TemplatingProps["autocompleteVariables"]
>(() => {
if (valueAutocompleteVariables === "environment") {
return (v): boolean => v.variable.name !== pair.name;
}
return valueAutocompleteVariables;
}, [pair.name, valueAutocompleteVariables]);
const handleSetRef = useCallback(
(n: HTMLDivElement | null) => {
setDraggableRef(n);
setDroppableRef(n);
},
[setDraggableRef, setDroppableRef],
);
// Skip the trailing placeholder row. It exists to start a new pair, so a picker there would
// imply it's already a real row. `Input` handles the rest, including having no options to show.
const showOptionsPicker = !isLast;
return (
<div
ref={handleSetRef}
className={classNames(
className,
"group/pair-row grid grid-cols-[auto_auto_minmax(0,1fr)_auto]",
"grid-rows-1 items-center",
!pair.enabled && "opacity-60",
)}
>
<Checkbox
hideLabel
title={pair.enabled ? "Disable item" : "Enable item"}
disabled={isLast || disabled || readOnly}
checked={isLast ? false : !!pair.enabled}
className={classNames(isLast && "opacity-disabled!")}
onChange={handleChangeEnabled}
/>
{!isLast && !disableDrag && !readOnly ? (
<div
{...attributes}
{...listeners}
className={classNames(
"py-2 h-7 w-4 flex items-center",
"justify-center opacity-0 group-hover/pair-row:opacity-70",
)}
>
<Icon size="sm" icon="grip_vertical" className="pointer-events-none" />
</div>
) : (
<span className="w-4" />
)}
<div
className={classNames(
"grid items-center",
"@xs:gap-2 @xs:grid-rows-1! @xs:grid-cols-[minmax(0,1fr)_minmax(0,1fr)]!",
"gap-0.5 grid-cols-1 grid-rows-2",
)}
>
<Input
setRef={initNameInputRef}
hideLabel
stateKey={`name.${pair.id}.${stateKey}`}
disabled={disabled}
wrapLines={false}
readOnly={readOnly || isDraggingGlobal}
size="sm"
required={!isLast && !!pair.enabled && !!pair.value}
validate={nameValidate}
forcedEnvironmentId={forcedEnvironmentId}
forceUpdateKey={forceUpdateKey}
containerClassName={classNames("bg-surface", isLast && "border-dashed")}
defaultValue={pair.name}
label="Name"
name={`name[${index}]`}
onBlur={handleBlurName}
onChange={handleChangeName}
onFocus={handleFocusName}
placeholder={namePlaceholder ?? "name"}
autocomplete={nameAutocomplete}
autocompleteVariables={nameAutocompleteVariables}
autocompleteFunctions={nameAutocompleteFunctions}
showOptionsPicker={showOptionsPicker}
/>
<div className="w-full grid grid-cols-[minmax(0,1fr)_auto] gap-1 items-center">
{pair.isFile && readOnly ? (
// A file value to read, not to pick: no picker and nothing to drop a file on
<span
title={pair.value}
data-file-value
className="font-mono text-xs truncate px-1.5 text-text-subtle select-text cursor-text"
>
{pair.filename || pair.value || "No file"}
</span>
) : pair.isFile && SelectFile != null ? (
<SelectFile
disabled={disabled}
inline
size="xs"
filePath={pair.value}
nameOverride={pair.filename || null}
onChange={handleChangeValueFile}
/>
) : pair.value.includes("\n") ? (
<Button
color="secondary"
size="sm"
onClick={readOnly ? undefined : handleEditMultiLineValue}
title={pair.value}
className="text-xs font-mono"
>
{pair.value.split("\n").join(" ")}
</Button>
) : (
<Input
setRef={initValueInputRef}
hideLabel
stateKey={`value.${pair.id}.${stateKey}`}
wrapLines={false}
size="sm"
disabled={disabled}
readOnly={readOnly || isDraggingGlobal}
containerClassName={classNames("bg-surface", isLast && "border-dashed")}
validate={valueValidate}
forcedEnvironmentId={forcedEnvironmentId}
forceUpdateKey={forceUpdateKey}
defaultValue={pair.value}
label="Value"
name={`value[${index}]`}
onChange={handleChangeValueText}
onFocus={handleFocusValue}
type={isLast ? "text" : typeof valueType === "function" ? valueType(pair) : valueType}
placeholder={valuePlaceholder ?? "value"}
autocomplete={valueAutocomplete?.(pair.name)}
autocompleteFunctions={valueAutocompleteFunctions}
autocompleteVariables={valueAutocompleteVariablesFiltered}
showOptionsPicker={showOptionsPicker}
/>
)}
</div>
</div>
{readOnly ? null : allowFileValues ? (
<FileActionsDropdown
pair={pair}
onChangeFile={handleChangeValueFile}
onChangeText={handleChangeValueText}
onChangeContentType={handleChangeValueContentType}
onChangeFilename={handleChangeValueFilename}
onDelete={handleDelete}
editMultiLine={handleEditMultiLineValue}
/>
) : (
<Dropdown items={defaultItems}>
<IconButton
iconSize="sm"
size="xs"
// Ellipsis rather than a chevron: the name and value fields now carry chevrons for
// picking a suggestion, and this menu acts on the row instead of filling in a field.
icon={isLast || disabled ? "empty" : "ellipsis_vertical"}
title="More actions"
className="text-text-subtlest"
/>
</Dropdown>
)}
</div>
);
}
const fileItems: RadioDropdownItem<string>[] = [
{ label: "Text", value: "text" },
{ label: "File", value: "file" },
];
function FileActionsDropdown({
pair,
onChangeFile,
onChangeText,
onChangeContentType,
onChangeFilename,
onDelete,
editMultiLine,
}: {
pair: Pair;
onChangeFile: ({ filePath }: { filePath: string | null }) => void;
onChangeText: (text: string) => void;
onChangeContentType: (contentType: string) => void;
onChangeFilename: (filename: string) => void;
onDelete: () => void;
editMultiLine: () => void;
}) {
const { basename, showPrompt } = useEditorHost();
const onChange = useCallback(
(v: string) => {
if (v === "file") onChangeFile({ filePath: "" });
else onChangeText("");
},
[onChangeFile, onChangeText],
);
const itemsAfter = useMemo<DropdownItem[]>(
() => [
{
label: "Edit Multi-Line",
leftSlot: <Icon icon="file_code" />,
hidden: pair.isFile,
onSelect: editMultiLine,
},
{
label: "Set Content-Type",
leftSlot: <Icon icon="pencil" />,
onSelect: async () => {
const contentType = await showPrompt?.({
id: "content-type",
title: "Override Content-Type",
label: "Content-Type",
required: false,
placeholder: "text/plain",
defaultValue: pair.contentType ?? "",
confirmText: "Set",
description: "Leave blank to auto-detect",
});
if (contentType == null) return;
onChangeContentType(contentType);
},
},
{
label: "Set File Name",
leftSlot: <Icon icon="file_code" />,
onSelect: async () => {
console.log("PAIR", pair);
const defaultFilename = await basename(pair.value ?? "");
const filename = await showPrompt?.({
id: "filename",
title: "Override Filename",
label: "Filename",
required: false,
placeholder: defaultFilename ?? "myfile.png",
defaultValue: pair.filename,
confirmText: "Set",
description: "Leave blank to use the name of the selected file",
});
if (filename == null) return;
onChangeFilename(filename);
},
},
{
label: "Unset File",
leftSlot: <Icon icon="x" />,
hidden: pair.isFile,
onSelect: async () => {
onChangeFile({ filePath: null });
},
},
{
label: "Delete",
onSelect: onDelete,
variant: "danger",
leftSlot: <Icon icon="trash" />,
color: "danger",
},
],
[
editMultiLine,
onChangeContentType,
onChangeFile,
onDelete,
pair.contentType,
pair.isFile,
onChangeFilename,
pair.filename,
pair,
basename,
showPrompt,
],
);
return (
<RadioDropdown
value={pair.isFile ? "file" : "text"}
onChange={onChange}
items={fileItems}
itemsAfter={itemsAfter}
>
<IconButton
iconSize="sm"
size="xs"
// Matches the plain row menu, so the actions column reads the same for file and text rows
icon="ellipsis_vertical"
title="Select form data type"
className="text-text-subtlest"
/>
</RadioDropdown>
);
}
function emptyPair(): EditablePairWithId {
return ensurePairId({ enabled: true, name: "", value: "" });
}
function isPairEmpty(pair: Pair): boolean {
return !pair.name && !pair.value;
}
function MultilineEditDialog({
defaultValue,
contentType,
onChange,
hide,
}: {
defaultValue: string;
contentType: string | null;
onChange: (value: string) => void;
hide: () => void;
}) {
const { Editor } = useEditorHost();
const [value, setValue] = useState<string>(defaultValue);
const language = languageFromContentType(contentType, value);
return (
<div className="w-screen max-w-160 h-[50vh] max-h-full grid grid-rows-[minmax(0,1fr)_auto]">
<Editor
heightMode="auto"
defaultValue={defaultValue}
language={language}
onChange={setValue}
stateKey={null}
autocompleteFunctions
autocompleteVariables
/>
<div>
<Button
color="primary"
className="ml-auto my-2"
onClick={() => {
onChange(value);
hide();
}}
>
Done
</Button>
</div>
</div>
);
}
@@ -0,0 +1,10 @@
import type { Pair, PairWithId } from "./PairEditor";
import { generateId } from "@yaakapp-internal/lib/generateId";
// NOTE: Generic so callers keep whatever they passed in (eg. an EditablePair stays editable)
export function ensurePairId<T extends Pair>(p: T): T & PairWithId {
if (typeof p.id === "string") {
return p as T & PairWithId;
}
return { ...p, id: p.id ?? generateId() };
}
@@ -0,0 +1,39 @@
import classNames from "classnames";
import { useEditorHost } from "../EditorHost";
import { IconButton } from "../IconButton";
import { BulkPairEditor } from "./BulkPairEditor";
import type { PairEditorProps } from "./PairEditor";
import { PairEditor } from "./PairEditor";
interface Props extends PairEditorProps {
preferenceName: string;
forcedEnvironmentId?: string;
}
export function PairOrBulkEditor({ preferenceName, ...props }: Props) {
const { useStoredValue } = useEditorHost();
const [useBulk, setUseBulk] = useStoredValue<boolean>(
["bulk_edit", preferenceName],
false,
"global",
);
return (
<div className="relative h-full w-full group/wrapper">
{useBulk ? <BulkPairEditor {...props} /> : <PairEditor {...props} />}
<div className="absolute right-0 bottom-0">
<IconButton
size="sm"
variant="border"
title={useBulk ? "Enable form edit" : "Enable bulk edit"}
className={classNames(
"transition-opacity opacity-0 group-hover:opacity-80 hover:opacity-100! shadow",
"bg-surface hover:text group-hover/wrapper:opacity-100",
)}
onClick={() => setUseBulk((b) => !b)}
icon={useBulk ? "table" : "file_code"}
/>
</div>
</div>
);
}
+64 -8
View File
@@ -21,11 +21,15 @@ import {
useRef,
useState,
} from "react";
import { useInMemoryStoredValue, type UseStoredValue } from "../../hooks/useStoredValue";
import type { UseStoredValue } from "../../hooks/useStoredValue";
import { type UseHostStoredValue, useEditorHost } from "../EditorHost";
import { computeSideForDragMove } from "../../lib/dnd";
import type { ButtonProps } from "../Button";
import { Button } from "../Button";
import { DropMarker } from "../DropMarker";
import { Icon } from "../Icon";
import type { RadioDropdownProps } from "../RadioDropdown";
import { RadioDropdown } from "../RadioDropdown";
export interface TabButtonRenderProps {
buttonProps: Partial<ButtonProps>;
@@ -46,7 +50,16 @@ export type TabItem =
renderButton: (props: TabButtonRenderProps) => ReactNode;
leftSlot?: ReactNode;
rightSlot?: ReactNode;
};
}
| TabOptionsItem;
/** A tab whose button is also a menu of choices, labelled with the chosen one */
export interface TabOptionsItem {
value: string;
options: Omit<RadioDropdownProps, "children">;
leftSlot?: ReactNode;
rightSlot?: ReactNode;
}
export interface TabsStorage {
order: string[];
@@ -91,19 +104,23 @@ export const Tabs = forwardRef<TabsRef, TabsProps>(function Tabs(
layout = "vertical",
storageKey,
activeTabKey,
useStoredValue = useInMemoryStoredValue,
useStoredValue: useStoredValueProp,
}: TabsProps,
forwardedRef: Ref<TabsRef>,
) {
const ref = useRef<HTMLDivElement | null>(null);
const reorderable = !!storageKey;
const host = useEditorHost();
// Use key-value storage for persistence if storageKey is provided
// Use key-value storage for persistence if storageKey is provided. Without one passed in, the
// host's local storage keeps it.
// Handle migration from old format (string[]) to new format (TabsStorage)
const [rawStorage, setStorage] = useStoredValue<TabsStorage | string[]>(storageKey ?? null, {
order: [],
activeTabs: {},
});
const useStoredValue: UseHostStoredValue = useStoredValueProp ?? host.useStoredValue;
const [rawStorage, setStorage] = useStoredValue<TabsStorage | string[]>(
storageKey ?? null,
{ order: [], activeTabs: {} },
"no_sync",
);
// Migrate old format (string[]) to new format (TabsStorage)
const storage: TabsStorage = Array.isArray(rawStorage)
@@ -481,6 +498,8 @@ function TabButton({
const buttonContent =
"renderButton" in tab ? (
tab.renderButton({ buttonProps: btnProps, isActive })
) : "options" in tab ? (
<TabOptionsButton tab={tab} buttonProps={btnProps} isActive={isActive} />
) : (
<Button leftSlot={tab.leftSlot} rightSlot={tab.rightSlot} {...btnProps}>
{"label" in tab && tab.label ? tab.label : tab.value}
@@ -526,3 +545,40 @@ export const TabContent = memo(function TabContent({
);
return ErrorBoundary ? <ErrorBoundary name={`Tab ${value}`}>{content}</ErrorBoundary> : content;
});
function TabOptionsButton({
tab,
buttonProps,
isActive,
}: TabButtonRenderProps & { tab: TabOptionsItem }) {
const { options } = tab;
const option = options.items.find((i) => "value" in i && i.value === options.value);
return (
<RadioDropdown
items={options.items}
itemsAfter={options.itemsAfter}
itemsBefore={options.itemsBefore}
value={options.value}
onChange={options.onChange}
>
<Button
leftSlot={tab.leftSlot}
rightSlot={
<div className="flex items-center">
{tab.rightSlot}
<Icon
size="sm"
icon="chevron_down"
className={classNames("ml-1", isActive ? "text-text-subtle" : "text-text-subtlest")}
/>
</div>
}
{...buttonProps}
>
{option && "shortLabel" in option && option.shortLabel
? option.shortLabel
: (option?.label ?? "Unknown")}
</Button>
</RadioDropdown>
);
}
@@ -6,23 +6,51 @@ interface Props {
}
export function HttpResponseDurationTag({ response }: Props) {
return (
<HttpResponseDurationTagRaw
closed={response.state === "closed"}
startedAt={`${response.createdAt}Z`}
elapsedMs={response.elapsed}
elapsedHeadersMs={response.elapsedHeaders}
elapsedDnsMs={response.elapsedDns}
/>
);
}
interface RawProps {
/** Whether the response has ended; until then the time counts up from `startedAt` */
closed: boolean;
/** When the request started, as an ISO 8601 string with its zone */
startedAt: string;
elapsedMs: number;
elapsedHeadersMs: number;
elapsedDnsMs: number;
}
export function HttpResponseDurationTagRaw({
closed,
startedAt,
elapsedMs,
elapsedHeadersMs,
elapsedDnsMs,
}: RawProps) {
const [fallbackElapsed, setFallbackElapsed] = useState<number>(0);
const timeout = useRef<ReturnType<typeof setInterval>>(undefined);
// Calculate the duration of the response for use when the response hasn't finished yet
useEffect(() => {
clearInterval(timeout.current);
if (response.state === "closed") return;
if (closed) return;
timeout.current = setInterval(() => {
setFallbackElapsed(Date.now() - new Date(`${response.createdAt}Z`).getTime());
setFallbackElapsed(Date.now() - new Date(startedAt).getTime());
}, 100);
return () => clearInterval(timeout.current);
}, [response.createdAt, response.state]);
}, [startedAt, closed]);
const dnsValue = response.elapsedDns > 0 ? formatMillis(response.elapsedDns) : "--";
const title = `DNS: ${dnsValue}\nHEADER: ${formatMillis(response.elapsedHeaders)}\nTOTAL: ${formatMillis(response.elapsed)}`;
const dnsValue = elapsedDnsMs > 0 ? formatMillis(elapsedDnsMs) : "--";
const title = `DNS: ${dnsValue}\nHEADER: ${formatMillis(elapsedHeadersMs)}\nTOTAL: ${formatMillis(elapsedMs)}`;
const elapsed = response.state === "closed" ? response.elapsed : fallbackElapsed;
const elapsed = closed ? elapsedMs : fallbackElapsed;
return (
<span className="font-mono" title={title}>