PluginProbe
BeyondWords – AI audio for publishers / 7.2.0
BeyondWords – AI audio for publishers v7.2.0
7.2.0 7.1.0 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 All 44 releases
speechkit / src / settings / store / index.js

index.js in BeyondWords – AI audio for publishers 7.2.0, at src/settings/store/index.js

108 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * WordPress dependencies
3 */
4 import apiFetch from '@wordpress/api-fetch';
5 import { createReduxStore } from '@wordpress/data';
6
7 // `voices`/`videoSizes`/`project` are keyed by resolver argument: resolution is
8 // cached per selector-args, so a shared slot would go stale yet report fresh.
9 export const DEFAULT_STATE = {
10 settings: {},
11 languages: [],
12 voices: {}, // languageCode → voice list
13 scriptTemplates: [],
14 videoTemplates: [],
15 videoSizes: {}, // projectId → size list
16 project: {}, // projectId → project record
17 };
18
19 const reducer = ( state = DEFAULT_STATE, action ) => {
20 // Replace a session-wide key.
21 if ( action.type === 'SET' && action.key in state ) {
22 const empty = Array.isArray( DEFAULT_STATE[ action.key ] ) ? [] : {};
23 return { ...state, [ action.key ]: action.value || empty };
24 }
25 // Merge one argument's result, leaving other arguments' entries untouched.
26 if ( action.type === 'SET_BY' && action.key in state ) {
27 return {
28 ...state,
29 [ action.key ]: {
30 ...state[ action.key ],
31 [ action.arg ]: action.value,
32 },
33 };
34 }
35 return state;
36 };
37
38 const selectors = {
39 getSettings: ( state ) => state.settings,
40 getLanguages: ( state ) => state.languages,
41 getVoices: ( state, languageCode ) => state.voices[ languageCode ] ?? [],
42 getScriptTemplates: ( state ) => state.scriptTemplates,
43 getVideoTemplates: ( state ) => state.videoTemplates,
44 getVideoSizes: ( state, projectId ) => state.videoSizes[ projectId ] ?? [],
45 getProject: ( state, projectId ) => state.project[ projectId ] ?? {},
46 };
47
48 // Private to this store — no registered `actions`; only resolvers dispatch.
49 const set = ( key, value ) => ( { type: 'SET', key, value } );
50 const setBy = ( key, arg, value ) => ( { type: 'SET_BY', key, arg, value } );
51
52 // The REST proxies pass API error bodies through as objects; UI code `.map()`s lists.
53 const toList = ( value ) => ( Array.isArray( value ) ? value : [] );
54
55 const resolvers = {
56 async getSettings() {
57 const value = await apiFetch( { path: '/beyondwords/v1/settings' } );
58 return set( 'settings', value );
59 },
60 async getLanguages() {
61 const value = await apiFetch( { path: '/beyondwords/v1/languages' } );
62 return set( 'languages', toList( value ) );
63 },
64 async getVoices( languageCode ) {
65 const value = await apiFetch( {
66 path: `/beyondwords/v1/languages/${ languageCode }/voices`,
67 } );
68 return setBy( 'voices', languageCode, toList( value ) );
69 },
70 async getScriptTemplates() {
71 const value = await apiFetch( {
72 path: '/beyondwords/v1/summarization-settings-templates',
73 } );
74 return set( 'scriptTemplates', toList( value ) );
75 },
76 async getVideoTemplates() {
77 const value = await apiFetch( {
78 path: '/beyondwords/v1/video-settings-templates',
79 } );
80 return set( 'videoTemplates', toList( value ) );
81 },
82 async getVideoSizes( projectId ) {
83 if ( ! projectId ) {
84 return setBy( 'videoSizes', projectId, [] );
85 }
86 const r = await apiFetch( {
87 path: `/beyondwords/v1/projects/${ projectId }/video-settings`,
88 } );
89 return setBy( 'videoSizes', projectId, toList( r?.sizes ) );
90 },
91 // The project's default `language` pre-selects the Language dropdown.
92 async getProject( projectId ) {
93 if ( ! projectId ) {
94 return setBy( 'project', projectId, {} );
95 }
96 const value = await apiFetch( {
97 path: `/beyondwords/v1/projects/${ projectId }`,
98 } );
99 return setBy( 'project', projectId, value || {} );
100 },
101 };
102
103 export default createReduxStore( 'beyondwords/settings', {
104 reducer,
105 selectors,
106 resolvers,
107 } );
108