Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | 5x 5x 5x 140x 140x 140x 140x 140x 140x 28x 28x 140x 7x 7x 7x 7x 7x 1x 1x 6x 1x 1x 5x 5x 5x 3x 3x 2x 2x 1x 1x 2x 140x 1x 1x 1x 1x 140x 140x 140x | import { useEffect, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { cn } from '@/lib/utils'
import { CloseIcon } from '@/shared/icons/close-icon'
import { UploadIcon } from '@/shared/icons/upload-icon'
import { useUploadMilestonePhoto } from '../api/milestones.hooks'
const MAX_FILE_BYTES = 5 * 1024 * 1024
const ACCEPTED_TYPES = ['image/jpeg', 'image/png', 'image/webp']
const ACCEPT_ATTR = ACCEPTED_TYPES.join(',')
interface MilestonePhotoUploadFieldProps {
id?: string
value: string | undefined
onValueChange: (value: string) => void
className?: string
onFileChange?: (file: File | null, preview: string | null) => void
}
// Two visual states share this component: a local instant preview (an object
// URL, shown the moment a file is picked) while the real upload is in
// flight, then the server's returned URL once it lands — see handleFileChange.
export function MilestonePhotoUploadField({
id,
value,
onValueChange,
className,
onFileChange,
}: MilestonePhotoUploadFieldProps) {
const { t } = useTranslation()
const fileInputRef = useRef<HTMLInputElement>(null)
const [localPreviewUrl, setLocalPreviewUrl] = useState<string | null>(null)
const [clientError, setClientError] = useState<string | null>(null)
const uploadPhoto = useUploadMilestonePhoto()
// Revoke the local object URL on unmount / whenever it's replaced, so we
// don't leak blob references once the real (or no) photo is in place.
useEffect(() => {
return () => {
if (localPreviewUrl) URL.revokeObjectURL(localPreviewUrl)
}
}, [localPreviewUrl])
const handleFileChange = async (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0]
event.target.value = ''
Iif (!file) return
setClientError(null)
if (!ACCEPTED_TYPES.includes(file.type)) {
setClientError(t('milestones.form.photoInvalidType'))
return
}
if (file.size > MAX_FILE_BYTES) {
setClientError(t('milestones.form.photoTooLarge'))
return
}
const objectUrl = URL.createObjectURL(file)
setLocalPreviewUrl(objectUrl)
if (onFileChange) {
onFileChange(file, objectUrl)
return
}
try {
const url = await uploadPhoto.mutateAsync(file)
onValueChange(url)
} catch {
setClientError(t('milestones.form.photoUploadError'))
} finally {
setLocalPreviewUrl(null)
}
}
const handleRemove = () => {
setLocalPreviewUrl(null)
onFileChange?.(null, null)
onValueChange('')
setClientError(null)
}
const previewSrc = localPreviewUrl ?? (value || null)
const isUploading = uploadPhoto.isPending
return (
<div className={cn('space-y-2', className)}>
{previewSrc ? (
<div className="relative w-full">
<img
src={previewSrc}
alt=""
className={cn(
'h-44 w-full rounded-xl object-contain bg-muted shadow-sm',
isUploading && 'opacity-50',
)}
/>
{isUploading && (
<div className="absolute inset-0 flex items-center justify-center">
<div className="border-t-transparent h-6 w-6 animate-spin rounded-full border-2 border-primary" />
</div>
)}
{!isUploading && (
<button
type="button"
onClick={handleRemove}
aria-label={t('milestones.form.photoRemoveAria')}
className="bg-ink absolute right-2 top-2 flex h-11 w-11 items-center justify-center rounded-full text-white shadow-sm"
>
<CloseIcon className="h-3.5 w-3.5" />
</button>
)}
</div>
) : (
<button
type="button"
onClick={() => fileInputRef.current?.click()}
className="flex h-40 w-full flex-col items-center justify-center gap-2 rounded-xl border-2 border-dashed border-border text-ink-muted transition-colors hover:border-primary hover:text-primary"
>
<UploadIcon className="h-5 w-5" />
<span className="text-[11px] font-semibold">
{t('milestones.form.photoUploadAction')}
</span>
</button>
)}
<input
ref={fileInputRef}
id={id}
type="file"
accept={ACCEPT_ATTR}
onChange={handleFileChange}
className="sr-only"
aria-hidden="true"
tabIndex={-1}
/>
{previewSrc && !isUploading && (
<button
type="button"
className="text-sm font-semibold underline"
onClick={() => fileInputRef.current?.click()}
>
{t('milestones.editor.photoReplace')}
</button>
)}
<p className="text-xs text-ink-muted">{t('milestones.editor.photoHint')}</p>
{clientError && (
<p role="alert" className="text-destructive text-sm">
{clientError}
</p>
)}
</div>
)
}
|