| 
 | 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 | +					ls.engineCredentials.set(getConfig().apiUrl, values.token);  | 
 | 39 | + | 
 | 40 | +					toast.success(  | 
 | 41 | +						"Successfully authenticated with Rivet Engine",  | 
 | 42 | +					);  | 
 | 43 | + | 
 | 44 | +					await queryClient.refetchQueries();  | 
 | 45 | + | 
 | 46 | +					onClose?.();  | 
 | 47 | +				} catch (e) {  | 
 | 48 | +					if (e && typeof e === "object" && "statusCode" in e) {  | 
 | 49 | +						if (e.statusCode === 403) {  | 
 | 50 | +							form.setError("token", {  | 
 | 51 | +								message: "Invalid token.",  | 
 | 52 | +							});  | 
 | 53 | +							return;  | 
 | 54 | +						}  | 
 | 55 | +					}  | 
 | 56 | + | 
 | 57 | +					form.setError("token", {  | 
 | 58 | +						message: "Failed to connect. Please try again.",  | 
 | 59 | +					});  | 
 | 60 | +					return;  | 
 | 61 | +				}  | 
 | 62 | +			}}  | 
 | 63 | +		>  | 
 | 64 | +			<DialogHeader>  | 
 | 65 | +				<DialogTitle>Missing Rivet Engine credentials</DialogTitle>  | 
 | 66 | +				<DialogDescription>  | 
 | 67 | +					It looks like the instance of Rivet Engine that you're  | 
 | 68 | +					connected to requires additional credentials, please provide  | 
 | 69 | +					them below.  | 
 | 70 | +				</DialogDescription>  | 
 | 71 | +			</DialogHeader>  | 
 | 72 | +			<Flex gap="4" direction="col">  | 
 | 73 | +				<EngineCredentialsForm.Token />  | 
 | 74 | +			</Flex>  | 
 | 75 | +			<DialogFooter>  | 
 | 76 | +				<EngineCredentialsForm.Submit type="submit" allowPristine>  | 
 | 77 | +					Save  | 
 | 78 | +				</EngineCredentialsForm.Submit>  | 
 | 79 | +			</DialogFooter>  | 
 | 80 | +		</EngineCredentialsForm.Form>  | 
 | 81 | +	);  | 
 | 82 | +}  | 
0 commit comments