Skip to content

Commit 80d1188

Browse files
committed
Break out useExternalAuth hook
We will use this on the tasks page.
1 parent 1a00eae commit 80d1188

File tree

5 files changed

+55
-56
lines changed

5 files changed

+55
-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 {
3233
type FC,
@@ -47,10 +48,7 @@ import {
4748
useValidationSchemaForRichParameters,
4849
} from "utils/richParameters";
4950
import * as Yup from "yup";
50-
import type {
51-
CreateWorkspaceMode,
52-
ExternalAuthPollingState,
53-
} from "./CreateWorkspacePage";
51+
import type { CreateWorkspaceMode } from "./CreateWorkspacePage";
5452
import { ExperimentalFormContext } from "./ExperimentalFormContext";
5553
import { ExternalAuthButton } from "./ExternalAuthButton";
5654
import type { CreateWorkspacePermissions } from "./permissions";

site/src/pages/CreateWorkspacePage/CreateWorkspacePageViewExperimental.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from "components/Tooltip/Tooltip";
2727
import { UserAutocomplete } from "components/UserAutocomplete/UserAutocomplete";
2828
import { type FormikContextType, useFormik } from "formik";
29+
import type { ExternalAuthPollingState } from "hooks/useExternalAuth";
2930
import { ArrowLeft, CircleHelp, Undo2 } from "lucide-react";
3031
import { useSyncFormParameters } from "modules/hooks/useSyncFormParameters";
3132
import { Diagnostics } from "modules/workspaces/DynamicParameter/DynamicParameter";
@@ -48,10 +49,7 @@ import { docs } from "utils/docs";
4849
import { nameValidator } from "utils/formUtils";
4950
import type { AutofillBuildParameter } from "utils/richParameters";
5051
import * as Yup from "yup";
51-
import type {
52-
CreateWorkspaceMode,
53-
ExternalAuthPollingState,
54-
} from "./CreateWorkspacePage";
52+
import type { CreateWorkspaceMode } from "./CreateWorkspacePage";
5553
import { ExperimentalFormContext } from "./ExperimentalFormContext";
5654
import { ExternalAuthButton } from "./ExternalAuthButton";
5755
import type { CreateWorkspacePermissions } from "./permissions";

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy