Back

80.95% Statements 255/315
72.72% Branches 152/209
75% Functions 66/88
80.87% Lines 241/298

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478                                                                                          2x                                                                                                       2x   14x 14x     14x 14x   14x 2x               12x     2x 6x 6x 6x                             2x 3x 3x     2x 6x 6x                                               2x       8x   8x 8x   2x 2x     6x 6x   6x 6x 6x   6x 6x 6x       6x 4x 4x 4x         4x 2x 2x         2x     2x                       6x 2x                   6x                             2x                     60x                                                                   2x 14x 12x 12x 12x 12x 12x                                                                                                     4x 4x 2x                           4x 4x 4x     4x 4x 1x         3x 1x           4x                                                                                                                                     2x                 3x 3x 2x   3x 3x 3x 3x     3x 1x 1x 1x 1x   1x               1x 1x           1x 1x   1x           1x       3x 3x                               3x 3x 3x                                                                                                                                                                                                                                                     2x             1x 1x   1x                           1x 1x                                                               1x 1x 1x   1x                                                                                                                                                                                                                       2x                                                                                                   2x             13x 13x 13x 10x             13x 13x       13x                 13x 10x 1x       13x   1x 1x                   13x 10x                                                 13x         13x   13x 2x 11x 3x               8x     13x                                                                                     2x                                             2x                     75x 75x   75x 40x 40x 40x   40x 16x 16x               75x 39x 39x       39x 39x 39x         75x   75x                                                                                               2x                                     46x 46x 46x 46x 46x         46x             46x 23x 6x 5x   46x 46x 46x   46x                           46x 24x     24x 31x       46x                   15x       15x     15x 1x   15x 15x 15x   15x                 8x 2x 1x 1x   7x 7x       7x 7x     8x           7x 7x   7x 8x 8x 8x     2x           2x     6x                                           46x 13x 12x 12x                                     46x       46x 1x 1x 1x             46x 44x     13x   1x 1x         1x   1x       46x   1x   1x 1x 1x   1x                   1x 1x 1x 1x                         1x             46x   46x 102x 65x                 37x 20x       17x 13x   4x 1x   3x                 46x                                                   40x 40x                         40x   23x     40x 40x   40x 16x 16x   40x   40x                          
import { useGlobalNotices } from '@automattic/jetpack-components/global-notices';
import { useQueryClient } from '@tanstack/react-query';
import apiFetch from '@wordpress/api-fetch';
import { Button, ProgressBar, TextControl } from '@wordpress/components';
import {
	useRef,
	useState,
	useCallback,
	useLayoutEffect,
	useEffect,
	useMemo,
} from '@wordpress/element';
import { __, _n, sprintf } from '@wordpress/i18n';
import { Icon, media, check, copy } from '@wordpress/icons';
import { useNavigate } from '@wordpress/route';
import { Card, Stack, Text } from '@wordpress/ui';
import { markFirstPublish, useFirstRunState } from '../../hooks/use-first-run-state';
import { useFreeTier } from '../../hooks/use-free-tier';
import { LIBRARY_QUERY_KEY } from '../../hooks/use-library';
import { isAdoptableUpload, setUploadDraft, useUpload } from '../../hooks/use-upload';
import { useVideo } from '../../hooks/use-video';
import { isWpcomConnected } from '../../utils/connection';
import AddToContentMenu from '../add-to-content-menu';
import FreeTierNotice from '../free-tier-notice';
import UploadDropzone from '../upload-dropzone';
import {
	selectFilesForPlan,
	UPLOAD_BATCH_CONTEXT,
	UPLOAD_ONBOARDING_CONTEXT,
} from '../upload-dropzone/select-files';
import Editor from '../video-details/editor';
import './style.scss';
import type { LibraryItem } from '../../types/library';
import type { EditorUploadState } from '../video-details/editor';
import type { VideoDetailsFormValues } from '../video-details/use-video-details-form';
import type { ReactNode } from 'react';
 
type Step = 'upload' | 'uploading' | 'edit' | 'details' | 'success';
 
// Tags this flow's uploads in the shared queue so a remounted flow instance —
// or one freshly handed files by Home's emptied-library dropzone — can
// re-find them. See the batchQueueIds initializer and UploadOnboarding's
// adoption-aware initial step. Exported so the Library, which renders this
// flow as its empty state, can suppress the upload pill for the flow's own
// queue items the same way the old /upload route did.
export const UPLOAD_CONTEXT = UPLOAD_ONBOARDING_CONTEXT;
type UploadStatus = 'pending' | 'uploading' | 'success' | 'failed';
 
type UploadedMedia = {
	id: number;
	source_url?: string;
	// VideoPress GUID, when the site has already registered the attachment as a
	// VideoPress video. See `readVideoPressGuid` for why it is often absent.
	videopressGuid?: string;
};
 
// The subset of a /wp/v2/media response this route reads. `jetpack_videopress`
// is the REST field registered by WPCOM_REST_API_V2_Attachment_VideoPress_Data
// (see src/class-initializer.php); `jetpack_videopress_guid` is the Simple-only
// flat field. Both are absent on sites without a VideoPress-backed attachment.
type MediaApiResponse = {
	id?: unknown;
	source_url?: string;
	message?: string;
	jetpack_videopress?: { guid?: unknown } | null;
	jetpack_videopress_guid?: unknown;
};
 
type UploadItem = {
	id: string;
	file: File;
	progress: number;
	status: UploadStatus;
	media?: UploadedMedia;
	error?: string;
	// Carried straight off the queue row: the edit session writes it through
	// so a remount — or the handoff to /video/:id — re-seeds what was typed.
	draft?: Partial< VideoDetailsFormValues >;
};
 
type MediaDetailsPatch = {
	mediaId: number;
	title: string;
	description?: string;
	shareUrl?: string;
	videopressGuid?: string;
};
 
type PublishedVideo = {
	mediaId: number;
	title: string;
	shareUrl?: string;
	// Present only when the attachment is already a VideoPress video. The
	// "Add to a post or page" action is hidden without it — see SuccessCard.
	videopressGuid?: string;
};
 
const restApiConfig = () => {
	const initialState =
		typeof JPVIDEOPRESS_INITIAL_STATE !== 'undefined' ? JPVIDEOPRESS_INITIAL_STATE : undefined;
	const wpApiSettings = (
		window as typeof window & { wpApiSettings?: { root?: string; nonce?: string } }
	 ).wpApiSettings;
	const root = initialState?.API?.WP_API_root ?? wpApiSettings?.root;
	const nonce = initialState?.API?.WP_API_nonce ?? wpApiSettings?.nonce;
 
	if ( ! root || ! nonce ) {
		throw new Error(
			__(
				'Video upload is unavailable because the REST API credentials are missing.',
				'jetpack-videopress-pkg'
			)
		);
	}
 
	return { root, nonce };
};
 
