Skip to content

Commit

Permalink
🎉 feat: ely 1.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Mar 20, 2024
1 parent 26109b8 commit e3fd4a2
Show file tree
Hide file tree
Showing 32 changed files with 294,292 additions and 137 deletions.
16 changes: 16 additions & 0 deletions apps/medium/__tests__/page.specs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";
/**
* @jest-environment jsdom
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const page_1 = __importDefault(require("@app/page"));
const react_1 = require("@testing-library/react");
describe('App', () => {
it('renders without crashing', () => {
const { baseElement } = (0, react_1.render)(React.createElement(page_1.default, null));
expect(baseElement).toBeTruthy();
});
});
6 changes: 3 additions & 3 deletions apps/medium/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"lint": "eslint 'src/**/*.{ts,tsx}'"
},
"dependencies": {
"@elysiajs/eden": "^0.8.0",
"@elysiajs/eden": "^1.0.0-rc.1",
"@hookform/resolvers": "^3.3.3",
"@iconify/react": "^4.1.1",
"@radix-ui/react-slot": "^1.0.2",
Expand All @@ -24,7 +24,7 @@
"@vercel/og": "^0.5.20",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"elysia": "^0.8.8",
"elysia": "^1.0.7",
"framer-motion": "^10.16.16",
"jotai": "^2.6.1",
"lucide-react": "^0.302.0",
Expand Down Expand Up @@ -82,4 +82,4 @@
"volta": {
"node": "20.10.0"
}
}
}
41 changes: 13 additions & 28 deletions apps/medium/src/app/(user)/chat/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,29 @@
import { useEffect } from 'react'
import { useParams } from 'next/navigation'
'use client'

import {
HydrationBoundary,
QueryClient,
dehydrate
} from '@tanstack/react-query'
import { HydrationBoundary, dehydrate, isServer } from '@tanstack/react-query'

import { queryClient } from '@app/providers'
import { Composer, Chat, Conversation } from '@modules/chat'
import CleanUp from './clean-up'

import { isServer, resonator } from '@services'
import { useHydrateAtoms } from '@stores/jotai'
import { characterIdAtom } from '@components/modules/chat/store'
import { resonator } from '@services'

export default async function Chatroom({
export default function Chatroom({
params: { id }
}: {
params: { id: string }
}) {
const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
networkMode: 'offlineFirst'
}
}
})

await queryClient.prefetchQuery({
queryKey: ['character', id],
async queryFn() {
const { data, error } = await resonator.character[id!].get()
if (!isServer)
queryClient.prefetchQuery({
queryKey: ['character', id],
async queryFn() {
const { data, error } = await resonator.character({ id }).get()

if (error) throw error
if (error) throw error

return data
}
})
return data
}
})

