one-click-accessibility/modules/scanner/assets/js/components/alt-text-form/index.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

250 lines
7.1 KiB
JavaScript

import AIIcon from '@elementor/icons/AIIcon';
import CircleCheckFilledIcon from '@elementor/icons/CircleCheckFilledIcon';
import AlertTitle from '@elementor/ui/AlertTitle';
import Box from '@elementor/ui/Box';
import Button from '@elementor/ui/Button';
import Checkbox from '@elementor/ui/Checkbox';
import CircularProgress from '@elementor/ui/CircularProgress';
import Divider from '@elementor/ui/Divider';
import FormHelperText from '@elementor/ui/FormHelperText';
import IconButton from '@elementor/ui/IconButton';
import Infotip from '@elementor/ui/Infotip';
import InputAdornment from '@elementor/ui/InputAdornment';
import TextField from '@elementor/ui/TextField';
import Tooltip from '@elementor/ui/Tooltip';
import Typography from '@elementor/ui/Typography';
import PropTypes from 'prop-types';
import { useToastNotification } from '@ea11y-apps/global/hooks';
import { mixpanelEvents, mixpanelService } from '@ea11y-apps/global/services';
import { ImagePreview } from '@ea11y-apps/scanner/components/alt-text-form/image-preview';
import BulkAltTextBanner from '@ea11y-apps/scanner/components/bulk-alt-text/bulk-alt-text-banner';
import { SetGlobal } from '@ea11y-apps/scanner/components/manage-footer-actions/page/set-global';
import { UpgradeContent } from '@ea11y-apps/scanner/components/upgrade-info-tip/upgrade-content';
import { AI_QUOTA_LIMIT, IS_PRO_PLAN } from '@ea11y-apps/scanner/constants';
import { useScannerWizardContext } from '@ea11y-apps/scanner/context/scanner-wizard-context';
import { useAltTextForm } from '@ea11y-apps/scanner/hooks/use-alt-text-form';
import { StyledLabel } from '@ea11y-apps/scanner/styles/alt-text-form.styles';
import { StyledAlert, StyledBox } from '@ea11y-apps/scanner/styles/app.styles';
import { scannerItem } from '@ea11y-apps/scanner/types/scanner-item';
import { __ } from '@wordpress/i18n';
export const AltTextForm = ({ item, current, setCurrent, setIsEdit }) => {
const { isManage } = useScannerWizardContext();
const { error } = useToastNotification();
const {
isGlobal,
setIsGlobal,
data,
loadingAiText,
isSubmitDisabled,
loading,
handleChange,
handleCheck,
handleSubmit,
handleUpdate,
generateAltText,
} = useAltTextForm({
current,
item,
});
const onGlobalChange = (value) => {
setIsGlobal(value);
};
const onCancel = () => {
setIsEdit(false);
};
const onSubmit = async () => {
try {
await handleSubmit();
setCurrent(current + 1);
} catch (e) {
error(__('An error occurred.', 'pojo-accessibility'));
}
};
const onUpdate = async () => {
await handleUpdate();
void (setIsEdit ? setIsEdit(false) : setCurrent(current + 1));
};
const onUpgradeHover = () => {
mixpanelService.sendEvent(mixpanelEvents.upgradeSuggestionViewed, {
current_plan: window.ea11yScannerData?.planData?.plan?.name,
action_trigger: 'fix_with_ai',
feature_locked: 'AI alt-text',
});
};
const applyBtnText = isManage
? __('Apply changes', 'pojo-accessibility')
: __('Apply fix', 'pojo-accessibility');
return (
<>
<Divider />
<BulkAltTextBanner />
<StyledBox padding={2}>
<ImagePreview element={item.node} />
<StyledLabel>
<Checkbox
checked={data?.[current]?.makeDecorative ?? false}
color="info"
sx={{ margin: '-7px 0 0 -10px' }}
size="small"
onChange={handleCheck}
/>
<Box>
<Typography variant="body1">
{__('Mark image as decorative', 'pojo-accessibility')}
</Typography>
<FormHelperText sx={{ mt: 0 }}>
{__(
"(decorative images don't need description)",
'pojo-accessibility',
)}
</FormHelperText>
</Box>
</StyledLabel>
{!data?.[current]?.makeDecorative ? (
<TextField
placeholder={__(
'Add or generate the description here',
'pojo-accessibility',
)}
aria-label={__(
'Add or generate the description here',
'pojo-accessibility',
)}
color="secondary"
value={data?.[current]?.altText ?? ''}
onChange={handleChange}
fullWidth
multiline
disabled={loadingAiText}
InputProps={{
endAdornment: (
<InputAdornment position="end">
{IS_PRO_PLAN && AI_QUOTA_LIMIT ? (
<Tooltip
placement="top-end"
title={__(
'Generate an Alt text description with AI.',
'pojo-accessibility',
)}
PopperProps={{
disablePortal: true,
}}
slotProps={{
tooltip: {
sx: {
maxWidth: '101px',
whiteSpace: 'normal',
lineHeight: 1.4,
},
},
}}
>
<IconButton
size="small"
onClick={generateAltText}
disabled={loadingAiText}
>
{loadingAiText ? (
<CircularProgress color="info" size={24} />
) : (
<AIIcon color="info" />
)}
</IconButton>
</Tooltip>
) : (
<Infotip
placement="top-end"
slotProps={{
tooltip: {
id: 'ai-btn-description',
},
}}
PopperProps={{
disablePortal: true,
}}
content={<UpgradeContent isAlt />}
>
<IconButton
size="small"
aria-labelledby="ai-btn-description"
onHover={onUpgradeHover}
>
<AIIcon color="promotion" />
</IconButton>
</Infotip>
)}
</InputAdornment>
),
}}
/>
) : (
<Box display="flex" gap={1}>
<CircleCheckFilledIcon color="success" />
<Typography variant="body1">
{__('no description needed', 'pojo-accessibility')}
</Typography>
</Box>
)}
<StyledAlert color="info" sx={{ p: 2 }} icon={false}>
<Box>
<AlertTitle sx={{ display: 'inline-block' }}>
{__('Tips:', 'pojo-accessibility')}
</AlertTitle>
{__(
"Keep descriptions short and simple, describing what the image shows or why it's on the page.",
'pojo-accessibility',
)}
</Box>
</StyledAlert>
<Box>
{!isManage && (
<SetGlobal
item={item}
onGlobalChange={onGlobalChange}
isChecked={isGlobal}
/>
)}
<Box display="flex" gap={1} justifyContent="flex-end">
{isManage && (
<Button color="secondary" variant="text" onClick={onCancel}>
{__('Cancel', 'pojo-accessibility')}
</Button>
)}
<Button
variant="contained"
color="info"
fullWidth={!isManage}
loading={loading}
disabled={isSubmitDisabled}
onClick={
isManage || data?.[current]?.resolved ? onUpdate : onSubmit
}
sx={{ mt: isManage ? 0 : 1.5 }}
>
{isGlobal
? __('Apply to all', 'pojo-accessibility')
: applyBtnText}
</Button>
</Box>
</Box>
</StyledBox>
</>
);
};
AltTextForm.propTypes = {
item: scannerItem,
current: PropTypes.number.isRequired,
setCurrent: PropTypes.func.isRequired,
setIsEdit: PropTypes.func,
};