const restUrl = ( path: string ) => {
	const { root } = restApiConfig();
	const base = new URL( root.endsWith( '/' ) ? root : `${ root }/`, window.location.origin );
	return new URL( path.replace( /^\/+/, '' ), base ).toString();
};
 
/**
 * Pull the VideoPress GUID out of a /wp/v2/media response.
 *
 * A GUID only exists once the site has registered the attachment as a
 * VideoPress video, which needs a WordPress.com connection. This onboarding
 * route uploads through plain `wp/v2/media` (see `uploadToMediaLibrary`), so on
 * an unconnected/local site the field is empty and every caller must treat the
 * GUID as optional.
 *
 * @param response - A raw media item from the REST API.
 * @return The GUID, or undefined when the attachment is not a VideoPress video.
 */
const readVideoPressGuid = ( response: MediaApiResponse | undefined ): string | undefined => {
	const guid = response?.jetpack_videopress?.guid ?? response?.jetpack_videopress_guid;
	return typeof guid === 'string' && guid !== '' ? guid : undefined;
};
 
const errorMessage = ( error: unknown ) => {
	Eif ( error instanceof Error ) {
		return error.message;
	}
	if ( typeof error === 'string' ) {
		return error;
	}
	const message = ( error as { message?: unknown } | null )?.message;
	return typeof message === 'string' && message !== ''
		? message
		: __( 'Unexpected upload error.', 'jetpack-videopress-pkg' );
};
 
/**
 * Upload a video as a regular WordPress attachment. The dashboard's shared
 * useUpload() path is a VideoPress/tus upload that depends on WordPress.com
 * upload JWTs and does not complete in offline/local Docker environments.
 * This onboarding route therefore uses wp/v2/media so the selected files
 * genuinely land in the WordPress Media Library. Those local attachments may
 * not appear in the VideoPress Library where the listing is constrained to
 * WordPress.com VideoPress rows.
 *
 * @param file       - File selected by the user.
 * @param onProgress - Called with the XHR upload progress percentage.
 * @return The created media attachment.
 */
const uploadToMediaLibrary = (
	file: File,
	onProgress: ( percent: number ) => void
): Promise< UploadedMedia > =>
	new Promise( ( resolve, reject ) => {
		let config: ReturnType< typeof restApiConfig >;
		try {
			config = restApiConfig();
		} catch ( error ) {
			reject( error );
			return;
		}
 
		const formData = new FormData();
		formData.append( 'file', file, file.name );
 
		const xhr = new XMLHttpRequest();
		xhr.open( 'POST', restUrl( '/wp/v2/media' ) );
		xhr.setRequestHeader( 'X-WP-Nonce', config.nonce );
 
		xhr.upload.onprogress = event => {
			Eif ( event.lengthComputable && event.total > 0 ) {
				onProgress( Math.min( 99, Math.round( ( event.loaded / event.total ) * 100 ) ) );
			}
		};
 
		xhr.onload = () => {
			let response: MediaApiResponse = {};
			try {
				response = JSON.parse( xhr.responseText || '{}' );
			} catch {
				// Leave response empty; the status-derived error below is clearer.
			}
 
			if ( xhr.status >= 200 && xhr.status < 300 && typeof response.id === 'number' ) {
				onProgress( 100 );
				resolve( {
					id: response.id,
					source_url: response.source_url,
					videopressGuid: readVideoPressGuid( response ),
				} );
				return;
			}
 
			reject(
				new Error(
					response.message ??
						sprintf(
							/* translators: %d: HTTP status code. */
							__( 'Upload failed with HTTP status %d.', 'jetpack-videopress-pkg' ),
							xhr.status
						)
				)
			);
		};
 
		xhr.onerror = () => {
			reject(
				new Error(
					__(
						'Upload failed. Please check your connection and try again.',
						'jetpack-videopress-pkg'
					)
				)
			);
		};
 
		xhr.send( formData );
	} );
 
/**
 * Step 1 — the dropzone card. The drop surface itself is the shared
 * UploadDropzone (Home's emptied-library state renders the same one); this
 * card owns the heading and the free-tier notice.
 *
 * @param props                  - Component props.
 * @param props.onFiles          - Called with the selected files.
 * @param props.isUploadDisabled - Whether the free-tier limit blocks uploading.
 * @param props.allowMultiple    - Whether the plan allows selecting several files.
 * @param props.isFirstRun       - Whether this is the user's first video.
 * @return The dropzone card.
 */
const UploadCard = ( {
	onFiles,
	isUploadDisabled,
	allowMultiple,
	isFirstRun,
}: {
	onFiles: ( files: File[] ) => void;
	isUploadDisabled: boolean;
	allowMultiple: boolean;
	isFirstRun: boolean;
} ) => (
	<Card.Root className="vp-onboarding__card vp-onboarding__card--upload">
		<Card.Header>
			<Card.Title render={ <h2 /> }>
				{ /* Someone arriving here with a library of 27 videos is not
				     uploading their first one. */ }
				{ isFirstRun
					? __( 'Upload your first video', 'jetpack-videopress-pkg' )
					: __( 'Upload a video', 'jetpack-videopress-pkg' ) }
			</Card.Title>
		</Card.Header>
		<Card.Content>
			{ isUploadDisabled && (
				<div className="vp-onboarding__limit-notice">
					<FreeTierNotice />
				</div>
			) }
			<UploadDropzone
				onFiles={ onFiles }
				disabled={ isUploadDisabled }
				allowMultiple={ allowMultiple }
			/>
		</Card.Content>
	</Card.Root>
);
 
/**
 * Interstitial — DS ProgressBar(s) while videos upload. One bar for a single
 * file; a per-file list for a bulk upload.
 *
 * @param props         - Component props.
 * @param props.uploads - The uploading files and their real upload state.
 * @param props.onBack  - Return to the upload step after an error.
 * @return The uploading card.
 */
