theme.keycloak.v2.admin.resources.assets.TimeSelector-DcQmr_f3.js.map Maven / Gradle / Ivy
{"version":3,"file":"TimeSelector-DcQmr_f3.js","sources":["../../src/components/time-selector/TimeSelector.tsx"],"sourcesContent":["import {\n KeycloakSelect,\n KeycloakSelectProps,\n SelectVariant,\n} from \"@keycloak/keycloak-ui-shared\";\nimport {\n SelectOption,\n Split,\n SplitItem,\n TextInput,\n TextInputProps,\n} from \"@patternfly/react-core\";\nimport { useEffect, useMemo, useState } from \"react\";\nimport { useTranslation } from \"react-i18next\";\n\nexport type Unit = \"second\" | \"minute\" | \"hour\" | \"day\";\n\ntype TimeUnit = { unit: Unit; label: string; multiplier: number };\n\nconst allTimes: TimeUnit[] = [\n { unit: \"second\", label: \"times.seconds\", multiplier: 1 },\n { unit: \"minute\", label: \"times.minutes\", multiplier: 60 },\n { unit: \"hour\", label: \"times.hours\", multiplier: 3600 },\n { unit: \"day\", label: \"times.days\", multiplier: 86400 },\n];\n\nexport type TimeSelectorProps = Omit<\n TextInputProps,\n \"onChange\" | \"defaultValue\"\n> &\n Pick & {\n value?: number;\n units?: Unit[];\n onChange?: (time: number | string) => void;\n className?: string;\n };\n\nconst getTimeUnit = (units: TimeUnit[], value = 0) =>\n units.reduce(\n (v, time) =>\n value % time.multiplier === 0 && v.multiplier < time.multiplier\n ? time\n : v,\n units[0],\n );\n\nexport const toHumanFormat = (value: number, locale: string) => {\n const timeUnit = getTimeUnit(allTimes, value);\n const formatter = new Intl.NumberFormat(locale, {\n style: \"unit\",\n unit: timeUnit.unit,\n unitDisplay: \"long\",\n });\n return formatter.format(value / timeUnit.multiplier);\n};\n\nexport const TimeSelector = ({\n value,\n units = [\"second\", \"minute\", \"hour\", \"day\"],\n onChange,\n className,\n min,\n menuAppendTo,\n ...rest\n}: TimeSelectorProps) => {\n const { t } = useTranslation();\n\n const [lastMultiplier, setLastMultiplier] = useState();\n\n const defaultMultiplier = useMemo(\n () => allTimes.find((time) => time.unit === units[0])?.multiplier,\n [units],\n );\n\n const [timeValue, setTimeValue] = useState<\"\" | number>(\"\");\n const [multiplier, setMultiplier] = useState(defaultMultiplier);\n const [open, setOpen] = useState(false);\n\n const times = useMemo(() => {\n const filteredUnits = units.map(\n (unit) => allTimes.find((time) => time.unit === unit)!,\n );\n if (\n !filteredUnits.every((u) => u.multiplier === multiplier) &&\n filteredUnits[0] !== allTimes[0]\n ) {\n filteredUnits.unshift(allTimes[0]);\n }\n return filteredUnits;\n }, [units, multiplier]);\n\n useEffect(() => {\n const multiplier = getTimeUnit(times, value).multiplier;\n\n if (value) {\n setMultiplier(multiplier);\n setTimeValue(value / multiplier);\n setLastMultiplier(multiplier);\n } else {\n setTimeValue(value || \"\");\n setMultiplier(lastMultiplier ?? defaultMultiplier);\n setLastMultiplier(lastMultiplier ?? defaultMultiplier);\n }\n }, [value, defaultMultiplier]);\n\n const updateTimeout = (\n timeout: \"\" | number,\n times: number | undefined = multiplier,\n ) => {\n if (timeout !== \"\") {\n onChange?.(timeout * (times || 1));\n setTimeValue(timeout);\n } else {\n onChange?.(\"\");\n }\n };\n\n return (\n \n \n {\n updateTimeout(\"\" === value ? value : parseInt(value));\n }}\n />\n \n \n {\n setMultiplier(value as number);\n updateTimeout(timeValue, value as number);\n setOpen(false);\n }}\n menuAppendTo={menuAppendTo}\n selections={multiplier}\n onToggle={() => {\n setOpen(!open);\n }}\n isOpen={open}\n isDisabled={rest.isDisabled}\n >\n {times.map((time) => (\n \n {t(time.label)}\n \n ))}\n \n \n \n );\n};\n"],"names":["allTimes","getTimeUnit","units","value","v","time","toHumanFormat","locale","timeUnit","TimeSelector","onChange","className","min","menuAppendTo","rest","t","useTranslation","lastMultiplier","setLastMultiplier","useState","defaultMultiplier","useMemo","timeValue","setTimeValue","multiplier","setMultiplier","open","setOpen","times","filteredUnits","unit","u","useEffect","updateTimeout","timeout","jsxs","Split","jsx","SplitItem","TextInput","_event","KeycloakSelect","SelectVariant","SelectOption"],"mappings":"qMAmBA,MAAMA,EAAuB,CA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