import _ from 'lodash'
import { useEffect, useState } from 'preact/hooks'
import { durationToString } from './lib/utils'
/**
* @param {import('@/jobs.ts').QueuedJob} props
*/
export const QueuedJob = ({ uuid, status, name, submitter, submittedAt }) => (
(location.href = `/jobs/${uuid}`)}>
{name}
)
/**
* @param {import('@/jobs.ts').CompletedJob} props
*/
export const CompletedJob = ({ uuid, name, submittedAt, startedAt, completedAt }) => {
return (
(location.href = `/jobs/${uuid}`)}>
{name}
)
}
export const JobsPage = ({}) => {
const [jobStore, setJobStore] = useState({})
useEffect(async () => {
// const { searchParams } = new URL(location.href)
// const limit = searchParams.has('limit') ? parseInt(searchParams.get('limit')) : 10
// const offset = (searchParams.has('page') ? parseInt(searchParams.get('page')) : 1) - 1
const res = await fetch(location.href + '?format=json')
const jobs = await res.json()
// const result = {}
// for (const item of jobs) {
// result[item.uuid] = item
// }
const result = _.keyBy(jobs, 'uuid')
console.log(result)
setJobStore(result)
// Setup SSE
const es = new EventSource(location.href + '?format=sse')
es.addEventListener('message', ({ data }) => {
const event = JSON.parse(data)
setJobStore(s => ({
...s,
[event.job.uuid]: event.job,
}))
})
}, [])
return (
<>
Queued Jobs
{Object.values(jobStore)
.filter(job => job.status !== 'completed')
.map(job => (
))}
Completed Jobs
{Object.values(jobStore)
.filter(job => job.status === 'completed')
.map(job => (
))}
>
)
}