Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use getDisplayableAccountId #1

Merged
merged 1 commit into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/components/Navbar/components/ButtonConnectWallet.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { ChevronDownIcon } from "@chakra-ui/icons";
import { Button, Menu, MenuButton, MenuList } from "@chakra-ui/react";
import { useWalletSelector } from "../../../contexts/WalletSelectorContext";
import useScreenSize from "../../../hooks/useScreenSize";
import { YellowButton } from "../../../shared/components/YellowButton";

export function ButtonConnectWallet() {
const walletSelector = useWalletSelector();
const { width } = useScreenSize();

const handleOnClick = async () => {
if (walletSelector.selector.isSignedIn() && walletSelector.wallet) {
Expand All @@ -25,7 +27,7 @@ export function ButtonConnectWallet() {
borderRadius="full"
rightIcon={<ChevronDownIcon />}
>
{walletSelector.accountId}
{walletSelector.ticTacToeLogic?.getDisplayableAccountId(width)}
</MenuButton>
<MenuList
minWidth="auto"
Expand Down
14 changes: 12 additions & 2 deletions src/components/TicTacToe/components/ActiveGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
} from "../containers/TicTacToe";
import cheddarIcon from "../../../assets/cheddar-icon.svg";
import TokenName from "./TokenName";
import useScreenSize from "../../../hooks/useScreenSize";
import { formatAccountId } from "../../../shared/helpers/formatAccountId";

type Props = {
data: GameParams | undefined;
Expand All @@ -37,6 +39,7 @@ export function ActiveGame({
const [timeLeft, settimeLeft] = useState<number | undefined>();
const [loadingFinalizedGame, setLoadingFinalizedGame] = useState(false);
const walletSelector = useWalletSelector();
const { width } = useScreenSize();

const handleGiveUp = () => {
if (data?.active_game?.[0]) {
Expand Down Expand Up @@ -150,7 +153,11 @@ export function ActiveGame({
{activeGameParams.game_result.winner_id ===
walletSelector.accountId
? "You Are The Winner!"
: `The Winner is ${activeGameParams.game_result.winner_id}`}
: `The Winner is
${formatAccountId(
activeGameParams.game_result.winner_id,
width
)}`}
</Text>
<Text>
Reward:{" "}
Expand Down Expand Up @@ -222,7 +229,10 @@ export function ActiveGame({
{data.active_game[1].current_player.account_id ===
walletSelector.accountId
? "You"
: data.active_game[1].current_player.account_id}
: formatAccountId(
data.active_game[1].current_player.account_id,
width
)}
</Text>
</Flex>
<Text>
Expand Down
32 changes: 24 additions & 8 deletions src/components/TicTacToe/components/WaiitingListElement.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Button, Grid, Text } from "@chakra-ui/react";
import { Button, Flex, Grid, Text } from "@chakra-ui/react";
import { utils } from "near-api-js";
import { useWalletSelector } from "../../../contexts/WalletSelectorContext";
import { AvailablePlayerConfig } from "../../../near/contracts/TicTacToe";
import { PurpleButton } from "../../../shared/components/PurpleButton";
import { formatAccountId } from "../../../shared/helpers/formatAccountId";
import TokenName from "./TokenName";

type Props = {
player: [string, AvailablePlayerConfig];
width: number;
};

export function WaiitingListElement({ player }: Props) {
export function WaiitingListElement({ player, width }: Props) {
const walletSelector = useWalletSelector();

const handleAcceptButton = (
Expand Down Expand Up @@ -37,19 +39,33 @@ export function WaiitingListElement({ player }: Props) {
walletSelector.ticTacToeLogic?.removeBet();
};

const isSmallDesign = width < 480 || (width > 768 && width < 992);
return (
<Grid
mb="5px"
bg="#1111"
templateColumns="2.5fr 2fr 1fr"
templateColumns={isSmallDesign ? "2.5fr 1fr" : "2.2fr 2fr 1fr"}
p="6px 12px"
borderRadius="8px"
alignItems="center"
>
<Text textAlign="initial">{player[0]}</Text>
<Text textAlign="initial">
{utils.format.formatNearAmount(player[1].deposit)}{" "}
{<TokenName tokenId={player[1].token_id} />}
</Text>
{isSmallDesign ? (
<Flex flexDirection="column" fontSize="0.9em">
<Text textAlign="initial">{formatAccountId(player[0], width)}</Text>
<Text textAlign="initial">
{utils.format.formatNearAmount(player[1].deposit)}&nbsp;
{<TokenName tokenId={player[1].token_id} />}
</Text>
</Flex>
) : (
<>
<Text textAlign="initial">{formatAccountId(player[0], width)}</Text>
<Text textAlign="initial">
{utils.format.formatNearAmount(player[1].deposit)}&nbsp;
{<TokenName tokenId={player[1].token_id} />}
</Text>
</>
)}
{player[0] !== walletSelector.accountId ? (
<PurpleButton
size="sm"
Expand Down
8 changes: 5 additions & 3 deletions src/components/TicTacToe/components/WaitingList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "@chakra-ui/react";
import { useWalletSelector } from "../../../contexts/WalletSelectorContext";
import { useContractParams } from "../../../hooks/useContractParams";
import useScreenSize from "../../../hooks/useScreenSize";
import { WaiitingListElement } from "./WaiitingListElement";

type Props = {
Expand All @@ -19,6 +20,7 @@ type Props = {
export default function WaitingList({ showingActiveGame }: Props) {
const { data } = useContractParams();
const walletSelector = useWalletSelector();
const { width } = useScreenSize();

return (
<AccordionItem
Expand Down Expand Up @@ -52,7 +54,7 @@ export default function WaitingList({ showingActiveGame }: Props) {
My Challenge
</Text>
)}
<WaiitingListElement player={player} />
<WaiitingListElement player={player} width={width} />
<Spacer mb="30px" />
</Box>
))}
Expand All @@ -71,7 +73,7 @@ export default function WaitingList({ showingActiveGame }: Props) {
Private Challenges
</Text>
)}
<WaiitingListElement player={player} />
<WaiitingListElement player={player} width={width} />
<Spacer mb="30px" />
</Box>
))}
Expand All @@ -88,7 +90,7 @@ export default function WaitingList({ showingActiveGame }: Props) {
Public Challenges
</Text>
)}
<WaiitingListElement player={player} />
<WaiitingListElement player={player} width={width} />
</Box>
))}
</AccordionPanel>
Expand Down
12 changes: 2 additions & 10 deletions src/near/contracts/TicTacToe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,8 @@ export class TicTacToeContract {
};
}

getDisplayableAccountId(
startLength?: number,
endLength?: number,
maxLength?: number
): string {
return this.wallet.getDisplayableAccountId(
startLength,
endLength,
maxLength
);
getDisplayableAccountId(screenWidth: number): string {
return this.wallet.getDisplayableAccountId(screenWidth);
}

/**
Expand Down
12 changes: 2 additions & 10 deletions src/near/logics/TicTacToeLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,8 @@ export class TicTacToeLogic {
return this.ticTacToeContract.get_contract_params();
}

getDisplayableAccountId(
startLength?: number,
endLength?: number,
maxLength?: number
): string {
return this.ticTacToeContract.getDisplayableAccountId(
startLength,
endLength,
maxLength
);
getDisplayableAccountId(screenWidth: number): string {
return this.ticTacToeContract.getDisplayableAccountId(screenWidth);
}

getLastGames(): Promise<[number, FinalizedGame][]> {
Expand Down
2 changes: 1 addition & 1 deletion src/near/wallet/wallet-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type EventHandler = (this: Document, ev: any) => any;
//-----------------------
export interface WalletInterface {
getAccountId(): string;
getDisplayableAccountId(): string;
getDisplayableAccountId(screenWidth: number): string;

//getAccountBalance(accountId?:string):Promise<U128String>;

Expand Down
26 changes: 20 additions & 6 deletions src/near/wallet/wallet-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,31 @@ export class SelectorWallet implements WalletInterface {
?.accountId!;
}

getDisplayableAccountId(
startLength = 10,
endLength = 10,
maxLength = 22
): string {
getDisplayableAccountId(screenWidth: number): string {
const { startLength, endLength, maxLength } =
this.getAccountLengths(screenWidth);
const accountId = this.getAccountId();
return accountId.length > maxLength
? accountId.slice(0, startLength) + ".." + accountId.slice(0 - endLength)
? accountId.slice(0, startLength) +
".." +
(endLength === 0 ? "" : accountId.slice(0 - endLength))
: accountId;
}

private getAccountLengths(screenWidth: number): {
startLength: number;
endLength: number;
maxLength: number;
} {
if (screenWidth < 480) {
return { startLength: 7, endLength: 0, maxLength: 8 };
} else if (screenWidth < 768) {
return { startLength: 8, endLength: 8, maxLength: 18 };
} else {
return { startLength: 10, endLength: 10, maxLength: 22 };
}
}

async getAccountBalance(accountId?: string | undefined): Promise<string> {
if (!accountId) {
accountId = this.walletState.accounts.find(
Expand Down
23 changes: 23 additions & 0 deletions src/shared/helpers/formatAccountId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export function formatAccountId(
accountId: string,
screenWidth: number
): string {
const { startLength, endLength, maxLength } = getAccountLengths(screenWidth);
return accountId.length > maxLength
? accountId.slice(0, startLength) +
".." +
(endLength === 0 ? "" : accountId.slice(0 - endLength))
: accountId;
}

function getAccountLengths(screenWidth: number): {
startLength: number;
endLength: number;
maxLength: number;
} {
if (screenWidth < 1200) {
return { startLength: 7, endLength: 7, maxLength: 6 };
} else {
return { startLength: 9, endLength: 9, maxLength: 20 };
}
}