const UploadingCard = ( { uploads, onBack }: { uploads: UploadItem[]; onBack: () => void } ) => {
	if ( uploads.length <= 1 ) {
		const item = uploads[ 0 ];
		const name = item?.file.name || __( 'your-video.mp4', 'jetpack-videopress-pkg' );
		const p = item?.progress ?? 0;
		const failed = item?.status === 'failed';
		return (
			<Card.Root className="vp-onboarding__card">
				<Card.Header>
					<Card.Title render={ <h2 /> }>
						{ failed
							? __( 'Upload failed', 'jetpack-videopress-pkg' )
							: __( 'Uploading your video', 'jetpack-videopress-pkg' ) }
					</Card.Title>
				</Card.Header>
				<Card.Content>
					<Stack direction="column" gap="lg">
						<div className="vp-details__file">
							<span className="vp-details__file-icon">
								<Icon icon={ media } size={ 20 } />
							</span>
							<span className="vp-details__file-name">{ name }</span>
							{ failed ? (
								<span className="vp-details__file-status is-error">
									{ __( 'Failed', 'jetpack-videopress-pkg' ) }
								</span>
							) : (
								<span className="vp-uploading__pct">{ Math.round( p ) }%</span>
							) }
						</div>
						<ProgressBar value={ p } className="vp-uploading__bar" />
						{ failed ? (
							<>
								<Text variant="body-sm" className="vp-uploading__error">
									{ item?.error ??
										__( 'The video could not be uploaded.', 'jetpack-videopress-pkg' ) }
								</Text>
								<div className="vp-uploading__actions">
									<Button variant="tertiary" onClick={ onBack }>
										{ __( 'Back', 'jetpack-videopress-pkg' ) }
									</Button>
								</div>
							</>
						) : (
							<Text variant="body-sm" className="vp-uploading__hint">
								{ __(
									'Hang tight — we’re processing your video. You can add its details next.',
									'jetpack-videopress-pkg'
								) }
							</Text>
						) }
					</Stack>
				</Card.Content>
			</Card.Root>
		);
	}
 
	const done = uploads.filter( item => item.status === 'success' ).length;
	const failed = uploads.filter( item => item.status === 'failed' ).length;
	return (
		<Card.Root className="vp-onboarding__card">
			<Card.Header>
				<Card.Title render={ <h2 /> }>
					{ sprintf(
						/* translators: %d: number of videos. */
						__( 'Uploading %d videos', 'jetpack-videopress-pkg' ),
						uploads.length
					) }
				</Card.Title>
			</Card.Header>
			<Card.Content>
				<Stack direction="column" gap="lg">
					{ uploads.map( item => {
						const p = item.progress;
						const complete = item.status === 'success';
						const rowFailed = item.status === 'failed';
						// Flattened out of a nested ternary so the row's three
						// states (failed / done / in progress) read in order.
						let rowStatus = <span className="vp-uploading__pct">{ Math.round( p ) }%</span>;
						if ( rowFailed ) {
							rowStatus = (
								<span className="vp-details__file-status is-error">
									{ __( 'Failed', 'jetpack-videopress-pkg' ) }
								</span>
							);
						} else if ( complete ) {
							rowStatus = (
								<span className="vp-details__file-status">
									<Icon icon={ check } size={ 16 } />
								</span>
							);
						}
						return (
							<div className="vp-bulk__uprow" key={ item.id }>
								<span className="vp-details__file-icon">
									<Icon icon={ media } size={ 20 } />
								</span>
								<div className="vp-bulk__uprow-main">
									<div className="vp-bulk__uprow-top">
										<span className="vp-details__file-name">{ item.file.name }</span>
										{ rowStatus }
									</div>
									<ProgressBar value={ p } className="vp-uploading__bar" />
									{ rowFailed && item.error && (
										<Text variant="body-sm" className="vp-uploading__error">
											{ item.error }
										</Text>
									) }
								</div>
							</div>
						);
					} ) }
					{ failed > 0 ? (
						<>
							<Text variant="body-sm" className="vp-uploading__error">
								{ sprintf(
									/* translators: %d: number of failed uploads. */
									_n(
										'%d upload failed. Go back and try again.',
										'%d uploads failed. Go back and try again.',
										failed,
										'jetpack-videopress-pkg'
									),
									failed
								) }
							</Text>
							<div className="vp-uploading__actions">
								<Button variant="tertiary" onClick={ onBack }>
									{ __( 'Back', 'jetpack-videopress-pkg' ) }
								</Button>
							</div>
						</>
					) : (
						<Text variant="body-sm" className="vp-uploading__hint">
							{ sprintf(
								/* translators: 1: uploaded count, 2: total count. */
								__( '%1$d of %2$d uploaded — add details next.', 'jetpack-videopress-pkg' ),
								done,
								uploads.length
							) }
						</Text>
					) }
				</Stack>
			</Card.Content>
		</Card.Root>
	);
};
 
/**
 * Step 2 — the details form the flow morphs to after upload. A single video
 * gets file name + title + description; a bulk upload gets an inline list,
 * with independent file name and title fields per video.
 *
 * @param props           - Component props.
 * @param props.uploads   - The uploaded files and their media IDs.
 * @param props.onBack    - Return to the upload step.
 * @param props.onPublish - Persist details for each uploaded media item.
 * @return The details card.
 */
