index.js
114 lines
| 1 | import { useState } from "@wordpress/element"; |
| 2 | import { useDispatch } from "@wordpress/data"; |
| 3 | import { store as coreStore } from "@wordpress/core-data"; |
| 4 | import { store as noticesStore } from "@wordpress/notices"; |
| 5 | import { __ } from "@wordpress/i18n"; |
| 6 | import ProvidersPlaceholder from "../ProvidersPlaceholder"; |
| 7 | import providerIcons from "../ProvidersPlaceholder/icons"; |
| 8 | |
| 9 | /** |
| 10 | * MediaProviders component for handling common video operations |
| 11 | * |
| 12 | * @param {Object} props Component props |
| 13 | * @param {Function} props.onSyncedMediaCreated Callback when synced video is created |
| 14 | * @param {Function} props.onSelect Callback when provider is selected |
| 15 | * @param {Function} props.onSelectMedia Callback when existing media is selected |
| 16 | * @param {boolean} props.sync Whether to sync media to media hub |
| 17 | */ |
| 18 | const MediaProviders = ({ |
| 19 | onSyncedMediaCreated, |
| 20 | onSelect, |
| 21 | onSelectMedia, |
| 22 | sync = false, |
| 23 | }) => { |
| 24 | const { saveEntityRecord } = useDispatch(coreStore); |
| 25 | const { createErrorNotice } = useDispatch(noticesStore); |
| 26 | const [saving, setSaving] = useState(false); |
| 27 | |
| 28 | const providers = [ |
| 29 | { |
| 30 | id: "self-hosted", |
| 31 | name: __("Video", "presto-player"), |
| 32 | icon: providerIcons?.video, |
| 33 | premium: false, |
| 34 | hasAccess: true, |
| 35 | }, |
| 36 | { |
| 37 | id: "youtube", |
| 38 | name: __("YouTube", "presto-player"), |
| 39 | icon: providerIcons?.youtube, |
| 40 | premium: false, |
| 41 | hasAccess: true, |
| 42 | }, |
| 43 | { |
| 44 | id: "vimeo", |
| 45 | name: __("Vimeo", "presto-player"), |
| 46 | icon: providerIcons?.vimeo, |
| 47 | premium: false, |
| 48 | hasAccess: true, |
| 49 | }, |
| 50 | { |
| 51 | id: "audio", |
| 52 | name: __("Audio", "presto-player"), |
| 53 | icon: providerIcons?.audio, |
| 54 | premium: false, |
| 55 | hasAccess: true, |
| 56 | }, |
| 57 | { |
| 58 | id: "bunny", |
| 59 | name: __("Bunny.net", "presto-player"), |
| 60 | icon: providerIcons?.bunny, |
| 61 | premium: true, |
| 62 | hasAccess: !!prestoPlayer?.isPremium, |
| 63 | }, |
| 64 | ]; |
| 65 | |
| 66 | // Create a video with media hub sync |
| 67 | const createSyncedVideo = async (provider) => { |
| 68 | if (saving) return; |
| 69 | try { |
| 70 | setSaving(true); |
| 71 | const { id } = await saveEntityRecord( |
| 72 | "postType", |
| 73 | "pp_video_block", |
| 74 | { |
| 75 | status: "publish", |
| 76 | content: `<!-- wp:presto-player/reusable-edit --> |
| 77 | <div class="wp-block-presto-player-reusable-edit"><!-- wp:presto-player/${provider} /--></div> |
| 78 | <!-- /wp:presto-player/reusable-edit -->`, |
| 79 | }, |
| 80 | { throwOnError: true } |
| 81 | ); |
| 82 | |
| 83 | if (onSyncedMediaCreated) { |
| 84 | onSyncedMediaCreated(id); |
| 85 | } |
| 86 | } catch (e) { |
| 87 | createErrorNotice( |
| 88 | e?.message || __("Something went wrong", "presto-player") |
| 89 | ); |
| 90 | } finally { |
| 91 | setSaving(false); |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | const handleProviderSelect = (provider) => { |
| 96 | if (sync) { |
| 97 | createSyncedVideo(provider); |
| 98 | } else { |
| 99 | onSelect(provider); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | return ( |
| 104 | <ProvidersPlaceholder |
| 105 | loading={saving} |
| 106 | onSelect={handleProviderSelect} |
| 107 | onSelectMedia={onSelectMedia} |
| 108 | providers={providers} |
| 109 | /> |
| 110 | ); |
| 111 | }; |
| 112 | |
| 113 | export default MediaProviders; |
| 114 |