|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useAccount, useConnect, useDisconnect } from "@starknet-react/core"; |
| 4 | +import ControllerConnector from "@cartridge/connector/controller"; |
| 5 | +import React, { useEffect, useState, useRef } from "react"; |
| 6 | +import { Button } from "@cartridge/ui"; |
| 7 | + |
| 8 | +export function ConnectWallet() { |
| 9 | + const { connect, connectors } = useConnect(); |
| 10 | + const { disconnect } = useDisconnect(); |
| 11 | + const { address } = useAccount(); |
| 12 | + |
| 13 | + const controller = connectors[0] as ControllerConnector; |
| 14 | + const [isDragging, setIsDragging] = useState(false); |
| 15 | + const [position, setPosition] = useState({ x: -1000, y: -1000 }); |
| 16 | + const dragRef = useRef<{ startX: number; startY: number }>(undefined); |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + setPosition({ |
| 20 | + x: window.innerWidth - 220, |
| 21 | + y: window.innerHeight - 220, |
| 22 | + }); |
| 23 | + }, []); |
| 24 | + |
| 25 | + const [username, setUsername] = useState<string>(); |
| 26 | + useEffect(() => { |
| 27 | + if (!address) return; |
| 28 | + controller.username()?.then((n) => setUsername(n)); |
| 29 | + }, [address, controller]); |
| 30 | + |
| 31 | + const handleMouseDown = (e: React.MouseEvent) => { |
| 32 | + setIsDragging(true); |
| 33 | + dragRef.current = { |
| 34 | + startX: e.pageX - position.x, |
| 35 | + startY: e.pageY - position.y, |
| 36 | + }; |
| 37 | + }; |
| 38 | + |
| 39 | + const handleMouseMove = (e: React.MouseEvent) => { |
| 40 | + if (!isDragging || !dragRef.current) return; |
| 41 | + |
| 42 | + setPosition({ |
| 43 | + x: e.pageX - dragRef.current.startX, |
| 44 | + y: e.pageY - dragRef.current.startY, |
| 45 | + }); |
| 46 | + }; |
| 47 | + |
| 48 | + const handleMouseUp = () => { |
| 49 | + setIsDragging(false); |
| 50 | + dragRef.current = undefined; |
| 51 | + }; |
| 52 | + |
| 53 | + const registerSessionUrl = |
| 54 | + "http://localhost:3001/session?public_key=0x2cb057c18198ae4555a144bfdace051433b9a545dc88224de58fa04e323f269&redirect_uri=http://localhost:3002&policies=%5B%7B%22target%22:%220x03661Ea5946211b312e8eC71B94550928e8Fd3D3806e43c6d60F41a6c5203645%22,%22method%22:%22attack%22,%22description%22:%22Attack%20the%20beast%22%7D,%7B%22target%22:%220x03661Ea5946211b312e8eC71B94550928e8Fd3D3806e43c6d60F41a6c5203645%22,%22method%22:%22claim%22,%22description%22:%22Claim%20your%20tokens%22%7D%5D&rpc_url=http://localhost:8001/x/starknet/sepolia"; |
| 55 | + |
| 56 | + const openRegisterSessionUrl = () => { |
| 57 | + window.open(registerSessionUrl, "_blank", "noopener,noreferrer"); |
| 58 | + }; |
| 59 | + |
| 60 | + return ( |
| 61 | + <div |
| 62 | + style={{ position: "relative" }} |
| 63 | + onMouseMove={handleMouseMove} |
| 64 | + onMouseUp={handleMouseUp} |
| 65 | + onMouseLeave={handleMouseUp} |
| 66 | + > |
| 67 | + {address && ( |
| 68 | + <> |
| 69 | + <p>Account: {address} </p> |
| 70 | + {username && <p>Username: {username}</p>} |
| 71 | + </> |
| 72 | + )} |
| 73 | + {address ? ( |
| 74 | + <Button onClick={() => disconnect()}>Disconnect</Button> |
| 75 | + ) : ( |
| 76 | + <div className="flex gap-1"> |
| 77 | + <Button onClick={() => connect({ connector: controller })}> |
| 78 | + Connect |
| 79 | + </Button> |
| 80 | + {/* <Button onClick={() => connect({ connector: session })}> |
| 81 | + Create Session |
| 82 | + </Button> */} |
| 83 | + <Button onClick={openRegisterSessionUrl}>Register Session</Button> |
| 84 | + </div> |
| 85 | + )} |
| 86 | + |
| 87 | + {/* Draggable test overlay */} |
| 88 | + <div |
| 89 | + onMouseDown={handleMouseDown} |
| 90 | + style={{ |
| 91 | + position: "fixed", |
| 92 | + top: position.y, |
| 93 | + left: position.x, |
| 94 | + width: 200, |
| 95 | + height: 200, |
| 96 | + backgroundColor: "rgba(255, 0, 0, 0.3)", |
| 97 | + border: "2px solid red", |
| 98 | + cursor: isDragging ? "grabbing" : "grab", |
| 99 | + zIndex: 10001, |
| 100 | + display: "flex", |
| 101 | + alignItems: "center", |
| 102 | + justifyContent: "center", |
| 103 | + userSelect: "none", |
| 104 | + }} |
| 105 | + > |
| 106 | + <div |
| 107 | + style={{ |
| 108 | + color: "white", |
| 109 | + textAlign: "center", |
| 110 | + textShadow: "1px 1px 2px rgba(0,0,0,0.8)", |
| 111 | + pointerEvents: "none", |
| 112 | + }} |
| 113 | + > |
| 114 | + Drag me around |
| 115 | + <br /> |
| 116 | + (Clickjacking Test) |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + ); |
| 121 | +} |
0 commit comments