const DetailsCard = ( {
	uploads,
	onBack,
	onPublish,
}: {
	uploads: UploadItem[];
	onBack: () => void;
	onPublish: ( patches: MediaDetailsPatch[] ) => Promise< void >;
} ) => {
	const files = uploads.map( item => item.file );
	const [ titles, setTitles ] = useState< string[] >( () =>
		files.length ? files.map( () => '' ) : [ '' ]
	);
	const [ description, setDescription ] = useState( '' );
	const [ isPublishing, setIsPublishing ] = useState( false );
	const [ publishError, setPublishError ] = useState< string | null >( null );
	const setTitle = ( i: number, value: string ) =>
		setTitles( prev => prev.map( ( t, j ) => ( j === i ? value : t ) ) );
 
	const publish = useCallback( async () => {
		setIsPublishing( true );
		setPublishError( null );
		try {
			await onPublish(
				uploads.map( ( item, i ) => {
					Iif ( ! item.media ) {
						throw new Error(
							__(
								'Upload details cannot be saved before the upload finishes.',
								'jetpack-videopress-pkg'
							)
						);
					}
					const title = titles[ i ]?.trim() || item.file.name;
					const patch: MediaDetailsPatch = {
						mediaId: item.media.id,
						title,
						shareUrl: item.media.source_url,
						videopressGuid: item.media.videopressGuid,
					};
					Eif ( uploads.length <= 1 ) {
						patch.description = description;
					}
					return patch;
				} )
			);
		} catch ( error ) {
			setPublishError( errorMessage( error ) );
		} finally {
			setIsPublishing( false );
		}
	}, [ description, onPublish, titles, uploads ] );
 
	const actions = ( publishLabel: string ) => (
		<div className="vp-details__actions">
			<Button variant="tertiary" onClick={ onBack } disabled={ isPublishing }>
				{ __( 'Back', 'jetpack-videopress-pkg' ) }
			</Button>
			<Button
				variant="primary"
				__next40pxDefaultSize
				onClick={ publish }
				disabled={ isPublishing }
				isBusy={ isPublishing }
			>
				{ publishLabel }
			</Button>
		</div>
	);
 
	Eif ( files.length <= 1 ) {
		const name = files[ 0 ]?.name || __( 'your-video.mp4', 'jetpack-videopress-pkg' );
		return (
			<Card.Root className="vp-onboarding__card">
				<Card.Header>
					<Card.Title render={ <h2 /> }>
						{ __( 'Add your video details', 'jetpack-videopress-pkg' ) }
					</Card.Title>
				</Card.Header>
				<Card.Content>
					<Stack direction="column" gap="lg">
						{ /*
						 * The filename is shown, not edited: the old File Name
						 * field never saved anywhere (the PATCH carries only
						 * title/description), so an input was a lie.
						 */ }
						<div className="vp-details__file">
							<span className="vp-details__file-icon">
								<Icon icon={ media } size={ 20 } />
							</span>
							<span className="vp-details__file-name">{ name }</span>
							<span className="vp-details__file-status">
								<Icon icon={ check } size={ 16 } />
								{ __( 'Uploaded', 'jetpack-videopress-pkg' ) }
							</span>
						</div>
						<TextControl
							__next40pxDefaultSize
							__nextHasNoMarginBottom
							label={ __( 'Video Title', 'jetpack-videopress-pkg' ) }
							value={ titles[ 0 ] }
							onChange={ v => setTitle( 0, v ) }
							placeholder={ __( 'Give your video a title', 'jetpack-videopress-pkg' ) }
							help={ __(
								'This title will appear with your thumbnail (optional)',
								'jetpack-videopress-pkg'
							) }
						/>
						<TextControl
							__next40pxDefaultSize
							__nextHasNoMarginBottom
							label={ __( 'Description', 'jetpack-videopress-pkg' ) }
							value={ description }
							onChange={ setDescription }
							placeholder={ __( 'What is this video about?', 'jetpack-videopress-pkg' ) }
						/>
						{ publishError && (
							<Text variant="body-sm" className="vp-details__error">
								{ publishError }
							</Text>
						) }
					</Stack>
				</Card.Content>
				{ actions( __( 'Publish video', 'jetpack-videopress-pkg' ) ) }
			</Card.Root>
		);
	}
 
	return (
		<Card.Root className="vp-onboarding__card">
			<Card.Header>
				<Card.Title render={ <h2 /> }>
					{ sprintf(
						/* translators: %d: number of videos. */
						__( 'Add details · %d videos', 'jetpack-videopress-pkg' ),
						files.length
					) }
				</Card.Title>
			</Card.Header>
			<Card.Content>
				<Stack direction="column" gap="lg">
					{ files.map( ( file, i ) => (
						<div className="vp-bulk__row" key={ i }>
							<div className="vp-bulk__row-head">
								<span className="vp-details__file-icon">
									<Icon icon={ media } size={ 20 } />
								</span>
								<span className="vp-details__file-name">{ file.name }</span>
								<span className="vp-details__file-status">
									<Icon icon={ check } size={ 16 } />
									{ __( 'Uploaded', 'jetpack-videopress-pkg' ) }
								</span>
							</div>
							<TextControl
								__next40pxDefaultSize
								__nextHasNoMarginBottom
								label={ __( 'Video Title', 'jetpack-videopress-pkg' ) }
								value={ titles[ i ] }
								onChange={ v => setTitle( i, v ) }
								placeholder={ __( 'Give your video a title', 'jetpack-videopress-pkg' ) }
								help={ __(
									'This title will appear with your thumbnail (optional)',
									'jetpack-videopress-pkg'
								) }
							/>
						</div>
					) ) }
					{ publishError && (
						<Text variant="body-sm" className="vp-details__error">
							{ publishError }
						</Text>
					) }
				</Stack>
			</Card.Content>
			{ actions(
				sprintf(
					/* translators: %d: number of videos. */
					__( 'Publish %d videos', 'jetpack-videopress-pkg' ),
					files.length
				)
			) }
		</Card.Root>
	);
};
 
/**
 * Step 3 — confirmation after the media details are saved. Shows the share
 * links returned by the local wp/v2/media upload path and leaves navigation to
 * the user.
 *
 * @param props            - Component props.
 * @param props.published  - Published videos and their share links.
 * @param props.onGoToHome - Navigate to the Home tab.
 * @return The success card.
 */
const SuccessCard = ( {
	published,
	onGoToHome,
}: {
	published: PublishedVideo[];
	onGoToHome: () => void;
} ) => {
	const [ copiedMediaId, setCopiedMediaId ] = useState< number | null >( null );
	const [ copyError, setCopyError ] = useState< string | null >( null );
 
	const copyShareLink = useCallback( ( video: PublishedVideo ) => {
		setCopyError( null );
		if ( ! video.shareUrl ) {
			setCopyError( __( 'The share link could not be copied.', 'jetpack-videopress-pkg' ) );
			return;
		}
 
		void copyTextToClipboard( video.shareUrl )
			.then( () => setCopiedMediaId( video.mediaId ) )
			.catch( () => {
				setCopyError( __( 'The share link could not be copied.', 'jetpack-videopress-pkg' ) );
			} );
	}, [] );
 
	const renderShareField = ( video: PublishedVideo, index?: number ) => (
		<div className="vp-success__share" key={ video.mediaId }>
			<TextControl
				__next40pxDefaultSize
				__nextHasNoMarginBottom
				className="vp-success__share-field"
				label={
					index == null
						? __( 'Share link', 'jetpack-videopress-pkg' )
						: sprintf(
								/* translators: %d: number of the video in the published list. */
								__( 'Share link for video %d', 'jetpack-videopress-pkg' ),
								index + 1
						  )
				}
				value={ video.shareUrl ?? '' }
				onChange={ () => undefined }
				readOnly
			/>
			<Button
				variant="secondary"
				__next40pxDefaultSize
				icon={ copy }
				onClick={ () => copyShareLink( video ) }
				disabled={ ! video.shareUrl }
			>
				{ copiedMediaId === video.mediaId
					? __( 'Copied', 'jetpack-videopress-pkg' )
					: __( 'Copy link', 'jetpack-videopress-pkg' ) }
			</Button>
		</div>
	);
 
	Eif ( published.length <= 1 ) {
		const video = published[ 0 ];
		const title = video?.title || __( 'Your video', 'jetpack-videopress-pkg' );
 
		return (
			<Card.Root className="vp-onboarding__card">
				<Card.Header>
					<Card.Title render={ <h2 /> }>
						{ __( 'Your video is published', 'jetpack-videopress-pkg' ) }
					</Card.Title>
				</Card.Header>
				<Card.Content>
					<Stack direction="column" gap="lg">
						<Text variant="body-md" className="vp-success__summary">
							{ __( 'Your video is live and ready to share.', 'jetpack-videopress-pkg' ) }
						</Text>
						<Text variant="body-lg" className="vp-success__title">
							{ title }
						</Text>
						{ video && renderShareField( video ) }
						{ copyError && (
							<Text variant="body-sm" className="vp-success__copy-status is-error">
								{ copyError }
							</Text>
						) }
					</Stack>
				</Card.Content>
				<div className="vp-success__actions">
					{ /* Single video: the hand-off sits beside "Go to Library" in the footer. */ }
					<AddToContentMenu guid={ video?.videopressGuid } />
					<Button variant="primary" __next40pxDefaultSize onClick={ onGoToHome }>
						{ __( 'Go to Library', 'jetpack-videopress-pkg' ) }
					</Button>
				</div>
			</Card.Root>
		);
	}
 
	return (
		<Card.Root className="vp-onboarding__card">
			<Card.Header>
				<Card.Title render={ <h2 /> }>
					{ sprintf(
						/* translators: %d: number of videos. */
						__( '%d videos published', 'jetpack-videopress-pkg' ),
						published.length
					) }
				</Card.Title>
			</Card.Header>
			<Card.Content>
				<Stack direction="column" gap="lg">
					<Text variant="body-md" className="vp-success__summary">
						{ __( 'Your videos are live and ready to share.', 'jetpack-videopress-pkg' ) }
					</Text>
					<div className="vp-success__list">
						{ published.map( ( video, index ) => (
							<div className="vp-success__item" key={ video.mediaId }>
								<Text variant="body-md" className="vp-success__title">
									{ video.title }
								</Text>
								{ renderShareField( video, index ) }
								{ /*
								 * Bulk upload: the hand-off is per video rather than in the
								 * card footer, because a footer action would have to guess
								 * which of several videos the user meant. Each row owns its
								 * own menu, and rows without a GUID simply render none.
								 */ }
								{ video.videopressGuid && (
									<div className="vp-success__item-actions">
										<AddToContentMenu
											guid={ video.videopressGuid }
											label={ sprintf(
												/* translators: %s: video title. */
												__( 'Add “%s” to a post or page', 'jetpack-videopress-pkg' ),
												video.title
											) }
										/>
									</div>
								) }
							</div>
						) ) }
					</div>
					{ copyError && (
						<Text variant="body-sm" className="vp-success__copy-status is-error">
							{ copyError }
						</Text>
					) }
				</Stack>
			</Card.Content>
			<div className="vp-success__actions">
				<Button variant="primary" __next40pxDefaultSize onClick={ onGoToHome }>
					{ __( 'Go to Library', 'jetpack-videopress-pkg' ) }
				</Button>
			</div>
		</Card.Root>
	);
};
 
