📈 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
Make FileUpload a reusable UI component
Blue Mousef42643cdcb61
diff --git a/web/app/pages/Project/Settings/components/DataImportTab.tsx b/web/app/pages/Project/Settings/components/DataImportTab.tsxindex 517164fd3..a858336ae 100644--- a/web/app/pages/Project/Settings/components/DataImportTab.tsx+++ b/web/app/pages/Project/Settings/components/DataImportTab.tsx@@ -16,6 +16,7 @@ import { useTranslation } from 'react-i18next'import { DataImport } from '~/lib/models/Project'import Modal from '~/ui/Modal'import Loader from '~/ui/Loader'+import FileUpload from '~/ui/FileUpload'import { Text } from '~/ui/Text'import UmamiSVG from '~/ui/icons/Umami'@@ -81,7 +82,6 @@ export default function DataImportTab({ projectId }: DataImportTabProps) {const [uploading, setUploading] = useState(false)const [deleteTarget, setDeleteTarget] = useState<DataImport | null>(null)const [deleting, setDeleting] = useState(false)- const fileInputRef = useRef<HTMLInputElement>(null)const pollRef = useRef<ReturnType<typeof setInterval> | null>(null)const statusLabels: Record<DataImport['status'], string> = {@@ -438,59 +438,16 @@ export default function DataImportTab({ projectId }: DataImportTabProps) {</Text></div>- {/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */}- <div- className={cx(- 'group relative cursor-pointer rounded-lg border-2 border-dashed p-6 text-center transition-colors',- uploading- ? 'cursor-not-allowed border-gray-200 dark:border-slate-700'- : 'border-gray-300 hover:border-gray-400 dark:border-slate-600 dark:hover:border-slate-500',- )}- onClick={() => {- if (!uploading) fileInputRef.current?.click()- }}- onDragOver={(e) => {- e.preventDefault()- e.stopPropagation()- }}- onDrop={(e) => {- e.preventDefault()- e.stopPropagation()- if (uploading) return- const file = e.dataTransfer.files?.[0]- if (file) handleUpload(file)- }}- >- <input- ref={fileInputRef}- type='file'- className='hidden'- accept={providerFileType}- onChange={(e) => {- const file = e.target.files?.[0]- if (file) handleUpload(file)- e.target.value = ''- }}- disabled={uploading}- />-- {uploading ? (- <div className='space-y-2'>- <SpinnerIcon className='mx-auto size-8 animate-spin text-gray-400' />- <Text- as='p'- size='sm'- colour='inherit'- className='text-gray-500 dark:text-gray-400'- >- {t('project.settings.dataImport.uploadingProcessing')}- </Text>- </div>- ) : (- <div className='space-y-2'>- <UploadIcon className='mx-auto size-8 text-gray-400 dark:text-gray-500' />+ <FileUpload+ accept={providerFileType}+ loading={uploading}+ onFile={handleUpload}+ label={+ uploading ? (+ t('project.settings.dataImport.uploadingProcessing')+ ) : (<Text- as='p'+ as='span'size='sm'colour='inherit'className='text-gray-600 dark:text-gray-300'@@ -505,19 +462,16 @@ export default function DataImportTab({ projectId }: DataImportTabProps) {</Text>{' '}{t('project.settings.dataImport.orDragDrop')}</Text>- <Text- as='p'- size='xs'- colour='inherit'- className='text-gray-500 dark:text-gray-400'- >- {t('project.settings.dataImport.maxFileSize', {+ )+ }+ hint={+ uploading+ ? undefined+ : t('project.settings.dataImport.maxFileSize', {fileType: providerFileType.toUpperCase(),- })}- </Text>- </div>- )}- </div>+ })+ }+ /></div>}closeText={t('common.cancel')}diff --git a/web/app/ui/FileUpload.tsx b/web/app/ui/FileUpload.tsxnew file mode 100644index 000000000..ac039337b--- /dev/null+++ b/web/app/ui/FileUpload.tsx@@ -0,0 +1,138 @@+import { useCallback, useId, useRef, useState } from 'react'+import { UploadIcon, SpinnerIcon } from '@phosphor-icons/react'++import { cn } from '~/utils/generic'++import { Text } from './Text'++interface FileUploadProps {+ accept?: string+ disabled?: boolean+ loading?: boolean+ onFile: (file: File) => void+ label?: React.ReactNode+ hint?: React.ReactNode+ className?: string+}++export default function FileUpload({+ accept,+ disabled,+ loading,+ onFile,+ label,+ hint,+ className,+}: FileUploadProps) {+ const inputId = useId()+ const inputRef = useRef<HTMLInputElement>(null)+ const [dragOver, setDragOver] = useState(false)++ const isDisabled = disabled || loading++ const handleFile = useCallback(+ (file: File | undefined) => {+ if (!file || isDisabled) return+ onFile(file)+ },+ [isDisabled, onFile],+ )++ const handleDragOver = useCallback(+ (e: React.DragEvent) => {+ e.preventDefault()+ e.stopPropagation()+ if (!isDisabled) setDragOver(true)+ },+ [isDisabled],+ )++ const handleDragLeave = useCallback((e: React.DragEvent) => {+ e.preventDefault()+ e.stopPropagation()+ setDragOver(false)+ }, [])++ const handleDrop = useCallback(+ (e: React.DragEvent) => {+ e.preventDefault()+ e.stopPropagation()+ setDragOver(false)+ if (isDisabled) return+ handleFile(e.dataTransfer.files?.[0])+ },+ [isDisabled, handleFile],+ )++ const handleChange = useCallback(+ (e: React.ChangeEvent<HTMLInputElement>) => {+ handleFile(e.target.files?.[0])+ e.target.value = ''+ },+ [handleFile],+ )++ return (+ <label+ htmlFor={inputId}+ className={cn(+ 'group relative flex cursor-pointer flex-col items-center justify-center rounded-lg border-2 border-dashed p-6 text-center transition-colors',+ isDisabled+ ? 'cursor-not-allowed border-gray-200 dark:border-slate-700'+ : dragOver+ ? 'border-slate-900 bg-gray-50 dark:border-slate-400 dark:bg-slate-800/50'+ : 'border-gray-300 hover:border-gray-400 dark:border-slate-600 dark:hover:border-slate-500',+ className,+ )}+ aria-disabled={isDisabled || undefined}+ onDragOver={handleDragOver}+ onDragLeave={handleDragLeave}+ onDrop={handleDrop}+ >+ <input+ ref={inputRef}+ id={inputId}+ type='file'+ className='sr-only'+ accept={accept}+ disabled={isDisabled}+ onChange={handleChange}+ />++ {loading ? (+ <div className='space-y-2'>+ <SpinnerIcon className='mx-auto size-8 animate-spin text-gray-400' />+ {label && (+ <Text+ as='p'+ size='sm'+ colour='inherit'+ className='text-gray-500 dark:text-gray-400'+ >+ {label}+ </Text>+ )}+ </div>+ ) : (+ <div className='space-y-2'>+ <UploadIcon className='mx-auto size-8 text-gray-400 dark:text-gray-500' />+ {label && (+ <Text as='p' size='sm' colour='inherit'>+ {label}+ </Text>+ )}+ {hint && (+ <Text+ as='p'+ size='xs'+ colour='inherit'+ className='text-gray-500 dark:text-gray-400'+ >+ {hint}+ </Text>+ )}+ </div>+ )}+ </label>+ )+}