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

chalabi/mobile on desktop #150

Merged
merged 11 commits into from
Jan 6, 2025
Binary file modified bun.lockb
Binary file not shown.
348 changes: 305 additions & 43 deletions components/react/modal.tsx

Large diffs are not rendered by default.

120 changes: 120 additions & 0 deletions components/react/qrCode.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import Image from 'next/image';
import QRCodeUtil from 'qrcode';
import React, { FunctionComponent, ReactElement, useMemo } from 'react';

const generateMatrix = (
value: string,
errorCorrectionLevel: QRCodeUtil.QRCodeErrorCorrectionLevel
) => {
const arr = Array.prototype.slice.call(
QRCodeUtil.create(value, { errorCorrectionLevel }).modules.data,
0
);
const sqrt = Math.sqrt(arr.length);
return arr.reduce(
(rows, key, index) =>
(index % sqrt === 0 ? rows.push([key]) : rows[rows.length - 1].push(key)) && rows,
[]
);
};

export const QRCode: FunctionComponent<{
errorCorrectionLevel?: QRCodeUtil.QRCodeErrorCorrectionLevel;
logoUrl?: string;
logoSize?: number;
size?: number;
value: string;
}> = ({ errorCorrectionLevel = 'M', logoSize = 50, logoUrl, size = 280, value }) => {
const dots = useMemo(() => {
const dots: ReactElement[] = [];
const matrix = generateMatrix(value, errorCorrectionLevel);
const cellSize = size / matrix.length;
let qrList = [
{ x: 0, y: 0 },
{ x: 1, y: 0 },
{ x: 0, y: 1 },
];

qrList.forEach(({ x, y }) => {
const x1 = (matrix.length - 7) * cellSize * x;
const y1 = (matrix.length - 7) * cellSize * y;
for (let i = 0; i < 3; i++) {
dots.push(
<rect
fill={i % 2 !== 0 ? 'white' : 'black'}
height={cellSize * (7 - i * 2)}
key={`${i}-${x}-${y}`}
rx={(i - 2) * -5 + (i === 0 ? 2 : 0)} // calculated border radius for corner squares
ry={(i - 2) * -5 + (i === 0 ? 2 : 0)} // calculated border radius for corner squares
width={cellSize * (7 - i * 2)}
x={x1 + cellSize * i}
y={y1 + cellSize * i}
/>
);
}
});

const clearArenaSize = Math.floor(logoSize / cellSize);
const matrixMiddleStart = matrix.length / 2 - clearArenaSize / 2;
const matrixMiddleEnd = matrix.length / 2 + clearArenaSize / 2 - 1;

matrix.forEach((row: QRCodeUtil.QRCode[], i: number) => {
row.forEach((_: any, j: number) => {
if (matrix[i][j]) {
if (
!(
(i < 7 && j < 7) ||
(i > matrix.length - 8 && j < 7) ||
(i < 7 && j > matrix.length - 8)
)
) {
if (
!(
i > matrixMiddleStart &&
i < matrixMiddleEnd &&
j > matrixMiddleStart &&
j < matrixMiddleEnd
)
) {
dots.push(
<circle
cx={i * cellSize + cellSize / 2}
cy={j * cellSize + cellSize / 2}
fill="black"
key={`circle-${i}-${j}`}
r={cellSize / 3} // calculate size of single dots
/>
);
}
}
}
});
});

return dots;
}, [errorCorrectionLevel, logoSize, size, value]);

const logoPosition = size / 2 - logoSize / 2;

return (
<div className="relative flex items-center justify-center rounded-xl bg-white p-4">
<div className="relative" style={{ height: size, width: size }}>
{logoUrl && (
<div
className="absolute flex rounded-lg justify-center"
style={{
top: logoPosition,
width: size,
}}
>
<Image height={logoSize} src={logoUrl} width={logoSize} alt="Wallet logo" />
</div>
)}
<svg height={size} width={size}>
<rect fill="transparent" height={size} width={size} />
{dots}
</svg>
</div>
</div>
);
};
8 changes: 3 additions & 5 deletions components/react/views/Connecting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import { XMarkIcon } from '@heroicons/react/24/outline';
import { ChevronLeftIcon } from '@heroicons/react/20/solid';
import { getRealLogo } from '@/utils';
import { useTheme } from '@/contexts';

