Hook up header editor!

This commit is contained in:
Gregory Schier
2023-03-09 13:07:13 -08:00
parent 109006db66
commit de0bd57622
9 changed files with 57 additions and 21 deletions

View File

@@ -1,24 +1,35 @@
import React, { useEffect, useState } from 'react';
import type { HttpHeader } from '../lib/models';
import { useRequestUpdate } from '../hooks/useRequest';
import type { HttpHeader, HttpRequest } from '../lib/models';
import { IconButton } from './IconButton';
import { Input } from './Input';
import { VStack } from './Stacks';
interface Props {
request: HttpRequest;
}
type PairWithId = { header: Partial<HttpHeader>; id: string };
export function HeaderEditor() {
export function HeaderEditor({ request }: Props) {
const updateRequest = useRequestUpdate(request);
const newPair = () => {
return { header: { name: '', value: '' }, id: Math.random().toString() };
};
const [pairs, setPairs] = useState<PairWithId[]>([newPair()]);
const [pairs, setPairs] = useState<PairWithId[]>(
request.headers.map((h) => ({ header: h, id: Math.random().toString() })),
);
const handleChangeHeader = (pair: PairWithId) => {
setPairs((pairs) =>
pairs.map((p) =>
setPairs((pairs) => {
const newPairs = pairs.map((p) =>
pair.id !== p.id ? p : { id: p.id, header: { ...p.header, ...pair.header } },
),
);
);
const headers = newPairs.map((p) => ({ name: '', value: '', ...p.header }));
updateRequest.mutate({ headers });
return newPairs;
});
};
useEffect(() => {

View File

@@ -54,7 +54,7 @@ export function RequestPane({ fullHeight, request, className }: Props) {
/>
</TabContent>
<TabContent value="headers" className="pl-2">
<HeaderEditor />
<HeaderEditor key={request.id} request={request} />
</TabContent>
</Tabs>
</div>

View File

@@ -0,0 +1,5 @@
.tab-content {
&[data-state="inactive"] {
@apply hidden;
}
}

View File

@@ -6,6 +6,8 @@ import { Button } from './Button';
import { ScrollArea } from './ScrollArea';
import { HStack } from './Stacks';
import './Tabs.css';
interface Props {
defaultValue?: string;
label: string;
@@ -49,9 +51,12 @@ export function TabTrigger({ value, children, active }: TabTriggerProps) {
return (
<T.Trigger value={value} asChild>
<Button
color="custom"
size="sm"
disabled={active}
className={classnames(active ? 'bg-gray-100' : '!text-gray-500 hover:!text-gray-800')}
className={classnames(
active ? 'bg-gray-100 text-gray-900' : 'text-gray-600 hover:text-gray-900',
)}
>
{children}
</Button>
@@ -67,7 +72,11 @@ interface TabContentProps {
export function TabContent({ value, children, className }: TabContentProps) {
return (
<T.Content value={value} className={classnames(className, 'w-full h-full')}>
<T.Content
forceMount
value={value}
className={classnames(className, 'tab-content', 'w-full h-full')}
>
{children}
</T.Content>
);