wp-optimize
/
vendor
/
team-updraft
/
lib-onboarding-wizard
/
Wizard
/
Onboarding
/
src
/
components
/
Fields
/
Plugins.jsx
Plugins.jsx in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance trunk, at vendor/team-updraft/lib-onboarding-wizard/Wizard/Onboarding/src/components/Fields/Plugins.jsx
| 1 | import FieldWrapper from "./FieldWrapper"; |
| 2 | import {__} from "@wordpress/i18n"; |
| 3 | import CheckboxInput from "@/components/Fields/CheckboxInput"; |
| 4 | import Icon from "@/utils/Icon"; |
| 5 | import useOnboardingStore from "@/store/useOnboardingStore"; |
| 6 | import Tooltip from "../../utils/Tooltip/Tooltip"; |
| 7 | |
| 8 | const Plugins = ({ |
| 9 | field, |
| 10 | onChange, |
| 11 | value, |
| 12 | }) => { |
| 13 | |
| 14 | const { |
| 15 | areAllInstalled, |
| 16 | } = useOnboardingStore(); |
| 17 | |
| 18 | |
| 19 | // Convert array of objects to Record<string, string> |
| 20 | // convert value to array if not already |
| 21 | |
| 22 | // Convert an incoming value to an array if not already one |
| 23 | let valueValidated = value; |
| 24 | if (!Array.isArray(valueValidated)) { |
| 25 | valueValidated = valueValidated === '' ? [] : [valueValidated]; |
| 26 | } |
| 27 | |
| 28 | const selected = Array.isArray(valueValidated) ? valueValidated : []; |
| 29 | |
| 30 | /** |
| 31 | * Handles a change on an individual checkbox. |
| 32 | * For boolean mode, simply toggles the value. |
| 33 | * Otherwise, adds or removes the selected option. |
| 34 | */ |
| 35 | const handleCheckboxChange = (_checked, option) => { |
| 36 | const newSelected = selected.includes('' + option) || selected.includes(parseInt(option)) |
| 37 | ? selected.filter((item) => item !== '' + option && item !== parseInt(option)) |
| 38 | : [...selected, option]; |
| 39 | onChange(newSelected); |
| 40 | |
| 41 | }; |
| 42 | |
| 43 | let options = Object.values(field.options) || []; |
| 44 | |
| 45 | const optionsState = useOnboardingStore((state) => { |
| 46 | const pluginStep = state.getPluginsStep(); |
| 47 | const pluginField = pluginStep?.fields?.find(f => f.id === 'plugins'); |
| 48 | return pluginField?.options ?? []; |
| 49 | }); |
| 50 | |
| 51 | /** |
| 52 | * Determines if an option is considered checked. |
| 53 | */ |
| 54 | const isEnabled = (optionId) => { |
| 55 | return selected.includes('' + optionId) || selected.includes(parseInt(optionId)); |
| 56 | }; |
| 57 | |
| 58 | if (Object.keys(options).length === 0) { |
| 59 | return <>{__('No options found', 'ONBOARDING_WIZARD_TEXT_DOMAIN')}</>; |
| 60 | } |
| 61 | const allInstalled = areAllInstalled(options ?? []); |
| 62 | |
| 63 | return ( |
| 64 | <FieldWrapper inputId={field.id} label={field.label}> |
| 65 | {!allInstalled && ( |
| 66 | <div className="flex flex-col space-y-2 gap-1"> |
| 67 | {optionsState.map((option) => |
| 68 | { |
| 69 | const key = option.id || option.value; |
| 70 | const label = option.title || key; |
| 71 | const isInstalled = option.action === 'installed' || option.action === 'upgrade-to-pro' ; |
| 72 | return ( |
| 73 | <div key={key}> |
| 74 | <div |
| 75 | className="flex items-center" |
| 76 | > |
| 77 | <CheckboxInput |
| 78 | label={label} |
| 79 | onChange={(checked) => handleCheckboxChange(!!checked, key)} |
| 80 | value={isEnabled(key)} |
| 81 | id={`${field.id}_${key}`} |
| 82 | disabled={isInstalled} |
| 83 | /> |
| 84 | <label className={`ml-2 flex ${isInstalled ? 'cursor-not-allowed' : ''}`} htmlFor={`${field.id}_${key}`}> |
| 85 | <div className="text-md font-medium text-black mr-2">{}{label}</div> |
| 86 | </label> |
| 87 | {isInstalled |
| 88 | ? <Tooltip tooltip={{ |
| 89 | text: __('Already installed', 'ONBOARDING_WIZARD_TEXT_DOMAIN') |
| 90 | }}> |
| 91 | <Icon |
| 92 | name="circle-check" |
| 93 | color="green" |
| 94 | size="18" |
| 95 | strokeWidth={2.5} |
| 96 | /> |
| 97 | </Tooltip> |
| 98 | : <Icon |
| 99 | name="link-arrow" |
| 100 | fill="#888D91" |
| 101 | size="12" |
| 102 | onClick={() => { |
| 103 | window.open("https://wordpress.org/plugins/"+option['slug'], "_blank", "noopener,noreferrer"); |
| 104 | }} |
| 105 | /> |
| 106 | } |
| 107 | </div> |
| 108 | {option.description && ( |
| 109 | <div className="text-md text-[var(--teamupdraft-grey-600)] ml-8"> |
| 110 | {option.description} |
| 111 | </div> |
| 112 | )} |
| 113 | </div> |
| 114 | ) |
| 115 | } |
| 116 | )} |
| 117 | </div> |
| 118 | )} |
| 119 | </FieldWrapper> |
| 120 | ); |
| 121 | }; |
| 122 | |
| 123 | export default Plugins; |