Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VideoPress: Add/use preview hook #29164

Merged
merged 10 commits into from
Mar 7, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Add usePreview hook
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { createBlock } from '@wordpress/blocks';
import { Spinner, Placeholder, Button, withNotices, ToolbarButton } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect, useDispatch } from '@wordpress/data';
import { useDispatch } from '@wordpress/data';
import { useEffect, useState, useCallback, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { caption as captionIcon } from '@wordpress/icons';
Expand All @@ -22,6 +22,7 @@ import debugFactory from 'debug';
*/
import { isStandaloneActive, isVideoPressActive } from '../../../lib/connection';
import { buildVideoPressURL, getVideoPressUrl } from '../../../lib/url';
import { usePreview } from '../../hooks/use-preview';
import { useSyncMedia } from '../../hooks/use-video-data-update';
import ConnectBanner from './components/banner/connect-banner';
import ColorPanel from './components/color-panel';
Expand Down Expand Up @@ -155,24 +156,7 @@ export default function VideoPressEdit( {
const { filename, private_enabled_for_site: privateEnabledForSite } = videoData;

// Get video preview status.
const defaultPreview = { html: null, scripts: [], width: null, height: null };
const { preview, isRequestingEmbedPreview } = useSelect(
select => {
if ( ! videoPressUrl ) {
return {
preview: defaultPreview,
isRequestingEmbedPreview: false,
};
}

return {
preview: select( coreStore ).getEmbedPreview( videoPressUrl ) || defaultPreview,
isRequestingEmbedPreview:
select( coreStore ).isRequestingEmbedPreview( videoPressUrl ) || false,
};
},
[ videoPressUrl ]
);
const { preview, isRequestingEmbedPreview } = usePreview( videoPressUrl );

// Pick video properties from preview.
const { html: previewHtml, scripts, width: previewWidth, height: previewHeight } = preview;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,10 @@ export type DetailsPanelProps = VideoControlProps & {
updateError: object | null;
isRequestingVideoData: boolean;
};

export type VideoPreview = {
html?: string;
scripts: Array< string >;
width?: number;
height?: number;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* WordPress dependencies
*/
import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
/*
* Internal dependencies
*/
import { VideoPreview } from '../blocks/video/types';
jhnstn marked this conversation as resolved.
Show resolved Hide resolved

export type UsePreviewResult = {
preview: VideoPreview;
isRequestingEmbedPreview: boolean;
};

const defaultPreview: VideoPreview = { html: null, scripts: [], width: null, height: null };

export const usePreview = ( videoPressUrl?: string ): UsePreviewResult => {
return useSelect(
select => {
if ( ! videoPressUrl ) {
return { preview: defaultPreview, isRequestingEmbedPreview: false };
}
return {
preview: select( coreStore ).getEmbedPreview( videoPressUrl ) || defaultPreview,
isRequestingEmbedPreview:
select( coreStore ).isRequestingEmbedPreview( videoPressUrl ) || false,
};
},
[ videoPressUrl ]
);
};