Skip to content

Commit 3b07068

Browse files
committed
Break out useExternalAuth hook
We will use this on the tasks page.
1 parent f1cca03 commit 3b07068

File tree

5 files changed

+54
-56
lines changed

5 files changed

+54
-56
lines changed

site/src/hooks/useExternalAuth.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { templateVersionExternalAuth } from "api/queries/templates";
2+
import { useCallback, useEffect, useState } from "react";
3+
import { useQuery } from "react-query";
4+
5+
export type ExternalAuthPollingState = "idle" | "polling" | "abandoned";
6+
7+
export const useExternalAuth = (versionId: string | undefined) => {
8+
const [externalAuthPollingState, setExternalAuthPollingState] =
9+
useState<ExternalAuthPollingState>("idle");
10+
11+
const startPollingExternalAuth = useCallback(() => {
12+
setExternalAuthPollingState("polling");
13+
}, []);
14+
15+
const { data: externalAuth, isPending: isLoadingExternalAuth } = useQuery({
16+
...templateVersionExternalAuth(versionId ?? ""),
17+
enabled: !!versionId,
18+
refetchInterval: externalAuthPollingState === "polling" ? 1000 : false,
19+
});
20+
21+
const allSignedIn = externalAuth?.every((it) => it.authenticated);
22+
23+
useEffect(() => {
24+
if (allSignedIn) {
25+
setExternalAuthPollingState("idle");
26+
return;
27+
}
28+
29+
if (externalAuthPollingState !== "polling") {
30+
return;
31+
}
32+
33+
// Poll for a maximum of one minute
34+
const quitPolling = setTimeout(
35+
() => setExternalAuthPollingState("abandoned"),
36+
60_000,
37+
);
38+
return () => {
39+
clearTimeout(quitPolling);
40+
};
41+
}, [externalAuthPollingState, allSignedIn]);
42+
43+
return {
44+
startPollingExternalAuth,
45+
externalAuth,
46+
externalAuthPollingState,
47+
isLoadingExternalAuth,
48+
};
49+
};

