/* ============================================================================
* 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 (