-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathuse-edit-top-eight.ts
More file actions
125 lines (102 loc) · 4.17 KB
/
use-edit-top-eight.ts
File metadata and controls
125 lines (102 loc) · 4.17 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { toast } from 'sonner'
import { useAccount } from 'wagmi'
import { isAddress, type Address } from 'viem'
import { useTranslation } from 'react-i18next'
import { useEffect, useMemo, useState } from 'react'
import { fetchFollowState, useTransactions } from '@encrypteddegen/identity-kit'
import { useCart } from '#/hooks/use-cart'
import { resolveEnsAddress } from '#/utils/ens'
import type { TagListOp } from '#/types/list-op'
import type { TopEightProfileType } from './use-top-eight'
import { useEFPProfile } from '#/contexts/efp-profile-context'
import { isTagListOp, listOpAddTag, listOpAddListRecord, extractAddressAndTag } from '#/utils/list-ops'
let prevTopEightInCart: { address: Address }[] = []
export const useEditTopEight = (profiles: TopEightProfileType[]) => {
const [loadingItems, setLoadingItems] = useState(0)
const { t } = useTranslation()
const { address: userAddress } = useAccount()
const { roles, selectedList } = useEFPProfile()
const { setTxModalOpen } = useTransactions()
const { cart, addToCart, hasListOpRemoveRecord } = useCart()
const topEightInCart = useMemo(
() =>
cart
.filter((listOp) => listOp.opcode === 3 && isTagListOp(listOp) && extractAddressAndTag(listOp).tag === 'top8')
.map((listOp) => ({
address: extractAddressAndTag(listOp as TagListOp).address,
})),
[cart]
)
const [editedProfiles, setEditedProfiles] = useState([...profiles, ...topEightInCart])
const currentTopEightLength = useMemo(() => {
const topEightRemoved = cart.filter(
(listOp) => listOp.opcode === 4 && isTagListOp(listOp) && extractAddressAndTag(listOp).tag === 'top8'
)
const removedProfiles = profiles.filter((profile) => hasListOpRemoveRecord(profile.address))
return editedProfiles.length - topEightRemoved.length - removedProfiles.length
}, [editedProfiles, cart])
const canConfirm = currentTopEightLength <= 8
const isTopEightFull = currentTopEightLength >= 8
useEffect(() => {
if (prevTopEightInCart.length !== topEightInCart.length) {
setEditedProfiles([...profiles, ...topEightInCart])
prevTopEightInCart = topEightInCart
}
}, [topEightInCart])
const getFollowingState = async (address: Address) => {
if (!userAddress) return 'none'
const followingStatus = await fetchFollowState({
lookupAddressOrName: address,
connectedAddress: userAddress,
list: selectedList,
type: 'following',
})
if (!followingStatus) return 'none'
if (followingStatus.state.block) return 'blocks'
if (followingStatus.state.mute) return 'mutes'
if (followingStatus.state.follow) return 'follows'
return 'none'
}
const addProfileToCart = async (user: string) => {
if (!roles?.isManager) {
toast.error(t('not manager'))
return
}
setLoadingItems((prevLoading) => prevLoading + 1)
const address = isAddress(user) ? user : await resolveEnsAddress(user)
if (editedProfiles.find((profile) => profile.address.toLowerCase() === address?.toLowerCase()))
return setLoadingItems((prevLoading) => (prevLoading > 0 ? prevLoading - 1 : prevLoading))
if (!address) return { unresolved: true }
const followState = await getFollowingState(address)
const addCartItems = [listOpAddTag(address, 'top8')]
if (followState === 'none') addCartItems.push(listOpAddListRecord(address))
addToCart(addCartItems)
}
const [addProfileSearch, setAddProfileSearch] = useState('')
const onSubmit = async () => {
if (isTopEightFull) return toast.error(t('top eight limit'))
if (!roles?.isManager) return toast.error(t('not manager'))
setAddProfileSearch('')
const addedToCart = await addProfileToCart(addProfileSearch)
if (addedToCart?.unresolved) toast.error(`${t('unresolved')} ${addProfileSearch}`)
setLoadingItems(0)
}
const onConfirm = async () => {
if (!canConfirm) return
if (!roles?.isManager) return toast.error(t('not manager'))
if (topEightInCart.length > 0) {
setTxModalOpen(true)
}
}
return {
onSubmit,
onConfirm,
canConfirm,
loadingItems,
topEightInCart,
isTopEightFull,
editedProfiles,
addProfileSearch,
setAddProfileSearch,
}
}