-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5a5520
commit b1eb788
Showing
6 changed files
with
144 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
'use client'; | ||
import { useState, useRef, useEffect } from 'react'; | ||
import Queue, { type Job } from '@/components/Queue'; | ||
import { capFirst } from '@/utils/strings'; | ||
|
||
export function useTimeout(callback: () => void, delay: number | null): void { | ||
const savedCallback = useRef(callback); | ||
|
||
// Set up the timeout. | ||
useEffect(() => { | ||
// Don't schedule if no delay is specified. | ||
// Note: 0 is a valid value for delay. | ||
if (!delay && delay !== 0) { | ||
return; | ||
} | ||
|
||
const id = setTimeout(() => { | ||
savedCallback.current(); | ||
}, delay); | ||
|
||
return () => { | ||
clearTimeout(id); | ||
}; | ||
}, [delay]); | ||
} | ||
|
||
function useJobQueue(initialJobs: Job[] = []): [Job[], (job: Job) => void] { | ||
const [jobs, setJobs] = useState<Job[]>(initialJobs); | ||
|
||
function createJob(job: Job) {} | ||
|
||
useTimeout(() => { | ||
console.log('Polling for jobs'); | ||
const jobs; | ||
}, 500); | ||
|
||
return [jobs, createJob]; | ||
} | ||
|
||
export default function Jobs() { | ||
const [users, setUsers] = useState<string[]>(['dan', 'ana']); | ||
const [jobs, createJob] = useJobQueue([ | ||
{ id: 'x', status: 'PENDING', user: 'dan' }, | ||
]); | ||
|
||
const aggregateJobs = jobs.reduce<{ [key: string]: Job[] }>((acc, job) => { | ||
if (job.user in acc) { | ||
acc[job.user].push(job); | ||
} else { | ||
acc[job.user] = [job]; | ||
} | ||
return acc; | ||
}, {}); | ||
|
||
return ( | ||
<div className="flex flex-col gap-4"> | ||
<h2 className="text-lg font-bold">Jobs</h2> | ||
<div className="flex flex-col gap-4"> | ||
{users.map((user, idx) => ( | ||
<div key={user}> | ||
<div className="mb-2">{capFirst(user)}</div> | ||
{/* <div className="flex flex-row gap-8"> | ||
<div className="flex flex-row gap-2"> | ||
<button | ||
onClick={onCreateJob.bind(null, user, 1)} | ||
className="py-1 px-3 rounded-full border border-slate-500 hover:border-slate-700 hover:bg-slate-200" | ||
> | ||
+1 | ||
</button> | ||
<button | ||
onClick={onCreateJob.bind(null, user, 5)} | ||
className="py-1 px-3 rounded-full border border-slate-500 hover:border-slate-700 hover:bg-slate-200" | ||
> | ||
+5 | ||
</button> | ||
</div> */} | ||
|
||
<Queue jobs={aggregateJobs[user] || []} /> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Header from '@/components/Header'; | ||
import Jobs from './Jobs'; | ||
|
||
export default async function Index() { | ||
return ( | ||
<div className="flex-1 w-full flex flex-col gap-20 items-center"> | ||
<div className="animate-in flex-1 flex flex-col 8 opacity-0 max-w-screen-xl w-full px-3"> | ||
<Header /> | ||
<main className="flex-1 flex flex-col gap-6"> | ||
<Jobs /> | ||
</main> | ||
</div> | ||
|
||
<footer className="w-full border-t border-t-foreground/10 p-8 flex justify-center text-center text-xs"> | ||
<p> | ||
Powered by{' '} | ||
<a | ||
href="https://www.inngest.com" | ||
target="_blank" | ||
className="font-bold hover:underline" | ||
rel="noreferrer" | ||
> | ||
Inngest | ||
</a> | ||
</p> | ||
</footer> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export type Job = { | ||
id: string; | ||
status: 'PENDING' | 'RUNNING' | 'COMPLETED'; | ||
user: string; | ||
time: Date; | ||
}; | ||
|
||
export default function Queue({ jobs }: { jobs: Job[] }) { | ||
return ( | ||
<div className="flex flex-row gap-2 items-center"> | ||
{jobs.map(({ id, status }) => { | ||
return ( | ||
<div | ||
key={id} | ||
className={`w-8 h-8 rounded-sm ${ | ||
status === 'RUNNING' | ||
? 'animate-pulse bg-amber-500' | ||
: status === 'COMPLETED' | ||
? 'bg-emerald-500' | ||
: 'bg-cyan-400' | ||
}`} | ||
title={`${id} - ${status}`} | ||
></div> | ||
); | ||
})} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters