📈 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
Add editable pills to IP address inputs
Blue Mousea45661eadca7
diff --git a/web/app/pages/Project/Settings/ProjectSettings.tsx b/web/app/pages/Project/Settings/ProjectSettings.tsxindex de41740b6..cd4cf9dcb 100644--- a/web/app/pages/Project/Settings/ProjectSettings.tsx+++ b/web/app/pages/Project/Settings/ProjectSettings.tsx@@ -1057,6 +1057,12 @@ const ProjectSettings = () => {beenSubmitted={beenSubmitted}handleInput={handleInput}handleBlur={handleInputBlur}+ onIpListChange={(name, value) =>+ handleFieldAutosave(+ { [name]: value },+ PROJECT_TEXT_AUTOSAVE_TOASTS[name],+ )+ }botsProtectionLevels={botsProtectionLevels}setBotsLevel={(name) =>handleFieldAutosave(diff --git a/web/app/pages/Project/Settings/tabs/Shields.tsx b/web/app/pages/Project/Settings/tabs/Shields.tsxindex 68c0cba61..eaf1be744 100644--- a/web/app/pages/Project/Settings/tabs/Shields.tsx+++ b/web/app/pages/Project/Settings/tabs/Shields.tsx@@ -8,6 +8,7 @@ import Flag from '~/ui/Flag'import Input from '~/ui/Input'import MultiSelect from '~/ui/MultiSelect'import Select from '~/ui/Select'+import TagInput from '~/ui/TagInput'import { Text } from '~/ui/Text'import countries from '~/utils/isoCountries'@@ -33,6 +34,7 @@ interface ShieldsProps {beenSubmitted: booleanhandleInput: (e: React.ChangeEvent<HTMLInputElement>) => voidhandleBlur: (e: React.FocusEvent<HTMLInputElement>) => void+ onIpListChange: (name: 'ipBlacklist' | 'ipWhitelist', value: string) => voidbotsProtectionLevels: readonly BotsProtectionLevelOption[]setBotsLevel: (name: string) => voidcountryBlacklist: string[]@@ -45,6 +47,7 @@ const Shields = ({beenSubmitted,handleInput,handleBlur,+ onIpListChange,botsProtectionLevels,setBotsLevel,countryBlacklist,@@ -126,15 +129,15 @@ const Shields = ({onBlur={handleBlur}error={beenSubmitted ? errors.origins : null}/>- <Input+ <TagInputname='ipBlacklist'label={t('project.settings.ipBlacklist')}hint={t('project.settings.ipBlacklistHint')}placeholder={t('project.settings.ipBlacklistPlaceholder')}value={form.ipBlacklist || ''}className='mt-4'- onChange={handleInput}- onBlur={handleBlur}+ onChange={(value) => onIpListChange('ipBlacklist', value)}+ instructions={t('project.settings.ipListInstructions')}error={beenSubmitted ? errors.ipBlacklist : null}/><div className='mt-4'>@@ -187,15 +190,15 @@ const Shields = ({)}/></div>- <Input+ <TagInputname='ipWhitelist'label={t('project.settings.ipWhitelist')}hint={t('project.settings.ipWhitelistHint')}placeholder={t('project.settings.ipWhitelistPlaceholder')}value={form.ipWhitelist || ''}className='mt-4'- onChange={handleInput}- onBlur={handleBlur}+ onChange={(value) => onIpListChange('ipWhitelist', value)}+ instructions={t('project.settings.ipListInstructions')}error={beenSubmitted ? errors.ipWhitelist : null}/></>diff --git a/web/app/ui/TagInput.tsx b/web/app/ui/TagInput.tsxnew file mode 100644index 000000000..bfdcbb53e--- /dev/null+++ b/web/app/ui/TagInput.tsx@@ -0,0 +1,226 @@+import { Description, Field, Input, Label } from '@headlessui/react'+import { XIcon } from '@phosphor-icons/react'+import cx from 'clsx'+import React, { useEffect, useRef, useState } from 'react'+import { useTranslation } from 'react-i18next'++import { Text } from './Text'++interface TagInputProps {+ name: string+ value: string+ onChange: (value: string) => void+ label: string+ hint?: string+ placeholder?: string+ instructions?: string+ error?: string | null+ className?: string+}++const splitValues = (value: string) =>+ value+ .split(/[,\r\n]+/)+ .map((item) => item.trim())+ .filter(Boolean)++const TagInput = ({+ name,+ value,+ onChange,+ label,+ hint,+ placeholder,+ instructions,+ error,+ className,+}: TagInputProps) => {+ const { t } = useTranslation('common')+ const inputRef = useRef<HTMLInputElement>(null)+ const [draft, setDraft] = useState('')+ const [editingIndex, setEditingIndex] = useState<number | null>(null)+ const values = splitValues(value)++ useEffect(() => {+ if (editingIndex !== null) {+ inputRef.current?.focus()+ inputRef.current?.select()+ }+ }, [editingIndex])++ const commit = (text = draft) => {+ const additions = splitValues(text)+ const next = [...values]++ if (editingIndex === null) {+ next.push(...additions)+ } else {+ next.splice(editingIndex, 1, ...additions)+ }++ const nextValue = next.join(', ')+ if (nextValue !== value) onChange(nextValue)+ setDraft('')+ setEditingIndex(null)+ return next+ }++ const edit = (index: number) => {+ const selected = values[index]+ const next = commit()+ const nextIndex =+ editingIndex !== null && editingIndex < index+ ? index + next.length - values.length+ : index++ setDraft(selected)+ setEditingIndex(nextIndex)+ }++ const remove = (index: number) => {+ onChange(values.filter((_, itemIndex) => itemIndex !== index).join(', '))+ if (editingIndex !== null && index < editingIndex) {+ setEditingIndex(editingIndex - 1)+ }+ inputRef.current?.focus()+ }++ const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {+ if (event.nativeEvent.isComposing) return++ if (event.key === ',' || event.key === 'Enter') {+ event.preventDefault()+ commit()+ } else if (+ event.key === 'Backspace' &&+ !draft &&+ editingIndex === null &&+ values.length+ ) {+ event.preventDefault()+ edit(values.length - 1)+ } else if (event.key === 'Escape') {+ event.preventDefault()+ setDraft('')+ setEditingIndex(null)+ }+ }++ return (+ <Field as='div' className={cx('flex flex-col gap-1', className)}>+ <Label>+ <Text+ as='span'+ size='sm'+ weight='medium'+ className='leading-tight'+ colour='primary'+ >+ {label}+ </Text>+ </Label>+ {hint ? (+ <Description as='div'>+ <Text+ as='span'+ size='sm'+ colour='secondary'+ className='block leading-tight'+ >+ {hint}+ </Text>+ </Description>+ ) : null}+ <div+ className={cx(+ 'flex min-h-9 flex-wrap items-center gap-1.5 rounded-md bg-white px-2 py-1.5 ring-1 transition-shadow duration-150 ease-out ring-inset focus-within:ring-2 dark:bg-slate-950',+ error+ ? 'ring-red-500 focus-within:ring-red-500 dark:ring-red-500/80 dark:focus-within:ring-red-400'+ : 'ring-gray-300 focus-within:ring-slate-900 hover:ring-gray-400 dark:ring-slate-700/80 dark:focus-within:ring-slate-300 dark:hover:ring-slate-600',+ )}+ onBlur={(event) => {+ if (!event.currentTarget.contains(event.relatedTarget)) commit()+ }}+ >+ {values.map((item, index) =>+ index === editingIndex ? null : (+ <span+ key={`${index}-${item}`}+ className='inline-flex max-w-full items-center rounded-md bg-gray-100 text-sm text-gray-700 ring-1 ring-gray-200/70 ring-inset dark:bg-slate-800 dark:text-gray-200 dark:ring-slate-700'+ >+ <button+ type='button'+ className='min-w-0 rounded-l-md px-2 py-0.5 text-left break-all hover:bg-gray-200/70 focus-visible:z-10 focus-visible:ring-2 focus-visible:ring-slate-900 focus-visible:outline-hidden dark:hover:bg-slate-700 dark:focus-visible:ring-slate-300'+ aria-label={`${t('common.edit')} ${item}`}+ onMouseDown={(event) => event.preventDefault()}+ onClick={() => edit(index)}+ >+ {item}+ </button>+ <button+ type='button'+ className='flex size-6 shrink-0 items-center justify-center rounded-r-md text-gray-400 hover:bg-gray-200/70 hover:text-gray-700 focus-visible:z-10 focus-visible:ring-2 focus-visible:ring-slate-900 focus-visible:outline-hidden dark:text-gray-500 dark:hover:bg-slate-700 dark:hover:text-gray-200 dark:focus-visible:ring-slate-300'+ aria-label={`${t('common.remove')} ${item}`}+ onMouseDown={(event) => event.preventDefault()}+ onClick={() => remove(index)}+ >+ <XIcon className='size-3.5' aria-hidden='true' />+ </button>+ </span>+ ),+ )}+ <Input+ ref={inputRef}+ className='min-w-24 flex-1 border-0 bg-transparent px-1 py-0.5 text-sm text-gray-900 shadow-none outline-hidden placeholder:text-gray-400 focus:ring-0 dark:text-gray-50 dark:placeholder:text-gray-500'+ value={draft}+ placeholder={values.length ? undefined : placeholder}+ autoComplete='off'+ autoCapitalize='none'+ spellCheck={false}+ invalid={Boolean(error)}+ aria-invalid={Boolean(error) || undefined}+ onChange={(event) => {+ const text = event.target.value+ if (+ text.includes(',') &&+ !(event.nativeEvent as InputEvent).isComposing+ ) {+ const lastComma = text.lastIndexOf(',')+ commit(text.slice(0, lastComma))+ setDraft(text.slice(lastComma + 1).trimStart())+ } else {+ setDraft(text)+ }+ }}+ onPaste={(event) => {+ const pasted = event.clipboardData.getData('text')+ if (!/[,\r\n]/.test(pasted)) return+ event.preventDefault()+ const { selectionStart, selectionEnd } = event.currentTarget+ commit(+ draft.slice(0, selectionStart ?? draft.length) ++ pasted ++ draft.slice(selectionEnd ?? draft.length),+ )+ }}+ onKeyDown={handleKeyDown}+ />+ <input type='hidden' name={name} value={value} />+ </div>+ {instructions ? (+ <Description className='text-xs text-gray-500 dark:text-gray-400'>+ {instructions}+ </Description>+ ) : null}+ {error ? (+ <Description as='div' role='alert'>+ <Text as='span' size='sm' colour='error'>+ {error}+ </Text>+ </Description>+ ) : null}+ </Field>+ )+}++export default TagInputdiff --git a/web/public/locales/en.json b/web/public/locales/en.jsonindex 8aa7a1d69..b32ce7f26 100644--- a/web/public/locales/en.json+++ b/web/public/locales/en.json@@ -2655,6 +2655,7 @@"ipWhitelist": "Trusted IP addresses","ipWhitelistHint": "Traffic from these IP addresses will never be flagged by bot protection. Separate multiple IP addresses or CIDR ranges with commas. Useful for server-side event tracking, as requests from your backend may otherwise be dropped as datacenter traffic. Example: 172.126.10.16, 192.168.0.1/32, ::1.","ipWhitelistPlaceholder": "No trusted IP addresses by default",+ "ipListInstructions": "Press comma or Enter to add. Click an address to edit, or × to remove.","countryBlacklist": "Blocked countries","countryBlacklistHint": "Choose the countries you don't want to track. Traffic from these countries will be ignored in your analytics.","countryBlacklistPlaceholder": "Not blocking any countries by default",