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 62a5e39
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 46 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
11 changes: 4 additions & 7 deletions packages/keychain/src/components/connect/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@ 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 +60,7 @@ export function Login({
}

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

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

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

return (
Expand Down
19 changes: 3 additions & 16 deletions packages/keychain/src/components/connect/Signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +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) {
const [isRegistering, setIsRegistering] = useState(false);
Expand Down Expand Up @@ -78,15 +74,10 @@ export function Signup({
validateOnBlur={false}
>
<Form
chainId={chainId}
rpcUrl={rpcUrl}
onLogin={onLogin}
onSuccess={onSuccess}
isRegistering={isRegistering}
isLoading={isLoading}
setIsRegistering={setIsRegistering}
origin={origin}
policies={policies}
isSlot={isSlot}
error={error}
/>
Expand All @@ -97,15 +88,10 @@ export function Signup({
}

function Form({
origin,
policies,
chainId,
rpcUrl,
isRegistering,
isLoading,
isSlot,
onLogin: onLoginProp,
onSuccess,
setIsRegistering,
error,
}: SignupProps & {
Expand All @@ -114,6 +100,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 +147,7 @@ function Form({
controller.store();
await controller.account.sync();

onSuccess(controller);
setController(controller);
},
},
);
Expand Down
8 changes: 0 additions & 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,7 @@ export type AuthProps = SignupProps | LoginProps;

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

export type SignupProps = AuthBaseProps & {
Expand Down
2 changes: 2 additions & 0 deletions packages/keychain/src/hooks/connection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export function ConnectionProvider({ children }: PropsWithChildren) {
}

const connection = connectToController({
setOrigin,
setRpcUrl,
setPolicies,
setContext,
setController,
});
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
8 changes: 6 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,21 @@ 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 62a5e39

Please sign in to comment.