import classnames from 'classnames'; import type { CSSProperties } from 'react'; import { memo, useCallback, useMemo, useState } from 'react'; import { useActiveRequest } from '../hooks/useActiveRequest'; import { useKeyValue } from '../hooks/useKeyValue'; import { useUpdateRequest } from '../hooks/useUpdateRequest'; import { useWindowFocus } from '../hooks/useWindowFocus'; import { tryFormatJson } from '../lib/formatters'; import type { HttpHeader, HttpRequest } from '../lib/models'; import { AUTH_TYPE_BASIC, AUTH_TYPE_BEARER, AUTH_TYPE_NONE, BODY_TYPE_GRAPHQL, BODY_TYPE_JSON, BODY_TYPE_NONE, BODY_TYPE_XML, } from '../lib/models'; import { BasicAuth } from './BasicAuth'; import { BearerAuth } from './BearerAuth'; import { Editor } from './core/Editor'; import type { TabItem } from './core/Tabs/Tabs'; import { TabContent, Tabs } from './core/Tabs/Tabs'; import { EmptyStateText } from './EmptyStateText'; import { GraphQLEditor } from './GraphQLEditor'; import { HeaderEditor } from './HeaderEditor'; import { ParametersEditor } from './ParameterEditor'; import { UrlBar } from './UrlBar'; interface Props { style?: CSSProperties; fullHeight: boolean; className?: string; } export const RequestPane = memo(function RequestPane({ style, fullHeight, className }: Props) { const activeRequest = useActiveRequest(); const activeRequestId = activeRequest?.id ?? null; const updateRequest = useUpdateRequest(activeRequestId); const [forceUpdateHeaderEditorKey, setForceUpdateHeaderEditorKey] = useState(0); const activeTab = useKeyValue({ key: ['active_request_body_tab'], defaultValue: 'body', }); const tabs: TabItem[] = useMemo( () => activeRequest === null ? [] : [ { value: 'body', options: { value: activeRequest.bodyType, items: [ { label: 'JSON', value: BODY_TYPE_JSON }, { label: 'XML', value: BODY_TYPE_XML }, { label: 'GraphQL', value: BODY_TYPE_GRAPHQL }, { type: 'separator' }, { label: 'No Body', shortLabel: 'Body', value: BODY_TYPE_NONE }, ], onChange: async (bodyType) => { const patch: Partial = { bodyType }; if (bodyType == BODY_TYPE_GRAPHQL) { patch.method = 'POST'; patch.headers = [ ...(activeRequest?.headers.filter( (h) => h.name.toLowerCase() !== 'content-type', ) ?? []), { name: 'Content-Type', value: 'application/json', enabled: true, }, ]; setTimeout(() => { setForceUpdateHeaderEditorKey((u) => u + 1); }, 100); } await updateRequest.mutate(patch); }, }, }, { value: 'auth', label: 'Auth', options: { value: activeRequest.authenticationType, items: [ { label: 'Basic Auth', shortLabel: 'Basic', value: AUTH_TYPE_BASIC }, { label: 'Bearer Token', shortLabel: 'Bearer', value: AUTH_TYPE_BEARER }, { type: 'separator' }, { label: 'No Authentication', shortLabel: 'Auth', value: AUTH_TYPE_NONE }, ], onChange: async (authenticationType) => { let authentication: HttpRequest['authentication'] = activeRequest?.authentication; if (authenticationType === AUTH_TYPE_BASIC) { authentication = { username: authentication.username ?? '', password: authentication.password ?? '', }; } else if (authenticationType === AUTH_TYPE_BEARER) { authentication = { token: authentication.token ?? '', }; } await updateRequest.mutate({ authenticationType, authentication }); }, }, }, // { value: 'params', label: 'URL Params' }, { value: 'headers', label: 'Headers' }, ], [ activeRequest?.bodyType, activeRequest?.headers, activeRequest?.authenticationType, activeRequest?.authentication, ], ); const handleBodyChange = useCallback((body: string) => updateRequest.mutate({ body }), []); const handleHeadersChange = useCallback( (headers: HttpHeader[]) => updateRequest.mutate({ headers }), [], ); const visible = useWindowFocus(); const multiWindowKey = useMemo(() => { // If the window has focus, don't ever force an update if (visible) return undefined; // If the window is not focused, force an update if the request has been updated return activeRequest?.updatedAt; }, [visible, activeRequest?.updatedAt]); return (
{activeRequest && ( <> {activeRequest.authenticationType === AUTH_TYPE_BASIC ? ( ) : activeRequest.authenticationType === AUTH_TYPE_BEARER ? ( ) : ( No Authentication {activeRequest.authenticationType} )} null} /> {activeRequest.bodyType === BODY_TYPE_JSON ? ( tryFormatJson(v)} /> ) : activeRequest.bodyType === BODY_TYPE_XML ? ( ) : activeRequest.bodyType === BODY_TYPE_GRAPHQL ? ( ) : ( No Body )} )}
); });