Skip to content

Commit 8caf862

Browse files
committed
fix: Fix sandbox empty security rendering
1 parent a205928 commit 8caf862

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

src/runtime/components/Security.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import {useAuthSessionStorage} from '../hooks/useAuthSessionStorage';
1111
import {Column, SecurityApiKey, SecurityOAuthImplicit, SecurityOAuthInline} from '.';
1212

1313
type SecurityProps = {
14-
security: OpenAPIV3.SecuritySchemeObject[];
14+
security?: OpenAPIV3.SecuritySchemeObject[];
1515
projectName: string;
1616
};
1717

1818
export const Security: React.FC<SecurityProps> = (props) => {
19-
const {security} = props;
19+
const {security = []} = props;
2020
const {
2121
isOpen,
2222
close,
@@ -31,8 +31,8 @@ export const Security: React.FC<SecurityProps> = (props) => {
3131
hasAnyAuthorization,
3232
} = useEnhance(props);
3333

34-
if (!activeSecurityTab) {
35-
throw new Error();
34+
if (!security || !security.length || !activeSecurityTab) {
35+
return null;
3636
}
3737

3838
return (
@@ -73,15 +73,18 @@ export const Security: React.FC<SecurityProps> = (props) => {
7373
};
7474

7575
function useEnhance({projectName, security}: SecurityProps) {
76+
security = security || [];
77+
78+
const initialType: V3SecurityType | undefined = security[0]?.type;
7679
const [isOpen, setOpen] = useState(false);
7780
const [auth, setAuth] = useAuthSessionStorage({
7881
projectName,
79-
initialType: security[0].type,
82+
initialType,
8083
});
8184
const hasAnyAuthorization = Boolean(auth.value);
8285
const [activeType, setActiveType] = useState(auth.type);
8386
const activeSecurityTab = useMemo(
84-
() => security.find(({type}) => activeType === type),
87+
() => security?.find(({type}) => activeType === type) || undefined,
8588
[activeType],
8689
);
8790

@@ -90,9 +93,9 @@ function useEnhance({projectName, security}: SecurityProps) {
9093
}, [setOpen]);
9194

9295
const open = useCallback(() => {
93-
setActiveType(auth.type || security[0].type);
96+
setActiveType(auth.type || initialType);
9497
setOpen(true);
95-
}, [setOpen]);
98+
}, [setOpen, initialType]);
9699

97100
return {
98101
isOpen,

src/runtime/hooks/useAuthSessionStorage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ import {useCallback, useState} from 'react';
55
import {getSelectedAuth, setAuth as setAuthFromUtils} from '../utils';
66

77
type UseAuthSessionStorageProps = {
8-
initialType: V3SecurityType;
8+
initialType: V3SecurityType | undefined;
99
projectName: string;
1010
};
1111

1212
type UseAuthSessionStorageResult = [
13-
{type: V3SecurityType; value: string},
13+
{type: V3SecurityType | undefined; value: string},
1414
(params: {type: V3SecurityType; value: string}) => void,
1515
];
1616

1717
export const useAuthSessionStorage = (
1818
props: UseAuthSessionStorageProps,
1919
): UseAuthSessionStorageResult => {
2020
const {projectName} = props;
21-
const [state, setState] = useState<{type: V3SecurityType; value: string}>(
21+
const [state, setState] = useState<{type: V3SecurityType | undefined; value: string}>(
2222
createGetInitialAuthFunction(props),
2323
);
2424

src/runtime/sandbox.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ export const Sandbox: React.FC<SandboxProps> = (props) => {
4848
return (
4949
<form onSubmit={onSubmit} className={yfmSandbox()}>
5050
<Column>
51-
{props.security?.length && (
52-
<Security security={props.security} projectName={props.projectName} />
53-
)}
51+
<Security security={props.security} projectName={props.projectName} />
5452
<Params
5553
ref={refs.path}
5654
title={Text.PATH_PARAMS_SECTION_TITLE}

0 commit comments

Comments
 (0)