PluginProbe ʕ •ᴥ•ʔ
Presto Player / 4.3.3
Presto Player v4.3.3
4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.4 4.2.3 4.2.2 4.2.0 4.2.1 trunk 1.10.0 1.10.1 1.10.2 1.11.0 1.12.0 1.13.0 1.14.0 1.14.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 1.6.13 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.9.0 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.14 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3-beta1 2.3.0 2.3.1 2.3.2 2.3.3 3.0.0 3.0.0-beta1 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.1.0 3.1.1 3.1.2 3.1.3 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4
presto-player / src / admin / dashboard / pages / settings / general / PresetsPage.js
presto-player / src / admin / dashboard / pages / settings / general Last commit date
BrandingPage.js 3 months ago ContributePage.js 3 months ago CustomCssPage.js 3 months ago LicensePage.js 3 months ago MediaHubPage.js 3 months ago PresetsPage.js 3 months ago UninstallPage.js 3 months ago ViewingAnalyticsPage.js 3 months ago
PresetsPage.js
216 lines
1 import { __ } from '@wordpress/i18n';
2 import { useCallback, useEffect, useRef, useState } from 'react';
3 import wpApiFetch from '@wordpress/api-fetch';
4 import { Container, Select, toast } from '@bsf/force-ui';
5 import SettingsPageShell from '../shared/SettingsPageShell';
6 import SectionCard from '../shared/SectionCard';
7 import ProGate from '../shared/ProGate';
8 import LabelWithTooltip from '../shared/LabelWithTooltip';
9 import useSettingOption from '../../../hooks/useSettingOption';
10 import useRegisterActivePage from '../../../hooks/useRegisterActivePage';
11 import { useSettingsData } from '../shared/SettingsDataProvider';
12 import { OPTION_KEYS } from '../config';
13
14 const DEFAULT_PRESET = { default_player_preset: 0 };
15 const isPremium = window.prestoPlayer?.isPremium ?? false;
16
17 const PresetsPage = ( { registerActivePage } ) => {
18 const video = useSettingOption( OPTION_KEYS.presets, DEFAULT_PRESET );
19 const audio = useSettingOption( OPTION_KEYS.audioPresets, DEFAULT_PRESET );
20 const { saveSlice } = useSettingsData();
21
22 const [ presetList, setPresetList ] = useState( [] );
23 const [ audioPresetList, setAudioPresetList ] = useState( [] );
24
25 // Refs so that the render props always read the latest list even when
26 // Force UI's Select.Button caches the render callback in useCallback.
27 const presetListRef = useRef( [] );
28 const audioPresetListRef = useRef( [] );
29 presetListRef.current = presetList;
30 audioPresetListRef.current = audioPresetList;
31
32 useEffect( () => {
33 let cancelled = false;
34 Promise.all( [
35 wpApiFetch( { path: '/presto-player/v1/preset' } ).catch( () => [] ),
36 wpApiFetch( { path: '/presto-player/v1/audio-preset' } ).catch(
37 () => []
38 ),
39 ] ).then( ( [ presets, audioPresets ] ) => {
40 if ( cancelled ) {
41 return;
42 }
43 if ( Array.isArray( presets ) ) {
44 setPresetList(
45 presets.map( ( p ) => ( {
46 value: String( p.id ),
47 label: p.name || p.title,
48 } ) )
49 );
50 }
51 if ( Array.isArray( audioPresets ) ) {
52 setAudioPresetList(
53 audioPresets.map( ( p ) => ( {
54 value: String( p.id ),
55 label: p.name || p.title,
56 } ) )
57 );
58 }
59 } );
60 return () => {
61 cancelled = true;
62 };
63 }, [] );
64
65 const isDirty = video.isDirty || audio.isDirty;
66 const isSaving = video.isSaving || audio.isSaving;
67 const isLoading = video.isLoading;
68
69 const handleSave = useCallback( async () => {
70 try {
71 await saveSlice( [ OPTION_KEYS.presets, OPTION_KEYS.audioPresets ] );
72 toast.success( __( 'Settings saved.', 'presto-player' ), {
73 autoDismiss: 3000,
74 } );
75 } catch ( err ) {
76 toast.error( __( 'Could not save settings.', 'presto-player' ), {
77 autoDismiss: 5000,
78 } );
79 }
80 }, [ saveSlice ] );
81
82 const handleDiscard = useCallback( () => {
83 video.reset();
84 audio.reset();
85 }, [ video, audio ] );
86
87 useRegisterActivePage( registerActivePage, {
88 isDirty,
89 save: handleSave,
90 discard: handleDiscard,
91 } );
92
93 return (
94 <SettingsPageShell
95 title={ __( 'Presets', 'presto-player' ) }
96 isDirty={ isDirty }
97 isSaving={ isSaving }
98 isLoading={ isLoading }
99 onSave={ handleSave }
100 >
101 <SectionCard>
102 <ProGate
103 enabled={ isPremium }
104 title={ __( 'Player Presets', 'presto-player' ) }
105 description={ __(
106 'Set default presets for video and audio players.',
107 'presto-player'
108 ) }
109 >
110 <Container direction="column" className="gap-4">
111 <Container.Item className="flex flex-col gap-2">
112 <LabelWithTooltip
113 label={ __( 'Default Video Preset', 'presto-player' ) }
114 tooltip={ __(
115 'This preset will be automatically applied to every new video block you add.',
116 'presto-player'
117 ) }
118 />
119 <Select
120 size="md"
121 combobox
122 value={
123 video.data?.default_player_preset
124 ? String( video.data.default_player_preset )
125 : ''
126 }
127 onChange={ ( val ) =>
128 video.setData( ( prev ) => ( {
129 ...( prev || DEFAULT_PRESET ),
130 default_player_preset: val ? Number( val ) : 0,
131 } ) )
132 }
133 >
134 <Select.Button
135 render={ ( val ) =>
136 presetListRef.current.find(
137 ( p ) => p.value === String( val )
138 )?.label ?? __( 'Select a preset', 'presto-player' )
139 }
140 />
141 <Select.Options>
142 { presetList.length > 0 ? (
143 presetList.map( ( preset ) => (
144 <Select.Option
145 key={ preset.value }
146 value={ preset.value }
147 >
148 { preset.label }
149 </Select.Option>
150 ) )
151 ) : (
152 <Select.Option value="">
153 { __( 'No presets available', 'presto-player' ) }
154 </Select.Option>
155 ) }
156 </Select.Options>
157 </Select>
158 </Container.Item>
159
160 <Container.Item className="flex flex-col gap-2">
161 <LabelWithTooltip
162 label={ __( 'Default Audio Preset', 'presto-player' ) }
163 tooltip={ __(
164 'This preset will be automatically applied to every new audio block you add.',
165 'presto-player'
166 ) }
167 />
168 <Select
169 size="md"
170 combobox
171 value={
172 audio.data?.default_player_preset
173 ? String( audio.data.default_player_preset )
174 : ''
175 }
176 onChange={ ( val ) =>
177 audio.setData( ( prev ) => ( {
178 ...( prev || DEFAULT_PRESET ),
179 default_player_preset: val ? Number( val ) : 0,
180 } ) )
181 }
182 >
183 <Select.Button
184 render={ ( val ) =>
185 audioPresetListRef.current.find(
186 ( p ) => p.value === String( val )
187 )?.label ?? __( 'Select a preset', 'presto-player' )
188 }
189 />
190 <Select.Options>
191 { audioPresetList.length > 0 ? (
192 audioPresetList.map( ( preset ) => (
193 <Select.Option
194 key={ preset.value }
195 value={ preset.value }
196 >
197 { preset.label }
198 </Select.Option>
199 ) )
200 ) : (
201 <Select.Option value="">
202 { __( 'No audio presets available', 'presto-player' ) }
203 </Select.Option>
204 ) }
205 </Select.Options>
206 </Select>
207 </Container.Item>
208 </Container>
209 </ProGate>
210 </SectionCard>
211 </SettingsPageShell>
212 );
213 };
214
215 export default PresetsPage;
216