/**
 * Copy text to the clipboard, working on plain-HTTP dev environments where
 * `navigator.clipboard` is undefined (it is secure-context-only). Falls back
 * to the hidden-textarea `execCommand` path the clipboard libraries use.
 *
 * The rest of the dashboard copies through `useCopyToClipboard`
 * (`video-details/video-info-card.tsx`), which is ref-based and so wants a
 * component per button. The only caller left is the list of share rows in
 * `SuccessCard`, which is reachable only on disconnected sites, so converting
 * it is churn. Left as the deliberate third mechanism.
 *
 * @param text - Text to place on the clipboard.
 * @return Resolves when the text has been copied; rejects when neither path works.
 */
const copyTextToClipboard = ( text: string ): Promise< void > => {
	if ( navigator.clipboard ) {
		return navigator.clipboard.writeText( text );
	}
 
	return new Promise( ( resolve, reject ) => {
		const textarea = document.createElement( 'textarea' );
		textarea.value = text;
		textarea.setAttribute( 'readonly', '' );
		textarea.style.position = 'fixed';
		textarea.style.opacity = '0';
		document.body.appendChild( textarea );
		textarea.select();
		try {
			// execCommand is deprecated but is exactly the insecure-context
			// fallback the clipboard libraries use; there is no alternative.
			const ok = document.execCommand( 'copy' );
			if ( ok ) {
				resolve();
			} else {
				reject( new Error( 'copy rejected' ) );
			}
		} catch ( error ) {
			reject( error );
		} finally {
			textarea.remove();
		}
	} );
};
 
/**
 * Step 'edit' — the bridge between a dropped file and its real screen. One
 * dropped file lands here immediately: the edit surface (the same Editor the
 * /video/:id route renders, embedded under the dashboard tabs) with the
 * player slot serving as the upload's stage, so title and description are
 * editable from the first frame.
 *
 * This step owns the PRE-REGISTRATION phase only. The moment the attachment
 * exists as a VideoPress video it hands off to `/video/:id` — the real,
 * linkable, refreshable screen for that video — instead of duplicating it
 * here. Anything typed in the meantime travels on the queue row (see
 * `onDraftChange`), because this component cannot read the form's state at
 * navigate time.
 *
 * @param props         - Component props.
 * @param props.upload  - The queue item driving the stage, mapped to the
 *                      flow's local shape. Undefined once acknowledged.
 * @param props.onRetry - Re-dispatches the upload after a failure.
 * @return The edit-step element.
 */
const EditStep = ( {
	upload: uploadItem,
	onRetry,
}: {
	upload: UploadItem | undefined;
	onRetry: () => void;
} ) => {
	const navigate = useNavigate();
	const [ chaptersOpen, setChaptersOpen ] = useState( false );
	const [ fileMeta ] = useState( () =>
		uploadItem ? { name: uploadItem.file.name, size: uploadItem.file.size } : null
	);
 
	// DERIVED, not copied into state: the first upload on a first-run site
	// flips the tab order and remounts this whole subtree mid-session (live
	// testing found the corpse twice). The queue row survives that, so it —
	// not component state — carries the attachment id and the draft.
	const rowId = uploadItem?.id;
	const mediaId = uploadItem?.media ? Number( uploadItem.media.id ) : null;
 
	// With the GUID-less polling in useVideo, this follows the record through
	// registration until WordPress.com has made it a VideoPress video.
	const { video } = useVideo( mediaId ?? '' );
 
	// The handoff. `type === 'videopress'` and not merely "the id resolves":
	// /video/:id's own editable check requires that type, and a GUID-less
	// record maps to 'local' — navigating a moment earlier lands on NotFound.
	// `replace` because this bridge is not a place: Back from the video screen
	// must reach whatever preceded the upload, not a session already handed
	// over. The record is warm in the shared cache under the identical query
	// key, so the arrival renders from cache with no refetch.
	useEffect( () => {
		if ( mediaId != null && video?.type === 'videopress' ) {
			navigate( { href: `/video/${ mediaId }`, replace: true } );
		}
	}, [ mediaId, video?.type, navigate ] );
 
	const onDraftChange = useCallback(
		( draft: Partial< VideoDetailsFormValues > | undefined ) => {
			Eif ( rowId ) {
				setUploadDraft( rowId, draft );
			}
		},
		[ rowId ]
	);
 
	// The record the surface renders until the real one arrives — the same
	// synthetic shape the Library splices in for in-flight uploads. The id is
	// a draft id; useVideoDetailsForm's preserve-on-rebind keeps typed edits
	// when it swaps to the attachment id.
	const draftVideo = useMemo< LibraryItem >(
		() => ( {
			id: `draft-${ fileMeta?.name ?? 'upload' }`,
			guid: '',
			type: 'local',
			title: ( fileMeta?.name ?? '' ).replace( /\.[^.]+$/, '' ),
			filename: fileMeta?.name ?? '',
			thumbnailUrl: null,
			durationSeconds: 0,
			uploadDate: new Date().toISOString(),
			privacy: 'site-default',
			isPrivate: false,
			fileSizeBytes: fileMeta?.size ?? 0,
			upload: { status: 'uploading', progress: 0 },
			description: '',
			rating: 'G',
			displayEmbed: false,
			allowDownloads: false,
			shortcode: '',
			isProcessing: false,
			orientation: null,
			tracks: [],
		} ),
		[ fileMeta ]
	);
 
	const boundVideo = video ?? draftVideo;
 
	// Always set: this step exists only while the video is not yet a
	// registered VideoPress video, and the effect above owns the moment it
	// becomes one.
	const fileName = fileMeta?.name ?? '';
	let uploadState: EditorUploadState;
	if ( mediaId != null ) {
		uploadState = { status: 'processing', progress: 100, fileName };
	} else if ( uploadItem?.status === 'failed' ) {
		uploadState = {
			status: 'failed',
			progress: uploadItem.progress,
			fileName,
			error: uploadItem.error,
			onRetry,
		};
	} else {
		uploadState = { status: 'uploading', progress: uploadItem?.progress ?? 0, fileName };
	}
 
	return (
		<Editor
			embedded
			video={ boundVideo }
			isSaving={ false }
			// Every action below needs a registered video, which is exactly the
			// state this step hands off in. Save is held by `saveDisabled` and
			// the ⋯ menu is hidden while `uploadState` is present, so none of
			// them is reachable here — the real wiring lives on /video/:id.
			onSave={ () => undefined }
			onDelete={ () => undefined }
			onDownload={ () => undefined }
			onManageCaptions={ () => undefined }
			chaptersOpen={ chaptersOpen }
			setChaptersOpen={ setChaptersOpen }
			uploadSession={ {
				uploadState,
				// The wpcom/v2/videopress/meta POST needs a registered video, so
				// a GUID-less record must not enable Save even once the
				// attachment id exists.
				saveDisabled: ! video?.guid,
				draft: uploadItem?.draft,
				onDraftChange,
			} }
		/>
	);
};
 
