homeman-deno/routes/index.tsx

49 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-01-05 21:10:38 -06:00
import { Handlers, PageProps } from '$fresh/server.ts'
2024-01-16 16:15:10 -06:00
import { db, kv, schema, Todo, UserWithTodos } from '@homeman/models.ts'
2024-01-11 16:23:09 -06:00
import Dashboard from '@homeman/islands/Dashboard.tsx'
2024-01-16 16:15:10 -06:00
import { useSignal } from '@preact/signals'
import { useEffect } from 'preact/hooks'
2024-01-05 13:22:56 -06:00
interface Data {
2024-01-11 16:23:09 -06:00
users: Record<string, UserWithTodos>
2024-01-05 21:10:38 -06:00
unassignedTodos: Todo[]
2024-01-05 13:22:56 -06:00
}
2024-01-16 16:15:10 -06:00
const allTableNames = Object.keys(schema).map((s) => [s])
async function watcher() {
console.log('watching:', allTableNames)
for await (const entry of kv.watch(allTableNames)) {
console.log('entry:', entry)
}
}
watcher()
2024-01-05 13:22:56 -06:00
export const handler: Handlers = {
async GET(_req, ctx) {
2024-01-11 16:23:09 -06:00
const users = Object.fromEntries(
(await db.users.findMany({ include: { assignedTodos: true } })).map(
(u) => [u.id, u],
),
)
2024-01-05 21:10:38 -06:00
const unassignedTodos = await db.todos.findMany({
where: { assigneeUserId: null },
})
2024-01-11 16:23:09 -06:00
return ctx.render({ users, unassignedTodos })
2024-01-05 13:22:56 -06:00
},
}
2024-01-11 16:23:09 -06:00
export default function Home({ data }: PageProps<Data>) {
2024-01-16 16:15:10 -06:00
const rdata = useSignal(data)
useEffect(() => {
async function watcher() {
console.log('watcher watching...')
for await (const entry of kv.watch(allTableNames)) {
console.log('entry:', entry)
}
}
watcher().catch(console.error)
}, [rdata])
console.log('Home rendered')
return <Dashboard {...rdata.value} />
2024-01-05 13:22:56 -06:00
}