|
| 1 | +import { useCallback, useEffect, useState } from "react"; |
| 2 | +import { ResponseCodes, getPresetSessionPolicies } from "@cartridge/controller"; |
| 3 | +import { loadConfig } from "@cartridge/presets"; |
| 4 | +import { useConnection } from "@/hooks/connection"; |
| 5 | +import { |
| 6 | + parseSessionPolicies, |
| 7 | + type ParsedSessionPolicies, |
| 8 | +} from "@/hooks/session"; |
| 9 | +import { cleanupCallbacks } from "@/utils/connection/callbacks"; |
| 10 | +import { parseUpdateSessionParams } from "@/utils/connection/update-session"; |
| 11 | +import { CreateSession } from "./connect/CreateSession"; |
| 12 | +import { |
| 13 | + createVerifiedSession, |
| 14 | + requiresSessionApproval, |
| 15 | +} from "@/utils/connection/session-creation"; |
| 16 | +import { |
| 17 | + useRouteParams, |
| 18 | + useRouteCompletion, |
| 19 | + useRouteCallbacks, |
| 20 | +} from "@/hooks/route"; |
| 21 | +import { ControllerErrorAlert } from "@/components/ErrorAlert"; |
| 22 | +import { |
| 23 | + Button, |
| 24 | + HeaderInner, |
| 25 | + LayoutContent, |
| 26 | + LayoutFooter, |
| 27 | + SpinnerIcon, |
| 28 | +} from "@cartridge/ui"; |
| 29 | + |
| 30 | +const CANCEL_RESPONSE = { |
| 31 | + code: ResponseCodes.CANCELED, |
| 32 | + message: "Canceled", |
| 33 | +}; |
| 34 | + |
| 35 | +export function UpdateSessionRoute() { |
| 36 | + const { controller, origin, theme, chainId, verified } = useConnection(); |
| 37 | + const [resolvedPolicies, setResolvedPolicies] = |
| 38 | + useState<ParsedSessionPolicies>(); |
| 39 | + const [isLoading, setIsLoading] = useState(false); |
| 40 | + const [isSessionCreating, setIsSessionCreating] = useState(false); |
| 41 | + const [sessionError, setSessionError] = useState<Error>(); |
| 42 | + |
| 43 | + const params = useRouteParams((searchParams: URLSearchParams) => { |
| 44 | + return parseUpdateSessionParams(searchParams); |
| 45 | + }); |
| 46 | + |
| 47 | + const handleCompletion = useRouteCompletion(); |
| 48 | + |
| 49 | + useRouteCallbacks(params, CANCEL_RESPONSE); |
| 50 | + |
| 51 | + // Resolve policies from params (either direct policies or from preset) |
| 52 | + useEffect(() => { |
| 53 | + if (!params) return; |
| 54 | + |
| 55 | + const { policies, preset } = params.params; |
| 56 | + |
| 57 | + if (policies) { |
| 58 | + // Direct policies provided - parse them |
| 59 | + const parsed = parseSessionPolicies({ |
| 60 | + policies, |
| 61 | + verified, |
| 62 | + }); |
| 63 | + setResolvedPolicies(parsed); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if (preset && chainId) { |
| 68 | + // Resolve policies from preset |
| 69 | + setIsLoading(true); |
| 70 | + loadConfig(preset) |
| 71 | + .then((config) => { |
| 72 | + if (!config) { |
| 73 | + console.error(`Failed to load preset: ${preset}`); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + const sessionPolicies = getPresetSessionPolicies( |
| 78 | + config as Record<string, unknown>, |
| 79 | + chainId, |
| 80 | + ); |
| 81 | + if (!sessionPolicies) { |
| 82 | + console.error( |
| 83 | + `No policies found for chain ${chainId} in preset ${preset}`, |
| 84 | + ); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + const parsed = parseSessionPolicies({ |
| 89 | + policies: sessionPolicies, |
| 90 | + verified, |
| 91 | + }); |
| 92 | + setResolvedPolicies(parsed); |
| 93 | + }) |
| 94 | + .catch((error) => { |
| 95 | + console.error("Failed to resolve preset policies:", error); |
| 96 | + }) |
| 97 | + .finally(() => { |
| 98 | + setIsLoading(false); |
| 99 | + }); |
| 100 | + } |
| 101 | + }, [params, chainId, verified]); |
| 102 | + |
| 103 | + const handleConnect = useCallback(async () => { |
| 104 | + if (!params || !controller) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + params.resolve?.({ |
| 109 | + code: ResponseCodes.SUCCESS, |
| 110 | + address: controller.address(), |
| 111 | + }); |
| 112 | + if (params.params.id) { |
| 113 | + cleanupCallbacks(params.params.id); |
| 114 | + } |
| 115 | + handleCompletion(); |
| 116 | + }, [params, controller, handleCompletion]); |
| 117 | + |
| 118 | + // Auto-create session for verified policies that don't require approval |
| 119 | + useEffect(() => { |
| 120 | + if (!resolvedPolicies || !controller || !params) return; |
| 121 | + |
| 122 | + if (!requiresSessionApproval(resolvedPolicies)) { |
| 123 | + const autoCreate = async () => { |
| 124 | + try { |
| 125 | + setIsSessionCreating(true); |
| 126 | + await createVerifiedSession({ |
| 127 | + controller, |
| 128 | + origin, |
| 129 | + policies: resolvedPolicies, |
| 130 | + }); |
| 131 | + params.resolve?.({ |
| 132 | + code: ResponseCodes.SUCCESS, |
| 133 | + address: controller.address(), |
| 134 | + }); |
| 135 | + if (params.params.id) { |
| 136 | + cleanupCallbacks(params.params.id); |
| 137 | + } |
| 138 | + handleCompletion(); |
| 139 | + } catch (e) { |
| 140 | + console.error("Failed to auto-create session:", e); |
| 141 | + setSessionError(e instanceof Error ? e : new Error(String(e))); |
| 142 | + } finally { |
| 143 | + setIsSessionCreating(false); |
| 144 | + } |
| 145 | + }; |
| 146 | + |
| 147 | + void autoCreate(); |
| 148 | + } |
| 149 | + }, [resolvedPolicies, controller, params, origin, handleCompletion]); |
| 150 | + |
| 151 | + // Loading state |
| 152 | + if (!controller || isLoading) { |
| 153 | + return ( |
| 154 | + <> |
| 155 | + <HeaderInner |
| 156 | + className="pb-0" |
| 157 | + title={theme ? theme.name : "Update Session"} |
| 158 | + /> |
| 159 | + <LayoutContent className="flex items-center justify-center"> |
| 160 | + <SpinnerIcon className="animate-spin" /> |
| 161 | + </LayoutContent> |
| 162 | + </> |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + if (!resolvedPolicies) { |
| 167 | + return null; |
| 168 | + } |
| 169 | + |
| 170 | + // Verified policies auto-creating |
| 171 | + if (resolvedPolicies.verified && !requiresSessionApproval(resolvedPolicies)) { |
| 172 | + if (sessionError) { |
| 173 | + return ( |
| 174 | + <> |
| 175 | + <HeaderInner |
| 176 | + className="pb-0" |
| 177 | + title={theme ? theme.name : "Update Session"} |
| 178 | + /> |
| 179 | + <LayoutContent /> |
| 180 | + <LayoutFooter> |
| 181 | + <ControllerErrorAlert className="mb-3" error={sessionError} /> |
| 182 | + <Button |
| 183 | + className="w-full" |
| 184 | + disabled={isSessionCreating} |
| 185 | + isLoading={isSessionCreating} |
| 186 | + onClick={async () => { |
| 187 | + if (!controller) return; |
| 188 | + setIsSessionCreating(true); |
| 189 | + setSessionError(undefined); |
| 190 | + try { |
| 191 | + await createVerifiedSession({ |
| 192 | + controller, |
| 193 | + origin, |
| 194 | + policies: resolvedPolicies, |
| 195 | + }); |
| 196 | + params?.resolve?.({ |
| 197 | + code: ResponseCodes.SUCCESS, |
| 198 | + address: controller.address(), |
| 199 | + }); |
| 200 | + if (params?.params.id) { |
| 201 | + cleanupCallbacks(params.params.id); |
| 202 | + } |
| 203 | + handleCompletion(); |
| 204 | + } catch (e) { |
| 205 | + setSessionError( |
| 206 | + e instanceof Error ? e : new Error(String(e)), |
| 207 | + ); |
| 208 | + } finally { |
| 209 | + setIsSessionCreating(false); |
| 210 | + } |
| 211 | + }} |
| 212 | + > |
| 213 | + retry |
| 214 | + </Button> |
| 215 | + </LayoutFooter> |
| 216 | + </> |
| 217 | + ); |
| 218 | + } |
| 219 | + return null; |
| 220 | + } |
| 221 | + |
| 222 | + // Show CreateSession for policies that require approval |
| 223 | + return ( |
| 224 | + <CreateSession |
| 225 | + policies={resolvedPolicies} |
| 226 | + onConnect={handleConnect} |
| 227 | + isUpdate |
| 228 | + /> |
| 229 | + ); |
| 230 | +} |
0 commit comments