Placeholder CM tags working

This commit is contained in:
Gregory Schier
2024-09-02 12:35:05 -07:00
parent f8b317e94b
commit 0bfafb284a
13 changed files with 163 additions and 59 deletions

View File

@@ -1,37 +1,14 @@
import type { HttpRequest } from '@yaakapp/api';
import { useMemo } from 'react';
import type { Pair } from './core/PairEditor';
import { PairOrBulkEditor } from './core/PairOrBulkEditor';
import { VStack } from './core/Stacks';
type Props = {
forceUpdateKey: string;
urlParameters: HttpRequest['headers'];
pairs: HttpRequest['headers'];
onChange: (headers: HttpRequest['urlParameters']) => void;
url: string;
};
export function UrlParametersEditor({ urlParameters, forceUpdateKey, onChange, url }: Props) {
const placeholderNames = Array.from(url.matchAll(/\/(:[^/]+)/g)).map((m) => m[1] ?? '');
const pairs = useMemo(() => {
const items: Pair[] = [...urlParameters];
for (const name of placeholderNames) {
const index = items.findIndex((p) => p.name === name);
if (index >= 0) {
items[index]!.readOnlyName = true;
} else {
items.push({
name,
value: '',
enabled: true,
readOnlyName: true,
});
}
}
return items;
}, [placeholderNames, urlParameters]);
export function UrlParametersEditor({ pairs, forceUpdateKey, onChange }: Props) {
return (
<VStack className="h-full">
<PairOrBulkEditor
@@ -42,7 +19,7 @@ export function UrlParametersEditor({ urlParameters, forceUpdateKey, onChange, u
valuePlaceholder="Value"
pairs={pairs}
onChange={onChange}
forceUpdateKey={forceUpdateKey + placeholderNames.join(':')}
forceUpdateKey={forceUpdateKey}
/>
</VStack>
);