-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #315 from BibliothecaDAO/main
update cartridge controller connector
- Loading branch information
Showing
4 changed files
with
108 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 54 additions & 32 deletions
86
apps/nextjs/src/app/_components/wallet/StarknetLoginButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,84 @@ | ||
"use client"; | ||
|
||
import type ControllerConnector from "@cartridge/connector/controller"; | ||
import type { buttonVariants } from "@realms-world/ui/components/ui/button"; | ||
import type { VariantProps } from "class-variance-authority"; | ||
import React from "react"; | ||
import StarknetLogo from "@/icons/starknet.svg"; | ||
import { useUIStore } from "@/providers/UIStoreProvider"; | ||
import { shortenHex } from "@/utils/utils"; | ||
import { useAccount } from "@starknet-react/core"; | ||
|
||
import type { buttonVariants } from "@realms-world/ui/components/ui/button"; | ||
import { Button } from "@realms-world/ui/components/ui/button"; | ||
import { Loader } from "lucide-react"; | ||
import { useAccount } from "@starknet-react/core"; | ||
import { Loader, User } from "lucide-react"; | ||
|
||
export const StarknetLoginButton = ({ | ||
openAccount = false, | ||
variant, | ||
textClass, | ||
buttonClass, | ||
children, | ||
newTransactionCount | ||
newTransactionCount, | ||
}: { | ||
openAccount?: boolean; | ||
variant?: VariantProps<typeof buttonVariants>["variant"]; | ||
textClass?: string; | ||
buttonClass?: string; | ||
children?: React.ReactNode, | ||
newTransactionCount?: number | ||
children?: React.ReactNode; | ||
newTransactionCount?: number; | ||
}) => { | ||
const { address, isConnected, isConnecting } = useAccount(); | ||
const { address, isConnected, isConnecting, connector } = useAccount(); | ||
const { toggleAccount, toggleStarknetLogin } = useUIStore((state) => state); | ||
|
||
const onConnectClick = () => { | ||
return !isConnected | ||
? toggleStarknetLogin() | ||
: openAccount && toggleAccount(); | ||
if (!isConnected) { | ||
return toggleStarknetLogin(); | ||
} | ||
|
||
if (connector?.id === "controller") { | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
(connector as unknown as ControllerConnector).controller.openProfile( | ||
"inventory", | ||
); | ||
} else if (openAccount) { | ||
toggleAccount(); | ||
} | ||
}; | ||
|
||
return ( | ||
<Button | ||
className={buttonClass} | ||
variant={variant ?? "outline"} | ||
size="sm" | ||
onClick={onConnectClick} | ||
disabled={isConnecting} | ||
> | ||
<span className="flex items-center font-sans normal-case"> | ||
<StarknetLogo className="h-6 w-6" /> | ||
<span className={`pl-2 ${textClass ?? "sm:block"}`}> | ||
{isConnecting && <Loader className="animate-spin" />} | ||
{address ? ( | ||
<>{shortenHex(address, 8)}</> | ||
) : (<> | ||
{children ?? "Starknet"}</>) | ||
} | ||
<> | ||
<Button | ||
className={buttonClass} | ||
variant={variant ?? "outline"} | ||
size="sm" | ||
onClick={onConnectClick} | ||
disabled={isConnecting} | ||
> | ||
<span className="flex items-center font-sans normal-case"> | ||
<StarknetLogo className="h-6 w-6" /> | ||
<span className={`pl-2 ${textClass ?? "sm:block"}`}> | ||
{isConnecting && <Loader className="animate-spin" />} | ||
{address ? ( | ||
<>{shortenHex(address, 8)}</> | ||
) : ( | ||
<>{children ?? "Starknet"}</> | ||
)} | ||
</span> | ||
</span> | ||
</span> | ||
{(isConnected && newTransactionCount) && newTransactionCount > 0 ? <span | ||
className={'bg-green-600 w-[20px] absolute -top-1.5 -right-1.5 rounded-full text-black'} | ||
>{newTransactionCount}</span> : null} | ||
</Button> | ||
{isConnected && newTransactionCount && newTransactionCount > 0 ? ( | ||
<span | ||
className={ | ||
"absolute -right-1.5 -top-1.5 w-[20px] rounded-full bg-green-600 text-black" | ||
} | ||
> | ||
{newTransactionCount} | ||
</span> | ||
) : null} | ||
</Button> | ||
{connector?.id === "controller" ? ( | ||
<Button size="sm" onClick={() => openAccount && toggleAccount()}> | ||
<User /> | ||
</Button> | ||
) : null} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.