Skip to content

Commit

Permalink
Merge branch 'jribbink/wip-connect' into jribbink/features
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Aug 7, 2024
2 parents 4ace03a + 58eb262 commit bc7ca77
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 50 deletions.
16 changes: 9 additions & 7 deletions components/QRCode.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Box, Flex, Image } from '@chakra-ui/react'
import { Fragment, memo } from 'react'
import { Fragment, memo, useMemo } from 'react'
import QRCodeLib from 'qrcode'
import NextImage, { StaticImageData } from 'next/image'
import { isDataURL } from '../helpers/urls'
Expand Down Expand Up @@ -30,18 +30,20 @@ const QRCode = memo(function QRCode({
(x < 8 && (y < 8 || y >= length - 8)) || (x >= length - 8 && y < 8)

// Compute the scale factor so we can determine which modules are behind the image
const sizePixels = useMemo(() => convertToPixels(size), [size])
const imageSizePixels = useMemo(() => convertToPixels(imageSize), [imageSize])
let isModuleBehindImage: (x: number, y: number) => boolean = () => false
if (image) {
const scaleFactor = length / convertToPixels(size)
const imageViewboxSize = convertToPixels(imageSize) * scaleFactor
const scaleFactor = length / sizePixels
const imageViewboxSize = imageSizePixels * scaleFactor
const imagePadding = 1
const imageTop = Math.floor((length - imageViewboxSize) / 2 - imagePadding)
const imageBottom = Math.ceil(
imageTop + imageViewboxSize + imagePadding * 2
imageTop + imageViewboxSize + imagePadding * 2,
)
const imageLeft = Math.floor((length - imageViewboxSize) / 2 - imagePadding)
const imageRight = Math.ceil(
imageLeft + imageViewboxSize + imagePadding * 2
imageLeft + imageViewboxSize + imagePadding * 2,
)
isModuleBehindImage = (x: number, y: number) => {
return (
Expand All @@ -54,7 +56,7 @@ const QRCode = memo(function QRCode({
modules.push(
[...modules1D.slice(i * length, (i + 1) * length)].map((bit, j) => {
return bit && !isFindingPattern(i, j) && !isModuleBehindImage(i, j)
})
}),
)
}

Expand Down Expand Up @@ -82,7 +84,7 @@ const QRCode = memo(function QRCode({
r={0.4}
fill={cell ? 'black' : 'white'}
/>
))
)),
)}

{findingPatternPositions.map(([x, y]) => (
Expand Down
7 changes: 4 additions & 3 deletions helpers/__tests__/wallets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ describe('wallets helpers', () => {
uid: `${walletId}#authnpost`,
endpoint: `http://localhost:8701/api/authn`,
provider: {},
walletUid: walletId,
},
] as ServiceWithWallet[]

const servicesPipe = jest.fn(() => clone(mockResult))
const servicesPipe = jest.fn(() => [
{ ...mockResult[0], walletUid: walletId },
])
const makeServicesPipe = jest.fn(() => servicesPipe)

const res = pipeWalletServices(makeServicesPipe)(wallets)
Expand All @@ -47,7 +48,7 @@ describe('wallets helpers', () => {
...y,
walletUid: x.uid,
})),
[] as ServiceWithWallet[]
[] as ServiceWithWallet[],
),
],
])
Expand Down
2 changes: 1 addition & 1 deletion helpers/sizes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export function convertToPixels(
size: string,
context = document.documentElement
context = document.documentElement,
) {
// Create a temporary element
const tempEl = document.createElement('div')
Expand Down
18 changes: 9 additions & 9 deletions helpers/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const pipeWalletServices =
pipe(
extractWalletServices,
makeServicesPipe({ wallets }),
collectWalletsFromServices({ wallets })
collectWalletsFromServices({ wallets }),
)(wallets)

export const extractWalletServices = (wallets: Wallet[]): ServiceWithWallet[] =>
Expand All @@ -21,7 +21,7 @@ export const extractWalletServices = (wallets: Wallet[]): ServiceWithWallet[] =>
...wallet.services.map(service => ({
...service,
walletUid: wallet.uid,
}))
})),
)
return acc
}, [])
Expand All @@ -39,21 +39,21 @@ export const walletsForNetwork =
export const collectWalletsFromServices =
({ wallets }: { wallets: Wallet[] }) =>
(services: ServiceWithWallet[]) =>
services.reduce((acc, service) => {
services.reduce((acc, _service) => {
// Append service without reference to wallet
const { walletUid, ...service } = _service
const existingWalletIdx = acc.findIndex(
wallet => wallet.uid === service.walletUid
wallet => wallet.uid === walletUid,
)

if (existingWalletIdx === -1) {
const wallet = wallets.find(wallet => wallet.uid === service.walletUid)
const wallet = wallets.find(wallet => wallet.uid === walletUid)
acc.push({
...wallet,
services: [service],
})
} else {
// Append service without reference to wallet
const { walletUid, ...serviceWithoutWallet } = service
acc[existingWalletIdx].services.push(serviceWithoutWallet)
acc[existingWalletIdx].services.push(service)
}
return acc
}, [])
Expand All @@ -79,7 +79,7 @@ export function extractAllServicesWithProvider(wallets: Wallet[]) {
...walletToProvider(wallet),
...service.provider,
},
}))
})),
)
return acc
}, [])
Expand Down
32 changes: 4 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@
"framer-motion": "^8.4.6",
"mixpanel": "^0.14.0",
"next": "^14.2.4",
"qrcode": "^1.5.3",
"qrcode": "^1.5.4",
"rambda": "^9.2.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-icons": "^4.7.1",
"react-qr-code": "^2.0.15",
"swr": "^1.3.0"
},
"devDependencies": {
Expand Down

0 comments on commit bc7ca77

Please sign in to comment.