import { CharacterHints } from "@/utils/generators/generatePassword"; import { ExtensionOptions, GeneratorOptions, useStorage } from "@/utils/storage"; import * as fui from "@fluentui/react-components"; import { ArrowUndoRegular } from "@fluentui/react-icons"; import { ReactElement } from "react"; import infoLabel from "../../utils/infoLabel"; import { useStyles } from "./SettingsSection.styles"; import { MAX_PASSWORD_LENGTH, MIN_PASSWORD_LENGTH } from "@/utils/constants"; export default function SettingsSection(): ReactElement { const { extOptions, generatorOptions, updateStorage } = useStorage(); const cls = useStyles(); const resetRange = useCallback(() => { updateStorage({ MinLength: defaultOptions.extension.MinLength, MaxLength: defaultOptions.extension.MaxLength }); }, []); const setOption = (option: keyof (GeneratorOptions & ExtensionOptions)) => (_: unknown, args: fui.CheckboxOnChangeData) => updateStorage({ [option]: args.checked }); const updateNumberField = (key: keyof (ExtensionOptions & GeneratorOptions), defaultValue: number) => (_: unknown, e: fui.InputOnChangeData): void => { if (e.value.length >= 1) { const value = parseInt(e.value, 10); if (!isNaN(value) && value >= 0) updateStorage({ [key]: value }); } else updateStorage({ [key]: defaultValue }); }; const validateMinLimit = () => { if (extOptions.MinLength < MIN_PASSWORD_LENGTH) updateStorage({ MinLength: MIN_PASSWORD_LENGTH }); else if (extOptions.MinLength > MAX_PASSWORD_LENGTH - 1) updateStorage({ MinLength: MAX_PASSWORD_LENGTH - 1, MaxLength: MAX_PASSWORD_LENGTH }); else if (extOptions.MinLength >= extOptions.MaxLength) updateStorage({ MaxLength: extOptions.MinLength + 1 }); }; const validateMaxLimit = () => { if (extOptions.MaxLength > MAX_PASSWORD_LENGTH) updateStorage({ MaxLength: MAX_PASSWORD_LENGTH }); else if (extOptions.MaxLength < MIN_PASSWORD_LENGTH + 1) updateStorage({ MinLength: MIN_PASSWORD_LENGTH, MaxLength: MIN_PASSWORD_LENGTH + 1 }); else if (extOptions.MaxLength <= extOptions.MinLength) updateStorage({ MinLength: extOptions.MaxLength - 1 }); }; const validateLength = () => { updateStorage({ Length: Math.max(Math.min(generatorOptions.Length, extOptions.MaxLength), extOptions.MinLength) }); }; return (
} onClick={ resetRange } />
{ i18n.t("common.sections.include") }
updateStorage({ IncludeCustomSet: e.value }) } />
{ i18n.t("common.sections.exclude") }
updateStorage({ ExcludeCustomSet: e.value }) } />
); }; const defaultOptions = { generator: new GeneratorOptions(), extension: new ExtensionOptions() };