// Which steps share a shape, and so can be CROSS-FADED into one another.
//
// The four card steps are the same 660px column of copy and controls, one after
// another; fading between them reads as a single card morphing, which is what
// the animation was designed for. `edit` is not a card — it is the whole editor:
// full dashboard width, two columns, a player, a form, a Thumbnail panel, a
// Subtitles panel. Cross-fading THAT with a short drop zone is what printed the
// dropzone's hint and sub-copy over the editor's Description field for a quarter
// of a second, and no opacity curve can make two unrelated layouts overlap
// tidily — the shapes have nothing in common to fade between. Those handovers
// are sequenced instead (`.vp-flow.is-sequenced`; see style.scss).
//
// A table rather than `step === 'edit' || prev === 'edit'`: the condition is
// about shape, and a fifth card step added later should join the cross-fade by
// declaring its shape here, not by being remembered in a boolean.
const STEP_SHAPE: Record< Step, 'card' | 'editor' > = {
	upload: 'card',
	uploading: 'card',
	details: 'card',
	success: 'card',
	edit: 'editor',
};
 
/**
 * Height-morphing wrapper for the multi-step card flow. The active card animates
 * in from below, the previous one slides up and fades out, and the container
 * height eases between the two — the Cloudflare onboarding step transition.
 *
 * The two cards CROSS-FADE when the steps share a shape and are SEQUENCED when
 * they don't — see {@link STEP_SHAPE}.
 *
 * @param props            - Component props.
 * @param props.step       - The active step.
 * @param props.prev       - The exiting step (null when settled).
 * @param props.onExitDone - Called once the exit animation completes.
 * @param props.render     - Renders the card for a given step.
 * @return The morphing flow element.
 */
const StepFlow = ( {
	step,
	prev,
	onExitDone,
	render,
}: {
	step: Step;
	prev: Step | null;
	onExitDone: () => void;
	render: ( s: Step ) => ReactNode;
} ) => {
	const wrapRef = useRef< HTMLDivElement >( null );
	const [ height, setHeight ] = useState< number | undefined >( undefined );
 
	useLayoutEffect( () => {
		const active = wrapRef.current?.querySelector< HTMLElement >( '[data-active="true"]' );
		Eif ( active ) {
			setHeight( active.offsetHeight );
		}
		if ( prev != null ) {
			const t = setTimeout( onExitDone, 400 );
			return () => clearTimeout( t );
		}
	}, [ step, prev, onExitDone ] );
 
	// The wrapper's height is frozen inline, so content that grows WITHIN a
	// step (an error banner appearing, per-row messages) would overflow the
	// frame. Track the active card's size for the step's whole life, not just
	// its entrance.
	useLayoutEffect( () => {
		const active = wrapRef.current?.querySelector< HTMLElement >( '[data-active="true"]' );
		Iif ( ! active || typeof ResizeObserver === 'undefined' ) {
			return;
		}
 
		const observer = new ResizeObserver( () => setHeight( active.offsetHeight ) );
		observer.observe( active );
		return () => observer.disconnect();
	}, [ step ] );
 
	// Only while a handover is actually running: settled, the class would sit on
	// the wrapper describing a transition that isn't happening.
	const isSequenced = prev != null && prev !== step && STEP_SHAPE[ prev ] !== STEP_SHAPE[ step ];
 
	return (
		<div
			className={ `vp-flow${ isSequenced ? ' is-sequenced' : '' }` }
			ref={ wrapRef }
			style={ height != null ? { height: `${ height }px` } : undefined }
		>
			{ prev != null && prev !== step && (
				<div className="vp-flow__card is-exit" data-step={ prev } key={ `exit-${ prev }` }>
					{ render( prev ) }
				</div>
			) }
			<div
				className={ `vp-flow__card${ prev != null ? ' is-enter' : '' }` }
				data-step={ step }
				data-active="true"
				key={ step }
			>
				{ render( step ) }
			</div>
		</div>
	);
};
 
/**
 * VideoPress first-run onboarding (the Library's empty state for a brand-new
 * account). One job: activation — get the user to their first uploaded video.
 * Content is grounded in the growth-brain teardown (see
 * ~/dev/active/videopress-revamp).
 *
 * The upload → uploading → details → success hand-off uses a disappearing-card step morph
 * modelled on the Cloudflare email-routing onboarding, and supports bulk
 * uploads (per-file progress, then an inline list of detail fields).
 *
 * The step state lives in the default export rather than here, so the flow
 * does not lose its place if this component's props change mid-session.
 *
 * @param props                 - Component props.
 * @param props.isFree          - Whether the site is on the free tier.
 * @param props.isUnlimited     - Whether the plan has unlimited video storage.
 * @param props.isAtLimit       - Whether the free-tier video limit is already used.
 * @param props.step            - The active step.
 * @param props.prev            - The exiting step (null when settled).
 * @param props.go              - Move to `next`, animating out of `prior`.
 * @param props.onExitDone      - Called when the exiting card has finished animating.
 * @param props.onExitToLibrary - Hand control back to the Library listing (a
 *                              multi-file batch has no surface in this flow).
 * @return The onboarding flow element.
 */
