Skip to content

Commit

Permalink
Use connection provider instead of context
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Jun 6, 2024
1 parent 0c39977 commit 8ff9348
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 49 deletions.
14 changes: 1 addition & 13 deletions packages/keychain/src/components/connect/CreateController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,32 @@ import { useState } from "react";
import { Signup } from "./Signup";
import { Login } from "./Login";
import { useConnection } from "hooks/connection";
import { ConnectCtx } from "utils/connection";

export function CreateController({ isSlot }: { isSlot?: boolean }) {
const { chainId, rpcUrl, context, setController, error } = useConnection();
const { error } = useConnection();
const [showSignup, setShowSignup] = useState(false);
const [prefilledUsername, setPrefilledUsername] = useState<string>();
const ctx = context as ConnectCtx;

if (error) {
return <>{error.message}</>;
}

return showSignup ? (
<Signup
origin={ctx?.origin}
policies={ctx?.policies}
chainId={chainId}
rpcUrl={rpcUrl}
prefilledName={prefilledUsername}
onLogin={(username) => {
setPrefilledUsername(username);
setShowSignup(false);
}}
onSuccess={setController}
isSlot={isSlot}
/>
) : (
<Login
origin={ctx?.origin}
policies={ctx?.policies}
chainId={chainId}
rpcUrl={rpcUrl}
prefilledName={prefilledUsername}
onSignup={(username) => {
setPrefilledUsername(username);
setShowSignup(true);
}}
onSuccess={setController}
isSlot={isSlot}
/>
);
Expand Down
14 changes: 8 additions & 6 deletions packages/keychain/src/components/connect/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ import { RegistrationLink } from "./RegistrationLink";
import { useControllerTheme } from "hooks/theme";
import { doLogin } from "hooks/account";
import { Error as ErrorComp } from "components/Error";
import { useConnection } from "hooks/connection";

export function Login({
chainId,
rpcUrl,
origin,
policies,
prefilledName = "",
isSlot,
onSuccess,
onSignup,
}: LoginProps) {
const { origin, policies, chainId, rpcUrl, setController } = useConnection();
const { event: log } = useAnalytics();
const theme = useControllerTheme();
const [isLoading, setIsLoading] = useState(false);
Expand Down Expand Up @@ -63,7 +61,11 @@ export function Login({
}

controller.store();
onSuccess(controller);
setController(controller);

if (onSuccess) {
onSuccess();
}

log({ type: "webauthn_login", address });
} catch (e) {
Expand All @@ -80,7 +82,7 @@ export function Login({

setIsLoading(false);
},
[chainId, rpcUrl, origin, policies, expiresAt, isSlot, log, onSuccess],
[chainId, rpcUrl, origin, policies, expiresAt, isSlot, log],
);

return (
Expand Down
22 changes: 8 additions & 14 deletions packages/keychain/src/components/connect/Signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ import { doSignup } from "hooks/account";
import { useControllerTheme } from "hooks/theme";
import { Error as ErrorComp } from "components/Error";
import { shortString } from "starknet";
import { useConnection } from "hooks/connection";

export function Signup({
prefilledName = "",
origin,
policies,
isSlot,
chainId,
rpcUrl,
onSuccess,
onLogin,
}: SignupProps) {
Expand Down Expand Up @@ -78,15 +75,11 @@ export function Signup({
validateOnBlur={false}
>
<Form
chainId={chainId}
rpcUrl={rpcUrl}
onLogin={onLogin}
onSuccess={onSuccess}
isRegistering={isRegistering}
isLoading={isLoading}
onSuccess={onSuccess}
setIsRegistering={setIsRegistering}
origin={origin}
policies={policies}
isSlot={isSlot}
error={error}
/>
Expand All @@ -97,10 +90,6 @@ export function Signup({
}

function Form({
origin,
policies,
chainId,
rpcUrl,
isRegistering,
isLoading,
isSlot,
Expand All @@ -114,6 +103,7 @@ function Form({
setIsRegistering: (val: boolean) => void;
error: Error;
}) {
const { origin, policies, chainId, rpcUrl, setController } = useConnection();
const theme = useControllerTheme();
const { values, isValidating } = useFormikContext<FormValues>();

Expand Down Expand Up @@ -160,7 +150,11 @@ function Form({
controller.store();
await controller.account.sync();

onSuccess(controller);
setController(controller);

if (onSuccess) {
onSuccess();
}
},
},
);
Expand Down
9 changes: 1 addition & 8 deletions packages/keychain/src/components/connect/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import Controller from "utils/controller";
import { Policy } from "@cartridge/controller";

export type FormValues = {
username: string;
};
Expand All @@ -9,12 +6,8 @@ export type AuthProps = SignupProps | LoginProps;

type AuthBaseProps = {
prefilledName?: string;
origin?: string;
policies?: Policy[];
isSlot?: boolean;
chainId: string;
rpcUrl: string;
onSuccess: (controller: Controller) => void;
onSuccess?: () => void;
};

export type SignupProps = AuthBaseProps & {
Expand Down
8 changes: 6 additions & 2 deletions packages/keychain/src/hooks/connection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,23 @@ export function ConnectionProvider({ children }: PropsWithChildren) {
const parsePolicies = (policiesStr: string | null): Policy[] => {
if (!policiesStr) return [];
return JSON.parse(policiesStr);
}
};

useEffect(() => {
if (!isIframe()) {
const urlParams = new URLSearchParams(window.location.search);
setOrigin(urlParams.get("origin") || process.env.NEXT_PUBLIC_ORIGIN);
setRpcUrl(urlParams.get("rpc_url") || process.env.NEXT_PUBLIC_RPC_SEPOLIA);
setRpcUrl(
urlParams.get("rpc_url") || process.env.NEXT_PUBLIC_RPC_SEPOLIA,
);
setPolicies(parsePolicies(urlParams.get("policies")));
return;
}

const connection = connectToController({
setOrigin,
setRpcUrl,
setPolicies,
setContext,
setController,
});
Expand Down
2 changes: 0 additions & 2 deletions packages/keychain/src/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ const Login: NextPage = () => {

return (
<LoginComponent
chainId={chainId}
rpcUrl={rpcUrl}
onSignup={() => router.push({ pathname: "/signup", query: router.query })}
onSuccess={async () => {
router.replace(`${process.env.NEXT_PUBLIC_ADMIN_URL}/profile`);
Expand Down
2 changes: 0 additions & 2 deletions packages/keychain/src/pages/signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export default function Signup() {

return (
<SignupComponent
chainId={chainId}
rpcUrl={rpcUrl}
onLogin={() => router.push({ pathname: "/login", query: router.query })}
onSuccess={() => {
router.replace(`${process.env.NEXT_PUBLIC_ADMIN_URL}/profile`);
Expand Down
6 changes: 6 additions & 0 deletions packages/keychain/src/utils/connection/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ import { ConnectCtx, ConnectionCtx } from "./types";
import Controller from "utils/controller";

export function connectFactory({
setOrigin,
setRpcUrl,
setPolicies,
setContext,
}: {
setOrigin: (origin: string) => void;
setRpcUrl: (url: string) => void;
setPolicies: (policies: Policy[]) => void;
setContext: (context: ConnectionCtx) => void;
}) {
return (origin: string) =>
(policies: Policy[], rpcUrl: string): Promise<ConnectReply> => {
setOrigin(origin);
setRpcUrl(rpcUrl);
setPolicies(policies);

return new Promise((resolve, reject) => {
setContext({
Expand Down
10 changes: 8 additions & 2 deletions packages/keychain/src/utils/connection/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from "./types";

import { ConnectError, ResponseCodes } from "@cartridge/controller";
import { ConnectError, Policy, ResponseCodes } from "@cartridge/controller";
import { connectToParent } from "@cartridge/penpal";
import { normalize as normalizeOrigin } from "utils/url";
import Controller from "utils/controller";
Expand All @@ -15,17 +15,23 @@ import { username } from "./username";
import { ConnectionCtx } from "./types";

export function connectToController({
setOrigin,
setRpcUrl,
setPolicies,
setContext,
setController,
}: {
setOrigin: (origin: string) => void;
setRpcUrl: (url: string) => void;
setPolicies: (policies: Policy[]) => void;
setContext: (ctx: ConnectionCtx) => void;
setController: (controller: Controller) => void;
}) {
return connectToParent({
methods: {
connect: normalize(connectFactory({ setRpcUrl, setContext })),
connect: normalize(
connectFactory({ setOrigin, setRpcUrl, setPolicies, setContext }),
),
disconnect: normalize(validate(disconnectFactory(setController))),
execute: normalize(validate(executeFactory({ setContext }))),
estimateDeclareFee: normalize(validate(estimateDeclareFee)),
Expand Down

0 comments on commit 8ff9348

Please sign in to comment.