/* ============================================================================ * Copyright (c) Palo Alto Networks * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * ========================================================================== */ import React, { useState } from "react"; import { translate } from "@docusaurus/Translate"; import FloatingButton from "@theme/ApiExplorer/FloatingButton"; import FormItem from "@theme/ApiExplorer/FormItem"; import FormSelect from "@theme/ApiExplorer/FormSelect"; import FormTextInput from "@theme/ApiExplorer/FormTextInput"; import { useTypedDispatch, useTypedSelector } from "@theme/ApiItem/hooks"; import { OPENAPI_SERVER } from "@theme/translationIds"; import { setServer, setServerVariable } from "./slice"; interface ServerProps { labelId?: string; } function Server({ labelId }: ServerProps) { const [isEditing, setIsEditing] = useState(false); const value = useTypedSelector((state: any) => state.server.value); const options = useTypedSelector((state: any) => state.server.options); const dispatch = useTypedDispatch(); if (options.length <= 0) { return null; } if (options.length < 1 && value?.variables === undefined) { return null; } if (!value) { const defaultOption = options[0]; dispatch(setServer(JSON.stringify(defaultOption))); } // Default to first option when existing server state is mismatched if (value) { const urlExists = options.find((s: any) => s.url === value.url); if (!urlExists) { const defaultOption = options[0]; dispatch(setServer(JSON.stringify(defaultOption))); } } if (!isEditing) { let url = ""; if (value) { url = value.url.replace(/\/$/, ""); if (value.variables) { Object.keys(value.variables).forEach((variable) => { url = url.replace( `{${variable}}`, value.variables?.[variable].default ?? "" ); }); } } return ( setIsEditing(true)} label={translate({ id: OPENAPI_SERVER.EDIT_BUTTON, message: "Edit" })} > {url} ); } return (
setIsEditing(false)} label={translate({ id: OPENAPI_SERVER.HIDE_BUTTON, message: "Hide" })} > s.url)} onChange={(e: React.ChangeEvent) => { dispatch( setServer( JSON.stringify( options.filter((s: any) => s.url === e.target.value)[0] ) ) ); }} value={value?.url} /> {value?.description} {value?.variables && Object.keys(value.variables).map((key) => { if (value.variables?.[key].enum !== undefined) { return ( ) => { dispatch( setServerVariable( JSON.stringify({ key, value: e.target.value }) ) ); }} value={value?.variables[key].default} /> ); } return ( ) => { dispatch( setServerVariable( JSON.stringify({ key, value: e.target.value }) ) ); }} value={value?.variables?.[key].default} /> ); })}
); } export default Server;