MCPcopy
hub / github.com/AndrewWalsh/openapi-devtools / Main

Function Main

src/ui/Main.tsx:16–169  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

14import { isEmpty } from "lodash";
15
16function Main() {
17 const [spec, setSpec] = useState<OpenAPIObject | null>(null);
18 const [endpoints, setEndpoints] = useState<Array<Endpoint>>([]);
19 const [endpointsByHost, setEndpointsByHost] = useState<EndpointsByHost>([]);
20 const [allHosts, setAllHosts] = useState<Set<string>>(new Set());
21 const [disabledHosts, setDisabledHosts] = useState<Set<string>>(new Set());
22 const initialStatus = isEmpty(requestStore.get())
23 ? Status.INIT
24 : Status.RECORDING;
25 const [status, setStatus] = useState(initialStatus);
26
27 const requestFinishedHandler = useCallback(
28 (harRequest: chrome.devtools.network.Request) => {
29 async function getCurrentTab() {
30 try {
31 harRequest.getContent((content) => {
32 try {
33 const contentStr = content || '';
34 const wasInserted = requestStore.insert(harRequest, contentStr);
35 if (!wasInserted) return;
36 setSpecEndpoints();
37 const host = safelyGetURLHost(harRequest.request.url);
38 if (host && !allHosts.has(host)) {
39 setAllHosts((prev) => new Set(prev).add(host));
40 }
41 } catch {
42 return;
43 }
44 });
45 } catch {
46 return;
47 }
48 }
49
50 getCurrentTab();
51 },
52 []
53 );
54
55 useEffect(() => {
56 return () => {
57 chrome.devtools.network.onRequestFinished.removeListener(
58 requestFinishedHandler
59 );
60 };
61 }, []);
62
63 const setSpecEndpoints = useCallback(async () => {
64 const nextEndpoints = requestStore.endpoints();
65 setSpec(endpointsToOAI31(nextEndpoints, requestStore.options()).getSpec());
66 setEndpoints(sortEndpoints(nextEndpoints));
67 }, []);
68
69 useEffect(() => {
70 requestStore.setDisabledHosts(disabledHosts);
71 setSpecEndpoints();
72 }, [disabledHosts]);
73

Callers

nothing calls this directly

Calls 10

sortEndpointsFunction · 0.90
getCurrentTabFunction · 0.85
endpointsToOAI31Function · 0.85
getMethod · 0.80
endpointsMethod · 0.80
setDisabledHostsMethod · 0.80
clearMethod · 0.80
parameteriseMethod · 0.80
importMethod · 0.80
hostsMethod · 0.80

Tested by

no test coverage detected