-
-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Expand file tree
/
Copy pathmembers-list-item.tsx
More file actions
316 lines (288 loc) · 10.1 KB
/
Copy pathmembers-list-item.tsx
File metadata and controls
316 lines (288 loc) · 10.1 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import moment from 'moment-timezone';
import {Avatar, TableCell, TableRow} from '@tryghost/shade/components';
import {Member} from '@tryghost/admin-x-framework/api/members';
import {buildMemberDetailPath} from '../member-detail-hash';
import {cn} from '@tryghost/shade/utils';
import {getActiveColumnValue} from '../member-query-params';
import type {ActiveColumn} from '../member-query-params';
import type {CSSProperties} from 'react';
import type {MemberTableColumnStyles} from './member-table-layout';
const PINNED_EDGE_FADE_POSITION_STYLE = {
left: '100%'
} as CSSProperties;
// --- Helpers ---
function formatLocation(geolocation: Member['geolocation']): {
text: string;
isKnown: boolean;
} {
if (!geolocation) {
return {text: 'Unknown', isKnown: false};
}
try {
const parsed = JSON.parse(geolocation) as {
country?: string;
region?: string;
country_code?: string;
};
if (!parsed.country) {
return {text: 'Unknown', isKnown: false};
}
// For US, show "State, US"
if (parsed.country_code === 'US' && parsed.region) {
return {text: `${parsed.region}, US`, isKnown: true};
}
return {text: parsed.country, isKnown: true};
} catch {
return {text: 'Unknown', isKnown: false};
}
}
function getStatusLabel(status: Member['status']): string {
switch (status) {
case 'paid':
return 'Paid';
case 'comped':
return 'Complimentary';
case 'gift':
return 'Gift';
default:
return 'Free';
}
}
function isModifiedClick(event: Pick<React.MouseEvent<HTMLElement>, 'button' | 'metaKey' | 'ctrlKey' | 'shiftKey' | 'altKey'>) {
return event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey;
}
function openMemberInNewTab(memberId: string, backPath?: string) {
window.open(`#${buildMemberDetailPath(memberId, backPath)}`, '_blank', 'noopener');
}
// --- Sub-components ---
function MembersListItemName({item, backPath, onClick}: { item: Member; backPath?: string; onClick?: (memberId: string) => void }) {
return (
<div className="flex min-w-0 items-center gap-3">
<Avatar
className="size-8 min-w-8"
email={item.email}
name={item.name}
src={item.avatar_image}
/>
<div className="min-w-0">
<a
className="block min-w-0 cursor-pointer"
href={`#${buildMemberDetailPath(item.id, backPath)}`}
onClick={onClick ? (e) => {
if (isModifiedClick(e)) {
e.stopPropagation();
return;
}
e.preventDefault();
e.stopPropagation();
onClick(item.id);
} : undefined}
>
<span className="block truncate text-md font-semibold">
{item.name || item.email || 'Anonymous'}
</span>
</a>
{item.name && item.email && (
<div
className="truncate text-muted-foreground"
data-testid="member-email"
>
{item.email}
</div>
)}
</div>
</div>
);
}
function MembersListItemStatus({
status,
tiers
}: {
status: Member['status'];
tiers?: Member['tiers'];
}) {
const tierNames = tiers?.map(t => t.name).join(', ');
return (
<div className="flex min-w-0 justify-start">
<div className="min-w-0">
<div className="truncate">{getStatusLabel(status)}</div>
{tierNames && (
<div className="truncate text-sm text-muted-foreground">
{tierNames}
</div>
)}
</div>
</div>
);
}
function MembersListItemOpenRate({
emailOpenRate
}: {
emailOpenRate: number | null | undefined;
}) {
const isKnown = emailOpenRate !== null && emailOpenRate !== undefined;
return (
<div
className={cn('text-base', isKnown ? 'text-foreground' : 'text-muted-foreground')}
>
{isKnown ? `${Math.round(emailOpenRate)}%` : 'N/A'}
</div>
);
}
function MembersListItemLocation({
geolocation
}: {
geolocation: Member['geolocation'];
}) {
const location = formatLocation(geolocation);
return (
<div
className={cn('truncate text-base', location.isKnown ? 'text-foreground' : 'text-muted-foreground')}
>
{location.text}
</div>
);
}
function MembersListItemCreated({createdAt}: { createdAt: string }) {
return (
<div>
<div className="text-base">
{moment.utc(createdAt).format('D MMM YYYY')}
</div>
<div className="text-base text-muted-foreground">
{moment.utc(createdAt).fromNow()}
</div>
</div>
);
}
function MembersListItemDynamicColumn({
column,
member,
timezone
}: {
column: ActiveColumn;
member: Member;
timezone: string;
}) {
const value = getActiveColumnValue(column, member, timezone);
if (!value) {
return (
<span className="text-base text-muted-foreground">-</span>
);
}
return (
<div className="min-w-0">
<div className="truncate text-base">{value.text}</div>
{value.subtext && (
<div className="truncate text-base text-muted-foreground">
{value.subtext}
</div>
)}
</div>
);
}
// --- Main component ---
interface MembersListItemProps {
item: Member;
activeColumns: ActiveColumn[];
backPath?: string;
columnStyles: MemberTableColumnStyles;
showPinnedEdge: boolean;
showEmailOpenRate: boolean;
timezone: string;
onClick: (memberId: string) => void;
}
function MembersListItem({
item,
activeColumns,
backPath,
columnStyles,
showPinnedEdge,
showEmailOpenRate,
timezone,
onClick,
...props
}: MembersListItemProps &
Omit<React.HTMLAttributes<HTMLTableRowElement>, 'onClick'>) {
const handleRowClick = (event: React.MouseEvent<HTMLTableRowElement>) => {
if (isModifiedClick(event)) {
openMemberInNewTab(item.id, backPath);
return;
}
onClick(item.id);
};
const handleRowAuxClick = (event: React.MouseEvent<HTMLTableRowElement>) => {
if (event.button !== 1) {
return;
}
event.preventDefault();
openMemberInNewTab(item.id, backPath);
};
return (
<TableRow
{...props}
className={cn('group cursor-pointer', props.className)}
data-testid="members-list-item"
onAuxClick={handleRowAuxClick}
onClick={handleRowClick}
>
<TableCell className={cn(
'min-w-0 bg-background px-4 py-3 group-hover:bg-[var(--members-sticky-hover-bg)] max-sm:!w-full max-sm:!min-w-0 lg:sticky lg:left-0 lg:z-20'
)}>
<MembersListItemName backPath={backPath} item={item} onClick={onClick} />
{showPinnedEdge && (
<>
<div
aria-hidden="true"
className="pointer-events-none absolute inset-y-px hidden w-[24px] group-hover:opacity-0 lg:block"
style={{
...PINNED_EDGE_FADE_POSITION_STYLE,
background: 'linear-gradient(to right, var(--background) 0px, color-mix(in hsl, var(--background) 78%, transparent) 6px, color-mix(in hsl, var(--background) 28%, transparent) 16px, transparent 24px)'
}}
/>
<div
aria-hidden="true"
className="pointer-events-none absolute inset-y-px hidden w-[24px] opacity-0 group-hover:opacity-100 lg:block"
style={{
...PINNED_EDGE_FADE_POSITION_STYLE,
background: 'linear-gradient(to right, var(--members-sticky-hover-bg) 0px, color-mix(in hsl, var(--members-sticky-hover-bg) 78%, transparent) 6px, color-mix(in hsl, var(--members-sticky-hover-bg) 28%, transparent) 16px, transparent 24px)'
}}
/>
</>
)}
</TableCell>
<TableCell className="hidden px-4 py-3 sm:table-cell" style={columnStyles.status}>
<MembersListItemStatus status={item.status} tiers={item.tiers} />
</TableCell>
{showEmailOpenRate && (
<TableCell className="hidden px-4 py-3 lg:table-cell" style={columnStyles.openRate}>
<MembersListItemOpenRate emailOpenRate={item.email_open_rate} />
</TableCell>
)}
<TableCell className="hidden px-4 py-3 lg:table-cell" style={columnStyles.location}>
<MembersListItemLocation geolocation={item.geolocation} />
</TableCell>
<TableCell className="hidden px-4 py-3 lg:table-cell" style={columnStyles.created}>
<MembersListItemCreated createdAt={item.created_at} />
</TableCell>
{activeColumns.map(col => (
<TableCell key={col.key} className="hidden px-4 py-3 lg:table-cell" style={columnStyles.dynamic}>
<MembersListItemDynamicColumn
column={col}
member={item}
timezone={timezone}
/>
</TableCell>
))}
</TableRow>
);
}
export default MembersListItem;
export {
MembersListItemName,
MembersListItemStatus,
MembersListItemOpenRate,
MembersListItemLocation,
MembersListItemCreated,
MembersListItemDynamicColumn
};