eexiv/src/app/document/view/[slug]/VersionChooser.tsx

92 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-02-14 16:38:54 -08:00
'use client'
import { useState } from 'react'
import { Document } from '@/app/db/data'
import Link from 'next/link'
2024-02-14 18:11:07 -08:00
import { loadAllAuthors } from '@/app/db/loaders'
import { useSuspenseQuery } from '@tanstack/react-query'
import { epoch2date } from '@/app/utils/epoch2datestring'
2024-02-14 16:38:54 -08:00
const VersionChooser = ({
doc,
slug,
}: Readonly<{ doc: Document; slug: string }>) => {
2024-02-14 18:11:07 -08:00
const { data, error } = useSuspenseQuery({
queryKey: ['authors_all'],
queryFn: () => {
const data = loadAllAuthors()
return data
},
})
if (error) throw error
let authorList = data
2024-02-14 16:38:54 -08:00
const { file } = doc
2024-02-14 18:11:07 -08:00
const { authors, latest } = doc.manifest
const date = epoch2date(doc.manifest.dates[doc.manifest.dates.length - 1])
2024-02-14 16:38:54 -08:00
const fileEnding = file === 'other' ? '' : `.${file}`
const [selectedRevision, setSelectedRevision] = useState<number>(latest) // Initialize the selected revision with the latest revision
return (
<div>
<Link
href={`/download/${slug}/file${selectedRevision}${fileEnding}`}
target='_blank'
>
<button className='button-default'>
Download{' '}
{(() => {
switch (file) {
case 'other':
return <></>
case 'tar.gz':
return 'Tarball'
}
})()}
</button>
</Link>
2024-02-14 18:11:07 -08:00
<button
className='ml-2 h-10 px-2.5 bg-slate-300 rounded-md'
onClick={() => {
const bibtex = `@article{
author={${
authors.map((a: string, i) => {
const author = authorList[a].name.first + ' ' + authorList[a].name.last
if (i === 0) return author
else if (i === authors.length - 1) return ` and ${author}`
else return `, ${author}`
}).join('')
}},
title={${doc.manifest.title}},
journal={eeXiv journal},
year={${date.getFullYear()}},
month={${date.toLocaleString('default', { month: 'short' })}},
url={${window.location.href}}
}`
navigator.clipboard.writeText(bibtex)
}}
>
Export BibTeX
</button>
2024-02-14 16:38:54 -08:00
<select
2024-02-14 18:11:07 -08:00
className='ml-2 h-10 px-2.5 bg-slate-300 rounded-md'
2024-02-14 16:38:54 -08:00
value={`v${selectedRevision}`}
2024-02-14 16:52:47 -08:00
onChange={(e) => {
setSelectedRevision(parseInt(e.target.value.replace(/\D/g, ''), 10))
}}
2024-02-14 16:38:54 -08:00
>
{Array.from({ length: latest }, (_, index) => index + 1).map(
(version) => (
<option key={version} value={`v${version}`} className='p-2.5'>
{version == latest ? 'Latest' : `v${version}`}
</option>
)
)}
</select>
</div>
)
}
export default VersionChooser