|
1 | 1 | "use client"; |
2 | 2 |
|
3 | | -import { FC } from "react"; |
4 | | - |
5 | | -import { ColorModeToggle } from "components/ColorModeToggle"; |
6 | | -import Header from "components/Header"; |
7 | | -import { DelegateAccount } from "components/DelegateAccount"; |
8 | | -import { InvalidTxn } from "components/InvalidTxn"; |
9 | | -import { LookupControllers } from "components/LookupControllers"; |
10 | | -import { ManualTransferEth } from "components/ManualTransferEth"; |
11 | | -import { PlayButton } from "components/PlayButton"; |
12 | | -import { Profile } from "components/Profile"; |
13 | | -import { SignMessage } from "components/SignMessage"; |
14 | | -import { Transfer } from "components/Transfer"; |
15 | | -import { Starterpack } from "components/Starterpack"; |
| 3 | +import { FC, useEffect, useMemo, useState } from "react"; |
| 4 | +import { useAccount, useConnect, useDisconnect } from "@starknet-react/core"; |
| 5 | +import ControllerConnector from "@cartridge/connector/controller"; |
| 6 | +import { Button } from "@cartridge/ui"; |
16 | 7 |
|
17 | 8 | const Home: FC = () => { |
| 9 | + const { status } = useAccount(); |
| 10 | + const { connect, connectors } = useConnect(); |
| 11 | + const { disconnect } = useDisconnect(); |
| 12 | + const [isControllerReady, setIsControllerReady] = useState(false); |
| 13 | + |
| 14 | + const controllerConnector = useMemo( |
| 15 | + () => ControllerConnector.fromConnectors(connectors), |
| 16 | + [connectors], |
| 17 | + ); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + const checkReady = () => { |
| 21 | + try { |
| 22 | + if (controllerConnector) { |
| 23 | + setIsControllerReady(controllerConnector.isReady()); |
| 24 | + } |
| 25 | + } catch (e) { |
| 26 | + console.error("Error checking controller readiness:", e); |
| 27 | + } |
| 28 | + }; |
| 29 | + |
| 30 | + checkReady(); |
| 31 | + const interval = setInterval(checkReady, 1000); |
| 32 | + return () => clearInterval(interval); |
| 33 | + }, [controllerConnector]); |
| 34 | + |
18 | 35 | return ( |
19 | | - <main className="w-screen overflow-x-hidden flex flex-col p-4 gap-4"> |
20 | | - <div className="flex justify-between"> |
21 | | - <h2 className="text-3xl font-bold underline text-primary"> |
22 | | - Controller Example (Next.js) |
23 | | - </h2> |
24 | | - <ColorModeToggle /> |
25 | | - </div> |
26 | | - <Header /> |
27 | | - <PlayButton /> |
28 | | - <Profile /> |
29 | | - <Transfer /> |
30 | | - <ManualTransferEth /> |
31 | | - <Starterpack /> |
32 | | - <DelegateAccount /> |
33 | | - <InvalidTxn /> |
34 | | - <SignMessage /> |
35 | | - <LookupControllers /> |
| 36 | + <main className="h-screen w-screen flex flex-col items-center justify-center gap-4"> |
| 37 | + {status !== "connected" ? ( |
| 38 | + <Button |
| 39 | + onClick={() => connect({ connector: controllerConnector })} |
| 40 | + disabled={!isControllerReady} |
| 41 | + > |
| 42 | + {isControllerReady ? "Connect" : "Waiting for keychain..."} |
| 43 | + </Button> |
| 44 | + ) : ( |
| 45 | + <div className="flex flex-col items-center gap-4"> |
| 46 | + <Button |
| 47 | + onClick={() => { |
| 48 | + controllerConnector.controller.openStarterPack(47); |
| 49 | + }} |
| 50 | + > |
| 51 | + Purchase Starterpack |
| 52 | + </Button> |
| 53 | + <Button |
| 54 | + variant="outline" |
| 55 | + onClick={() => disconnect()} |
| 56 | + className="mt-4" |
| 57 | + > |
| 58 | + Disconnect |
| 59 | + </Button> |
| 60 | + </div> |
| 61 | + )} |
36 | 62 | </main> |
37 | 63 | ); |
38 | 64 | }; |
|
0 commit comments