one-click-accessibility/modules/scanner/assets/js/context/bulk-generation-context.js
Nirbhay Singh 63ed290ba7
Feature/app 2261 bulk alt manager (#512) (#524)
* add: preview images for bulk-alt-text

* add: bulk alt text banner

* fix: padding

* add: bulk alt text banner component

* add: icons

* add: support for aborting the fetch request

* add: bulk alt text manager

* update: husky

* fix: optimized images

* update: memoize images

* fix: imports

* add: focustrap

* add: mixpanel events

* update: add logic to save alt text when editing manually

* fix: abort controller reference and cleanup

* fix: improve key stability in image grid rendering

* refactor: add PropTypes validation and memoization to ImageCard component

* feat: add error handling for ImageCard component using ErrorBoundary

* fix: auto selection logic for card with bulk generation

* add: loading and generation states

* refactor: restructure bulk alt text components and enhance progress handling and decentralized logic

* refactor: extract logic from progress bar to hook

* add: quota exceeded alert

* add: promotional banner for generate button

* update: Generate button for selection mode

* fix: button alignment

* update: hide banner for 2 or less images

* fix: box shadow

* add: mixpanel events

* update: rename variable

* add: bulk upgrade url

* update: link handling for upgrade

* fix: box shadow

* add: border radius to dialog

* update: improved accessibility notifications

* fix: height and padding for banner

* update: add more context to image card

* add: ARIA labels

* add: focus on card

* add: decode URI component

* fix: accessibility issues

* add: encoding for file names

* fix: styled components

* update: made progress bar sticky

* update: add logic to disable button on bulk generation

* fix: style

* fix: closing the dialog while generating

* add: error handling

* fix: mark prop as not required

* Fix: Prevent MUI from adding aria-hidden to dialog parent elements [APP-2261]

* add: error handling for quota api error

* fix: prop requirement

* add: close handler to dialog

* fix: ui

* fix: icons

* fix: width of dialog

* fix: width of bulk dialog

---------

Co-authored-by: Kniazevych <pavlok@elementor.red>
2026-02-18 21:52:36 +02:00

143 lines
3.5 KiB
JavaScript

import PropTypes from 'prop-types';
import { speak } from '@wordpress/a11y';
import {
createContext,
useContext,
useState,
useRef,
useCallback,
} from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
const BulkGenerationContext = createContext(null);
export const BulkGenerationProvider = ({ children }) => {
const [currentGeneratingIndex, setCurrentGeneratingIndex] = useState(null);
const [isGenerating, setIsGenerating] = useState(false);
const [quotaError, setQuotaError] = useState(null);
const shouldAbortRef = useRef(false);
const queueRef = useRef([]);
const progressRef = useRef({ completed: 0, total: 0, errors: 0 });
const [progress, setProgress] = useState({
completed: 0,
total: 0,
errors: 0,
});
const startBulkGeneration = useCallback((cardIndices) => {
shouldAbortRef.current = false;
queueRef.current = [...cardIndices];
progressRef.current = {
completed: 0,
total: cardIndices.length,
errors: 0,
};
setProgress({ completed: 0, total: cardIndices.length, errors: 0 });
setQuotaError(null);
setIsGenerating(true);
if (cardIndices.length > 0) {
setCurrentGeneratingIndex(cardIndices[0]);
} else {
setIsGenerating(false);
}
}, []);
const stopBulkGeneration = useCallback(() => {
shouldAbortRef.current = true;
queueRef.current = [];
setCurrentGeneratingIndex(null);
setIsGenerating(false);
}, []);
const onCardComplete = useCallback((success) => {
progressRef.current.completed += 1;
if (!success) {
progressRef.current.errors += 1;
}
setProgress({ ...progressRef.current });
queueRef.current.shift();
if (shouldAbortRef.current || queueRef.current.length === 0) {
setCurrentGeneratingIndex(null);
setIsGenerating(false);
// Announce completion
if (queueRef.current.length === 0) {
const { completed, errors } = progressRef.current;
if (errors > 0) {
const message = sprintf(
// Translators: %1$d successful count, %2$d failed count
__(
'Generation complete. %1$d succeeded, %2$d failed',
'pojo-accessibility',
),
completed - errors,
errors,
);
speak(message, 'assertive');
} else {
const message = sprintf(
// Translators: %d number of images
__(
'Generation complete. %d images processed successfully',
'pojo-accessibility',
),
completed,
);
speak(message, 'assertive');
}
}
} else {
setCurrentGeneratingIndex(queueRef.current[0]);
}
}, []);
const resetProgress = useCallback(() => {
setIsGenerating(false);
shouldAbortRef.current = false;
queueRef.current = [];
setCurrentGeneratingIndex(null);
progressRef.current = { completed: 0, total: 0, errors: 0 };
setProgress({ completed: 0, total: 0, errors: 0 });
setQuotaError(null);
}, []);
const setQuotaExceeded = useCallback((errorMessage) => {
setQuotaError(errorMessage);
shouldAbortRef.current = true;
queueRef.current = [];
setCurrentGeneratingIndex(null);
setIsGenerating(false);
speak(errorMessage, 'assertive');
}, []);
const value = {
currentGeneratingIndex,
isGenerating,
shouldAbort: shouldAbortRef,
progress,
quotaError,
startBulkGeneration,
stopBulkGeneration,
onCardComplete,
resetProgress,
setQuotaExceeded,
};
return (
<BulkGenerationContext.Provider value={value}>
{children}
</BulkGenerationContext.Provider>
);
};
BulkGenerationProvider.propTypes = {
children: PropTypes.node.isRequired,
};
export const useBulkGeneration = () => {
const context = useContext(BulkGenerationContext);
return context;
};