Skip to content

Commit

Permalink
refactor: use swr
Browse files Browse the repository at this point in the history
  • Loading branch information
kwaa committed Jan 30, 2025
1 parent 3d28f9f commit 3ec086c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"remark-gfm": "^4.0.0",
"sonner": "^1.7.3",
"sqlocal": "^0.13.0",
"swr": "^2.3.0",
"uuid": "^11.0.5",
"virtua": "^0.39.3"
},
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions src/hooks/use-character.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { eq } from 'drizzle-orm'
import useSWR from 'swr'

import { db } from '../db'
import { charactersTable } from '../db/schema'

export const useCharacter = (uuid: string) => {
const { data, isLoading } = useSWR('/db/characters', async () => db
.select()
.from(charactersTable)
.where(eq(charactersTable.id, uuid))
.get())

return {
character: data,
isLoading,
}
}
23 changes: 3 additions & 20 deletions src/pages/room/[uuid]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
import { Flex } from '@radix-ui/themes'
import { eq } from 'drizzle-orm'
import { useEffect, useState } from 'react'
import { useEffect } from 'react'

import { Header } from '../../../components/header'
import { InputArea } from '../../../components/input-area'
import { Messages } from '../../../components/messages'
import { useSetMessages } from '../../../context/messages'
import { db } from '../../../db'
import { charactersTable } from '../../../db/schema'
import { useCharacter } from '../../../hooks/use-character'
import { useParams } from '../../../router'
import { loadCharacterCard } from '../../../utils/ccv3/load'

const Room = () => {
const { uuid } = useParams('/room/:uuid')
const [character, setCharacter] = useState<typeof charactersTable.$inferSelect | undefined>()
const setMessages = useSetMessages()

useEffect(() => {
const getCharacter = async () => {
const character = await db
.select()
.from(charactersTable)
.where(eq(charactersTable.id, uuid))
.get()

if (character)
setCharacter(character)
}

void getCharacter()
}, [uuid])
const { character } = useCharacter(uuid)

useEffect(() => {
if (character)
Expand Down

0 comments on commit 3ec086c

Please sign in to comment.