import React, { useEffect, useState } from 'react'; import { Box, Button, Field, Flex, Grid, SingleSelect, SingleSelectOption, Typography, } from '@strapi/design-system'; import { Check } from '@strapi/icons'; import { Layouts, Page, useFetchClient, useNotification, useRBAC, } from '@strapi/strapi/admin'; import { useIntl } from 'react-intl'; import { OptimizationQualityFields } from '../components/OptimizationQualityFields'; import { getTranslationKey, PLUGIN_ID, type GlobalOptimizationSettings, type OptimizationChoice, type OptimizationMode, } from '../pluginId'; const SETTINGS_READ = [{ action: 'plugin::image-optimizer.settings.read', subject: null }]; const SETTINGS_UPDATE = [{ action: 'plugin::image-optimizer.settings.update', subject: null }]; const CHOICES: OptimizationChoice[] = ['original', 'global', 'custom']; const MODES: OptimizationMode[] = ['webp', 'compress']; export const SettingsPage = () => { const { formatMessage } = useIntl(); const { get, put } = useFetchClient(); const { toggleNotification } = useNotification(); const { allowedActions: readActions } = useRBAC(SETTINGS_READ); const { allowedActions: updateActions } = useRBAC(SETTINGS_UPDATE); const canReadGlobal = readActions.canRead; const canUpdateGlobal = updateActions.canUpdate; const [globalSettings, setGlobalSettings] = useState({ defaultChoice: 'original', defaultMode: 'compress', webpQuality: 82, jpegQuality: 80, pngCompressionLevel: 9, }); const [isLoading, setIsLoading] = useState(true); const [isSaving, setIsSaving] = useState(false); useEffect(() => { const load = async () => { if (!canReadGlobal) { setIsLoading(false); return; } try { const { data: settings } = await get(`/${PLUGIN_ID}/settings`); setGlobalSettings(settings); } catch { toggleNotification({ type: 'danger', message: formatMessage({ id: getTranslationKey('settings.error') }), }); } finally { setIsLoading(false); } }; load(); }, [canReadGlobal, formatMessage, get, toggleNotification]); const handleSave = async () => { if (!canUpdateGlobal) { return; } setIsSaving(true); try { await put(`/${PLUGIN_ID}/settings`, globalSettings); toggleNotification({ type: 'success', message: formatMessage({ id: getTranslationKey('settings.saved') }), }); } catch { toggleNotification({ type: 'danger', message: formatMessage({ id: getTranslationKey('settings.error') }), }); } finally { setIsSaving(false); } }; if (isLoading) { return ; } if (!canReadGlobal) { return ( {formatMessage({ id: getTranslationKey('settings.page.title') })} {formatMessage({ id: getTranslationKey('settings.global.noPermission') })} ); } return ( {formatMessage({ id: getTranslationKey('settings.page.title') })} } size="S"> {formatMessage({ id: getTranslationKey('settings.save') })} ) : undefined } /> {formatMessage({ id: getTranslationKey('settings.global.defaultChoiceTitle') })} {formatMessage({ id: getTranslationKey('settings.global.defaultChoiceDescription') })} {formatMessage({ id: getTranslationKey('settings.global.defaultChoice') })} setGlobalSettings((prev) => ({ ...prev, defaultChoice: value })) } disabled={!canUpdateGlobal || isSaving} > {CHOICES.map((choice) => ( {formatMessage({ id: getTranslationKey(`choice.${choice}`) })} ))} {formatMessage({ id: getTranslationKey('settings.global.defaultChoiceHint') })} {formatMessage({ id: getTranslationKey('settings.global.profileTitle') })} {formatMessage({ id: getTranslationKey('settings.global.profileDescription') })} {formatMessage({ id: getTranslationKey('settings.global.defaultMode') })} setGlobalSettings((prev) => ({ ...prev, defaultMode: value })) } disabled={!canUpdateGlobal || isSaving} > {MODES.map((mode) => ( {formatMessage({ id: getTranslationKey(`settings.mode.${mode}`) })} ))} {formatMessage({ id: getTranslationKey('settings.global.defaultModeHint') })} setGlobalSettings((prev) => ({ ...prev, ...patch }))} disabled={!canUpdateGlobal || isSaving} namePrefix="global" /> ); }; export const ProtectedSettingsPage = () => { return ; };