📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
Added uptime settings form validation
Blue Mouse498c432c9ad9
diff --git a/app/pages/Project/uptime/Settings.tsx b/app/pages/Project/uptime/Settings.tsxindex 012f0de33..bc087361c 100644--- a/app/pages/Project/uptime/Settings.tsx+++ b/app/pages/Project/uptime/Settings.tsx@@ -1,4 +1,4 @@-import React, { useEffect, useMemo, useState } from 'react'+import React, { ChangeEvent, ChangeEventHandler, useEffect, useId, useMemo, useState } from 'react'import { useNavigate, useLocation } from '@remix-run/react'import { useTranslation } from 'react-i18next'import { ExclamationTriangleIcon } from '@heroicons/react/24/outline'@@ -9,6 +9,8 @@ import _replace from 'lodash/replace'import _join from 'lodash/join'import _split from 'lodash/split'import _find from 'lodash/find'+import _includes from 'lodash/includes'+import _isNaN from 'lodash/isNaN'import _map from 'lodash/map'import _keys from 'lodash/keys'import _filter from 'lodash/filter'@@ -28,9 +30,121 @@ import { alertsActions } from 'redux/reducers/alerts'import UIActions from 'redux/reducers/ui'import { useRequiredParams } from 'hooks/useRequiredParams'import Select from 'ui/Select'+import { formatTime } from 'utils/date'+import { isValidUrl } from 'utils/validator'const MONITOR_TYPES = ['HTTP']+const INTERVALS_IN_SECONDS = [+ 30, // 30 seconds+ 60, // 1 minute+ 120, // 2 minutes+ 180, // 3 minutes+ 300, // 5 minutes+ 600, // 10 minutes+ 900, // 15 minutes+ 1200, // 20 minutes+ 1800, // 30 minutes+ 2700, // 45 minutes+ 3600, // 1 hour+ 7200, // 2 hours+ 10800, // 3 hours+ 21600, // 6 hours+ 43200, // 12 hours+ 57600, // 16 hours+ 72000, // 20 hours+ 86400, // 24 hours+]++const INTERVALS_ALLOWED_DATALIST = [30, 60, 300, 1800, 3600, 21600, 86400]++const getIntervalDisplayName = (interval: number, language: string, style: Intl.RelativeTimeFormatStyle = 'short') => {+ let displayName++ if (interval < 60) {+ displayName = formatTime(interval, 'second', language, style)+ } else if (interval < 3600) {+ displayName = formatTime(interval / 60, 'minute', language, style)+ } else {+ displayName = formatTime(interval / 3600, 'hour', language, style)+ }++ return displayName+}++interface IntervalSelectorProps {+ value?: number+ onChange: ChangeEventHandler<HTMLInputElement>+ label: string+ hint: string+ name: string+}++const IntervalSelector = ({ value, onChange, label, hint, name }: IntervalSelectorProps) => {+ const id = useId()+ const {+ i18n: { language },+ } = useTranslation('common')++ const handleChange = (ev: ChangeEvent<HTMLInputElement>) => {+ const { value, name } = ev.target++ onChange({+ // @ts-expect-error+ target: {+ name,+ value: INTERVALS_IN_SECONDS[Number(value)].toString(),+ },+ })+ }++ return (+ <div className='mt-4'>+ <Input+ list={id}+ name={name}+ label={label}+ hint={hint}+ hintPosition='top'+ type='range'+ min='0'+ max={INTERVALS_IN_SECONDS.length - 1}+ value={value ? INTERVALS_IN_SECONDS.indexOf(Number(value)) : 4}+ classes={{+ input: 'arrows-handle mt-4 h-2 w-full rounded-full bg-gray-200 dark:bg-slate-600',+ }}+ onChange={handleChange}+ />+ <datalist+ style={{+ writingMode: 'vertical-lr',+ }}+ className='flex w-full flex-col justify-between'+ id={id}+ >+ {_map(INTERVALS_IN_SECONDS, (interval, index) => {+ if (!_includes(INTERVALS_ALLOWED_DATALIST, interval)) {+ return <option key={interval} value={index} />+ }++ const displayName = getIntervalDisplayName(interval, language)++ return (+ <option+ className='mt-4 text-sm lg:mt-0 lg:-rotate-90 lg:text-center'+ key={interval}+ value={index}+ label={displayName}+ />+ )+ })}+ </datalist>+ </div>+ )+}++const MAX_RETRIES = 100+const UptimeSettings = (): JSX.Element => {const { monitors, total } = useSelector((state: StateType) => state.ui.monitors)@@ -39,7 +153,10 @@ const UptimeSettings = (): JSX.Element => {const navigate = useNavigate()const { id, pid } = useRequiredParams<{ id: string; pid: string }>()const { pathname } = useLocation()- const { t } = useTranslation('common')+ const {+ t,+ i18n: { language },+ } = useTranslation('common')const isSettings =!_isEmpty(id) && _replace(_replace(routes.uptime_settings, ':id', id as string), ':pid', pid as string) === pathnameconst monitor = useMemo(() => _find(monitors, { id }), [monitors, id])@@ -89,6 +206,45 @@ const UptimeSettings = (): JSX.Element => {allErrors.name = t('alert.noNameError')}+ if (_isEmpty(form.url)) {+ allErrors.url = t('apiNotifications.inputCannotBeEmpty')+ }++ if (!isValidUrl(form.url!)) {+ allErrors.url = t('monitor.error.urlInvalid')+ }++ const retries = Number(form.retries)++ if (_isNaN(retries)) {+ allErrors.retries = t('apiNotifications.enterACorrectNumber')+ }++ if (retries <= 0) {+ allErrors.retries = t('apiNotifications.numberCantBeNegative')+ }++ if (retries > MAX_RETRIES) {+ allErrors.retries = t('apiNotifications.numberCantBeBigger', {+ max: MAX_RETRIES,+ })+ }++ const acceptedStatusCodes = _split(form.acceptedStatusCodes, ',')++ if (_isEmpty(acceptedStatusCodes)) {+ allErrors.acceptedStatusCodes = t('apiNotifications.inputCannotBeEmpty')+ }++ if (+ acceptedStatusCodes.some((statusCode) => {+ const trimmedStatusCode = Number(statusCode.trim())+ return !_isNaN(trimmedStatusCode) && trimmedStatusCode >= 100 && trimmedStatusCode <= 599+ })+ ) {+ allErrors.acceptedStatusCodes = t('monitor.error.acceptedStatusCodesNotValid')+ }+const valid = _isEmpty(_keys(allErrors))setErrors(allErrors)@@ -213,33 +369,37 @@ const UptimeSettings = (): JSX.Element => {onChange={handleInput}error={beenSubmitted ? errors.url : null}/>- <Input+ <IntervalSelectorname='interval'label={t('monitor.form.interval')}- value={form.interval || ''}- className='mt-4'+ hint={t('monitor.form.intervalHint', {+ intervalTranslated: getIntervalDisplayName(Number(form.interval), language, 'long'),+ })}+ value={form.interval}onChange={handleInput}- error={beenSubmitted ? errors.interval : null}/><Inputname='retries'label={t('monitor.form.retries')}- value={form.retries || ''}+ hint={t('monitor.form.retriesHint', { retries: form.retries || 'N/A' })}+ value={form.retries}className='mt-4'onChange={handleInput}error={beenSubmitted ? errors.retries : null}/>- <Input+ <IntervalSelectorname='retryInterval'label={t('monitor.form.retryInterval')}- value={form.retryInterval || ''}- className='mt-4'+ hint={t('monitor.form.retryIntervalHint', {+ retryIntervalTranslated: getIntervalDisplayName(Number(form.retryInterval), language, 'long'),+ })}+ value={form.retryInterval}onChange={handleInput}- error={beenSubmitted ? errors.retryInterval : null}/><Inputname='timeout'label={t('monitor.form.timeout')}+ hint={t('monitor.form.timeoutHint', { x: form.timeout || 'N/A' })}value={form.timeout || ''}className='mt-4'onChange={handleInput}@@ -253,14 +413,6 @@ const UptimeSettings = (): JSX.Element => {onChange={handleInput}error={beenSubmitted ? errors.acceptedStatusCodes : null}/>- <Input- name='description'- label={t('monitor.form.description')}- value={form.description || ''}- className='mt-4'- onChange={handleInput}- error={beenSubmitted ? errors.description : null}- />{isSettings ? (<div className='mt-5 flex items-center justify-between'>diff --git a/public/locales/en.json b/public/locales/en.jsonindex 1e3900e4c..7a1092b2a 100644--- a/public/locales/en.json+++ b/public/locales/en.json@@ -26,6 +26,7 @@"integrationSaveError": "Failed to save integration, make sure you've entered a valid input","enterACorrectNumber": "Please enter a correct number","numberCantBeNegative": "The number cannot be negative",+ "numberCantBeBigger": "The number cannot be bigger than {{max}}","forecastNumberCantBeBigger": "The forecast number cannot be bigger than {{max}} for the selected frequency","invalidToken": "Invalid token","transferRequestSent": "Transfer request sent",@@ -740,14 +741,22 @@"form": {"name": "Monitor name","type": "Monitor type",- "url": "Monitor URL",+ "url": "URL to monitor","interval": "Monitor interval",+ "intervalHint": "We will check the URL every {{intervalTranslated}}","retries": "Monitor retries",+ "retriesHint": "We will retry the URL {{retries}} times before marking it as failed","retryInterval": "Monitor retry interval",+ "retryIntervalHint": "We will retry the URL every {{retryIntervalTranslated}}","timeout": "Monitor timeout",+ "timeoutHint": "We will cancel the request to the URL if it takes longer than {{x}} seconds and mark it as failed","acceptedStatusCodes": "Monitor accepted status codes (comma-separated)","description": "Monitor description"},+ "error": {+ "urlInvalid": "Please enter a valid URL",+ "acceptedStatusCodesNotValid": "Some of the values provided are not valid HTTP status codes"+ },"backToMonitors": "Back to monitors list","metrics": {"avg": "Avg. response time",