- Added `taken_at` timestamp to the photos table for improved photo metadata. - Updated the photo upload API to support optional EXIF data extraction and file last modified timestamp. - Enhanced the photo upload process to allow skipping EXIF data, defaulting to today's date if no other date is provided. - Improved the photo display in various components to utilize a unified caption format. - Refactored photo sorting and grouping logic for better organization in the UI.
221 lines
6.8 KiB
React
221 lines
6.8 KiB
React
import { useState, useEffect, useRef } from 'react'
|
|
import { Camera, Trash2 } from 'lucide-react'
|
|
import dayjs from 'dayjs'
|
|
import { api } from '../utils/api'
|
|
import UsageBadge from '../components/UsageBadge'
|
|
import { formatPhotoCaption } from '../utils/photoDisplay'
|
|
|
|
export default function PhotosCapturePage() {
|
|
const [date, setDate] = useState(() => dayjs().format('YYYY-MM-DD'))
|
|
const [photos, setPhotos] = useState([])
|
|
const [photoUsage, setPhotoUsage] = useState(null)
|
|
const [uploading, setUploading] = useState(false)
|
|
const [error, setError] = useState(null)
|
|
const [big, setBig] = useState(null)
|
|
const [skipExif, setSkipExif] = useState(false)
|
|
const fileRef = useRef(null)
|
|
|
|
const load = () => api.listPhotos().then(setPhotos).catch(() => setPhotos([]))
|
|
|
|
const loadUsage = () => {
|
|
api
|
|
.getFeatureUsage()
|
|
.then((features) => {
|
|
const f = features.find((x) => x.feature_id === 'photos')
|
|
setPhotoUsage(f)
|
|
})
|
|
.catch(() => setPhotoUsage(null))
|
|
}
|
|
|
|
useEffect(() => {
|
|
load()
|
|
loadUsage()
|
|
}, [])
|
|
|
|
const onPickFiles = async (e) => {
|
|
const files = Array.from(e.target.files || [])
|
|
e.target.value = ''
|
|
if (!files.length) return
|
|
setError(null)
|
|
setUploading(true)
|
|
try {
|
|
for (const file of files) {
|
|
await api.uploadPhoto(file, date, { skipExif })
|
|
}
|
|
await load()
|
|
await loadUsage()
|
|
} catch (err) {
|
|
setError(err.message || 'Upload fehlgeschlagen')
|
|
} finally {
|
|
setUploading(false)
|
|
}
|
|
}
|
|
|
|
const handleDelete = async (id) => {
|
|
if (!confirm('Dieses Foto löschen?')) return
|
|
try {
|
|
await api.deletePhoto(id)
|
|
if (big === id) setBig(null)
|
|
await load()
|
|
} catch (err) {
|
|
alert(err.message || 'Löschen fehlgeschlagen')
|
|
}
|
|
}
|
|
|
|
const blocked = photoUsage && !photoUsage.allowed
|
|
|
|
return (
|
|
<div className="capture-page">
|
|
<h1 className="page-title" style={{ marginBottom: 16 }}>
|
|
Fotos
|
|
</h1>
|
|
|
|
<div className="card section-gap">
|
|
<div className="card-title badge-container-right">
|
|
<span>Neues Foto</span>
|
|
{photoUsage && <UsageBadge {...photoUsage} />}
|
|
</div>
|
|
<p style={{ fontSize: 13, color: 'var(--text2)', marginTop: 0, marginBottom: 14, lineHeight: 1.5 }}>
|
|
Reihenfolge: zuerst <strong>EXIF</strong> (Aufnahme aus dem Bild), sonst die{' '}
|
|
<strong>Datei-Zeit</strong> (Letzte Änderung der Datei, wie vom Browser übermittelt), sonst das
|
|
Fallback-Datum unten (Standard: heute).
|
|
</p>
|
|
<div className="form-row">
|
|
<label className="form-label">Datum (Fallback ohne EXIF)</label>
|
|
<input
|
|
type="date"
|
|
className="form-input"
|
|
style={{ width: '100%', maxWidth: 220 }}
|
|
value={date}
|
|
onChange={(e) => setDate(e.target.value)}
|
|
/>
|
|
<span className="form-unit" />
|
|
</div>
|
|
<label
|
|
style={{
|
|
display: 'flex',
|
|
alignItems: 'center',
|
|
gap: 8,
|
|
fontSize: 13,
|
|
color: 'var(--text2)',
|
|
marginBottom: 12,
|
|
cursor: 'pointer',
|
|
}}
|
|
>
|
|
<input type="checkbox" checked={skipExif} onChange={(e) => setSkipExif(e.target.checked)} />
|
|
EXIF und Datei-Zeit ignorieren — nur das Fallback-Datum verwenden
|
|
</label>
|
|
<input
|
|
ref={fileRef}
|
|
type="file"
|
|
accept="image/*"
|
|
multiple
|
|
style={{ display: 'none' }}
|
|
onChange={onPickFiles}
|
|
/>
|
|
{error && (
|
|
<div
|
|
style={{
|
|
padding: '10px',
|
|
background: 'var(--danger-bg)',
|
|
border: '1px solid var(--danger)',
|
|
borderRadius: 8,
|
|
fontSize: 13,
|
|
color: 'var(--danger)',
|
|
marginBottom: 10,
|
|
}}
|
|
>
|
|
{error}
|
|
</div>
|
|
)}
|
|
<button
|
|
type="button"
|
|
className="btn btn-primary btn-full"
|
|
disabled={uploading || blocked}
|
|
title={
|
|
blocked
|
|
? `Limit erreicht (${photoUsage.used}/${photoUsage.limit}).`
|
|
: undefined
|
|
}
|
|
onClick={() => fileRef.current?.click()}
|
|
>
|
|
<Camera size={16} style={{ marginRight: 8 }} />
|
|
{uploading ? 'Wird hochgeladen…' : 'Foto(s) auswählen'}
|
|
</button>
|
|
</div>
|
|
|
|
<div className="card section-gap">
|
|
<div className="card-title">Meine Fotos ({photos.length})</div>
|
|
{!photos.length ? (
|
|
<div style={{ fontSize: 13, color: 'var(--text3)', padding: '8px 0' }}>Noch keine Fotos.</div>
|
|
) : (
|
|
<>
|
|
{big && (
|
|
<div
|
|
style={{
|
|
position: 'fixed',
|
|
inset: 0,
|
|
background: 'rgba(0,0,0,0.9)',
|
|
zIndex: 100,
|
|
display: 'flex',
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
}}
|
|
onClick={() => setBig(null)}
|
|
role="presentation"
|
|
>
|
|
<img src={api.photoUrl(big)} style={{ maxWidth: '100%', maxHeight: '100%', borderRadius: 8 }} alt="" />
|
|
</div>
|
|
)}
|
|
<div className="photo-grid">
|
|
{photos.map((p) => (
|
|
<div key={p.id} style={{ position: 'relative' }}>
|
|
<img
|
|
src={api.photoUrl(p.id)}
|
|
className="photo-thumb"
|
|
alt=""
|
|
onClick={() => setBig(p.id)}
|
|
/>
|
|
<div
|
|
style={{
|
|
position: 'absolute',
|
|
bottom: 4,
|
|
left: 4,
|
|
fontSize: 9,
|
|
background: 'rgba(0,0,0,0.6)',
|
|
color: 'white',
|
|
padding: '1px 4px',
|
|
borderRadius: 3,
|
|
}}
|
|
>
|
|
{formatPhotoCaption(p)}
|
|
</div>
|
|
<button
|
|
type="button"
|
|
className="btn btn-secondary"
|
|
style={{
|
|
position: 'absolute',
|
|
top: 4,
|
|
right: 4,
|
|
padding: 6,
|
|
minWidth: 0,
|
|
borderRadius: 8,
|
|
}}
|
|
title="Löschen"
|
|
onClick={(e) => {
|
|
e.stopPropagation()
|
|
handleDelete(p.id)
|
|
}}
|
|
>
|
|
<Trash2 size={14} />
|
|
</button>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|