Skip to content

Commit 881d4a3

Browse files
committed
fix: remove unnecessary state
1 parent d126314 commit 881d4a3

File tree

1 file changed

+15
-35
lines changed

1 file changed

+15
-35
lines changed

site/src/pages/CreateWorkspacePage/CreateWorkspacePageView.tsx

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,10 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
7171
}) => {
7272
const styles = useStyles();
7373
const [owner, setOwner] = useState(defaultOwner);
74-
const { verifyExternalAuth, externalAuthErrors } =
75-
useExternalAuthVerification(externalAuth);
7674
const [searchParams] = useSearchParams();
7775
const disabledParamsList = searchParams?.get("disable_params")?.split(",");
7876

77+
const { authErrors, errorCount } = getAuthErrors(externalAuth);
7978
const form: FormikContextType<TypesGen.CreateWorkspaceRequest> =
8079
useFormik<TypesGen.CreateWorkspaceRequest>({
8180
initialValues: {
@@ -92,7 +91,7 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
9291
}),
9392
enableReinitialize: true,
9493
onSubmit: (request) => {
95-
if (!verifyExternalAuth()) {
94+
if (errorCount > 0) {
9695
form.setSubmitting(false);
9796
return;
9897
}
@@ -180,7 +179,7 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
180179
startPollingExternalAuth={startPollingExternalAuth}
181180
displayName={auth.display_name}
182181
displayIcon={auth.display_icon}
183-
error={externalAuthErrors[auth.id]}
182+
error={authErrors[auth.id]}
184183
/>
185184
))}
186185
</FormFields>
@@ -245,40 +244,21 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
245244

246245
type ExternalAuthErrors = Record<string, string>;
247246

248-
const useExternalAuthVerification = (
249-
externalAuth: TypesGen.TemplateVersionExternalAuth[],
250-
) => {
251-
const [externalAuthErrors, setExternalAuthErrors] =
252-
useState<ExternalAuthErrors>({});
247+
function getAuthErrors(
248+
externalAuth: readonly TypesGen.TemplateVersionExternalAuth[],
249+
) {
250+
const authErrors: ExternalAuthErrors = {};
251+
let errorCount = 0;
253252

254-
// Clear errors when externalAuth is refreshed
255-
useEffect(() => {
256-
setExternalAuthErrors({});
257-
}, [externalAuth]);
258-
259-
const verifyExternalAuth = () => {
260-
const errors: ExternalAuthErrors = {};
261-
262-
for (let i = 0; i < externalAuth.length; i++) {
263-
const auth = externalAuth.at(i);
264-
if (!auth) {
265-
continue;
266-
}
267-
if (!auth.authenticated) {
268-
errors[auth.id] = "You must authenticate to create a workspace!";
269-
}
253+
for (const auth of externalAuth) {
254+
if (!auth.authenticated) {
255+
authErrors[auth.id] = "You must authenticate to create a workspace!";
256+
errorCount++;
270257
}
258+
}
271259

272-
setExternalAuthErrors(errors);
273-
const isValid = Object.keys(errors).length === 0;
274-
return isValid;
275-
};
276-
277-
return {
278-
externalAuthErrors,
279-
verifyExternalAuth,
280-
};
281-
};
260+
return { authErrors, errorCount } as const;
261+
}
282262

283263
const useStyles = makeStyles((theme) => ({
284264
hasDescription: {

0 commit comments

Comments
 (0)