Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import type { EnvironmentId, ProviderInstanceId } from "@t3tools/contracts";
3
4import { useEnvironmentQuery } from "../../state/query";
5import { EMPTY_SERVER_PROVIDERS, serverEnvironment } from "../../state/server";
6import { Button } from "../ui/button";
7import { WizardFooter, WizardPanel } from "../ui/wizard";
8import { ProviderAuthenticationSection } from "./ProviderAuthenticationSection";
9import { SettingsGroup } from "./SettingsGroup";
10import { SettingsRow } from "./settingsLayout";
11
12export function ProviderWizardAuthenticationStep({
13 environmentId,
14 environmentLabel,
15 instanceId,
16 onFinish,
17}: {
18 readonly environmentId: EnvironmentId;
19 readonly environmentLabel: string;
20 readonly instanceId: ProviderInstanceId;
21 readonly onFinish: () => void;
22}) {
23 const providers =
24 useAtomValue(serverEnvironment.providersValueAtom(environmentId)) ?? EMPTY_SERVER_PROVIDERS;
25 const provider = providers.find((candidate) => candidate.instanceId === instanceId);
26 const query = useEnvironmentQuery(
27 serverEnvironment.providerAuthState({ environmentId, input: { instanceId } }),
28 );
29 const auth = query.data;
30 const active =
31 auth?.phase === "starting" || auth?.phase === "waiting" || auth?.phase === "verifying";
32 const signedIn =
33 provider?.auth.status === "authenticated" ||
34 (provider?.auth.status === "unknown" && auth?.phase === "succeeded");
35 const isDiscovering = !signedIn && !query.error && auth?.methods === undefined;
36 const canAuthenticate = (auth?.methods?.length ?? 0) > 0;
37
38 return (
39 <>
40 <WizardPanel className="min-h-72">
41 <SettingsGroup variant="plain">
42 {provider && (canAuthenticate || signedIn) ? (
43 <ProviderAuthenticationSection
44 environmentId={environmentId}
45 environmentLabel={environmentLabel}
46 instanceId={instanceId}
47 provider={{
48 ...provider,
49 setup: {
50 ...provider.setup,
51 canInstall: provider.setup?.canInstall ?? false,
52 canAuthenticate,
53 },
54 }}
55 readOnly={false}
56 />
57 ) : (
58 <SettingsRow
59 title="Account"
60 description={
61 isDiscovering
62 ? "Discovering sign-in methods…"
63 : (query.error ??
64 auth?.message ??
65 "No in-app sign-in advertised. Follow the provider's docs to finish setup.")
66 }
67 control={
68 isDiscovering ? (
69 <Button disabled size="sm" variant="secondary">
70 Sign in
71 </Button>
72 ) : provider?.setup?.documentationUrl ? (
73 <Button
74 size="sm"
75 variant="secondary"
76 render={
77 <a href={provider.setup.documentationUrl} target="_blank" rel="noreferrer" />
78 }
79 >
80 Open docs
81 </Button>
82 ) : undefined
83 }
84 />
85 )}
86 </SettingsGroup>
87 </WizardPanel>
88 <WizardFooter>
89 <Button
90 size="sm"
91 variant={signedIn ? "default" : "secondary"}
92 disabled={active}
93 onClick={onFinish}
94 >
95 {signedIn ? "Done" : "Skip for now"}
96 </Button>
97 </WizardFooter>
98 </>
99 );
100}