-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathprob-revshare.tsx
More file actions
209 lines (194 loc) · 6.21 KB
/
prob-revshare.tsx
File metadata and controls
209 lines (194 loc) · 6.21 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { useCallback, useMemo, useState } from 'react'
import { useNavigate, type MetaFunction } from 'react-router'
import {
Card,
CodeBlockLink,
HeadingCore,
ImportTagModal,
RevShareChart,
RevShareInfo,
ShareInput,
ShareInputHeader,
ShareInputTable,
ToolsPrimaryButton,
ToolsSecondaryButton
} from '@/components'
import { API_URL } from '@shared/defines'
import { Heading5 } from '../components/redesign/Typography'
import {
changeList,
dropIndex,
sharesToPaymentPointer,
tagOrPointerToShares,
validateShares
} from '../lib/revshare'
import { newShare, SharesProvider, useShares } from '../stores/revshareStore'
export const meta: MetaFunction = () => {
return [
{ title: 'Probabilistic Revshare - Publisher Tools' },
{
name: 'description',
content:
'Create a probabilistic revenue share to split Web Monetization earnings among multiple recipients.'
}
]
}
const baseUrl = new URL('/revshare/', API_URL).href
export default function RevsharePageWrapper() {
return (
<SharesProvider>
<Revshare />
</SharesProvider>
)
}
function Revshare() {
const navigate = useNavigate()
const [isModalOpen, setIsModalOpen] = useState(false)
const [importTag, setImportTag] = useState('')
const [importError, setImportError] = useState('')
const { shares, setShares } = useShares()
const showDeleteColumn = shares.length > 2
const revShareUrl = useMemo(
() => sharesToPaymentPointer(shares, baseUrl) ?? '',
[shares]
)
const totalWeight = useMemo(
() => shares.reduce((a, b) => a + Number(b.weight), 0),
[shares]
)
const hasValidShares = validateShares(shares)
const addShare = useCallback(() => {
setShares((prevShares) => [...prevShares, newShare()])
}, [setShares])
const handleRemove = useCallback(
(index: number) => {
setShares((prevShares) => dropIndex(prevShares, index))
},
[setShares]
)
const handleChangeName = useCallback(
(index: number, name: string) => {
setShares((prevShares) => changeList(prevShares, index, { name }))
},
[setShares]
)
const handleChangePointer = useCallback(
(index: number, pointer: string) => {
setShares((prevShares) =>
changeList(prevShares, index, { pointer: pointer.trim() })
)
},
[setShares]
)
const handleChangeWeight = useCallback(
(index: number, weight: number) => {
setShares((prevShares) => changeList(prevShares, index, { weight }))
},
[setShares]
)
const handleValidationChange = useCallback(
(index: number, isValid: boolean) => {
setShares((prevShares) => changeList(prevShares, index, { isValid }))
},
[setShares]
)
const handleLinkTagImport = useCallback(() => {
try {
setImportError('')
const importedShares = tagOrPointerToShares(importTag) || []
setShares(importedShares)
setIsModalOpen(false)
setImportTag('')
} catch (err: unknown) {
setImportError((err as Error).message || 'An unexpected error occurred.')
}
}, [importTag, setShares, setIsModalOpen])
const handleCloseModal = useCallback(() => {
setIsModalOpen(false)
setImportError('')
setImportTag('')
}, [setIsModalOpen, setImportError, setImportTag])
return (
<div className="bg-interface-bg-main w-full px-md">
<div className="max-w-[1280px] mx-auto pt-[60px] md:pt-3xl">
<HeadingCore
title="Probabilistic Revenue Share"
onBackClick={() => navigate('/')}
>
Probabilistic revenue sharing is a way to share a portion of a web
monetized page's earnings between multiple recipients.
<br />
Each time a web monetized user visits the page, a recipient will be
chosen at random.
<br />
Payments will go to the chosen recipient until the page is closed or
reloaded.
</HeadingCore>
<Card>
<Heading5>Recipients</Heading5>
<ShareInputTable>
<ShareInputHeader showDelete={showDeleteColumn} />
<div role="rowgroup" className="contents">
{shares.map((share, i) => {
return (
<ShareInput
key={share.id}
index={i}
name={share.name || ''}
pointer={share.pointer}
weight={share.weight || 0}
percent={
totalWeight > 0 ? (share.weight || 0) / totalWeight : 0
}
onChangeName={(name) => handleChangeName(i, name)}
onChangePointer={(pointer) =>
handleChangePointer(i, pointer)
}
onChangeWeight={(weight) => handleChangeWeight(i, weight)}
onValidationChange={handleValidationChange}
onRemove={() => handleRemove(i)}
showDelete={showDeleteColumn}
weightDisabled={!share.pointer}
/>
)
})}
</div>
</ShareInputTable>
<ToolsPrimaryButton
icon="share"
iconPosition="right"
className="self-center w-full md:w-fit"
onClick={addShare}
>
+ Add recipient
</ToolsPrimaryButton>
<hr className={!hasValidShares ? 'md:mt-2xs' : ''} />
<div className="flex flex-col-reverse md:flex-col gap-md">
{hasValidShares && <CodeBlockLink link={revShareUrl} />}
<ToolsSecondaryButton
className="self-end w-full md:w-fit"
onClick={() => setIsModalOpen(true)}
>
Import
</ToolsSecondaryButton>
</div>
</Card>
<div className="my-lg md:my-md">
{hasValidShares && <RevShareChart shares={shares} />}
</div>
<RevShareInfo />
</div>
{isModalOpen && (
<ImportTagModal
isOpen={isModalOpen}
onClose={handleCloseModal}
onConfirm={handleLinkTagImport}
tag={importTag}
setTag={setImportTag}
errorMessage={importError}
setImportError={setImportError}
/>
)}
</div>
)
}