|  | 
|  | 1 | +import * as EngineCredentialsForm from "@/app/forms/engine-credentials-form"; | 
|  | 2 | +import { | 
|  | 3 | +	DialogDescription, | 
|  | 4 | +	DialogFooter, | 
|  | 5 | +	DialogHeader, | 
|  | 6 | +	DialogTitle, | 
|  | 7 | +	Flex, | 
|  | 8 | +	getConfig, | 
|  | 9 | +	ls, | 
|  | 10 | +	toast, | 
|  | 11 | +} from "@/components"; | 
|  | 12 | +import type { DialogContentProps } from "@/components/actors/hooks"; | 
|  | 13 | +import { queryClient } from "@/queries/global"; | 
|  | 14 | +import { createClient } from "@/queries/manager-engine"; | 
|  | 15 | + | 
|  | 16 | +interface ProvideEngineCredentialsDialogContentProps | 
|  | 17 | +	extends DialogContentProps {} | 
|  | 18 | + | 
|  | 19 | +export default function ProvideEngineCredentialsDialogContent({ | 
|  | 20 | +	onClose, | 
|  | 21 | +}: ProvideEngineCredentialsDialogContentProps) { | 
|  | 22 | +	return ( | 
|  | 23 | +		<EngineCredentialsForm.Form | 
|  | 24 | +			defaultValues={{ token: "" }} | 
|  | 25 | +			errors={ | 
|  | 26 | +				ls.engineCredentials.get(getConfig().apiUrl) | 
|  | 27 | +					? { token: { message: "Invalid token.", type: "manual" } } | 
|  | 28 | +					: {} | 
|  | 29 | +			} | 
|  | 30 | +			onSubmit={async (values, form) => { | 
|  | 31 | +				const client = createClient({ | 
|  | 32 | +					token: values.token, | 
|  | 33 | +				}); | 
|  | 34 | + | 
|  | 35 | +				try { | 
|  | 36 | +					await client.namespaces.list(); | 
|  | 37 | + | 
|  | 38 | +					await queryClient.invalidateQueries({ | 
|  | 39 | +						refetchType: "all", | 
|  | 40 | +					}); | 
|  | 41 | + | 
|  | 42 | +					ls.engineCredentials.set(getConfig().apiUrl, values.token); | 
|  | 43 | + | 
|  | 44 | +					toast.success( | 
|  | 45 | +						"Successfully authenticated with Rivet Engine", | 
|  | 46 | +					); | 
|  | 47 | + | 
|  | 48 | +					onClose?.(); | 
|  | 49 | +				} catch (e) { | 
|  | 50 | +					if (e && typeof e === "object" && "statusCode" in e) { | 
|  | 51 | +						if (e.statusCode === 403) { | 
|  | 52 | +							form.setError("token", { | 
|  | 53 | +								message: "Invalid token.", | 
|  | 54 | +							}); | 
|  | 55 | +							return; | 
|  | 56 | +						} | 
|  | 57 | +					} | 
|  | 58 | + | 
|  | 59 | +					form.setError("token", { | 
|  | 60 | +						message: "Failed to connect. Please try again.", | 
|  | 61 | +					}); | 
|  | 62 | +					return; | 
|  | 63 | +				} | 
|  | 64 | +			}} | 
|  | 65 | +		> | 
|  | 66 | +			<DialogHeader> | 
|  | 67 | +				<DialogTitle>Missing Rivet Engine credentials</DialogTitle> | 
|  | 68 | +				<DialogDescription> | 
|  | 69 | +					It looks like the instance of Rivet Engine that you're | 
|  | 70 | +					connected to requires additional credentials, please provide | 
|  | 71 | +					them below. | 
|  | 72 | +				</DialogDescription> | 
|  | 73 | +			</DialogHeader> | 
|  | 74 | +			<Flex gap="4" direction="col"> | 
|  | 75 | +				<EngineCredentialsForm.Token /> | 
|  | 76 | +			</Flex> | 
|  | 77 | +			<DialogFooter> | 
|  | 78 | +				<EngineCredentialsForm.Submit type="submit" allowPristine> | 
|  | 79 | +					Save | 
|  | 80 | +				</EngineCredentialsForm.Submit> | 
|  | 81 | +			</DialogFooter> | 
|  | 82 | +		</EngineCredentialsForm.Form> | 
|  | 83 | +	); | 
|  | 84 | +} | 
0 commit comments