import type { Folder, GrpcRequest, HttpRequest, WebsocketRequest, Workspace, } from "@yaakapp-internal/models"; import { patchModel } from "@yaakapp-internal/models"; import { HStack, Icon, InlineCode } from "@yaakapp-internal/ui"; import { useCallback } from "react"; import { openFolderSettings } from "../commands/openFolderSettings"; import { openWorkspaceSettings } from "../commands/openWorkspaceSettings"; import { useHttpAuthenticationConfig } from "../hooks/useHttpAuthenticationConfig"; import { useInheritedAuthentication } from "../hooks/useInheritedAuthentication"; import { useRenderTemplate } from "../hooks/useRenderTemplate"; import { resolvedModelName } from "../lib/resolvedModelName"; import { Dropdown, type DropdownItem } from "./core/Dropdown"; import { IconButton } from "./core/IconButton"; import { Input, type InputProps } from "./core/Input"; import { Link } from "./core/Link"; import { SegmentedControl } from "./core/SegmentedControl"; import { DynamicForm } from "./DynamicForm"; import { EmptyStateText } from "./EmptyStateText"; interface Props { model: HttpRequest | GrpcRequest | WebsocketRequest | Folder | Workspace; } export function HttpAuthenticationEditor({ model }: Props) { const inheritedAuth = useInheritedAuthentication(model); const authConfig = useHttpAuthenticationConfig( model.authenticationType, model.authentication, model, ); const handleChange = useCallback( async (authentication: Record) => await patchModel(model, { authentication }), [model], ); if (model.authenticationType === "none") { return No authentication; } if (model.authenticationType != null && authConfig.data == null) { return (

Auth plugin not found for {model.authenticationType}

); } if (inheritedAuth == null) { if (model.model === "workspace" || model.model === "folder") { return (

Apply auth to all requests in {resolvedModelName(model)}

Documentation
); } return No authentication; } if (inheritedAuth.authenticationType === "none") { return No authentication; } const wasAuthInherited = inheritedAuth?.id !== model.id; if (wasAuthInherited) { const name = resolvedModelName(inheritedAuth); const cta = inheritedAuth.model === "workspace" ? "Workspace" : name; return (

Inherited from{" "}

); } return (
{ let disabled: boolean | string; if (enabled === "__TRUE__") { disabled = false; } else if (enabled === "__FALSE__") { disabled = true; } else { disabled = ""; } await handleChange({ ...model.authentication, disabled }); }} /> {authConfig.data?.actions && authConfig.data.actions.length > 0 && ( ({ label: a.label, leftSlot: a.icon ? : null, onSelect: () => a.call(model), }), )} > )} {typeof model.authentication.disabled === "string" && (
handleChange({ ...model.authentication, disabled: v })} />
)}
); } function AuthenticationDisabledInput({ value, onChange, stateKey, className, }: { value: string; onChange: InputProps["onChange"]; stateKey: string; className?: string; }) { const rendered = useRenderTemplate({ template: value, enabled: true, purpose: "preview", refreshKey: value, }); return (
{rendered.isPending ? "loading" : rendered.data ? "enabled" : "disabled"}
} autocompleteFunctions autocompleteVariables onChange={onChange} stateKey={stateKey} /> ); }