Skip to content

Commit

Permalink
feature/wallet-modal into main
Browse files Browse the repository at this point in the history
feature: wallet modal
  • Loading branch information
vinicius-sacramento authored Jun 27, 2024
2 parents a056034 + 7c564f4 commit dcd2070
Show file tree
Hide file tree
Showing 18 changed files with 807 additions and 12 deletions.
11 changes: 11 additions & 0 deletions apps/commune-governance/public/receive-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions apps/commune-governance/public/send-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions apps/commune-governance/public/stake-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions apps/commune-governance/public/transfer-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions apps/commune-governance/public/unstake-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions apps/commune-page/public/receive-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions apps/commune-page/public/send-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions apps/commune-page/public/stake-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions apps/commune-page/public/transfer-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions apps/commune-page/public/unstake-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 21 additions & 9 deletions packages/providers/src/context/commune.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "@polkadot/extension-inject/types";
import { toast } from "react-toastify";

import { WalletModal } from "@commune-ts/ui/wallet-modal";
import { Wallet } from "@commune-ts/ui/wallet";

import type {
AddCustomProposal,
Expand Down Expand Up @@ -40,7 +40,7 @@ import {
useNotDelegatingVoting,
useProposals,
} from "../hooks";
import { calculateAmount } from "../utils";
import { calculateAmount, formatToken } from "../utils";

interface CommuneApiState {
web3Accounts: (() => Promise<InjectedAccountWithMeta[]>) | null;
Expand Down Expand Up @@ -111,7 +111,7 @@ export function CommuneProvider({
});
const [isInitialized, setIsInitialized] = useState<boolean>(false);
const [isConnected, setIsConnected] = useState(false);
const [openModal, setOpenModal] = useState(false);
const [openWalletModal, setOpenWalletModal] = useState(false);
const [accounts, setAccounts] = useState<InjectedAccountWithMeta[]>([]);
const [selectedAccount, setSelectedAccount] =
useState<InjectedAccountWithMeta | null>(null);
Expand Down Expand Up @@ -177,22 +177,25 @@ export function CommuneProvider({
const allAccounts = await getWallets();
if (allAccounts) {
setAccounts(allAccounts);
setOpenModal(true);
}
setOpenWalletModal(true)
} catch (error) {
return undefined;
}
}

function handleWalletModal(state?: boolean): void {
setOpenWalletModal(state || !openWalletModal)
}

function handleConnectWrapper(): void {
void handleConnect();
handleConnect();
}

function handleWalletSelections(wallet: InjectedAccountWithMeta): void {
localStorage.setItem("favoriteWalletAddress", wallet.address);
setSelectedAccount(wallet);
setIsConnected(true);
setOpenModal(false);
}

// == Transaction Handler ==
Expand Down Expand Up @@ -500,10 +503,19 @@ export function CommuneProvider({
isDaosLoading,
}}
>
<WalletModal
<Wallet
addStake={addStake}
balance={formatToken(balance || 0n)}
handleConnect={handleConnect}
handleWalletModal={handleWalletModal}
handleWalletSelections={handleWalletSelections}
open={openModal}
setOpen={setOpenModal}
isInitialized={isInitialized}
openWalletModal={openWalletModal}
removeStake={removeStake}
selectedAccount={selectedAccount}
stakeOut={stakeOut}
transfer={transfer}
transferStake={transferStake}
wallets={accounts}
/>
{children}
Expand Down
8 changes: 8 additions & 0 deletions packages/subspace/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,11 @@ export function handleDaos(
): [DaoApplications[], Error[]] {
return handleEntries(rawDaos, parseDaos);
}

export const copyToClipboard = async (text: string) => {
try {
await navigator.clipboard.writeText(text);
} catch (err) {
console.error("Failed to copy: ", err);
}
};
3 changes: 3 additions & 0 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
".": "./src/index.ts",
"./styles.css": "./dist/index.css",
"./header": "./src/components/header.tsx",
"./copy-button": "./src/components/copy-button.tsx",
"./footer": "./src/components/footer.tsx",
"./loading": "./src/components/loading.tsx",
"./wallet-button": "./src/components/wallet-button.tsx",
"./select-wallet-modal": "./src/components/select-wallet-modal.tsx",
"./wallet": "./src/components/wallet.tsx",
"./wallet-modal": "./src/components/wallet-modal.tsx",
"./discord-widget": "./src/components/discord-widget.tsx",
"./data": "./src/data.ts",
Expand Down
47 changes: 47 additions & 0 deletions packages/ui/src/components/copy-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"use client";

import { useState } from "react";
import Image from "next/image";

import { cn } from "..";
import { copyToClipboard } from "../../../subspace/utils";

interface CodeComponentProps {
code: string;
}

export function CopyButton(props: CodeComponentProps): JSX.Element {
const { code } = props;
const [copied, setCopied] = useState(false);

async function copyTextToClipboard(text: string): Promise<void> {
setCopied(true);
setTimeout(() => {
setCopied(false);
}, 1000);
await copyToClipboard(text);

return;
}

return (
<button
className={cn(
`flex w-full max-w-28 items-center justify-center border border-gray-500 py-2 text-gray-400 hover:border-green-600 hover:text-green-600 ${copied && "cursor-not-allowed border-green-500 text-green-500 hover:!border-green-500 hover:!text-green-500"}`,
)}
onClick={() => void copyTextToClipboard(code)}
type="button"
>
<span className={cn(`flex items-center ${copied ? "text-green-" : ""}`)}>
{!copied ? "Copy" : "Copied"}{" "}
<Image
alt=""
className="ml-1"
height={20}
src="docs-icon.svg"
width={20}
/>
</span>
</button>
);
}
Loading

0 comments on commit dcd2070

Please sign in to comment.