test
2 months ago
useAbilities.js
2 weeks ago
useCompleteOnboarding.js
2 months ago
useDateRangePicker.js
3 months ago
useEmail.ts
3 months ago
useEngagementChartData.js
3 months ago
useLicenseSettings.js
3 months ago
useLink.js
3 months ago
useMcpAdapterInstall.js
2 weeks ago
useMediaDetail.js
3 months ago
useMediaLibrary.js
3 months ago
useMediaList.ts
3 months ago
usePerformanceSettings.js
3 months ago
useRegisterActivePage.js
3 months ago
useSettingOption.js
2 weeks ago
useSimpleSettingsPage.js
2 weeks ago
useTopPerforming.js
3 months ago
useTopVideosPaginated.js
3 months ago
useUpgradeCTA.js
3 months ago
useUserDetail.js
3 months ago
useSimpleSettingsPage.js
61 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { useCallback } from 'react'; |
| 3 | import { toast } from '@bsf/force-ui'; |
| 4 | import useSettingOption from './useSettingOption'; |
| 5 | import useRegisterActivePage from './useRegisterActivePage'; |
| 6 | |
| 7 | /** |
| 8 | * Collapses the "single-option page" ceremony into one call: option read, |
| 9 | * patch-style update, save-with-toast, and registration with the nav guard. |
| 10 | * |
| 11 | * Used by pages that have exactly one option and the canonical save/discard |
| 12 | * flow (YouTube, Google Analytics, Performance, etc.). Pages with multi-option |
| 13 | * combined saves (Bunny, Presets) or non-option REST flows (License, |
| 14 | * EmailCapture, Webhooks) talk to useSettingOption / useSettingsData directly. |
| 15 | * |
| 16 | * @param {string} optionKey Option name to read and write. |
| 17 | * @param {Object} defaults Shape to fall back to before the option loads. |
| 18 | * @param {Function} registerActivePage Nav-guard registration callback from the page. |
| 19 | */ |
| 20 | const useSimpleSettingsPage = ( optionKey, defaults, registerActivePage ) => { |
| 21 | const { |
| 22 | data, |
| 23 | savedData, |
| 24 | setData, |
| 25 | save, |
| 26 | reset, |
| 27 | isDirty, |
| 28 | isSaving, |
| 29 | isLoading, |
| 30 | } = useSettingOption( optionKey, defaults ); |
| 31 | |
| 32 | const update = useCallback( |
| 33 | ( patch ) => |
| 34 | setData( ( prev ) => ( { ...( prev || defaults ), ...patch } ) ), |
| 35 | [ setData, defaults ] |
| 36 | ); |
| 37 | |
| 38 | const handleSave = useCallback( async () => { |
| 39 | try { |
| 40 | await save(); |
| 41 | toast.success( __( 'Settings saved.', 'presto-player' ), { |
| 42 | autoDismiss: 3000, |
| 43 | } ); |
| 44 | } catch ( err ) { |
| 45 | toast.error( __( 'Could not save settings.', 'presto-player' ), { |
| 46 | autoDismiss: 5000, |
| 47 | } ); |
| 48 | } |
| 49 | }, [ save ] ); |
| 50 | |
| 51 | useRegisterActivePage( registerActivePage, { |
| 52 | isDirty, |
| 53 | save: handleSave, |
| 54 | discard: reset, |
| 55 | } ); |
| 56 | |
| 57 | return { data, savedData, update, handleSave, isDirty, isSaving, isLoading }; |
| 58 | }; |
| 59 | |
| 60 | export default useSimpleSettingsPage; |
| 61 |