import type { Folder, GrpcRequest, HttpRequest, WebsocketRequest, Workspace, } from '@yaakapp-internal/models'; import { patchModel } from '@yaakapp-internal/models'; import React, { useCallback } from 'react'; import { openFolderSettings } from '../commands/openFolderSettings'; import { openWorkspaceSettings } from '../commands/openWorkspaceSettings'; import { useHttpAuthenticationConfig } from '../hooks/useHttpAuthenticationConfig'; import { useInheritedAuthentication } from '../hooks/useInheritedAuthentication'; import { resolvedModelName } from '../lib/resolvedModelName'; import { Checkbox } from './core/Checkbox'; import type { DropdownItem } from './core/Dropdown'; import { Dropdown } from './core/Dropdown'; import { Icon } from './core/Icon'; import { IconButton } from './core/IconButton'; import { InlineCode } from './core/InlineCode'; import { Link } from './core/Link'; import { HStack } from './core/Stacks'; 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
); } else { 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 (
handleChange({ ...model.authentication, disabled: !disabled })} title="Enabled" /> {authConfig.data?.actions && authConfig.data.actions.length > 0 && ( ({ label: a.label, leftSlot: a.icon ? : null, onSelect: () => a.call(model), }), )} > )}
); }