site/src/pages/CreateWorkspacePage/CreateWorkspacePage.tsx

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { checkAuthorization } from "api/queries/authCheck";
44
import {
55
richParameters,
66
templateByName,
7-
templateVersionExternalAuth,
87
templateVersionPresets,
98
} from "api/queries/templates";
109
import { autoCreateWorkspace, createWorkspace } from "api/queries/workspaces";
@@ -17,6 +16,7 @@ import type {
1716
import { Loader } from "components/Loader/Loader";
1817
import { useAuthenticated } from "hooks";
1918
import { useEffectEvent } from "hooks/hookPolyfills";
19+
import { useExternalAuth } from "hooks/useExternalAuth";
2020
import { useDashboard } from "modules/dashboard/useDashboard";
2121
import { generateWorkspaceName } from "modules/workspaces/generateWorkspaceName";
2222
import { type FC, useCallback, useEffect, useRef, useState } from "react";
@@ -35,8 +35,6 @@ import {
3535
const createWorkspaceModes = ["form", "auto", "duplicate"] as const;
3636
export type CreateWorkspaceMode = (typeof createWorkspaceModes)[number];
3737

38-
export type ExternalAuthPollingState = "idle" | "polling" | "abandoned";
39-
4038
const CreateWorkspacePage: FC = () => {
4139
const { organization: organizationName = "default", template: templateName } =
4240
useParams() as { organization?: string; template: string };
@@ -237,50 +235,6 @@ const CreateWorkspacePage: FC = () => {
237235
);
238236
};
239237

240-
const useExternalAuth = (versionId: string | undefined) => {
241-
const [externalAuthPollingState, setExternalAuthPollingState] =
242-
useState<ExternalAuthPollingState>("idle");
243-
244-
const startPollingExternalAuth = useCallback(() => {
245-
setExternalAuthPollingState("polling");
246-
}, []);
247-
248-
const { data: externalAuth, isPending: isLoadingExternalAuth } = useQuery({
249-
...templateVersionExternalAuth(versionId ?? ""),
250-
enabled: !!versionId,
251-
refetchInterval: externalAuthPollingState === "polling" ? 1000 : false,
252-
});
253-
254-
const allSignedIn = externalAuth?.every((it) => it.authenticated);
255-
256-
useEffect(() => {
257-
if (allSignedIn) {
258-
setExternalAuthPollingState("idle");
259-
return;
260-
}
261-
262-
if (externalAuthPollingState !== "polling") {
263-
return;
264-
}
265-
266-
// Poll for a maximum of one minute
267-
const quitPolling = setTimeout(
268-
() => setExternalAuthPollingState("abandoned"),
269-
60_000,
270-
);
271-
return () => {
272-
clearTimeout(quitPolling);
273-
};
274-
}, [externalAuthPollingState, allSignedIn]);
275-
276-
return {
277-
startPollingExternalAuth,
278-
externalAuth,
279-
externalAuthPollingState,
280-
isLoadingExternalAuth,
281-
};
282-
};
283-
284238
const getAutofillParameters = (
285239
urlSearchParams: URLSearchParams,
286240
userParameters: UserParameter[],

site/src/pages/CreateWorkspacePage/CreateWorkspacePageView.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { Stack } from "components/Stack/Stack";
2727
import { Switch } from "components/Switch/Switch";
2828
import { UserAutocomplete } from "components/UserAutocomplete/UserAutocomplete";
2929
import { type FormikContextType, useFormik } from "formik";
30+
import type { ExternalAuthPollingState } from "hooks/useExternalAuth";
3031
import { generateWorkspaceName } from "modules/workspaces/generateWorkspaceName";
3132
import { type FC, useCallback, useEffect, useMemo, useState } from "react";
3233
import {
@@ -40,10 +41,7 @@ import {
4041
useValidationSchemaForRichParameters,
4142
} from "utils/richParameters";
4243
import * as Yup from "yup";
43-
import type {
44-
CreateWorkspaceMode,
45-
ExternalAuthPollingState,
46-
} from "./CreateWorkspacePage";
44+
import type { CreateWorkspaceMode } from "./CreateWorkspacePage";
4745
import { ExternalAuthButton } from "./ExternalAuthButton";
4846
import type { CreateWorkspacePermissions } from "./permissions";
4947

site/src/pages/CreateWorkspacePage/CreateWorkspacePageViewExperimental.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ import { docs } from "utils/docs";
4747
import { nameValidator } from "utils/formUtils";
4848
import type { AutofillBuildParameter } from "utils/richParameters";
4949
import * as Yup from "yup";
50-
import type {
51-
CreateWorkspaceMode,
52-
ExternalAuthPollingState,
53-
} from "./CreateWorkspacePage";
50+
import type { CreateWorkspaceMode } from "./CreateWorkspacePage";
5451
import { ExternalAuthButton } from "./ExternalAuthButton";
5552
import type { CreateWorkspacePermissions } from "./permissions";
5653

site/src/pages/UserSettingsPage/ExternalAuthPage/ExternalAuthPageView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import { Loader } from "components/Loader/Loader";
2727
import { Spinner } from "components/Spinner/Spinner";
2828
import { Stack } from "components/Stack/Stack";
2929
import { TableEmpty } from "components/TableEmpty/TableEmpty";
30+
import type { ExternalAuthPollingState } from "hooks/useExternalAuth";
3031
import { EllipsisVertical } from "lucide-react";
31-
import type { ExternalAuthPollingState } from "pages/CreateWorkspacePage/CreateWorkspacePage";
3232
import { type FC, useCallback, useEffect, useState } from "react";
3333
import { useQuery } from "react-query";
3434

0 commit comments

Comments
 (0)