return (
<>
Expand Down
9 changes: 6 additions & 3 deletions apps/medium/src/app/(user)/editor/[id]/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export const useCharacterEditor = (id: string) => {
async queryFn() {
if (!id) return

const { data, error } = await resonator.editor.character[id!].get()
const { data, error } = await resonator.editor
.character({ id })
.get()

if (error) throw error

Expand Down Expand Up @@ -70,8 +72,9 @@ export const useCharacterEditor = (id: string) => {
async mutationFn(input: form) {
if (!id) return

const { data, error } =
await resonator.editor.character[id].patch(input)
const { data, error } = await resonator.editor
.character({ id })
.patch(input)

queryClient.invalidateQueries({
queryKey: ['editor', 'character', 'get', id]
Expand Down
4 changes: 2 additions & 2 deletions apps/medium/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export const metadata: Metadata = {
}

export default async function Index() {
const { data, error } = await resonator.character.list[1].get({
$fetch: {
const { data, error } = await resonator.character.list({ page: 1 }).get({
fetch: {
next: { revalidate: 5 }
}
})
Expand Down
8 changes: 4 additions & 4 deletions apps/medium/src/app/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import { usePathname } from 'next/navigation'
import { motion, AnimatePresence } from 'framer-motion'
import { FrozenRouter } from './transition'

const persister = createSyncStoragePersister({
export const persister = createSyncStoragePersister({
storage: isServer ? undefined : window.localStorage
})

export const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
networkMode: 'offlineFirst'
staleTime: 0,
gcTime: 24 * 60 * 60 * 1000
}
}
})
Expand Down Expand Up @@ -76,7 +76,7 @@ export default function Provider({ children }: PropsWithChildren) {
}}
transition={{
when: 'afterChildren',
duration: isBig ? .2 : 0.425,
duration: isBig ? 0.2 : 0.425,
ease: easing.outQuint
}}
>
Expand Down
5 changes: 2 additions & 3 deletions apps/medium/src/components/modules/chat/chat/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use client'

import { useEffect, useLayoutEffect } from 'react'
import { useLayoutEffect } from 'react'

import { motion, AnimatePresence } from 'framer-motion'
import { useQuery } from '@tanstack/react-query'

import Markdown from 'react-markdown'

Expand Down Expand Up @@ -68,7 +67,7 @@ function PlaceholderMessages({

export default function Chat() {
const { chats, isTyping, isChatLoading, chatError } = useChat()
const { character, isCharacterLoading } = useCharacter()
const { character } = useCharacter()
const { isUserLoading, user } = useUser()
const [conversationId] = useConversationId()

Expand Down
4 changes: 3 additions & 1 deletion apps/medium/src/components/modules/chat/composer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use client'

import { useEffect, useRef, useState } from 'react'

import { useForm } from 'react-hook-form'
Expand All @@ -13,7 +14,8 @@ import { Textarea } from '@shared'

const model = z.object({
content: z.string(),
images: z.instanceof(FileList).optional()
// It should be .instanceOf(FileList) but is undefined for some reason
images: z.any().optional()
})

type model = z.infer<typeof model>
Expand Down
63 changes: 34 additions & 29 deletions apps/medium/src/components/modules/chat/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import { queryClient } from '@app/providers'
export const characterIdAtom = atom<string | null>(null)
export const useCharacterId = () => useAtom(characterIdAtom)

export type Character = NonNullable<
Awaited<ReturnType<resonator['character'][':id']['get']>>['data']
>
const CharacterData = resonator.character({ id: 1 }).get
type CharacterData = typeof CharacterData

export type Character = NonNullable<Awaited<ReturnType<CharacterData>>['data']>

export const characterAtom = atom<Character | null>(null)
export const useCharacter = () => {
Expand All @@ -31,7 +32,7 @@ export const useCharacter = () => {
queryKey: ['character', id],
staleTime: Infinity,
async queryFn() {
const { data, error } = await resonator.character[id!].get()
const { data, error } = await resonator.character({ id: id! }).get()

if (error) throw error

Expand All @@ -43,10 +44,11 @@ export const useCharacter = () => {
return { character, error, isCharacterLoading: isPending }
}

const CharacterList = resonator.character({ id: 1 }).chat.list.get
type CharacterList = typeof CharacterList

export type Conversation = NonNullable<
Awaited<
ReturnType<resonator['character'][':id']['chat']['list']['get']>
>['data']
Awaited<ReturnType<CharacterList>>['data']
>

type ConversationActions =
Expand Down Expand Up @@ -80,8 +82,9 @@ export const useConversation = () => {
queryKey: ['conversation', 'list', characterId],
staleTime: Infinity,
async queryFn() {
const { data, error } =
await resonator.character[characterId!].chat.list.get()
const { data, error } = await resonator
.character({ id: characterId! })
.chat.list.get()

if (error) throw error

Expand Down Expand Up @@ -109,8 +112,9 @@ export const useConversation = () => {
async mutationFn() {
if (!characterId) throw new Error('Missing character id')

const { data: conversationId, error } =
await resonator.character[characterId].chat.list.put()
const { data: conversationId, error } = await resonator
.character({ id: characterId! })
.chat.list.put()

if (error) throw error

Expand Down Expand Up @@ -169,9 +173,10 @@ export const useConversation = () => {
}
}

export type Chat = NonNullable<
Awaited<ReturnType<resonator['character'][':id']['chat']['get']>>['data']
>[0]
const CharacterChat = resonator.character({ id: 1 }).chat.get
type CharacterChat = typeof CharacterChat

export type Chat = NonNullable<Awaited<ReturnType<CharacterChat>>['data']>[0]

type ChatActions =
| {
Expand Down Expand Up @@ -209,13 +214,13 @@ export const useChat = () => {
],
staleTime: Infinity,
async queryFn() {
const { data, error } = await resonator.character[
characterId!
].chat.get({
$query: {
conversation: conversationId ?? ''
}
})
const { data, error } = await resonator
.character({ id: characterId! })
.chat.get({
query: {
conversation: conversationId ?? ''
}
})

if (error) throw error

Expand Down Expand Up @@ -252,14 +257,14 @@ export const useChat = () => {

if (!characterId) throw new Error('Missing character id')

const { data, error } = await resonator.character[
characterId
].chat.post({
conversationId: conversationId ? conversationId : undefined,
content,
images: images?.length ? images : undefined,
time: new Date().toString()
})
const { data, error } = await resonator
.character({ id: characterId! })
.chat.post({
conversationId: conversationId ? conversationId : undefined,
content,
images: images?.length ? images : undefined,
time: new Date().toString()
})

queryClient.invalidateQueries({
refetchType: 'none',
Expand Down
2 changes: 1 addition & 1 deletion apps/medium/src/components/shared/textarea/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const TextArea = forwardRef<
const [value, setValue] = useState('')

useEffect(() => {
if (props.defaultValue) setValue(props.defaultValue)
if (props.defaultValue) setValue(props.defaultValue as string)
}, [props.defaultValue])

return (
Expand Down
6 changes: 3 additions & 3 deletions apps/medium/src/services/eden.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { edenTreaty } from '@elysiajs/eden'
import { treaty } from '@elysiajs/eden'
import type { app } from 'resonator'

export const resonator = edenTreaty<app>(
export const resonator = treaty<app>(
process.env.NEXT_PUBLIC_RESONATOR ?? 'http://192.168.0.181:3001',
{
$fetch: {
fetch: {
credentials: 'include',
next: { revalidate: 0 }
}
Expand Down
18 changes: 12 additions & 6 deletions apps/medium/src/stores/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { atom, useAtom } from 'jotai'
import { useIsMutating, useMutation, useQuery } from '@tanstack/react-query'
import { resonator } from '@services'

type Room = NonNullable<
Awaited<ReturnType<resonator['character']['room'][':page']['get']>>['data']
>[0]
const CharacterRoom = resonator.character.room({ page: 1 }).get
type CharacterRoom = typeof CharacterRoom

type Room = NonNullable<Awaited<ReturnType<CharacterRoom>>['data']>[0]

const roomAtom = atom<{
active: boolean
Expand All @@ -22,13 +23,18 @@ const roomAtom = atom<{
export const useRooms = () => {
const [{ active, rooms, page, end }, setRooms] = useAtom(roomAtom)

const { data: roomNetwork, isFetching, isPending } = useQuery({
const {
data: roomNetwork,
isFetching,
isPending
} = useQuery({
enabled: active && !end,
queryKey: ['room', 'page', page],
staleTime: Infinity,
queryFn: async () => {
const { data: response, error } =
await resonator.character.room[page].get()
const { data: response, error } = await resonator.character
.room({ page })
.get()

if (error) throw error.value

Expand Down
Loading

0 comments on commit e3fd4a2

Please sign in to comment.