Back

95% Statements 57/60
92.59% Branches 50/54
82.35% Functions 14/17
94.73% Lines 54/57

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                                                                            3x 92x 92x                                                                                                                                                               3x 31x   31x 1x                                         30x                                                                                                                     3x                                     3x                 103x                                                                                 3x                       98x               98x           98x           98x 98x 98x 40x 31x 31x   9x 9x           98x 98x 77x           98x 51x 38x   13x 1x   1x   13x 13x             98x             98x   98x       98x       98x             98x 3x         98x 4x                       98x         98x                   98x             98x                           98x                                                                                                                                                                               98x           26x                           72x                         1x       1x 1x                                                                                                                
import AdminPage from '@automattic/jetpack-components/admin-page';
import useConnectionErrorNotice, {
	ConnectionError,
} from '@automattic/jetpack-connection/use-connection-error-notice';
import { Breadcrumbs } from '@wordpress/admin-ui';
import { Button, ProgressBar } from '@wordpress/components';
import { useCallback, useEffect, useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { Card, Skeleton, Stack, Text, VisuallyHidden } from '@wordpress/ui';
import { isChaptersEditorEnabled } from '../../utils/chapters-editor';
import VideoNav from '../video-nav';
import ChaptersHelpModal from './chapters-help-modal';
import HeaderActions from './header-actions';
import PreviewPlayer from './preview-player';
import PrivacySharingCard from './privacy-sharing-card';
import RatingCard from './rating-card';
import SubtitlesCard from './subtitles-card';
import ThumbnailCard from './thumbnail-card';
import { useVideoDetailsForm } from './use-video-details-form';
import VideoDetailsCard from './video-details-card';
import VideoInfoCard from './video-info-card';
import './editor.scss';
import type { VideoDetailsFormValues } from './use-video-details-form';
import type { LibraryItem, VideoRating } from '../../types/library';
import type { ReactElement } from 'react';
 
/**
 * Parent breadcrumb item — labelled "VideoPress" in every case, but the
 * link target depends on where the user arrived from. Overview's ranking
 * links tag their navigation with `state: { from: 'stats' }`; we read
 * that here so the breadcrumb routes back to the Overview tab instead of
 * defaulting to Library. TanStack stores user state on `window.history.state`,
 * so reading it directly avoids needing `useLocation` (which `@wordpress/route`
 * doesn't re-export from TanStack). Stable for the lifetime of the mount,
 * so no reactivity hook is needed.
 *
 * @return The parent breadcrumb item.
 */
export const getParentBreadcrumbItem = (): { label: string; to: string } => {
	const from = ( window.history.state as { from?: string } | null )?.from;
	return { label: 'VideoPress', to: from === 'stats' ? '/stats' : '/' };
};
 
export type EditorUploadState = {
	status: 'uploading' | 'processing' | 'failed';
	/** 0–100, matching the DS ProgressBar contract. Meaningful only while uploading. */
	progress: number;
	fileName: string;
	error?: string;
	/** Re-dispatches a failed upload. Only offered while `status` is 'failed'. */
	onRetry?: () => void;
};
 
export type EditorUploadSession = {
	/**
	 * The player slot's stage while the upload runs: progress, then processing,
	 * or the failure + retry. Cleared (undefined) once the bound video is
	 * playable, which hands the slot back to the player — the payoff the whole
	 * flow was promising, so nothing is allowed to stand in front of it.
	 */
	uploadState?: EditorUploadState;
	/**
	 * Save gate: the meta PATCH is keyed by attachment id, so Save must stay
	 * off until the surface is bound to a real media record — form dirtiness
	 * alone can't know that.
	 */
	saveDisabled: boolean;
	/**
	 * Edits carried in from the upload's queue row — typed on the /upload
	 * bridge before the handoff, or held across a mid-flow remount. Seeded
	 * once, at mount.
	 */
	draft?: Partial< VideoDetailsFormValues >;
	/**
	 * Write-through for the same diff. Form state lives in here, so the
	 * surfaces that own the queue row cannot read it at navigate time — it has
	 * to be pushed out as it changes rather than collected on the way out.
	 */
	onDraftChange?: ( draft: Partial< VideoDetailsFormValues > | undefined ) => void;
};
 
export type EditorProps = {
	video: LibraryItem;
	onSave: (
		values: ReturnType< typeof useVideoDetailsForm >[ 'values' ],
		reset: ReturnType< typeof useVideoDetailsForm >[ 'reset' ]
	) => void;
	isSaving: boolean;
	onDelete: () => void;
	onDownload: () => void;
	onManageCaptions: () => void;
	chaptersOpen: boolean;
	setChaptersOpen: ( open: boolean ) => void;
	/**
	 * Present while this Editor is bound to an upload still in the queue: it
	 * turns the player slot into the upload's stage (progress → processing),
	 * carries the draft in and out, and keeps in-progress edits when the
	 * record re-binds. Both the /upload bridge and `/video/:id` (which the
	 * bridge hands off to) pass one.
	 */
	uploadSession?: EditorUploadSession;
	/**
	 * Renders inside an existing AdminPage (the /upload bridge sits under the
	 * dashboard tabs) rather than owning one. Deliberately separate from
	 * `uploadSession`: `/video/:id` has an upload session AND its own page
	 * chrome.
	 */
	embedded?: boolean;
};
 
/**
 * The player slot while an upload is in flight: the same dark 16:9 frame the
 * player uses, carrying the file name and the upload's real state instead of
 * an embed that couldn't exist yet. No cancel — v1 treats a started upload as
 * committed; the failure state offers the retry.
 *
 * @param props             - Component props.
 * @param props.uploadState - The upload's current state.
 * @return The stage element.
 */
const UploadStagePanel = ( { uploadState }: { uploadState: EditorUploadState } ): ReactElement => {
	const { status, progress, fileName, error, onRetry } = uploadState;
 
	if ( status === 'failed' ) {
		return (
			<div
				className="vp-video-details__player vp-upload-stage is-failed"
				data-testid="upload-stage"
				role="alert"
			>
				<Text variant="body-md" render={ <span /> } className="vp-upload-stage__name">
					{ fileName }
				</Text>
				<Text variant="body-sm" render={ <span /> } className="vp-upload-stage__error">
					{ error ?? __( 'The video could not be uploaded.', 'jetpack-videopress-pkg' ) }
				</Text>
				{ onRetry && (
					<Button variant="secondary" onClick={ onRetry }>
						{ __( 'Retry upload', 'jetpack-videopress-pkg' ) }
					</Button>
				) }
			</div>
		);
	}
 
	return (
		<div className="vp-video-details__player vp-upload-stage" data-testid="upload-stage">
			{ /*
			 * The live region carries the PHASE only, never the percentage:
			 * with `aria-live` on the wrapper a screen reader spoke a new
			 * number on every progress tick — dozens to hundreds per upload.
			 * The upload pill announces the same way (phase transitions only),
			 * and it can't cover for this one: it is suppressed on /upload.
			 */ }
			<VisuallyHidden>
				<div aria-live="polite">
					{ status === 'uploading'
						? __( 'Uploading…', 'jetpack-videopress-pkg' )
						: __( 'Upload complete — processing…', 'jetpack-videopress-pkg' ) }
				</div>
			</VisuallyHidden>
			<Text variant="body-md" render={ <span /> } className="vp-upload-stage__name">
				{ fileName }
			</Text>
			{ /* No value while processing — the transcode reports no percentage,
			     so the bar goes indeterminate rather than lying at 100. */ }
			<ProgressBar
				className="vp-upload-stage__bar"
				value={ status === 'uploading' ? progress : undefined }
			/>
			<Text variant="body-sm" render={ <span /> } className="vp-upload-stage__status">
				{ status === 'uploading'
					? sprintf(
							/* translators: %d: upload progress percentage. */
							__( 'Uploading… %d%%', 'jetpack-videopress-pkg' ),
							Math.round( progress )
					  )
					: __( 'Upload complete — processing…', 'jetpack-videopress-pkg' ) }
			</Text>
		</div>
	);
};
 
/*
 * How long the placeholder cards below will stand in before they stop claiming
 * to be loading.
 *
 * They exist to hold the page's shape across registration and transcoding, and
 * they read `video.isProcessing` to know when that's over. A video whose
 * CONVERSION FAILS never clears that flag — the failure is reported inside the
 * player iframe and nowhere in the media REST payload this dashboard reads — so
 * without a bound the skeletons animate for the life of the tab. Live testing
 * watched them still going at three and a half minutes on a `.txt` renamed
 * `.mp4`, on a page that was otherwise behaving as if everything had worked.
 *
 * The clock starts when the bytes are up, not when the upload starts, so a slow
 * connection spends its time on the progress bar rather than on this. Two
 * minutes is past a normal transcode of the clips this flow is built around,
 * and the state is only a display bound, never a claim about the backend: the
 * poll keeps running (see PROCESSING_POLL_MAX_MS in use-library), and if the
 * record does resolve later the real cards simply take over. Erring short
 * therefore costs a pessimistic message on an unusually long transcode, not a
 * broken screen.
 */
const MEDIA_WAIT_TIMEOUT_MS = 2 * 60 * 1000;
 
/**
 * Skeleton stand-in for a card whose real content needs the VideoPress GUID
 * (the thumbnail poster, the subtitle tracks, the link/shortcode read-outs).
 * Rendering the card shell up front keeps the page's shape stable through the
 * upload — the real card fills the same slot when the record lands instead of
 * popping into a page the user is already typing on.
 *
 * Once the wait is over its budget the skeletons are replaced by
 * `stalledMessage`: a panel that cannot load has to say so rather than
 * animate indefinitely at someone.
 *
 * @param props                - Component props.
 * @param props.title          - The card title the real card will keep.
 * @param props.isStalled      - Whether the wait has outlived MEDIA_WAIT_TIMEOUT_MS.
 * @param props.stalledMessage - What to say instead of the skeletons then.
 * @return The placeholder card element.
 */
const PendingCard = ( {
	title,
	isStalled,
	stalledMessage,
}: {
	title: string;
	isStalled: boolean;
	stalledMessage: string;
} ): ReactElement => (
	<Card.Root>
		<Card.Header>
			<Card.Title render={ <h2 /> }>{ title }</Card.Title>
		</Card.Header>
		<Card.Content>
			{ isStalled ? (
				// `role="status"` rather than an alert: the page has already
				// settled by the time this appears, so it is a state to be
				// noticed, not an interruption.
				<Text variant="body-sm" className="vp-video-details__pending-stalled" role="status">
					{ stalledMessage }
				</Text>
			) : (
				<Stack direction="column" gap="md">
					<Skeleton className="vp-video-details__pending-line" />
					<Skeleton className="vp-video-details__pending-line is-short" />
				</Stack>
			) }
		</Card.Content>
	</Card.Root>
);
 
/**
 * The Video details edit surface: the authoring canvas, the preview player,
 * and the settings panel, plus the header Save actions. Used by the
 * /video/:id route (full AdminPage chrome) and by the upload flow's draft
 * session (embedded inside the dashboard tabs via `uploadSession`).
 *
 * @param props                  - Component props.
 * @param props.video            - The video record being edited (a draft item during an upload session).
 * @param props.onSave           - Persists the form values; receives them with the form's reset.
 * @param props.isSaving         - Whether a save (or delete) is in flight; holds Save off.
 * @param props.onDelete         - Deletes the video.
 * @param props.onDownload       - Opens the source file.
 * @param props.onManageCaptions - Opens the caption manager.
 * @param props.chaptersOpen     - Whether the chapters help modal is open.
 * @param props.setChaptersOpen  - Opens/closes the chapters help modal.
 * @param props.uploadSession    - Upload-session state; see EditorProps.
 * @param props.embedded         - Render without page chrome; see EditorProps.
 * @return The editor element.
 */
const Editor = ( {
	video,
	onSave,
	isSaving,
	onDelete,
	onDownload,
	onManageCaptions,
	chaptersOpen,
	setChaptersOpen,
	uploadSession,
	embedded,
}: EditorProps ): ReactElement => {
	const { values, update, isDirty, dirtyValues, reset } = useVideoDetailsForm( video, {
		// The bridge hands the form a synthetic record first and the real one
		// once the upload settles; the id change between them must not wipe
		// what the user has typed in the meantime.
		preserveDirtyOnRebind: Boolean( uploadSession ),
		initialDraft: uploadSession?.draft,
	} );
 
	const uploadState = uploadSession?.uploadState;
 
	// The video has no transcoded media behind it yet: it is still uploading,
	// still registering with WordPress.com, or still processing. The cards
	// that need a poster or a GUID stand in as skeletons for all of it.
	const isAwaitingMedia =
		Boolean( uploadState ) || video.type !== 'videopress' || video.isProcessing;
 
	// The placeholder cards' terminal state. Held off while the bytes are still
	// going up — that wait has its own progress bar and no bound worth
	// enforcing — and re-armed from scratch whenever the phase changes, so a
	// long upload followed by a normal transcode never inherits a spent budget.
	const isUploadingBytes = uploadState?.status === 'uploading';
	const [ isMediaWaitStalled, setIsMediaWaitStalled ] = useState( false );
	useEffect( () => {
		if ( ! isAwaitingMedia || isUploadingBytes ) {
			setIsMediaWaitStalled( false );
			return;
		}
		const timer = setTimeout( () => setIsMediaWaitStalled( true ), MEDIA_WAIT_TIMEOUT_MS );
		return () => clearTimeout( timer );
	}, [ isAwaitingMedia, isUploadingBytes ] );
 
	// Push the diff out on every change, so the queue row — not this mount —
	// is what holds the user's words. Undefined once the form is clean again,
	// which clears the row rather than pinning a stale draft to it.
	const onDraftChange = uploadSession?.onDraftChange;
	useEffect( () => {
		onDraftChange?.( Object.keys( dirtyValues ).length > 0 ? dirtyValues : undefined );
	}, [ dirtyValues, onDraftChange ] );
 
	// Warn before a reload or tab close drops unsaved edits. The in-app exits
	// are guarded by `confirmNavigation` below; this covers the ones the app
	// never sees. (Uploads still in flight get their own guard from the queue.)
	useEffect( () => {
		if ( ! isDirty ) {
			return;
		}
		const onBeforeUnload = ( event: BeforeUnloadEvent ) => {
			event.preventDefault();
			// Chrome still requires returnValue to be set.
			event.returnValue = '';
		};
		window.addEventListener( 'beforeunload', onBeforeUnload );
		return () => window.removeEventListener( 'beforeunload', onBeforeUnload );
	}, [ isDirty ] );
 
	// A broken WordPress.com connection is why saves and uploads fail here;
	// surface the reconnect notice at the top of the full-chrome screen. The
	// embedded bridge sits inside DashboardLayout, whose own surfaces carry
	// the notice already.
	const { hasConnectionError } = useConnectionErrorNotice();
 
	// The sub-nav's only sibling tab is the Editor, whose route is stripped
	// from the registry when the chapters editor is off — a one-tab strip
	// would be pointless chrome, and its Editor tab would dead-end. The
	// embedded bridge never shows it: its Editor tab is a route navigation,
	// which would abandon the /upload session mid-upload.
	const showVideoNav = ! embedded && isChaptersEditorEnabled();
 
	const openChapters = useCallback( () => {
		setChaptersOpen( true );
	}, [ setChaptersOpen ] );
 
	const closeChapters = useCallback( () => {
		setChaptersOpen( false );
	}, [ setChaptersOpen ] );
 
	const onRatingChange = useCallback(
		( next: VideoRating ) => {
			update( { rating: next } );
		},
		[ update ]
	);
 
	const handleSave = useCallback( () => {
		onSave( values, reset );
	}, [ onSave, values, reset ] );
 
	// Guard the sub-nav against losing unsaved form edits: the Editor tab
	// is a sibling route, so switching tabs unmounts this form entirely.
	const confirmNavigation = useCallback( () => {
		return (
			! isDirty ||
			// eslint-disable-next-line no-alert -- deliberate synchronous guard; the sub-nav navigation can't await a custom dialog.
			window.confirm(
				__(
					'You have unsaved changes. Leave this page and discard them?',
					'jetpack-videopress-pkg'
				)
			)
		);
	}, [ isDirty ] );
 
	const canSave = isDirty && ! isSaving && ! uploadSession?.saveDisabled;
 
	// `.trim()` matters for the heading in both chromes. Breadcrumbs only
	// short-circuits on `items.length === 0`, so a whitespace-only title would
	// render an empty <h1> — and that <h1> is the page's only accessible name.
	const headingLabel = values.title.trim() || __( 'Untitled', 'jetpack-videopress-pkg' );
 
	// The player stays MOUNTED underneath the upload stage rather than being
	// swapped for it. Swapping tore down the iframe the instant `isProcessing`
	// flipped, so a video the user had already started watching during the
	// transcode stopped dead and needed a second press of play — the
	// "transcoding interrupts playback" bug, which our 2s polling made land
	// promptly and reliably. Overlaid, the embed survives the transition and
	// simply becomes visible.
	const playerSlot = (
		<div className="vp-video-details__player-slot">
			<PreviewPlayer video={ video } />
			{ uploadState && <UploadStagePanel uploadState={ uploadState } /> }
		</div>
	);
 
	const headerActions = (
		<HeaderActions
			guid={ video.guid || undefined }
			canSave={ canSave }
			onSave={ handleSave }
			onManageCaptions={ onManageCaptions }
			onDownload={ onDownload }
			onDelete={ onDelete }
			// Until the attachment exists every item in the ⋯ menu would be a
			// silent no-op, and a menu of no-ops is worse than no menu.
			showMenu={ ! uploadState }
		/>
	);
 
	const layout = (
		<div className="vp-video-details__layout">
			<div className="vp-video-details__canvas">
				<VideoDetailsCard
					video={ video }
					title={ values.title }
					description={ values.description }
					onChange={ update }
					onOpenChapters={ openChapters }
					confirmNavigation={ confirmNavigation }
					// The bridge's record is a synthetic draft until the upload
					// registers, so its deep link would point at
					// /video/draft-clip.mp4/editor — a route that cannot resolve.
					allowEditorLink={ ! embedded }
				/>
				{ /*
				 * Both cards refuse to render until the video is transcoded
				 * (no poster endpoint, no GUID), so on a freshly uploaded
				 * video the settings column would sit two cards short and
				 * then jump as they appear. Hold their shape with skeletons
				 * for the whole wait — during the upload AND through the
				 * processing tail — rather than only while this surface owns
				 * the upload.
				 */ }
				{ isAwaitingMedia ? (
					<>
						<PendingCard
							title={ __( 'Thumbnail', 'jetpack-videopress-pkg' ) }
							isStalled={ isMediaWaitStalled }
							stalledMessage={ __(
								'This video hasn’t finished processing, so there’s no thumbnail to choose from yet. If it was uploaded a while ago, it may have failed to convert.',
								'jetpack-videopress-pkg'
							) }
						/>
						<PendingCard
							title={ __( 'Subtitles', 'jetpack-videopress-pkg' ) }
							isStalled={ isMediaWaitStalled }
							stalledMessage={ __(
								'This video hasn’t finished processing, so its subtitles can’t be loaded yet. If it was uploaded a while ago, it may have failed to convert.',
								'jetpack-videopress-pkg'
							) }
						/>
					</>
				) : (
					<>
						<ThumbnailCard video={ video } />
						<SubtitlesCard video={ video } onManageSubtitles={ onManageCaptions } />
					</>
				) }
			</div>
			{ /*
			 * Deliberately a sibling of the canvas rather than the first
			 * child of the aside: it is placed by grid area, so the stacked
			 * layout below 1100px can lead with the player while the
			 * settings stay at the bottom.
			 */ }
			{ playerSlot }
			<aside
				className="vp-video-details__inspector"
				aria-label={ __( 'Video settings', 'jetpack-videopress-pkg' ) }
			>
				{ uploadState ? (
					<PendingCard
						title={ __( 'Video info', 'jetpack-videopress-pkg' ) }
						isStalled={ isMediaWaitStalled }
						stalledMessage={ __(
							'This video hasn’t finished processing, so its link and shortcode aren’t available yet. If it was uploaded a while ago, it may have failed to convert.',
							'jetpack-videopress-pkg'
						) }
					/>
				) : (
					<VideoInfoCard video={ video } />
				) }
				{ /*
				 * Interactive even in the draft session: both cards only write
				 * form values, and the eventual save is the attachment-id-keyed
				 * meta PATCH — no GUID involved, so there is nothing to disable.
				 */ }
				<PrivacySharingCard
					privacy={ values.privacy }
					displayEmbed={ values.displayEmbed }
					allowDownloads={ values.allowDownloads }
					onChange={ update }
				/>
				<RatingCard value={ values.rating } onChange={ onRatingChange } />
			</aside>
		</div>
	);
 
	if ( embedded ) {
		// Embedded chrome: the upload flow already sits inside DashboardLayout's
		// AdminPage (masthead + tabs), and AdminPage inside AdminPage renders a
		// second masthead and footer inside the tab panel. A header row stands
		// in for the breadcrumb bar: the same live form-driven title, the same
		// Save actions.
		return (
			<div className="vp-video-details is-embedded">
				<div className="vp-video-details__embedded-header">
					<Text variant="body-lg" render={ <h2 /> } className="vp-video-details__embedded-title">
						{ headingLabel }
					</Text>
					{ headerActions }
				</div>
				{ layout }
				<ChaptersHelpModal isOpen={ chaptersOpen } onClose={ closeChapters } />
			</div>
		);
	}
 
	return (
		<AdminPage
			breadcrumbs={
				// display: contents wrapper — a pure scoping hook so the
				// stylesheet can clamp long video titles in the current-item
				// crumb (Breadcrumbs' own class names are CSS-module hashes).
				<div
					className="vp-video-details__breadcrumbs"
					// The parent crumb is a router link out of a form that
					// discards on unmount, and BreadcrumbItem carries no click
					// hook — so the same guard the sub-nav uses has to be caught
					// on the way down, before the link's own handler runs.
					onClickCapture={ event => {
						Eif (
							( event.target as HTMLElement ).closest( 'a' ) !== null &&
							! confirmNavigation()
						) {
							event.preventDefault();
							event.stopPropagation();
						}
					} }
				>
					{ /*
					 * The crumb reads the FORM's title, not the saved record, so
					 * the page heading tracks what is being typed without
					 * committing it. Two side benefits over reading
					 * `video.title`: no old→new flicker when the post-save
					 * refetch lands, and the 2s processing `refetchInterval`
					 * can't clobber the crumb mid-edit.
					 */ }
					<Breadcrumbs items={ [ getParentBreadcrumbItem(), { label: headingLabel } ] } />
				</div>
			}
			actions={ headerActions }
		>
			{ hasConnectionError && (
				<Stack direction="column">
					<ConnectionError />
				</Stack>
			) }
			{ showVideoNav && (
				<VideoNav
					videoId={ video.id }
					activeTab="details"
					confirmNavigation={ confirmNavigation }
				/>
			) }
			<div className="vp-video-details">
				{ /*
				 * Placement rule for this screen: the canvas holds what a person
				 * authors about this video — the words, the still, the captions.
				 * The right-hand column holds the video itself, the values that
				 * address it, and the settings picked once from a fixed set.
				 *
				 * The split is authoring vs. configuring rather than editable vs.
				 * read-only, which is why Privacy & sharing and Rating sit beside
				 * the read-outs: all three are things you set and leave, not
				 * things you write.
				 *
				 * The player used to lead the canvas. It was measured at 502px
				 * tall on a 1080p display — over half the visible page before a
				 * single field had been read — while the settings it pushed
				 * down could not fit their own column and grew a second
				 * scrollbar with no visible boundary. Those are the same
				 * problem, and moving one element fixes both.
				 */ }
				{ layout }
			</div>
			<ChaptersHelpModal isOpen={ chaptersOpen } onClose={ closeChapters } />
		</AdminPage>
	);
};
 
export default Editor;