const UploadOnboardingFlow = ( {
	isFree,
	isUnlimited,
	isAtLimit,
	step,
	prev,
	go,
	onExitDone,
	onExitToLibrary,
}: {
	isFree: boolean;
	isUnlimited: boolean;
	isAtLimit: boolean;
	step: Step;
	prev: Step | null;
	go: ( next: Step, prior: Step ) => void;
	onExitDone: () => void;
	onExitToLibrary: () => void;
} ) => {
	const queryClient = useQueryClient();
	const { createInfoNotice } = useGlobalNotices();
	const batchRef = useRef( 0 );
	const [ uploads, setUploads ] = useState< UploadItem[] >( [] );
	const [ publishedVideos, setPublishedVideos ] = useState< PublishedVideo[] >( [] );
	// On connected sites uploads run through the shared tus queue — the real
	// VideoPress pipeline, visible to every route and to useFreeTier. The
	// plain wp/v2/media XHR below survives only as the disconnected-site
	// fallback (tus needs a WordPress.com upload JWT).
	const { uploadQueue, startUpload, retryUpload, acknowledgeUpload } = useUpload();
	// Adopt any of this flow's own items already in the queue: uploading the
	// first video flips the first-run state (the library count goes 1), the
	// tab order changes, and this component remounts mid-upload — the queue
	// survives that, so the batch pointer must be recoverable from it. Only
	// rows with something left to say (see isAdoptableUpload): re-adopting a
	// settled success resurrects a session the user already finished.
	const [ batchQueueIds, setBatchQueueIds ] = useState< string[] >( () =>
		uploadQueue
			.filter( item => item.context === UPLOAD_CONTEXT && isAdoptableUpload( item ) )
			.map( item => item.id )
	);
	const isConnected = isWpcomConnected();
	const allowMultiple = ! isFree || isUnlimited;
	const firstRunState = useFirstRunState();
 
	const resetUploadStep = useCallback(
		( prior: Step ) => {
			batchRef.current += 1;
			setUploads( [] );
			// Settled queue rows from an abandoned batch are acknowledged away;
			// in-flight ones keep uploading (the Library shows them) — Back is
			// not Cancel.
			batchQueueIds.forEach( acknowledgeUpload );
			setBatchQueueIds( [] );
			setPublishedVideos( [] );
			go( 'upload', prior );
		},
		[ acknowledgeUpload, batchQueueIds, go ]
	);
	const updateUpload = useCallback( ( batch: number, id: string, patch: Partial< UploadItem > ) => {
		Iif ( batch !== batchRef.current ) {
			return;
		}
		setUploads( current =>
			current.map( item => ( item.id === id ? { ...item, ...patch } : item ) )
		);
	}, [] );
 
	const onFiles = useCallback(
		( selected: File[] ) => {
			// No at-limit guard here any more: the dropzone refuses the drop
			// itself, with the notice and the upgrade route, rather than this
			// callback dropping the files on the floor in silence. Same for
			// files that aren't videos — what arrives here is already the set
			// the backend accepts.
 
			// Home's emptied-library dropzone applies the same slice + notice
			// before handing its files to this flow — shared helper, one message.
			const { files: selectedForPlan, discardedNotice } = selectFilesForPlan(
				selected,
				allowMultiple
			);
			Iif ( ! selectedForPlan.length ) {
				return;
			}
			if ( discardedNotice ) {
				createInfoNotice( discardedNotice );
			}
			const batch = batchRef.current + 1;
			batchRef.current = batch;
			setPublishedVideos( [] );
 
			if ( isConnected ) {
				// Multi-file batches land on the Library listing: every file
				// starts in the shared queue (same context tag as the single
				// flow) and the Library splices the in-flight rows in at the
				// top, with the upload pill carrying the batch from there.
				// `onExitToLibrary` asks the Library — which renders this flow
				// as its empty state — to swap the listing back in. No
				// 'uploading' step — that interstitial survives only for the
				// disconnected XHR fallback below.
				if ( selectedForPlan.length > 1 ) {
					selectedForPlan.forEach( file => startUpload( file, UPLOAD_BATCH_CONTEXT ) );
					onExitToLibrary();
					return;
				}
				setUploads( [] );
				setBatchQueueIds( selectedForPlan.map( file => startUpload( file, UPLOAD_CONTEXT ) ) );
				// One file: skip the interstitial and cross-fade straight to the
				// edit surface — the upload carries on in its player slot, and
				// the user can write while it runs.
				go( 'edit', 'upload' );
				return;
			}
 
			const nextUploads = selectedForPlan.map( ( file, i ) => ( {
				id: `media-${ batch }-${ i }-${ file.name }`,
				file,
				progress: 0,
				status: 'pending' as UploadStatus,
			} ) );
			setUploads( nextUploads );
			go( 'uploading', 'upload' );
 
			nextUploads.forEach( item => {
				updateUpload( batch, item.id, { status: 'uploading' } );
				void uploadToMediaLibrary( item.file, percent =>
					updateUpload( batch, item.id, { progress: percent, status: 'uploading' } )
				)
					.then( mediaItem => {
						updateUpload( batch, item.id, {
							progress: 100,
							status: 'success',
							media: mediaItem,
							error: undefined,
						} );
						void queryClient.invalidateQueries( { queryKey: [ LIBRARY_QUERY_KEY ] } );
					} )
					.catch( error => {
						updateUpload( batch, item.id, {
							status: 'failed',
							error: errorMessage( error ),
						} );
					} );
			} );
		},
		[
			allowMultiple,
			createInfoNotice,
			go,
			isConnected,
			onExitToLibrary,
			queryClient,
			startUpload,
			updateUpload,
		]
	);
 
	// The queue is the source of truth while a connected-site batch uploads;
	// map its items into the flow's local shape so every step renders the same
	// structure regardless of pipeline.
	const queueUploads: UploadItem[] = batchQueueIds
		.map( id => uploadQueue.find( item => item.id === id ) )
		.filter( ( item ): item is NonNullable< typeof item > => Boolean( item ) )
		.map( item => ( {
			id: item.id,
			file: item.file,
			progress: Math.round( item.progress * 100 ),
			status: item.status,
			error: item.error,
			draft: item.draft,
			media: item.media
				? {
						id: Number( item.media.id ),
						source_url: item.media.src,
						// A GUID-less upload result is normal (registration
						// trails the attachment); String() on it would produce
						// the literal "undefined" and pass every truthiness gate
						// downstream.
						videopressGuid: item.media.guid ? String( item.media.guid ) : undefined,
				  }
				: undefined,
		} ) );
	const activeUploads = isConnected && batchQueueIds.length > 0 ? queueUploads : uploads;
 
	// The edit step's hooks into the shared queue. Stable identities so the
	// step's bind effect doesn't re-run on unrelated renders.
	const retryEditUpload = useCallback( () => {
		const id = batchQueueIds[ 0 ];
		Eif ( id ) {
			retryUpload( id );
		}
	}, [ batchQueueIds, retryUpload ] );
	// Advance to details once every file has finished. For queue batches the
	// settled items are snapshotted into local state and acknowledged out of
	// the shared queue — from here the flow owns them, and nothing else (the
	// Library splice, the future upload pill) should keep reporting them.
	useEffect( () => {
		if (
			step === 'uploading' &&
			activeUploads.length > 0 &&
			activeUploads.every( item => item.status === 'success' )
		) {
			const t = setTimeout( () => {
				Iif ( isConnected && batchQueueIds.length > 0 ) {
					setUploads( activeUploads );
					batchQueueIds.forEach( acknowledgeUpload );
					setBatchQueueIds( [] );
				}
				go( 'details', 'uploading' );
			}, 450 );
			return () => clearTimeout( t );
		}
	}, [ step, activeUploads, batchQueueIds, isConnected, acknowledgeUpload, go ] );
 
	const publishDetails = useCallback(
		async ( patches: MediaDetailsPatch[] ) => {
			const responses = await Promise.all(
				patches.map( patch => {
					const data: { title: string; description?: string } = { title: patch.title };
					Eif ( patch.description !== undefined ) {
						data.description = patch.description;
					}
					return apiFetch< MediaApiResponse >( {
						path: `/wp/v2/media/${ patch.mediaId }`,
						method: 'PATCH',
						data,
					} );
				} )
			);
			// The user has now published: first run is over for good, even if the
			// library is later emptied or the count reads stale. Written before the
			// refetch below so no query result can land while the flag is still cold.
			markFirstPublish();
			await queryClient.invalidateQueries( { queryKey: [ LIBRARY_QUERY_KEY ] } );
			setPublishedVideos(
				patches.map( ( patch, i ) => ( {
					mediaId: patch.mediaId,
					title: patch.title,
					// Local uploads go through wp/v2/media, so this is the WP attachment URL,
					// not a real VideoPress share/embed URL; on a wpcom-connected site it should
					// use the VideoPress URL/embed instead.
					shareUrl: patch.shareUrl,
					// Prefer the GUID from this PATCH response: it is the freshest read of
					// the attachment, and a connected site may only have registered the
					// VideoPress video after the original upload response was sent.
					videopressGuid: readVideoPressGuid( responses[ i ] ) ?? patch.videopressGuid,
				} ) )
			);
			go( 'success', 'details' );
		},
		[ go, queryClient ]
	);
 
	// Publishing wraps the session up: hand the surface back to the Library
	// listing, which now holds the published videos.
	const goToHome = onExitToLibrary;
 
	const renderStep = ( s: Step ) => {
		if ( s === 'upload' ) {
			return (
				<UploadCard
					onFiles={ onFiles }
					isUploadDisabled={ isAtLimit }
					allowMultiple={ allowMultiple }
					isFirstRun={ firstRunState === 'first-run' }
				/>
			);
		}
		if ( s === 'uploading' ) {
			return (
				<UploadingCard uploads={ activeUploads } onBack={ () => resetUploadStep( 'uploading' ) } />
			);
		}
		if ( s === 'edit' ) {
			return <EditStep upload={ queueUploads[ 0 ] } onRetry={ retryEditUpload } />;
		}
		if ( s === 'success' ) {
			return <SuccessCard published={ publishedVideos } onGoToHome={ goToHome } />;
		}
		return (
			<DetailsCard
				uploads={ uploads }
				onBack={ () => resetUploadStep( 'details' ) }
				onPublish={ publishDetails }
			/>
		);
	};
 
	return (
		<div className="vp-onboarding">
			{ /*
			 * The tile card that used to sit above the dropzone is gone by
			 * request: the dropzone below says everything the tiles said, and
			 * the welcome modal already made the pitch. One card, one action.
			 */ }
 
			{ /* Card 2 — the morphing upload → uploading → details → success step flow. */ }
			<StepFlow step={ step } prev={ prev } onExitDone={ onExitDone } render={ renderStep } />
		</div>
	);
};
 