export const Connecting = ({
onClose,
Expand All @@ -21,7 +20,8 @@
title: string;
subtitle: string;
}) => {
const { theme } = useTheme();
const isDarkMode = document.documentElement.classList.contains('dark');

Check warning on line 24 in components/react/views/Connecting.tsx

View check run for this annotation

Codecov / codecov/patch

components/react/views/Connecting.tsx#L23-L24

Added lines #L23 - L24 were not covered by tests
return (
<div className="mt-3 text-center sm:mt-1.5">
<div className="flex justify-between items-center mb-2">
Expand All @@ -46,9 +46,7 @@
<div className="flex flex-col w-full h-full mt-4 sm:px-8 sm:py-6">
<img
src={
name === 'Cosmos MetaMask Extension'
? '/metamask.svg'
: getRealLogo(logo, theme === 'dark')
name === 'Cosmos MetaMask Extension' ? '/metamask.svg' : getRealLogo(logo, isDarkMode)

Check warning on line 49 in components/react/views/Connecting.tsx

View check run for this annotation

Codecov / codecov/patch

components/react/views/Connecting.tsx#L49

Added line #L49 was not covered by tests
}
alt={name}
className="flex-shrink-0 w-20 h-20 mx-auto aspect-1"
Expand Down
11 changes: 6 additions & 5 deletions components/react/views/Error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { ChevronLeftIcon } from '@heroicons/react/20/solid';
import Image from 'next/image';
import { getRealLogo } from '@/utils';
import { useTheme } from '@/contexts';

export const Error = ({
currentWalletName,
onClose,
Expand All @@ -19,7 +19,8 @@
onReconnect: () => void;
logo: string;
}) => {
const { theme } = useTheme();
const isDarkMode = document.documentElement.classList.contains('dark');

Check warning on line 23 in components/react/views/Error.tsx

View check run for this annotation

Codecov / codecov/patch

components/react/views/Error.tsx#L22-L23

Added lines #L22 - L23 were not covered by tests
return (
<div className="mt-3 text-center sm:mt-1.5">
<div className="flex flex-row items-center justify-between">
Expand Down Expand Up @@ -48,9 +49,9 @@
<div className="p-3 border rounded-full border-red-600 mx-auto aspect-1 flex-shrink-0">
<Image
src={
currentWalletName === 'cosmos-extension-metamask'
currentWalletName === 'Cosmos MetaMask Extension'

Check warning on line 52 in components/react/views/Error.tsx

View check run for this annotation

Codecov / codecov/patch

components/react/views/Error.tsx#L52

Added line #L52 was not covered by tests
? '/metamask.svg'
: getRealLogo(logo, theme === 'dark')
: getRealLogo(logo, isDarkMode)

Check warning on line 54 in components/react/views/Error.tsx

View check run for this annotation

Codecov / codecov/patch

components/react/views/Error.tsx#L54

Added line #L54 was not covered by tests
}
alt="Wallet type logo"
className="flex-shrink-0 w-16 h-16 aspect-1"
Expand All @@ -66,7 +67,7 @@
className="rounded-lg w-[180px] btn btn-error inline-flex justify-center items-center py-2.5 font-medium mt-4 bg-mint mx-auto text-black dark:text-white"
onClick={onReconnect}
>
<ArrowPathIcon className="flex-shrink-0 w-5 h-5 mr-2 text-black dark:text-white" />
<ArrowPathIcon className="flex-shrink-0 w-5 h-5 mr-2 " />

Check warning on line 70 in components/react/views/Error.tsx

View check run for this annotation

Codecov / codecov/patch

components/react/views/Error.tsx#L70

Added line #L70 was not covered by tests
Reconnect
</button>
</div>
Expand Down
8 changes: 3 additions & 5 deletions components/react/views/NotExist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { XMarkIcon, ArrowDownTrayIcon } from '@heroicons/react/24/outline';
import { ChevronLeftIcon } from '@heroicons/react/20/solid';
import { getRealLogo } from '@/utils';
import { useTheme } from '@/contexts';

export const NotExist = ({
onClose,
Expand All @@ -18,7 +17,8 @@
logo: string;
name: string;
}) => {
const { theme } = useTheme();
const isDarkMode = document.documentElement.classList.contains('dark');

Check warning on line 21 in components/react/views/NotExist.tsx

View check run for this annotation

Codecov / codecov/patch

components/react/views/NotExist.tsx#L20-L21

Added lines #L20 - L21 were not covered by tests
return (
<div className="mt-3 text-center sm:mt-1.5">
<div className="flex justify-between items-center mb-2">
Expand All @@ -43,9 +43,7 @@
<div className="flex flex-col w-full h-full py-6 mt-4 sm:px-8">
<img
src={
name === 'Cosmos MetaMask Extension'
? '/metamask.svg'
: getRealLogo(logo, theme === 'dark')
name === 'Cosmos MetaMask Extension' ? '/metamask.svg' : getRealLogo(logo, isDarkMode)

Check warning on line 46 in components/react/views/NotExist.tsx

View check run for this annotation

Codecov / codecov/patch

components/react/views/NotExist.tsx#L46

Added line #L46 was not covered by tests
}
alt={name}
className="flex-shrink-0 w-16 h-16 mx-auto aspect-1"
Expand Down
54 changes: 0 additions & 54 deletions components/react/views/QRCode.tsx

This file was deleted.

Loading
Loading