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 | 1x 12x 1x 53x 53x 1x 1x 12x 1x 41x 41x 41x 41x 41x 41x 41x 41x 3x 41x 3x 41x 2x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 12x 12x 12x 17x 1x 1x 1x 1x 1x 1x 1x 17x 3x 2x 1x 2x 2x 1x 2x 1x 1x 24x 24x 24x 24x 12x 12x 12x 1x 12x | import AdminPage from '@automattic/jetpack-components/admin-page';
import useConnectionErrorNotice, {
ConnectionError,
} from '@automattic/jetpack-connection/use-connection-error-notice';
import { useQueryClient } from '@tanstack/react-query';
import { Breadcrumbs } from '@wordpress/admin-ui';
import { useDispatch } from '@wordpress/data';
import { useCallback, useEffect, useRef, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { Link, useNavigate, useParams } from '@wordpress/route';
import { Stack, Text } from '@wordpress/ui';
import CaptionManagerModal from '../../src/client/components/caption-manager-modal/lazy';
import { getVideoInfoQueryKeyPrefix } from '../../src/client/components/caption-manager-modal/use-video-tracks';
import { DeleteVideoConfirmationDialog } from '../../src/dashboard/components/delete-video-confirmation-modal';
import QueryClientWrapper from '../../src/dashboard/components/query-client-wrapper';
import ChaptersHelpModal from '../../src/dashboard/components/video-details/chapters-help-modal';
import HeaderActions from '../../src/dashboard/components/video-details/header-actions';
import PreviewPlayer from '../../src/dashboard/components/video-details/preview-player';
import PrivacySharingCard from '../../src/dashboard/components/video-details/privacy-sharing-card';
import RatingCard from '../../src/dashboard/components/video-details/rating-card';
import SubtitlesCard from '../../src/dashboard/components/video-details/subtitles-card';
import ThumbnailCard from '../../src/dashboard/components/video-details/thumbnail-card';
import { useVideoDetailsForm } from '../../src/dashboard/components/video-details/use-video-details-form';
import VideoDetailsCard from '../../src/dashboard/components/video-details/video-details-card';
import VideoInfoCard from '../../src/dashboard/components/video-details/video-info-card';
import VideoNav from '../../src/dashboard/components/video-nav';
import { useDeleteVideo } from '../../src/dashboard/hooks/use-delete-video';
import { useSettings } from '../../src/dashboard/hooks/use-settings';
import { useUpdateChapters } from '../../src/dashboard/hooks/use-update-chapters';
import { useUpdateVideoMeta } from '../../src/dashboard/hooks/use-update-video-meta';
import { useUploadUnloadGuard } from '../../src/dashboard/hooks/use-upload-unload-guard';
import { useInvalidateVideo, useVideo } from '../../src/dashboard/hooks/use-video';
import { isChaptersEditorEnabled } from '../../src/dashboard/utils/chapters-editor';
import { isTrimCutEnabled } from '../../src/dashboard/utils/trim-cut';
import './style.scss';
import type { LibraryItem, VideoRating } from '../../src/dashboard/types/library';
const isEditable = ( item: LibraryItem ): boolean =>
item.type === 'videopress' && item.upload.status !== 'failed';
/**
* 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.
*/
const getParentBreadcrumbItem = (): { label: string; to: string } => {
const from = ( window.history.state as { from?: string } | null )?.from;
return { label: 'VideoPress', to: from === 'stats' ? '/stats' : '/' };
};
const NotFound = () => (
<AdminPage
breadcrumbs={
<Breadcrumbs
items={ [
getParentBreadcrumbItem(),
{ label: __( 'Not found', 'jetpack-videopress-pkg' ) },
] }
/>
}
>
<div className="vp-video-details vp-video-details__not-found">
<Stack direction="column" gap="md" align="center">
<Text>{ __( "We couldn't find that video.", 'jetpack-videopress-pkg' ) }</Text>
<Link to="/">{ __( 'Back to Library', 'jetpack-videopress-pkg' ) }</Link>
</Stack>
</div>
</AdminPage>
);
// Placeholder shown while /wp/v2/media/{id} is in flight. Mirrors NotFound's
// AdminPage + breadcrumbs shell so the page chrome stays present rather than
// blanking out the viewport for the duration of the fetch.
const Loading = () => (
<AdminPage
breadcrumbs={
<Breadcrumbs
items={ [
getParentBreadcrumbItem(),
{ label: __( 'Loading…', 'jetpack-videopress-pkg' ) },
] }
/>
}
>
<div className="vp-video-details vp-video-details__loading" aria-busy="true" />
</AdminPage>
);
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;
};
const Editor = ( {
video,
onSave,
isSaving,
onDelete,
onDownload,
onManageCaptions,
chaptersOpen,
setChaptersOpen,
}: EditorProps ) => {
const { values, update, isDirty, reset } = useVideoDetailsForm( video );
const { hasConnectionError } = useConnectionErrorNotice();
const shareMenuDisabled = useSettings().data?.videoPressShareMenuDisabled ?? false;
// 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.
const showVideoNav = isChaptersEditorEnabled() || isTrimCutEnabled();
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 ] );
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 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.
*
* `.trim()` matters. Breadcrumbs only short-circuits on
* `items.length === 0`, so a whitespace-only title would
* render an empty <h1> — and that <h1> is this page's only
* accessible name.
*/ }
<Breadcrumbs
items={ [
getParentBreadcrumbItem(),
{ label: values.title.trim() || __( 'Untitled', 'jetpack-videopress-pkg' ) },
] }
/>
</div>
}
actions={
<HeaderActions
guid={ video.guid }
canSave={ isDirty && ! isSaving }
onSave={ handleSave }
onManageCaptions={ onManageCaptions }
onDownload={ onDownload }
onDelete={ onDelete }
/>
}
>
{ hasConnectionError && (
<Stack direction="column">
<ConnectionError trackingContext="videopress" />
</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.
*/ }
<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 }
/>
<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.
*/ }
<PreviewPlayer video={ video } />
<aside
className="vp-video-details__inspector"
aria-label={ __( 'Video settings', 'jetpack-videopress-pkg' ) }
>
<VideoInfoCard video={ video } />
<PrivacySharingCard
privacy={ values.privacy }
displayEmbed={ values.displayEmbed }
allowDownloads={ values.allowDownloads }
shareMenuDisabledForSite={ shareMenuDisabled }
onChange={ update }
/>
<RatingCard value={ values.rating } onChange={ onRatingChange } />
</aside>
</div>
</div>
<ChaptersHelpModal isOpen={ chaptersOpen } onClose={ closeChapters } />
</AdminPage>
);
};
type StageReadyProps = { video: LibraryItem };
// Per-video id so the settle notices replace the in-progress snackbar in
// place (the notices store drops an existing notice with the same id on
// create) instead of stacking a second notice next to it. Keyed by video id
// so two overlapping deletes — start one, navigate away mid-flight, delete
// another — can't clobber each other's notices.
const deletingNoticeId = ( videoId: string ) => `vp-video-deleting-${ videoId }`;
const StageReady = ( { video }: StageReadyProps ) => {
const navigate = useNavigate();
const invalidateVideo = useInvalidateVideo();
const { mutate: updateMeta, isPending: isSaving } = useUpdateVideoMeta();
const { syncChapters } = useUpdateChapters();
const { mutateAsync: deleteVideo, isPending: isDeleting } = useDeleteVideo();
const { createSuccessNotice, createErrorNotice, createInfoNotice } = useDispatch( noticesStore );
const [ chaptersOpen, setChaptersOpen ] = useState( false );
const [ captionsOpen, setCaptionsOpen ] = useState( false );
const [ isDeleteConfirmOpen, setDeleteConfirmOpen ] = useState( false );
const queryClient = useQueryClient();
/*
* The caption manager runs on its own query client, so the page's caches
* (the info card's Subtitles row) don't see its changes. Refresh the
* video info on close to pick up publishes and deletions.
*/
const closeCaptions = useCallback( () => {
setCaptionsOpen( false );
void queryClient.invalidateQueries( {
queryKey: getVideoInfoQueryKeyPrefix( video.guid ?? '' ),
} );
}, [ queryClient, video.guid ] );
// Deletes keep running after an unmount (the user can navigate away via
// the breadcrumb mid-flight). The notice cleanup below must still happen
// then, but we shouldn't yank them to the Library if they've moved on.
const isMountedRef = useRef( true );
useEffect( () => {
isMountedRef.current = true;
return () => {
isMountedRef.current = false;
};
}, [] );
const handleDelete = () => {
Iif ( isDeleting ) {
return;
}
setDeleteConfirmOpen( false );
// Deleting can take several seconds (the backend also removes the
// remote VideoPress copy); surface progress immediately so the
// action doesn't feel frozen. `explicitDismiss` keeps the snackbar
// from auto-expiring before the request settles.
createInfoNotice( __( 'Deleting video…', 'jetpack-videopress-pkg' ), {
id: deletingNoticeId( video.id ),
explicitDismiss: true,
type: 'snackbar',
} );
// Promise chain rather than mutate-level callbacks: those are
// dropped when the component unmounts mid-flight, which would
// orphan the explicitDismiss notice above forever.
deleteVideo( Number( video.id ) )
.then( () => {
createSuccessNotice( __( 'Video deleted.', 'jetpack-videopress-pkg' ), {
id: deletingNoticeId( video.id ),
type: 'snackbar',
} );
Eif ( isMountedRef.current ) {
navigate( { href: '/' } );
}
} )
.catch( () => {
createErrorNotice( __( 'Failed to delete video.', 'jetpack-videopress-pkg' ), {
id: deletingNoticeId( video.id ),
type: 'snackbar',
} );
} );
};
return (
<>
<Editor
video={ video }
// Treat an in-flight delete like an in-flight save: Save stays
// disabled so a slow delete can't be raced by a meta update
// against the attachment being removed.
isSaving={ isSaving || isDeleting }
onSave={ ( values, reset ) => {
updateMeta(
{ id: video.id, patch: values },
{
onSuccess: () => {
// The description is the single source of truth for the
// player's chapters VTT, so a description change must
// regenerate that track (the legacy dashboard did; without
// it the player's chapter menu silently de-syncs). Only
// after the meta save succeeds — syncing first would bake
// a never-persisted description into the VTT when the
// save fails. Fire-and-notice: syncChapters never rejects
// (failures surface as a warning notice from the hook),
// so it can't block the save result either way.
if ( values.description !== video.description ) {
void syncChapters( video, values.description );
}
createSuccessNotice( __( 'Video details saved.', 'jetpack-videopress-pkg' ), {
type: 'snackbar',
} );
reset( values );
},
onError: () => {
createErrorNotice(
__( 'Failed to save video details.', 'jetpack-videopress-pkg' ),
{ type: 'snackbar' }
);
},
}
);
} }
onDelete={ () => setDeleteConfirmOpen( true ) }
onDownload={ () => {
if ( video.sourceUrl ) {
window.open( video.sourceUrl, '_blank' );
}
} }
onManageCaptions={ () => setCaptionsOpen( true ) }
chaptersOpen={ chaptersOpen }
setChaptersOpen={ setChaptersOpen }
/>
{ captionsOpen && (
<CaptionManagerModal
isOpen={ captionsOpen }
guid={ video.guid }
title={ video.title }
poster={ video.thumbnailUrl }
isPrivate={ video.isPrivate }
tracks={ video.tracks }
onClose={ closeCaptions }
onTracksChange={ () => void invalidateVideo( video.id ) }
/>
) }
<DeleteVideoConfirmationDialog
isOpen={ isDeleteConfirmOpen }
count={ 1 }
isDeleting={ isDeleting }
onCancel={ () => setDeleteConfirmOpen( false ) }
onConfirm={ handleDelete }
/>
</>
);
};
const StageInner = () => {
useUploadUnloadGuard();
const { id } = useParams( { from: '/video/$id' } );
const { video, isLoading } = useVideo( id );
if ( isLoading ) {
return <Loading />;
}
Iif ( ! video || ! isEditable( video ) ) {
return <NotFound />;
}
return <StageReady video={ video } />;
};
const Stage = () => (
<QueryClientWrapper>
<StageInner />
</QueryClientWrapper>
);
export { Stage as stage };
|