Skip to content

Commit

Permalink
Create session displays error
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Jul 31, 2024
1 parent 90e3622 commit ead04c8
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions packages/keychain/src/components/connect/CreateSession.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Container, Content, Footer } from "components/layout";
import { BigNumberish } from "starknet";
import { Policy } from "@cartridge/controller";
import { Button, Text } from "@chakra-ui/react";
import { useState } from "react";
import { useCallback, useState } from "react";
import { useConnection } from "hooks/connection";
import { LockIcon } from "@cartridge/ui";
import { Policies } from "Policies";
import { ErrorAlert } from "components/ErrorAlert";

export function CreateSession({
onConnect,
Expand All @@ -16,6 +17,20 @@ export function CreateSession({
const [isConnecting, setIsConnecting] = useState(false);
const [expiresAt] = useState<bigint>(3000000000n);
const [maxFees] = useState<BigNumberish>();
const [error, setError] = useState<Error>();

const onCreateSession = useCallback(async () => {
try {
setError(undefined);
setIsConnecting(true);
await controller.approve(origin, expiresAt, policies, maxFees);
onConnect(policies);
} catch (e) {
setError(e)
setIsConnecting(false)
}
}, [controller, origin, expiresAt, policies, maxFees, onConnect]);

return (
<Container
variant="connect"
Expand All @@ -36,21 +51,14 @@ export function CreateSession({
</Content>

<Footer hideTxSummary>
{error && (
<ErrorAlert title="Create session failed" description={error.message} />
)}
<Button
colorScheme="colorful"
isDisabled={isConnecting}
isLoading={isConnecting}
onClick={async () => {
setIsConnecting(true);
await controller
.approve(origin, expiresAt, policies, maxFees)
.then(() => {
onConnect(policies);
})
.catch(() => {
setIsConnecting(false);
});
}}
onClick={()=> onCreateSession()}
>
create session
</Button>
Expand Down

0 comments on commit ead04c8

Please sign in to comment.