one-click-accessibility/modules/settings/assets/js/contexts/analytics-context.js
Nirbhay Singh 50fe1acead
[APP-1730] Add info details (#344)
* update: refactor the toggle function

* add: tooltip to analytics toggle

* update: confirm dialog modal content

* update: import

* update: not enough data alert content

* add: action to the alert and update text

* update: alert and infotip color based on previous data

* add: onclick handler to enable tracking

* update: alertboxes colors

* fix: show info only when analytics are enabled

* update: manually control the tooltip behaviour

* Update modules/settings/assets/js/components/sidebar-menu/tooltips/analytics.js

Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>

* fix: linter errors

---------

Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>
2025-08-05 11:57:32 +05:30

84 lines
1.9 KiB
JavaScript

import { useStorage } from '@ea11y/hooks';
import { mixpanelEvents, mixpanelService } from '@ea11y-apps/global/services';
import {
createContext,
useContext,
useEffect,
useState,
} from '@wordpress/element';
import APISettings from '../api';
const AnalyticsContext = createContext(null);
export const AnalyticsContextProvider = ({ children }) => {
const { save } = useStorage();
const [isAnalyticsEnabled, setIsAnalyticsEnabled] = useState(false);
const [isProVersion, setIsProVersion] = useState(false);
const [loading, setLoading] = useState(false);
const [period, setPeriod] = useState(30);
const [stats, setStats] = useState({
dates: [],
elements: [],
});
const [showConfirmPopup, setShowConfirmPopup] = useState(false);
/**
* Get initial logs list
*/
useEffect(() => {
if (isProVersion) {
setLoading(true);
void APISettings.getStatistic({ period }).then((data) => {
setStats(data);
setLoading(false);
});
}
}, [period, isAnalyticsEnabled, isProVersion]);
const updateIsAnalyticsEnabled = async () => {
await save({
ea11y_analytics_enabled: !isAnalyticsEnabled,
});
setIsAnalyticsEnabled(!isAnalyticsEnabled);
setPeriod(30);
};
const handleAnalyticsToggle = () => {
if (isAnalyticsEnabled) {
updateIsAnalyticsEnabled();
} else {
setShowConfirmPopup(true);
}
mixpanelService.sendEvent(mixpanelEvents.toggleClicked, {
state: isAnalyticsEnabled ? 'off' : 'on',
type: 'Enable analytics',
});
};
return (
<AnalyticsContext.Provider
value={{
isAnalyticsEnabled,
setIsAnalyticsEnabled,
isProVersion,
setIsProVersion,
updateIsAnalyticsEnabled,
handleAnalyticsToggle,
period,
setPeriod,
stats,
loading,
showConfirmPopup,
setShowConfirmPopup,
}}
>
{children}
</AnalyticsContext.Provider>
);
};
export const useAnalyticsContext = () => {
return useContext(AnalyticsContext);
};