one-click-accessibility/modules/scanner/assets/js/context/scanner-wizard-context.js
VasylD beb8deda2e
[APP-907] Accessibility assistant (#326)
* PHPCS and remediation POC WIP

* WIP Wizard module

* Added scanner module

* [APP-929][APP-930] Add initial setup for wizard, added btn to the topbar (#230)

* [APP-929][APP-930]

* [APP-929][APP-930]

* update logic

* update init app

* [APP-0000] store plan scope after register/switch

* change return of load

* add header

* Move into shadow dom, add context, add headers components

* add UI components

* add UI components

* add UI components

* merge current develop

* merge current develop

* Add AltText Form

* Add scroll to current element

* Align with current design, add loader, added preview for svg

* Align with current design, add loader, added preview for svg

* Resolve comments

* Resolve comments

* Resolve comments

* [APP-934] add submit logic (#259)

* [APP-934] add submit logic

* [APP-934] add submit logic

* [APP-934] add submit logic

* [APP-934] add submit logic

* Added replace remediation action

* Add submit logic

* Add submit alt text logic, generate AI alt text

* Add AI generate request, add convert from SVG to png base64, added manual fix block

* Add AI generate request, add convert from SVG to png base64, added manual fix block

* Add texts, add remediation submit, fix logic to store remediation

* Add texts, add remediation submit, fix logic to store remediation

* Add texts, add remediation submit, fix logic to store remediation

* Add texts, add remediation submit, fix logic to store remediation

* Add texts, add remediation submit, fix logic to store remediation

* Add texts, add remediation submit, fix logic to store remediation

---------

Co-authored-by: Raz Ohad <admin@bainternet.info>

* Add texts, add remediation submit, fix logic to store remediation

* [APP-0000] add store request to the Service API (#268)

* [APP-0000] add store request to the Service API

* [APP-0000] add store request to the Service API

* [APP-0000] add store request to the Service API

* [APP-0000] add store request to the Service API

* [APP-0000] add request to store scanner summary

* [APP-0000] add request to store scanner summary

* [APP-0000] add request to store scanner summary

* [APP-1432] fix with AI flow (#275)

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1446] sidebar menu update (#276)

* update: simplify menu

* add: headings and scanner menu options

* [APP-945] add column for tables (#282)

* [APP-945] add column for tables

* [APP-945] add column for tables

* [APP-945] add column for tables

* [APP-945] add column for tables

* [APP-1447] Updated quota bar designs (#278)

* update: quota bar designs

* fix: the popover was partially visible

* update: AI credits name

* update: usage calculation logic

* add: quota indicators

* update: quotas tooltip text

* update: quotas notices text

* update: make toggle icon dynamic

* fix: @elementor/ui imports

* Update modules/settings/assets/js/layouts/quota-bar.js

Co-authored-by: VasylD <vasyld@elementor.red>

* update: styling of component

---------

Co-authored-by: VasylD <vasyld@elementor.red>

* [APP-1507] add UI changes (#286)

* [APP-1507] add UI changes

* [APP-1507] add UI changes

* [APP-1507] add UI changes

* [APP-1507] add UI changes

* [APP-1507] add UI changes

* [APP-1507] add UI changes

* [APP-1507] add UI changes

* [APP-1387] add mixpanel events (#288)

* [APP-1387] add mixpanel events

* [APP-1387] add mixpanel events

* New: Add the a11y assistant dashboard's UI [APP-1445] (#289)

* [APP-1513] add disconnected msg (#290)

* [APP-1513] add disconnected msg

* [APP-1513] add disconnected msg

* [APP-1513] add disconnected msg

* [APP-1547][APP-1548][APP-948] update alt text view, fix list issues (#291)

* [APP-1547][APP-1548][APP-948] update alt text view, fix list issues, add rest route for update status

* [APP-1547][APP-1548][APP-948] update alt text view, fix list issues, add rest route for update status

* fix: infotip icon padding and font style (#293)

* fix: infotip icon padding and font style

* fix: infotip icon padding and font style

* update: caniuse database

* [APP-1525] add edit mode (#294)

* [APP-1525] add edit mode

* [APP-1525] add edit mode

* [APP-1525] add edit mode

* Bug/app 1544 (#295)

* Fix: Resolve QA comments [APP-1544]

* New: Add beta tags [APP-1555]

* New: Add the "no results" state [APP-1544]

* [APP-1554] rename violations (#297)

* [APP-1512] add backend logic for remediation management (#298)

* [APP-1512] add backend logic for remediation management

* [APP-1512] add backend logic for remediation management

* Update modules/remediation/database/remediation-entry.php

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

* Update modules/remediation/rest/items.php

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

* [APP-1512] add backend logic for remediation management

---------

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

* Fix: Resolve QA comments [n/a] (#299)

* [APP-1512] add FE remediation management (#300)

* [APP-1512] add backend logic for remediation management

* [APP-1512] add backend logic for remediation management

* Update modules/remediation/database/remediation-entry.php

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

* Update modules/remediation/rest/items.php

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

* [APP-1512] add backend logic for remediation management

* [APP-1512] add FE remediation management

* [APP-1512] add FE remediation management

* [APP-1512] add FE remediation management

* [APP-1512] add FE remediation management

* [APP-1512] add FE remediation management

* [APP-1512] add FE remediation management

* [APP-1512] add FE remediation management

* [APP-1512] add FE remediation management

---------

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

* [APP-1609] fix tooltip

* [APP-1609] fix resolved btn click

* [APP-1401] clear cache on update (#302)

* [APP-1401] clear cache on update

* [APP-1401] clear cache on update

* [APP-1401] clear cache on update

* [APP-1401] clear cache on update

* [APP-1401] clear cache on update

* update with latest develop branch

* fix copies

* [APP-1619] add mixpanel event (#303)

* [APP-1619] add mixpanel event

* [APP-1619] add mixpanel event

* [APP-1593] Add locked variant for quota bar (#301)

* update: add locked version of quota status bar

* fix: text color for infotip

* fix: width issues

* fix: add check for 0

* [APP-1637][APP-1643] add UI fixes, add excluded rules array (#307)

* fix: make sidebar collapsible (#306)

* Merge current dev

* [APP-1603] Add dashboard menu (#309)

* [APP-1603] Add dashboard menu

* [APP-1603] Add dashboard menu

* [APP-1603] Add dashboard menu

* [APP-1603] Add dashboard menu

* Bug/app 1607 (#311)

* Fix: Update stats calculation logic [APP-1607]

* Fix: Set fixed width for the results table [APP-1650]

* Fix: Update filter rules [APP-1634]

* New: Add the no search results state [APP-1651]

* Fix: Convert indents [n/a]

* [APP-1670] add menu on Manage Fixes (#315)

* [APP-1670] add menu on Manage Fixes

* [APP-1670] update menu

* [APP-1611] fix comments

* fix: direction of snippet (#313)

* [APP-1561] New menu layout (#308)

* update: app menu and layout

* merge: latest changes from feature/remediation

* add: alert indicator to the closed sidebar

* fix: page layout for statement page

* update: menu display names

* fix: topbar menu layout

* update: sidebar menu width

* update: sidebar menu width

* fix: popup menu layout

* add: hover action to the toggle button

* update: my account menu

* fix: quota indicator for closed sidebar

* fix: icon alignments

* fix: scroll behaviour

* fix: page scroll behaviour

* fix: popup menu hover state

* update: quota bar and group layouts

* add: tooltips to the menu items

* update: make scans page fixed height and scrollable

* update: styles with theme references and added new styled components

* fix: make sidebar smoother

* update: accessibility page heading

* Merge latest develop

* [APP-1611] trigger save entry for clean cache (#314)

* [APP-1611] trigger save entry for clean cache

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-0000] call trigger save async (#319)

* [APP-1611] trigger save entry for clean cache

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-0000] call trigger save async

* fix: scrollbar issues (#318)

* [APP-1615] add remediation for wp image (#321)

* POC - FrontEnd remediations to handle Dynamic Data [APP-1644] (#312)

* POC - FrontEnd remediations to handle Dynamic Data

* Update modules/remediation/components/remediation-runner.php

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

* added MutationObserver to cover elements that are added later on the page

runs until remediations are all done, and disconnect

* added $use_frontend flag instead of using "false"

* Update modules/remediation/components/remediation-runner.php

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

* WIP on poc/fe-remediations

* [APP-0000] call trigger save async

* [APP-1644] apply FE remediation

* [APP-1644] apply FE remediation

* [APP-1644] apply FE remediation

* Add timeout (wait for FE remediation timeout)

* Add timeout (wait for FE remediation timeout)

* Add timeout (wait for FE remediation timeout)

* Add timeout (wait for FE remediation timeout)

* Add timeout (wait for FE remediation timeout)

* Small fixes

* Small fixes

* Small fixes

* Small fixes

---------

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

* [APP-1611] Fix cache issue (#324)

* [APP-1611] trigger save entry for clean cache

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-0000] call trigger save async

* [APP-1611] clear cache

* [APP-1657] upgrade cta (#323)

* update: added logic to show button based on plan

* Update modules/settings/assets/js/components/quota-bar/quota-bar-group.js

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

* fix: eslint error

---------

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

* [APP-1658] Update accessibility admin column design (#322)

* update: wpadmin column layout

* update: move icon to assets

* Fix: Resolve remediation encoding issue [APP-1646] (#325)

---------

Co-authored-by: Ohad <ohad@elementor.com>
Co-authored-by: Raz Ohad <admin@bainternet.info>
Co-authored-by: Nirbhay Singh <121793120+nirbhayel@users.noreply.github.com>
Co-authored-by: Pavlo Kniazevych <139438463+pkniazevych@users.noreply.github.com>
Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>
2025-07-07 16:25:11 +07:00

336 lines
8.5 KiB
JavaScript

import { mixpanelEvents, mixpanelService } from '@ea11y-apps/global/services';
import { APIScanner } from '@ea11y-apps/scanner/api/APIScanner';
import {
BLOCK_TITLES,
BLOCKS,
INITIAL_SORTED_VIOLATIONS,
MANAGE_URL_PARAM,
MANUAL_GROUPS,
} from '@ea11y-apps/scanner/constants';
import { scannerWizard } from '@ea11y-apps/scanner/services/scanner-wizard';
import {
focusOnElement,
removeExistingFocus,
} from '@ea11y-apps/scanner/utils/focus-on-element';
import { getElementByXPath } from '@ea11y-apps/scanner/utils/get-element-by-xpath';
import {
sortRemediation,
sortViolations,
} from '@ea11y-apps/scanner/utils/sort-violations';
import {
createContext,
useContext,
useEffect,
useState,
} from '@wordpress/element';
export const ScannerWizardContext = createContext({
results: {},
remediations: [],
currentScanId: null,
resolved: 0,
openedBlock: '',
loading: null,
isError: false,
isManage: false,
isChanged: false,
sortedViolations: INITIAL_SORTED_VIOLATIONS,
sortedRemediation: INITIAL_SORTED_VIOLATIONS,
altTextData: [],
manualData: {},
remediationData: {},
violation: null,
openIndex: null,
setSortedRemediation: () => {},
setOpenedBlock: () => {},
setResolved: () => {},
getResults: () => {},
setAltTextData: () => {},
setManualData: () => {},
setLoading: () => {},
setRemediationData: () => {},
updateRemediationList: () => {},
setIsManage: () => {},
setIsManageChanged: () => {},
isResolved: () => {},
handleOpen: () => {},
setOpenIndex: () => {},
runNewScan: () => {},
});
export const ScannerWizardContextProvider = ({ children }) => {
const [results, setResults] = useState();
const [remediations, setRemediations] = useState([]);
const [sortedViolations, setSortedViolations] = useState(
INITIAL_SORTED_VIOLATIONS,
);
const [sortedRemediation, setSortedRemediation] = useState(
INITIAL_SORTED_VIOLATIONS,
);
const [resolved, setResolved] = useState(0);
const [currentScanId, setCurrentScanId] = useState(null);
const [openedBlock, setOpenedBlock] = useState(BLOCKS.main);
const [loading, setLoading] = useState(true);
const [isError, setIsError] = useState(false);
const [isManage, setIsManage] = useState(false);
const [isManageChanged, setIsManageChanged] = useState(false);
const [altTextData, setAltTextData] = useState([]);
const [manualData, setManualData] = useState(structuredClone(MANUAL_GROUPS));
const [remediationData, setRemediationData] = useState(
structuredClone(MANUAL_GROUPS),
);
const [openIndex, setOpenIndex] = useState(null);
useEffect(() => {
const items = isManage
? sortedRemediation[openedBlock]
: sortedViolations[openedBlock];
if (
openIndex !== null &&
sortedViolations[openedBlock]?.length &&
openIndex < items?.length
) {
const element = isManage
? getElementByXPath(
JSON.parse(sortedRemediation[openedBlock][openIndex].content)
?.xpath,
)
: sortedViolations[openedBlock][openIndex].node;
focusOnElement(element);
} else {
removeExistingFocus();
}
}, [openIndex]);
useEffect(() => {
if (
Object.keys(sortedRemediation).every(
(key) => sortedRemediation[key].length === 0,
)
) {
setRemediations([]);
}
}, [sortedRemediation]);
const updateRemediationList = async () => {
try {
const items = await APIScanner.getRemediations(
window.ea11yScannerData?.pageData?.url,
);
const filteredRemediations = items.data.filter(
(remediation) => remediation.group !== BLOCKS.altText,
);
const sorted = sortRemediation(filteredRemediations);
setRemediations(filteredRemediations);
setSortedRemediation(sorted);
} catch (error) {
setIsError(true);
}
};
const handleOpen = (index, item) => (event, isExpanded) => {
setOpenIndex(isExpanded ? index : null);
if (!isManage) {
mixpanelService.sendEvent(mixpanelEvents.issueSelected, {
issue_type: item.message,
rule_id: item.ruleId,
wcag_level: item.reasonCategory.match(/\((AAA?|AA?|A)\)/)?.[1] || '',
category_name: BLOCK_TITLES[openedBlock],
});
}
};
const handleOpenBlock = (block) => {
setOpenedBlock(block);
setOpenIndex(null);
};
const initialViolations =
window.ea11yScannerData.initialScanResult?.counts?.violation ?? 0;
const violation =
results?.summary?.counts?.violation >= 0
? Math.max(initialViolations, results?.summary?.counts?.violation)
: null;
const registerPage = async (data, sorted) => {
try {
if (window?.ea11yScannerData?.pageData?.unregistered) {
await APIScanner.registerPage(
window?.ea11yScannerData?.pageData,
data.summary,
);
}
setResults(data);
setSortedViolations(sorted);
setAltTextData([]);
setManualData(structuredClone(MANUAL_GROUPS));
setResolved(
initialViolations > data.summary?.counts?.violation
? initialViolations - data.summary?.counts?.violation
: 0,
);
} catch (e) {
setIsError(true);
}
};
const addScanResults = async (data) => {
try {
const response = await APIScanner.addScanResults({
url: window?.ea11yScannerData?.pageData?.url,
summary: data.summary,
});
void APIScanner.triggerSave({
object_id: window?.ea11yScannerData?.pageData?.object_id,
object_type: window?.ea11yScannerData?.pageData?.object_type,
});
setCurrentScanId(response.scanId);
} catch (e) {
console.error(e);
setIsError(true);
}
};
const getResults = async () => {
setLoading(true);
try {
const url = new URL(window.location.href);
const data = await window.ace.check(document);
const filtered = data.results.filter(
(item) => item.level === 'violation',
);
const sorted = sortViolations(filtered);
if (data?.summary?.counts) {
data.summary.counts.issuesResolved = 0;
}
await registerPage(data, sorted);
await addScanResults(data);
mixpanelService.sendEvent(mixpanelEvents.scanTriggered, {
page_url: window.ea11yScannerData?.pageData?.url,
issue_count: data.summary?.counts?.violation,
source: url.searchParams.get('open-ea11y-assistant-src'),
});
return data.summary;
} catch (error) {
setIsError(true);
} finally {
setLoading(false);
}
};
useEffect(() => {
if (window.ea11yScannerData?.isConnected) {
//Wait for apply FE remediation
setTimeout(() => {
scannerWizard
.load()
.then(() => {
void getResults();
})
.catch(() => {
setIsError(true);
setLoading(false);
});
}, 500);
void updateRemediationList();
} else {
setLoading(false);
}
}, [window.ea11yScannerData?.isConnected]);
useEffect(() => {
const params = new URLSearchParams(window.location.search);
if (params.get(MANAGE_URL_PARAM) === '1') {
setIsManage(true);
handleOpenBlock(BLOCKS.management);
}
}, [window.location.search]);
const isResolved = (block) => {
const indexes = Array.from(
{
length: sortedViolations[block]?.length || 0,
},
(_, i) => i,
);
return block === BLOCKS.altText
? (altTextData?.length === sortedViolations[block]?.length &&
indexes.every((index) => index in altTextData) &&
altTextData.every((data) => data?.resolved)) ||
sortedViolations[block]?.length === 0
: (manualData[block]?.length === sortedViolations[block]?.length &&
indexes.every((index) => index in manualData[block]) &&
manualData[block].every((data) => data?.resolved)) ||
sortedViolations[block]?.length === 0;
};
const isChanged =
altTextData.length > 0 ||
Object.keys(manualData).some(
(key) => manualData[key] && manualData[key].length > 0,
) ||
isManageChanged;
const runNewScan = () => {
const url = new URL(window.location.href);
url.searchParams.delete('open-ea11y-assistant');
url.searchParams.delete('open-ea11y-assistant-src');
url.searchParams.append('open-ea11y-assistant-src', 'rescan_button');
url.searchParams.append('open-ea11y-assistant', '1');
window.location.assign(url);
};
return (
<ScannerWizardContext.Provider
value={{
results,
resolved,
remediations,
currentScanId,
openedBlock,
loading,
isError,
isManage,
isChanged,
sortedViolations,
sortedRemediation,
altTextData,
manualData,
remediationData,
violation,
setOpenedBlock: handleOpenBlock,
setResolved,
setSortedRemediation,
getResults,
setLoading,
setAltTextData,
setManualData,
setRemediationData,
updateRemediationList,
setIsManage,
setIsManageChanged,
openIndex,
setOpenIndex,
isResolved,
handleOpen,
runNewScan,
}}
>
{children}
</ScannerWizardContext.Provider>
);
};
export const useScannerWizardContext = () => {
return useContext(ScannerWizardContext);
};