-
Notifications
You must be signed in to change notification settings - Fork 310
/
LensPopup.tsx
83 lines (77 loc) · 3 KB
/
LensPopup.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { isEqual, sortBy, uniqBy } from 'lodash-es'
import { memo, useEffect, useRef, useState } from 'react'
import { ShadowRootPopper, makeStyles } from '@masknet/theme'
import { NextIDProof } from '@masknet/web3-providers'
import type { FireflyBaseAPI, NextIDBaseAPI } from '@masknet/web3-providers/types'
import { Fade } from '@mui/material'
import { emitter } from '../emitter.js'
import { useControlLensPopup } from '../hooks/Lens/useControlLensPopup.js'
import { LensList } from './LensList.js'
const useStyles = makeStyles()((theme) => ({
popup: {
position: 'absolute',
zIndex: 99,
borderRadius: 16,
boxShadow:
theme.palette.mode === 'light' ?
'0px 4px 30px rgba(0, 0, 0, 0.1)'
: '0px 4px 30px rgba(255, 255, 255, 0.15)',
},
}))
export const NextIdLensToFireflyLens = (account: NextIDBaseAPI.LensAccount): FireflyBaseAPI.LensAccount => {
return {
address: account.address,
name: account.displayName,
handle: account.handle,
bio: '',
url: '',
profileUri: [],
}
}
export const LensPopup = memo(() => {
const { classes } = useStyles()
const holderRef = useRef<HTMLDivElement>(null)
const [lens, setLens] = useState<FireflyBaseAPI.LensAccount[]>([])
const active = useControlLensPopup(holderRef)
const [anchorEl, setAnchorEl] = useState<HTMLElement | null>()
const anchorElRef = useRef<HTMLElement | null>()
useEffect(() => {
const unsubscribeOpen = emitter.on('open', async ({ lensAccounts, popupAnchorEl }) => {
setLens((oldAccounts) => {
return isEqual(oldAccounts, lensAccounts) ? oldAccounts : lensAccounts
})
setAnchorEl(popupAnchorEl)
anchorElRef.current = popupAnchorEl
if (!lens[0]?.handle) return
const accounts = await NextIDProof.queryAllLens(lens[0].handle)
if (!accounts.length) return
setLens((oldAccounts) => {
if (accounts.length <= oldAccounts.length) return oldAccounts
const merged = uniqBy([...oldAccounts, ...accounts.map(NextIdLensToFireflyLens)], (x) => x.handle)
return sortBy(merged, [(x) => -accounts.findIndex((y) => x.handle === y.handle)])
})
})
const unsubscribeClose = emitter.on('close', ({ popupAnchorEl }) => {
if (popupAnchorEl !== anchorElRef.current) return
setAnchorEl(null)
})
return () => {
unsubscribeOpen()
unsubscribeClose()
}
}, [])
return (
<Fade in={active} easing="linear" timeout={250}>
<ShadowRootPopper
placeholder={undefined}
open={!!anchorEl}
anchorEl={anchorEl}
keepMounted
className={classes.popup}
ref={holderRef}>
<LensList accounts={lens} />
</ShadowRootPopper>
</Fade>
)
})
LensPopup.displayName = 'LensPopup'