Header editor to pair editor

This commit is contained in:
Gregory Schier
2023-03-15 08:09:45 -07:00
parent e2e25dc30b
commit 321941baab
8 changed files with 66 additions and 47 deletions

View File

@@ -5,7 +5,7 @@ import { useUpdateRequest } from '../hooks/useUpdateRequest';
import { Editor } from './core/Editor'; import { Editor } from './core/Editor';
import { TabContent, Tabs } from './core/Tabs/Tabs'; import { TabContent, Tabs } from './core/Tabs/Tabs';
import { GraphQLEditor } from './editors/GraphQLEditor'; import { GraphQLEditor } from './editors/GraphQLEditor';
import { HeaderEditor } from './HeaderEditor'; import { PairEditor } from './core/PairEditor';
import { UrlBar } from './UrlBar'; import { UrlBar } from './UrlBar';
interface Props { interface Props {
@@ -55,7 +55,11 @@ export function RequestPane({ fullHeight, className }: Props) {
label="Request body" label="Request body"
> >
<TabContent value="headers" className="pl-2"> <TabContent value="headers" className="pl-2">
<HeaderEditor key={activeRequest.id} request={activeRequest} /> <PairEditor
key={activeRequest.id}
pairs={activeRequest.headers}
onChange={(headers) => updateRequest.mutate({ headers })}
/>
</TabContent> </TabContent>
<TabContent value="body"> <TabContent value="body">
{activeRequest.bodyType === 'json' ? ( {activeRequest.bodyType === 'json' ? (

View File

@@ -71,6 +71,7 @@ export const ResponsePane = memo(function ResponsePane({ className }: Props) {
<HStack alignItems="center" className="ml-auto h-8"> <HStack alignItems="center" className="ml-auto h-8">
<IconButton <IconButton
title={viewMode === 'pretty' ? 'View Raw' : 'View Prettified'}
icon={viewMode === 'pretty' ? 'eye' : 'code'} icon={viewMode === 'pretty' ? 'eye' : 'code'}
size="sm" size="sm"
className="ml-1" className="ml-1"
@@ -97,7 +98,12 @@ export const ResponsePane = memo(function ResponsePane({ className }: Props) {
]} ]}
> >
<DropdownMenuTrigger> <DropdownMenuTrigger>
<IconButton icon="clock" className="ml-auto" size="sm" /> <IconButton
title="Show response history"
icon="clock"
className="ml-auto"
size="sm"
/>
</DropdownMenuTrigger> </DropdownMenuTrigger>
</Dropdown> </Dropdown>
</HStack> </HStack>

View File

@@ -31,6 +31,7 @@ export function Sidebar({ className }: Props) {
> >
<HStack as={WindowDragRegion} alignItems="center" justifyContent="end"> <HStack as={WindowDragRegion} alignItems="center" justifyContent="end">
<IconButton <IconButton
title="Add Request"
className="mx-1" className="mx-1"
icon="plusCircle" icon="plusCircle"
onClick={async () => { onClick={async () => {
@@ -49,8 +50,12 @@ export function Sidebar({ className }: Props) {
alignItems="center" alignItems="center"
justifyContent="end" justifyContent="end"
> >
<IconButton icon="trash" onClick={() => deleteRequest.mutate()} /> <IconButton title="Delete request" icon="trash" onClick={() => deleteRequest.mutate()} />
<IconButton icon={appearance === 'dark' ? 'moon' : 'sun'} onClick={toggleAppearance} /> <IconButton
title={appearance === 'dark' ? 'Enable light mode' : 'Enable dark mode'}
icon={appearance === 'dark' ? 'moon' : 'sun'}
onClick={toggleAppearance}
/>
</HStack> </HStack>
</VStack> </VStack>
</div> </div>

View File

@@ -54,13 +54,13 @@ export function UrlBar({ sendRequest, loading, onMethodChange, method, onUrlChan
} }
rightSlot={ rightSlot={
<IconButton <IconButton
title="Send Request"
type="submit" type="submit"
className="mr-0.5" className="mr-0.5"
size="sm" size="sm"
icon={loading ? 'update' : 'paperPlane'} icon={loading ? 'update' : 'paperPlane'}
spin={loading} spin={loading}
disabled={loading} disabled={loading}
title="Send Request"
/> />
} }
/> />

View File

@@ -22,6 +22,7 @@ export type ButtonProps = HTMLAttributes<HTMLElement> & {
type?: 'button' | 'submit'; type?: 'button' | 'submit';
forDropdown?: boolean; forDropdown?: boolean;
disabled?: boolean; disabled?: boolean;
title?: string;
}; };
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any

View File

@@ -40,7 +40,7 @@ export function Dialog({
)} )}
> >
<D.Close asChild className="ml-auto absolute right-1 top-1"> <D.Close asChild className="ml-auto absolute right-1 top-1">
<IconButton aria-label="Close" icon="x" size="sm" /> <IconButton title="Close dialog" aria-label="Close" icon="x" size="sm" />
</D.Close> </D.Close>
<VStack space={3}> <VStack space={3}>
<HStack alignItems="center" className="pb-3"> <HStack alignItems="center" className="pb-3">

View File

@@ -5,7 +5,8 @@ import { Button } from './Button';
import type { IconProps } from './Icon'; import type { IconProps } from './Icon';
import { Icon } from './Icon'; import { Icon } from './Icon';
type Props = IconProps & ButtonProps & { iconClassName?: string; iconSize?: IconProps['size'] }; type Props = IconProps &
ButtonProps & { iconClassName?: string; iconSize?: IconProps['size']; title: string };
export const IconButton = forwardRef<HTMLButtonElement, Props>(function IconButton( export const IconButton = forwardRef<HTMLButtonElement, Props>(function IconButton(
{ icon, spin, className, iconClassName, size = 'md', iconSize, ...props }: Props, { icon, spin, className, iconClassName, size = 'md', iconSize, ...props }: Props,

View File

@@ -1,57 +1,57 @@
import classnames from 'classnames'; import classnames from 'classnames';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { useUpdateRequest } from '../hooks/useUpdateRequest'; import { IconButton } from './IconButton';
import type { HttpHeader, HttpRequest } from '../lib/models'; import { Input } from './Input';
import { IconButton } from './core/IconButton'; import { VStack } from './Stacks';
import { Input } from './core/Input';
import { VStack } from './core/Stacks';
interface Props { interface Props {
request: HttpRequest; pairs: Pair[];
onChange: (pairs: Pair[]) => void;
className?: string; className?: string;
} }
type PairWithId = { header: Partial<HttpHeader>; id: string }; interface Pair {
name: string;
value: string;
}
export function HeaderEditor({ request, className }: Props) { interface PairContainer {
const updateRequest = useUpdateRequest(request); pair: Pair;
id: string;
}
const newPair = () => { export function PairEditor({ pairs: originalPairs, className, onChange }: Props) {
return { header: { name: '', value: '' }, id: Math.random().toString() }; const newPairContainer = (): PairContainer => {
return { pair: { name: '', value: '' }, id: Math.random().toString() };
}; };
const [pairs, setPairs] = useState<PairWithId[]>(() => { const [pairs, setPairs] = useState<PairContainer[]>(() => {
// Remove empty headers on initial render // Remove empty headers on initial render
const nonEmpty = request.headers.filter((h) => !(h.name === '' && h.value === '')); const nonEmpty = originalPairs.filter((h) => !(h.name === '' && h.value === ''));
const pairs = nonEmpty.map((h) => ({ header: h, id: Math.random().toString() })); const pairs = nonEmpty.map((h) => ({ pair: h, id: Math.random().toString() }));
return [...pairs, newPair()]; return [...pairs, newPairContainer()];
}); });
const setPairsAndSave = (fn: (pairs: PairWithId[]) => PairWithId[]) => { const setPairsAndSave = (fn: (pairs: PairContainer[]) => PairContainer[]) => {
setPairs((oldPairs) => { setPairs((oldPairs) => {
const newPairs = fn(oldPairs); const pairs = fn(oldPairs).map((p) => p.pair);
const headers = newPairs.map((p) => ({ name: '', value: '', ...p.header })); onChange(pairs);
updateRequest.mutate({ headers }); return fn(oldPairs);
return newPairs;
}); });
}; };
const handleChangeHeader = (pair: PairWithId) => { const handleChangeHeader = (pair: PairContainer) => {
setPairsAndSave((pairs) => setPairsAndSave((pairs) => pairs.map((p) => (pair.id !== p.id ? p : pair)));
pairs.map((p) =>
pair.id !== p.id ? p : { id: p.id, header: { ...p.header, ...pair.header } },
),
);
}; };
// Ensure there's always at least one pair // Ensure there's always at least one pair
useEffect(() => { useEffect(() => {
if (pairs.length === 0) { if (pairs.length === 0) {
setPairs((pairs) => [...pairs, newPair()]); setPairs((pairs) => [...pairs, newPairContainer()]);
} }
}, [pairs]); }, [pairs]);
const handleDelete = (pair: PairWithId) => { const handleDelete = (pair: PairContainer) => {
setPairsAndSave((oldPairs) => oldPairs.filter((p) => p.id !== pair.id)); setPairsAndSave((oldPairs) => oldPairs.filter((p) => p.id !== pair.id));
}; };
@@ -63,12 +63,12 @@ export function HeaderEditor({ request, className }: Props) {
return ( return (
<FormRow <FormRow
key={p.id} key={p.id}
pair={p} pairContainer={p}
isLast={isLast} isLast={isLast}
onChange={handleChangeHeader} onChange={handleChangeHeader}
onFocus={() => { onFocus={() => {
if (isLast) { if (isLast) {
setPairs((pairs) => [...pairs, newPair()]); setPairs((pairs) => [...pairs, newPairContainer()]);
} }
}} }}
onDelete={isLast ? undefined : handleDelete} onDelete={isLast ? undefined : handleDelete}
@@ -81,27 +81,28 @@ export function HeaderEditor({ request, className }: Props) {
} }
function FormRow({ function FormRow({
pair, pairContainer,
onChange, onChange,
onDelete, onDelete,
onFocus, onFocus,
isLast, isLast,
}: { }: {
pair: PairWithId; pairContainer: PairContainer;
onChange: (pair: PairWithId) => void; onChange: (pair: PairContainer) => void;
onDelete?: (pair: PairWithId) => void; onDelete?: (pair: PairContainer) => void;
onFocus?: () => void; onFocus?: () => void;
isLast?: boolean; isLast?: boolean;
}) { }) {
const { id } = pairContainer;
return ( return (
<div className="group grid grid-cols-[1fr_1fr_2.5rem] grid-rows-1 gap-2 items-center"> <div className="group grid grid-cols-[1fr_1fr_2.5rem] grid-rows-1 gap-2 items-center">
<Input <Input
hideLabel hideLabel
containerClassName={classnames(isLast && 'border-dashed')} containerClassName={classnames(isLast && 'border-dashed')}
defaultValue={pair.header.name} defaultValue={pairContainer.pair.name}
label="Name" label="Name"
name="name" name="name"
onChange={(name) => onChange({ id: pair.id, header: { name } })} onChange={(name) => onChange({ id, pair: { name, value: pairContainer.pair.value } })}
onFocus={onFocus} onFocus={onFocus}
placeholder={isLast ? 'new name' : 'name'} placeholder={isLast ? 'new name' : 'name'}
useEditor={{ useTemplating: true }} useEditor={{ useTemplating: true }}
@@ -109,10 +110,10 @@ function FormRow({
<Input <Input
hideLabel hideLabel
containerClassName={classnames(isLast && 'border-dashed')} containerClassName={classnames(isLast && 'border-dashed')}
defaultValue={pair.header.value} defaultValue={pairContainer.pair.value}
label="Value" label="Value"
name="value" name="value"
onChange={(value) => onChange({ id: pair.id, header: { value } })} onChange={(value) => onChange({ id, pair: { name: pairContainer.pair.name, value } })}
onFocus={onFocus} onFocus={onFocus}
placeholder={isLast ? 'new value' : 'value'} placeholder={isLast ? 'new value' : 'value'}
useEditor={{ useTemplating: true }} useEditor={{ useTemplating: true }}
@@ -120,7 +121,8 @@ function FormRow({
{onDelete && ( {onDelete && (
<IconButton <IconButton
icon="trash" icon="trash"
onClick={() => onDelete(pair)} title="Delete header"
onClick={() => onDelete(pairContainer)}
tabIndex={-1} tabIndex={-1}
className={classnames('opacity-0 group-hover:opacity-100')} className={classnames('opacity-0 group-hover:opacity-100')}
/> />