import { useQuery } from '@tanstack/react-query' import { useEffect, useState } from 'react' import { Helmet } from 'react-helmet-async' import { useLocation } from 'react-router-dom' import { useLocale, useLocaleNavigate } from '@/lib/useLocale' import PrefetchLink from '@/components/PrefetchLink' import FormField from '@/components/common/FormField' import PageTitle from '@/components/common/PageTitle' import Pagination from '@/components/common/Pagination' import MainArea from '@/components/layout/MainArea' import { SITE_TITLE } from '@/config' import { clientScrollBehaviour } from '@/lib/clientAnimation' import { fetchMaterialChanges } from '@/lib/materials' import { materialsKeys } from '@/lib/queryKeys' import { useClientBehaviourSettings } from '@/lib/useClientBehaviourSettings' import { useUITranslation } from '@/lib/useUITranslation' import { localisedDateString, inputClass } from '@/lib/utils' import type { FC, FormEvent } from 'react' const MaterialHistoryPage: FC = () => { const { message, label } = useUITranslation ('history') const locale = useLocale () const behaviourSettings = useClientBehaviourSettings () const animationMode = behaviourSettings.animation ?? 'normal' const location = useLocation () const navigate = useLocaleNavigate () const query = new URLSearchParams (location.search) const materialId = query.get ('material_id') ?? '' const tag = query.get ('tag') ?? '' const eventType = query.get ('event_type') ?? '' const page = Number (query.get ('page') ?? 1) const limit = Number (query.get ('limit') ?? 20) const [materialIdInput, setMaterialIdInput] = useState (materialId) const [tagInput, setTagInput] = useState (tag) const [eventTypeInput, setEventTypeInput] = useState (eventType) useEffect (() => { setMaterialIdInput (materialId) setTagInput (tag) setEventTypeInput (eventType) }, [eventType, materialId, tag]) const { data, isLoading, isError } = useQuery ({ queryKey: materialsKeys.changes ({ ...(materialId && { materialId }), ...(tag && { tag }), ...(eventType && { eventType }), page, limit }, locale), queryFn: () => fetchMaterialChanges ({ ...(materialId && { materialId }), ...(tag && { tag }), ...(eventType && { eventType }), page, limit }, locale) }) const versions = data?.versions ?? [] const totalPages = data ? Math.ceil (data.count / limit) : 0 useEffect (() => { document.querySelector ('table')?.scrollIntoView ({ behavior: clientScrollBehaviour (animationMode) }) }, [animationMode, location.search]) const handleSearch = (event: FormEvent) => { event.preventDefault () const qs = new URLSearchParams () if (materialIdInput.trim ()) qs.set ('material_id', materialIdInput.trim ()) if (tagInput.trim ()) qs.set ('tag', tagInput.trim ()) if (eventTypeInput.trim ()) qs.set ('event_type', eventTypeInput.trim ()) qs.set ('page', '1') qs.set ('limit', String (limit)) navigate (`/materials/changes?${ qs.toString () }`) } return ( {message ('materialTitle').text} | {SITE_TITLE}
{label ('materialTitle')}
{({ invalid }) => ( setMaterialIdInput (e.target.value)} className={inputClass (invalid)}/>)} {({ invalid }) => ( setTagInput (e.target.value)} className={inputClass (invalid)}/>)}
{isLoading ? label ('common:loading') : (isError ? (

{label ('materialError')}

) : ( <>
{versions.map (version => ( ))}
{label ('wiki:dateTime')} {label ('materialId')} {label ('version')} {label ('tagName')} {label ('filename')} {label ('shared:url')} {label ('exportPath')} {label ('actor')}
{localisedDateString (version.createdAt, locale)} #{version.materialId} {version.versionNo} {version.tagName} {version.fileFilename} {version.url} {version.exportPathsJson.legacy_drive} {version.createdByUser ? version.createdByUser.name || `#${ version.createdByUser.id }` : label ('bot')}
))}
) } export default MaterialHistoryPage