/**
 * The upload onboarding flow, rendered by the Library as its empty state (the
 * user has no videos yet, so upload *is* the page). Owns the step state and
 * the free-tier reads; the Library owns when it appears and disappears.
 *
 * @param props                 - Component props.
 * @param props.onExitToLibrary - Called when a multi-file batch starts and the
 *                              Library listing (with its in-flight rows and
 *                              the upload pill) should take over the surface.
 * @return The onboarding flow element.
 */
export default function UploadOnboarding( { onExitToLibrary }: { onExitToLibrary: () => void } ) {
	const { isAtLimit, isFree, isUnlimited } = useFreeTier();
	const { uploadQueue } = useUpload();
	// Arrival may already hold this flow's queue items — Home's emptied-library
	// dropzone starts the upload under UPLOAD_CONTEXT and navigates to the
	// Library, and UploadOnboardingFlow's batchQueueIds initializer adopts the
	// rows themselves. Exactly one adopted item resumes straight into the edit
	// session, upload running in its player slot. Two or more can only mean
	// arrival mid-batch (connected multi-drops stay on the listing instead), so
	// the step stays 'upload' and the Library's in-flight rows and the pill own
	// the batch — no new surface here.
	//
	// Counted with the same adoption predicate as the batch pointer: a settled
	// success is the flow's exit, and resuming into it is how a finished
	// session came back to haunt the user.
	const [ adoptedOnMount ] = useState(
		() =>
			uploadQueue.filter( item => item.context === UPLOAD_CONTEXT && isAdoptableUpload( item ) )
				.length
	);
	const [ step, setStep ] = useState< Step >( adoptedOnMount === 1 ? 'edit' : 'upload' );
	const [ prev, setPrev ] = useState< Step | null >( null );
 
	const go = useCallback( ( next: Step, prior: Step ) => {
		setPrev( prior );
		setStep( next );
	}, [] );
	const onExitDone = useCallback( () => setPrev( null ), [] );
 
	return (
		<UploadOnboardingFlow
			isFree={ isFree }
			isUnlimited={ isUnlimited }
			isAtLimit={ isAtLimit }
			step={ step }
			prev={ prev }
			go={ go }
			onExitDone={ onExitDone }
			onExitToLibrary={ onExitToLibrary }
		/>
	);
}