one-click-accessibility/modules/settings/assets/js/contexts/plugin-settings.js
Nirbhay Singh fcaba863b4
[APP-1417] Add support for custom icon (#270)
* add: media upload button

* add: support for custom icon

* fix: code alignment, phpcs

* fix: code alignment and linting

* add: mixpanel events

* fix: styling of custom svg

* fix: missing variable

* fix: missing variable

* fix: load gutenberg block without css

* fix: icon spacing in preview

* fix: indentation

* fix: indents
2025-05-12 22:48:08 +05:30

64 lines
1.5 KiB
JavaScript

import { useToastNotification } from '@ea11y/hooks';
import {
createContext,
useCallback,
useContext,
useEffect,
useState,
} from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import API from '../api';
const PluginSettingsContext = createContext({});
export const PluginSettingsProvider = ({ children }) => {
const { error } = useToastNotification();
const [pluginSettings, setPluginSettings] = useState();
const [loaded, setLoaded] = useState(false);
const refreshPluginSettings = useCallback(() => {
API.getPluginSettings()
.then((settings) => {
if ('isConnected' in settings) {
settings.isConnected = Boolean(settings.isConnected);
}
if ('closePostConnectModal' in settings) {
settings.closePostConnectModal = Boolean(
settings.closePostConnectModal,
);
}
if ('isUrlMismatch' in settings) {
settings.isUrlMismatch = Boolean(settings.isUrlMismatch);
}
if ('unfilteredUploads' in settings) {
settings.unfilteredUploads = Boolean(settings.unfilteredUploads);
}
setPluginSettings(settings);
setLoaded(true);
})
.catch(() => {
error(__('An error occurred.', 'pojo-accessibility'));
setLoaded(true);
});
}, []);
useEffect(() => {
refreshPluginSettings();
}, [refreshPluginSettings]);
return (
<PluginSettingsContext.Provider
value={{ ...pluginSettings, loaded, refreshPluginSettings }}
>
{children}
</PluginSettingsContext.Provider>
);
};
export const usePluginSettingsContext = () => {
return useContext(PluginSettingsContext);
};