-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.tsx
More file actions
598 lines (562 loc) · 17 KB
/
Copy pathindex.tsx
File metadata and controls
598 lines (562 loc) · 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/**
* WorkOS Users admin page.
*
* Mounts onto #workos-users-admin-root. Read-only list of WorkOS users
* with cursor pagination + email search; each row links to the WorkOS
* Dashboard where admins can re-enable a suppressed email (no public API
* for that action yet).
*/
import {
createRoot,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { promptModal } from '../shared/modal';
import './styles.css';
interface AdminConfig {
restUrl: string;
/** Base for the change-email endpoint: `${changeEmailUrl}{id}/email-change`. */
changeEmailUrl: string;
nonce: string;
environment: string;
environmentId: string;
dashboardBaseUrl: string;
defaultLimit: number;
pluginEnabled: boolean;
changeEmailEnabled: boolean;
}
declare global {
interface Window {
workosUsersAdmin?: AdminConfig;
}
}
interface WorkosUser {
id: string;
/** Linked WP user id, or 0 if this WorkOS user has no WP counterpart. */
wp_user_id: number;
email: string;
email_verified: boolean;
first_name: string;
last_name: string;
last_sign_in_at: string;
created_at: string;
updated_at: string;
dashboard_url: string;
}
interface ListMetadata {
before: string | null;
after: string | null;
}
interface ListResponse {
data: WorkosUser[];
list_metadata: ListMetadata;
error?: string;
}
interface ApiResult< T > {
ok: boolean;
status: number;
data: T;
}
const PAGE_SIZE_OPTIONS = [ 10, 25, 50, 100 ] as const;
function getConfig(): AdminConfig {
return (
window.workosUsersAdmin || {
restUrl: '/wp-json/workos/v1/admin/users',
changeEmailUrl: '/wp-json/workos/v1/users/',
nonce: '',
environment: '',
environmentId: '',
dashboardBaseUrl: 'https://dashboard.workos.com',
defaultLimit: 25,
pluginEnabled: false,
changeEmailEnabled: false,
}
);
}
async function apiCall< T >( query: Record< string, string | number > ): Promise< ApiResult< T > > {
const cfg = getConfig();
const search = new URLSearchParams();
for ( const [ key, value ] of Object.entries( query ) ) {
if ( value === '' || value === null || value === undefined ) {
continue;
}
search.append( key, String( value ) );
}
const url = `${ cfg.restUrl }${ search.toString() ? `?${ search.toString() }` : '' }`;
const res = await fetch( url, {
method: 'GET',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': cfg.nonce,
},
} );
const data = await res.json().catch( () => ( {} ) );
return { ok: res.ok, status: res.status, data: data as T };
}
function formatRelative( iso: string ): string {
if ( ! iso ) {
return '—';
}
const date = new Date( iso );
if ( Number.isNaN( date.getTime() ) ) {
return '—';
}
const diffMs = Date.now() - date.getTime();
const absSec = Math.abs( diffMs ) / 1000;
const future = diffMs < 0;
const units: Array< { limit: number; div: number; one: string; many: string } > = [
{ limit: 60, div: 1, one: __( '%d second', 'integration-workos' ), many: __( '%d seconds', 'integration-workos' ) },
{ limit: 3600, div: 60, one: __( '%d minute', 'integration-workos' ), many: __( '%d minutes', 'integration-workos' ) },
{ limit: 86400, div: 3600, one: __( '%d hour', 'integration-workos' ), many: __( '%d hours', 'integration-workos' ) },
{ limit: 2592000, div: 86400, one: __( '%d day', 'integration-workos' ), many: __( '%d days', 'integration-workos' ) },
{ limit: 31536000, div: 2592000, one: __( '%d month', 'integration-workos' ), many: __( '%d months', 'integration-workos' ) },
];
for ( const u of units ) {
if ( absSec < u.limit ) {
const n = Math.max( 1, Math.floor( absSec / u.div ) );
const tpl = n === 1 ? u.one : u.many;
return future
? sprintf( __( 'in %s', 'integration-workos' ), sprintf( tpl, n ) )
: sprintf( __( '%s ago', 'integration-workos' ), sprintf( tpl, n ) );
}
}
const years = Math.max( 1, Math.floor( absSec / 31536000 ) );
const tpl = years === 1
? __( '%d year', 'integration-workos' )
: __( '%d years', 'integration-workos' );
return future
? sprintf( __( 'in %s', 'integration-workos' ), sprintf( tpl, years ) )
: sprintf( __( '%s ago', 'integration-workos' ), sprintf( tpl, years ) );
}
function fullName( user: WorkosUser ): string {
const parts = [ user.first_name, user.last_name ].filter( Boolean );
return parts.length ? parts.join( ' ' ) : '—';
}
function App(): JSX.Element {
const cfg = getConfig();
const [ users, setUsers ] = useState< WorkosUser[] >( [] );
const [ metadata, setMetadata ] = useState< ListMetadata >( { before: null, after: null } );
const [ loading, setLoading ] = useState< boolean >( false );
const [ error, setError ] = useState< string >( '' );
const [ actionNotice, setActionNotice ] = useState< {
kind: 'success' | 'error';
text: string;
} | null >( null );
const [ busyUserId, setBusyUserId ] = useState< number >( 0 );
const [ searchInput, setSearchInput ] = useState< string >( '' );
const [ search, setSearch ] = useState< string >( '' );
const [ limit, setLimit ] = useState< number >( cfg.defaultLimit || 25 );
const [ cursor, setCursor ] = useState< { after?: string; before?: string } >( {} );
// Stack of `after` cursors we've passed through, so Prev can rewind without
// the upstream `before` cursor (WorkOS pagination is one-way per direction).
const cursorStack = useRef< string[] >( [] );
const fetchSeq = useRef< number >( 0 );
const load = useCallback( async () => {
setLoading( true );
setError( '' );
const seq = ++fetchSeq.current;
const query: Record< string, string | number > = { limit };
if ( search ) {
query.email = search;
}
if ( cursor.after ) {
query.after = cursor.after;
} else if ( cursor.before ) {
query.before = cursor.before;
}
const res = await apiCall< ListResponse >( query );
// Drop stale responses if a newer fetch has started.
if ( seq !== fetchSeq.current ) {
return;
}
if ( ! res.ok ) {
setLoading( false );
setError(
res.data?.error ||
sprintf(
__( 'Failed to load users (status %d).', 'integration-workos' ),
res.status
)
);
setUsers( [] );
setMetadata( { before: null, after: null } );
return;
}
if ( res.data.error ) {
setError( res.data.error );
}
setUsers( Array.isArray( res.data.data ) ? res.data.data : [] );
setMetadata( res.data.list_metadata || { before: null, after: null } );
setLoading( false );
}, [ limit, search, cursor ] );
useEffect( () => {
void load();
}, [ load ] );
// Debounce the search input → committed search term (300ms).
useEffect( () => {
const handle = window.setTimeout( () => {
setSearch( ( current ) => {
if ( current === searchInput ) {
return current;
}
cursorStack.current = [];
setCursor( {} );
return searchInput;
} );
}, 300 );
return () => window.clearTimeout( handle );
}, [ searchInput ] );
const handleNext = (): void => {
if ( ! metadata.after ) {
return;
}
cursorStack.current.push( metadata.after );
setCursor( { after: metadata.after } );
};
const handlePrev = (): void => {
// Drop the cursor that took us to the current page, then use the
// previous one (or reset if we're at the start).
cursorStack.current.pop();
const prev = cursorStack.current[ cursorStack.current.length - 1 ];
setCursor( prev ? { after: prev } : {} );
};
const handleLimitChange = ( value: number ): void => {
cursorStack.current = [];
setCursor( {} );
setLimit( value );
};
const handleChangeEmail = useCallback(
async ( user: WorkosUser ): Promise< void > => {
setActionNotice( null );
const newEmail = await promptModal( {
title: __( 'Change email address', 'integration-workos' ),
message: sprintf(
/* translators: %s: the user's current email address. */
__(
'Set a new email for %s. This updates WorkOS and the portal immediately — no verification email is sent.',
'integration-workos'
),
user.email || __( 'this user', 'integration-workos' )
),
inputLabel: __( 'New email address', 'integration-workos' ),
inputType: 'email',
placeholder: __( 'name@example.com', 'integration-workos' ),
confirmLabel: __( 'Change email', 'integration-workos' ),
cancelLabel: __( 'Cancel', 'integration-workos' ),
variant: 'danger',
validate: ( value: string ) =>
/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test( value )
? null
: __( 'Please enter a valid email address.', 'integration-workos' ),
} );
if ( ! newEmail ) {
return;
}
setBusyUserId( user.wp_user_id );
try {
const res = await fetch(
`${ cfg.changeEmailUrl }${ user.wp_user_id }/email-change`,
{
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': cfg.nonce,
},
body: JSON.stringify( { new_email: newEmail } ),
}
);
const data = await res.json().catch( () => ( {} ) );
if ( ! res.ok ) {
setActionNotice( {
kind: 'error',
text:
data?.message ||
__(
'Could not change the email. Please try again.',
'integration-workos'
),
} );
return;
}
// An admin acting on their own account falls into the verified
// flow (no `committed`), so branch on the response — don't
// assume a commit, or we'd rewrite the row for a change that
// hasn't landed.
if ( data.no_op ) {
setActionNotice( {
kind: 'success',
text: __(
'That is already this user’s email.',
'integration-workos'
),
} );
} else if ( data.committed ) {
const updatedEmail = ( data.email as string ) || newEmail;
// Reflect the committed change in place — the row's email is
// now the new address, and it's unverified until the user
// verifies it in WorkOS.
setUsers( ( prev ) =>
prev.map( ( u ) =>
u.id === user.id
? { ...u, email: updatedEmail, email_verified: false }
: u
)
);
setActionNotice( {
kind: 'success',
text: sprintf(
/* translators: %s: the new email address. */
__( 'Email changed to %s.', 'integration-workos' ),
updatedEmail
),
} );
} else {
setActionNotice( {
kind: 'success',
text: sprintf(
/* translators: %s: the masked new email address. */
__(
'Verification email sent to %s.',
'integration-workos'
),
( data.masked_new_email as string ) || newEmail
),
} );
}
} catch ( _err ) {
setActionNotice( {
kind: 'error',
text: __(
'Could not change the email. Please try again.',
'integration-workos'
),
} );
} finally {
setBusyUserId( 0 );
}
},
[ cfg.changeEmailUrl, cfg.nonce ]
);
const hasPrev = cursorStack.current.length > 0;
const hasNext = Boolean( metadata.after );
const columns = useMemo(
() => [
__( 'Email', 'integration-workos' ),
__( 'Name', 'integration-workos' ),
__( 'Last sign-in', 'integration-workos' ),
__( 'Created', 'integration-workos' ),
__( 'Actions', 'integration-workos' ),
],
[]
);
if ( ! cfg.pluginEnabled ) {
return (
<div className="workos-users-empty">
<p>
{ __(
'WorkOS is not configured. Save your API credentials on the Settings page to enable the user listing.',
'integration-workos'
) }
</p>
</div>
);
}
return (
<div className="workos-users-app">
<div className="workos-users-toolbar">
<input
type="search"
className="workos-users-search"
placeholder={ __( 'Search by email…', 'integration-workos' ) }
value={ searchInput }
onChange={ ( event ) => setSearchInput( event.target.value ) }
aria-label={ __( 'Search WorkOS users by email', 'integration-workos' ) }
/>
<label className="workos-users-pagesize">
<span>{ __( 'Per page', 'integration-workos' ) }</span>
<select
value={ limit }
onChange={ ( event ) => handleLimitChange( Number( event.target.value ) ) }
>
{ PAGE_SIZE_OPTIONS.map( ( size ) => (
<option key={ size } value={ size }>
{ size }
</option>
) ) }
</select>
</label>
<div className="workos-users-env">
{ sprintf(
__( 'Environment: %s', 'integration-workos' ),
cfg.environment || __( 'unknown', 'integration-workos' )
) }
</div>
</div>
{ error && (
<div className="notice notice-error workos-users-notice">
<p>{ error }</p>
</div>
) }
{ actionNotice && (
<div
className={ `notice notice-${
actionNotice.kind === 'success' ? 'success' : 'error'
} is-dismissible workos-users-notice` }
role={ actionNotice.kind === 'error' ? 'alert' : 'status' }
>
<p>{ actionNotice.text }</p>
</div>
) }
<div className="workos-users-table-wrap" aria-busy={ loading }>
<table className="wp-list-table widefat striped workos-users-table">
<thead>
<tr>
{ columns.map( ( label ) => (
<th key={ label } scope="col">
{ label }
</th>
) ) }
</tr>
</thead>
<tbody>
{ loading && users.length === 0 && (
<>
{ Array.from( { length: 5 } ).map( ( _, i ) => (
<tr key={ `skeleton-${ i }` } className="workos-users-row-skeleton">
{ columns.map( ( _label, j ) => (
<td key={ j }>
<span className="workos-users-skeleton-bar" />
</td>
) ) }
</tr>
) ) }
</>
) }
{ ! loading && users.length === 0 && ! error && (
<tr>
<td colSpan={ columns.length } className="workos-users-empty-row">
{ search
? sprintf(
__( 'No users match "%s".', 'integration-workos' ),
search
)
: __(
'No WorkOS users found in this environment.',
'integration-workos'
) }
</td>
</tr>
) }
{ users.map( ( user ) => (
<tr key={ user.id }>
<td>
<strong>{ user.email || '—' }</strong>
{ user.email_verified && (
<span
className="workos-users-verified"
title={ __( 'Email verified in WorkOS', 'integration-workos' ) }
>
{ ' ' }✓
</span>
) }
<div className="workos-users-id">
<code>{ user.id }</code>
</div>
</td>
<td>{ fullName( user ) }</td>
<td title={ user.last_sign_in_at || '' }>
{ formatRelative( user.last_sign_in_at ) }
</td>
<td title={ user.created_at || '' }>
{ formatRelative( user.created_at ) }
</td>
<td>
{ user.dashboard_url ? (
<a
className="button button-small"
href={ user.dashboard_url }
target="_blank"
rel="noopener noreferrer"
title={ __(
'Open this user in the WorkOS Dashboard — use the Emails section to re-enable a suppressed email.',
'integration-workos'
) }
>
{ __( 'Open in WorkOS', 'integration-workos' ) }
</a>
) : (
<span className="workos-users-no-link">
{ __( '—', 'integration-workos' ) }
</span>
) }
{ user.wp_user_id > 0 && (
<button
type="button"
className="button button-small workos-pwreset-trigger"
data-user-id={ user.wp_user_id }
title={ __(
'Send this user a WorkOS password reset email.',
'integration-workos'
) }
>
{ __( 'Send password reset', 'integration-workos' ) }
</button>
) }
{ user.wp_user_id > 0 && cfg.changeEmailEnabled && (
<button
type="button"
className="button button-small"
onClick={ () => handleChangeEmail( user ) }
disabled={ busyUserId === user.wp_user_id }
title={ __(
'Change this user’s email. Updates WorkOS and the portal immediately — no verification email is sent.',
'integration-workos'
) }
>
{ busyUserId === user.wp_user_id
? __( 'Changing…', 'integration-workos' )
: __( 'Change email', 'integration-workos' ) }
</button>
) }
</td>
</tr>
) ) }
</tbody>
</table>
</div>
<div className="workos-users-pagination">
<button
type="button"
className="button"
onClick={ handlePrev }
disabled={ ! hasPrev || loading }
>
{ __( '‹ Previous', 'integration-workos' ) }
</button>
<button
type="button"
className="button"
onClick={ handleNext }
disabled={ ! hasNext || loading }
>
{ __( 'Next ›', 'integration-workos' ) }
</button>
{ loading && (
<span className="workos-users-loading">
{ __( 'Loading…', 'integration-workos' ) }
</span>
) }
</div>
</div>
);
}
const mount = document.getElementById( 'workos-users-admin-root' );
if ( mount ) {
createRoot( mount ).render( <App /> );
}