Skip to content

Commit 8d6c2a7

Browse files
authored
Updates to filter and select/deselect URLs in HAR to Yaml (#308)
1 parent e92c55c commit 8d6c2a7

1 file changed

Lines changed: 124 additions & 3 deletions

File tree

  • guide/results-viewer-react/src/components/YamlWriterUpload

guide/results-viewer-react/src/components/YamlWriterUpload/index.tsx

Lines changed: 124 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
AccordionItemHeading,
66
AccordionItemPanel
77
} from "react-accessible-accordion";
8-
import { Button, Span } from "../YamlStyles";
8+
import { Button, Input, Span } from "../YamlStyles";
99
import { Har, HarEndpoint } from "../../util/yamlwriter";
1010
import { LogLevel, log } from "../../util/log";
1111
import { Modal, ModalObject, useEffectModal } from "../Modal";
@@ -169,6 +169,7 @@ export const YamlWriterUpload = (props: YamlWriterUploadProps) => {
169169
const defaultFilename: string = "";
170170
const [filename, setFilename] = useState(defaultFilename);
171171
const [state, setState] = useState(defaultState);
172+
const [urlFilter, setUrlFilter] = useState("");
172173
const fileModalRef = useRef<ModalObject| null>(null);
173174
useEffectModal(fileModalRef);
174175
const endpointModalRef = useRef<ModalObject| null>(null);
@@ -398,6 +399,71 @@ export const YamlWriterUpload = (props: YamlWriterUploadProps) => {
398399
});
399400
};
400401

402+
const handleSelectUrls = (selected?: "yes" | "no") => {
403+
const outputName = state.output[filename];
404+
if (!outputName) { return; }
405+
406+
const updatedUrls = { ...outputName.urls };
407+
const updatedEndpoints = [...outputName.endpoints];
408+
409+
Object.keys(updatedUrls).forEach((key) => {
410+
if (selected) {
411+
updatedUrls[key]!.selected = selected;
412+
updatedUrls[key]!.index.forEach((url) => {
413+
const endpoint = updatedEndpoints.find((ep) => ep.id === url.id);
414+
if (endpoint) { endpoint.selected = selected; }
415+
});
416+
} else if (urlFilter) {
417+
const selectedByFilter = key.includes(urlFilter) ? "yes" : "no";
418+
updatedUrls[key]!.selected = selectedByFilter;
419+
updatedUrls[key]!.index.forEach((url) => {
420+
const endpoint = updatedEndpoints.find((ep) => ep.id === url.id);
421+
if (endpoint) { endpoint.selected = selectedByFilter; }
422+
});
423+
}
424+
});
425+
426+
setState(({ output, ...prevState }: YamlWriterUploadState) => ({
427+
...prevState,
428+
output: {
429+
...output,
430+
[filename]: {
431+
...outputName,
432+
urls: updatedUrls,
433+
endpoints: updatedEndpoints
434+
}
435+
}
436+
}));
437+
};
438+
439+
const handleSelectAllHeaders = (selected: "yes" | "no") => {
440+
const outputName = state.output[filename];
441+
if (!outputName) {return;}
442+
443+
const updatedTypes = { ...outputName.types };
444+
const updatedEndpoints = [...outputName.endpoints];
445+
446+
Object.keys(updatedTypes).forEach((key) => {
447+
updatedTypes[key]!.selected = selected;
448+
updatedTypes[key]!.index.forEach((type) => {
449+
const endpoint = updatedEndpoints.find((ep) => ep.id === type.id);
450+
if (endpoint) {endpoint.selected = selected;}
451+
});
452+
});
453+
454+
setState(({ output, ...prevState }: YamlWriterUploadState) => ({
455+
...prevState,
456+
output: {
457+
...output,
458+
[filename]: {
459+
...outputName,
460+
types: updatedTypes,
461+
endpoints: updatedEndpoints
462+
}
463+
}
464+
}));
465+
};
466+
401467
return (
402468
<HeaderMain>
403469
<h2> Create from Har File </h2>
@@ -447,7 +513,40 @@ export const YamlWriterUpload = (props: YamlWriterUploadProps) => {
447513
<AccordianStyleDiv style={{paddingRight: "20px"}}>
448514
<div>
449515
<Span>
450-
<h2>Urls</h2>&nbsp;&nbsp;&nbsp;
516+
<h2>Urls</h2>
517+
<div style={{ display: "flex", alignItems: "center", marginLeft: "10px" }}>
518+
<Input
519+
type="text"
520+
placeholder="Filter URLs"
521+
value={urlFilter}
522+
onChange={(e) => setUrlFilter(e.target.value)}
523+
/>
524+
<Button
525+
onClick={() => handleSelectUrls()}
526+
style={{ marginLeft: "0"}}
527+
disabled={!urlFilter.length}
528+
>
529+
Filter
530+
</Button>
531+
<Button
532+
onClick={() => handleSelectUrls("yes")}
533+
disabled={
534+
state.output[filename]?.urls &&
535+
Object.values(state.output[filename]!.urls).every((url) => url?.selected === "yes")
536+
}
537+
>
538+
Select All
539+
</Button>
540+
<Button
541+
onClick={() => handleSelectUrls("no")}
542+
disabled={
543+
state.output[filename]?.urls &&
544+
Object.values(state.output[filename]!.urls).every((url) => url?.selected === "no")
545+
}
546+
>
547+
Deselect All
548+
</Button>
549+
</div>
451550
</Span>
452551
<Accordion allowMultipleExpanded={true} allowZeroExpanded={true}>
453552
{state.output[filename]?.urls && Object.keys(state.output[filename]!.urls).map((key, index) => {
@@ -496,7 +595,29 @@ export const YamlWriterUpload = (props: YamlWriterUploadProps) => {
496595
</Accordion>
497596
</div>
498597
<div style={{marginTop: "25px"}}>
499-
<h2>Return Types</h2>
598+
<Span>
599+
<h2>Return Types</h2>
600+
<div style={{ display: "flex", alignItems: "center", marginLeft: "10px" }}>
601+
<Button
602+
onClick={() => handleSelectAllHeaders("yes")}
603+
disabled={
604+
state.output[filename]?.types &&
605+
Object.values(state.output[filename]!.types).every((type) => type?.selected === "yes")
606+
}
607+
>
608+
Select All
609+
</Button>
610+
<Button
611+
onClick={() => handleSelectAllHeaders("no")}
612+
disabled={
613+
state.output[filename]?.types &&
614+
Object.values(state.output[filename]!.types).every((type) => type?.selected === "no")
615+
}
616+
>
617+
Deselect All
618+
</Button>
619+
</div>
620+
</Span>
500621
<Accordion allowMultipleExpanded={true} allowZeroExpanded={true}>
501622
{state.output[filename]?.types && Object.keys(state.output[filename]!.types).map((key, index) => {
502623
const type = state.output[filename]!.types[key];

0 commit comments

Comments
 (0)