Skip to content

Commit

Permalink
refactor: extract better-auth and emitter packages
Browse files Browse the repository at this point in the history
  • Loading branch information
natew committed Dec 20, 2024
1 parent 08c8557 commit bd9143c
Show file tree
Hide file tree
Showing 32 changed files with 452 additions and 522 deletions.
6 changes: 4 additions & 2 deletions apps/chat/app/login-github.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Home } from '@tamagui/lucide-icons'
import { useEffect } from 'react'
import { githubSignIn } from '~/better-auth/githubSignIn'
import { authClient } from '~/better-auth/authClient'

export default function () {
useEffect(() => {
githubSignIn()
authClient.signIn.social({
provider: 'github',
})
}, [])

return <Home />
Expand Down
3 changes: 2 additions & 1 deletion apps/chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"@tauri-apps/plugin-shell": "^2",
"@uiw/react-md-editor": "^4.0.5",
"@vxrn/color-scheme": "workspace:*",
"better-auth": "1.0.10",
"@vxrn/emitter": "workspace:*",
"better-auth": "1.0.23-beta.3",
"flexsearch": "^0.7.43",
"one": "workspace:*",
"pg": "^8.13.1",
Expand Down
5 changes: 2 additions & 3 deletions apps/chat/src/better-auth/AuthEffects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { onOpenUrl } from '@tauri-apps/plugin-deep-link'
import { useEffect } from 'react'
import { isTauri } from '~/tauri/constants'
import { setZeroAuth } from '../zero/zero'
import { setAuth } from './authClient'
import { useAuth } from './useAuth'
import { setAuthClientToken, useAuth } from './authClient'

export const AuthEffects = () => {
useAuthPassTokenToTauriEffect()
Expand Down Expand Up @@ -39,7 +38,7 @@ const useAuthPassTokenToTauriEffect = () => {
const session = url.searchParams.get('session')

if (token && session) {
setAuth({
setAuthClientToken({
token,
session,
})
Expand Down
36 changes: 2 additions & 34 deletions apps/chat/src/better-auth/authClient.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,3 @@
import { createAuthClient } from 'better-auth/client'
import { createEmitter } from '~/helpers/emitter'
import { createBetterAuthClient } from '@vxrn/better-auth'

const keys = {
token: 'TOKEN_KEY',
session: 'SESSION_KEY',
}

const existingSession =
typeof localStorage !== 'undefined' ? localStorage.getItem(keys.session) : ''

export let authClient = existingSession
? createAuthClientWithSession(existingSession)
: createAuthClient()

export const [emit, _, useRefreshAuthClient] = createEmitter<number>()

globalThis['authClient'] = authClient

function createAuthClientWithSession(session: string) {
return createAuthClient({
fetchOptions: {
headers: {
Authorization: `Bearer ${session}`,
},
},
})
}

export const setAuth = ({ token, session }: { token: string; session: string }) => {
localStorage.setItem(keys.token, token)
localStorage.setItem(keys.session, session)
authClient = createAuthClientWithSession(session)
emit(Math.random())
}
export const { authClient, setAuthClientToken, useAuth } = createBetterAuthClient()
7 changes: 0 additions & 7 deletions apps/chat/src/better-auth/githubSignIn.ts

This file was deleted.

36 changes: 0 additions & 36 deletions apps/chat/src/better-auth/useAuth.ts

This file was deleted.

8 changes: 4 additions & 4 deletions apps/chat/src/interface/SearchableList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useListNavigation,
useRole,
} from '@floating-ui/react'
import { createEmitter } from '@vxrn/emitter'
import { createContext, forwardRef, useContext, useMemo, useRef, useState } from 'react'
import {
type Input,
Expand All @@ -15,7 +16,6 @@ import {
useGet,
usePropsAndStyle,
} from 'tamagui'
import { createEmitter } from '~/helpers/emitter'

type ContextType = {
getItemProps: (index: number) => any
Expand All @@ -30,7 +30,7 @@ type ContextType = {

const Context = createContext<null | ContextType>(null)

const [activeIndexEmit, _, useActiveIndex] = createEmitter<number>()
const emitter = createEmitter<number>()

export function SearchableList<A>({
items,
Expand All @@ -52,7 +52,7 @@ export function SearchableList<A>({
onNavigate: (index) => {
if (typeof index === 'number') {
setActiveIndex(index)
activeIndexEmit(index)
emitter.emit(index)
}
},
})
Expand Down Expand Up @@ -128,7 +128,7 @@ export const SearchableListItem = ({
const context = useContext(Context)
const [active, setActive] = useState(false)

useActiveIndex((cur) => {
emitter.use((cur) => {
setActive(index === cur)
})

Expand Down
7 changes: 4 additions & 3 deletions apps/chat/src/interface/TopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { ChevronLeft, Search, Settings2, UserCircle } from '@tamagui/lucide-icon
import { memo, useRef } from 'react'
import { useHotkeys } from 'react-hotkeys-hook'
import { H1, Input, TooltipSimple, XStack } from 'tamagui'
import { githubSignIn } from '~/better-auth/githubSignIn'
import { useAuth } from '~/better-auth/useAuth'
import { authClient, useAuth } from '~/better-auth/authClient'
import { HotMenu } from '~/interface/hotmenu/HotMenu'
import { useCurrentChannel, useCurrentServer } from '~/state/server'
import { updateUserState, useUserState } from '~/state/user'
Expand Down Expand Up @@ -116,7 +115,9 @@ const UserButton = () => {

if (!isTauri) {
e.preventDefault()
githubSignIn()
authClient.signIn.social({
provider: 'github',
})
return
}
}}
Expand Down
12 changes: 6 additions & 6 deletions apps/chat/src/interface/dialogs/DialogAddFriend.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import { UserCheck, UserPlus, UserX } from '@tamagui/lucide-icons'
import { createEmitter } from '@vxrn/emitter'
import { useEffect, useRef, useState } from 'react'
import { Dialog, Input, TooltipSimple, XStack, YStack } from 'tamagui'
import { useAuth } from '~/better-auth/authClient'
import type { Friendship, User } from '~/zero/schema'
import { useAuth } from '~/better-auth/useAuth'
import { mutate, useQuery } from '~/zero/zero'
import { createEmitter } from '~/helpers/emitter'
import { Avatar } from '../Avatar'
import { Row } from '../Row'
import { dialogConfirm } from './DialogConfirm'
import { DialogContent, dialogEmit, DialogOverlay, useDialogEmit } from './shared'

const [emit, listen] = createEmitter<boolean>()
const emitter = createEmitter<boolean>()

export const dialogAddFriend = async () => {
dialogEmit({
type: 'add-friend',
})

return new Promise((res) => {
const dispose = listen((val) => {
const dispose = emitter.listen((val) => {
dispose()
res(val)
})
})
}

const success = () => emit(true)
const cancel = () => emit(false)
const success = () => emitter.emit(true)
const cancel = () => emitter.emit(false)

export const DialogAddFriend = () => {
const [show, setShow] = useState(false)
Expand Down
10 changes: 5 additions & 5 deletions apps/chat/src/interface/dialogs/DialogConfirm.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useState } from 'react'
import { Dialog, H3, Paragraph, XStack, YStack } from 'tamagui'
import { createEmitter } from '~/helpers/emitter'
import { createEmitter } from '@vxrn/emitter'
import { ButtonSimple } from '../ButtonSimple'
import { DialogContent, dialogEmit, DialogOverlay, useDialogEmit } from './shared'
import type { DialogConfirmType } from './types'

const [emit, listen] = createEmitter<boolean>()
const emitter = createEmitter<boolean>()

export const dialogConfirm = async (props: Omit<DialogConfirmType, 'type'>) => {
dialogEmit({
Expand All @@ -14,7 +14,7 @@ export const dialogConfirm = async (props: Omit<DialogConfirmType, 'type'>) => {
})

return new Promise((res) => {
const dispose = listen((val) => {
const dispose = emitter.listen((val) => {
dispose()
res(val)
})
Expand Down Expand Up @@ -49,7 +49,7 @@ export const DialogConfirm = () => {
<Dialog.Close asChild>
<ButtonSimple
onPress={() => {
emit(false)
emitter.emit(false)
setState(null)
}}
>
Expand All @@ -59,7 +59,7 @@ export const DialogConfirm = () => {

<ButtonSimple
onPress={() => {
emit(true)
emitter.emit(true)
setState(null)
}}
>
Expand Down
12 changes: 6 additions & 6 deletions apps/chat/src/interface/dialogs/DialogCreateJoinServer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useRef, useState } from 'react'
import { Button, Dialog, Input, ScrollView, XStack, YStack } from 'tamagui'
import { insertServer } from '~/state/mutateServer'
import { createEmitter } from '~/helpers/emitter'
import { createEmitter } from '@vxrn/emitter'
import { LabeledRow } from '../forms/LabeledRow'
import { Tabs } from '../tabs/Tabs'
import { AvatarUpload } from '../upload/AvatarUpload'
Expand All @@ -10,14 +10,14 @@ import { DialogJoinServerContent } from './DialogJoinServerContent'
import { DialogContent, dialogEmit, DialogOverlay, useDialogEmit } from './shared'
import type { TabContentPaneProps } from './types'

const [emit, listen] = createEmitter<boolean>()
const serverDialogEmitter = createEmitter<boolean>()

export const dialogCreateServer = async () => {
dialogEmit({
type: 'create-server',
})
return new Promise((res) => {
const dispose = listen((val) => {
const dispose = serverDialogEmitter.listen((val) => {
dispose()
res(val)
})
Expand All @@ -29,15 +29,15 @@ export const dialogJoinServer = async () => {
type: 'join-server',
})
return new Promise((res) => {
const dispose = listen((val) => {
const dispose = serverDialogEmitter.listen((val) => {
dispose()
res(val)
})
})
}

const success = () => emit(true)
const cancel = () => emit(false)
const success = () => serverDialogEmitter.emit(true)
const cancel = () => serverDialogEmitter.emit(false)

export const DialogCreateJoinServer = () => {
const [show, setShow] = useState(false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Check, DoorOpen } from '@tamagui/lucide-icons'
import { useEffect, useRef, useState } from 'react'
import { TooltipSimple, XStack, YStack } from 'tamagui'
import { useAuth } from '~/better-auth/useAuth'
import { useAuth } from '~/better-auth/authClient'
import { mutate, useQuery } from '~/zero/zero'
import { Avatar } from '../Avatar'
import { Row } from '../Row'
Expand Down
6 changes: 4 additions & 2 deletions apps/chat/src/interface/dialogs/shared.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createEmitter } from '~/helpers/emitter'
import { createEmitter } from '@vxrn/emitter'
import type { DialogType } from './types'
import { Dialog, styled } from 'tamagui'

export const [dialogEmit, dialogListen, useDialogEmit] = createEmitter<DialogType>()
export const dialogEmitter = createEmitter<DialogType>()
export const dialogEmit = dialogEmitter.emit
export const useDialogEmit = dialogEmitter.use

export const DialogOverlay = styled(Dialog.Overlay, {
// @ts-ignore
Expand Down
10 changes: 5 additions & 5 deletions apps/chat/src/interface/messages/MessageInput.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useEffect, useRef } from 'react'
import { Input, YStack } from 'tamagui'
import { useAuth } from '~/better-auth/useAuth'
import { useAuth } from '~/better-auth/authClient'
import { useCurrentChannel, useCurrentServer } from '~/state/server'
import { getDerivedUserState, updateUserCurrentChannel, useCurrentThread } from '~/state/user'
import { randomID } from '~/helpers/randomID'
import { mutate } from '~/zero/zero'
import { messagesListEmit } from './MessagesList'
import { messagesListEmitter } from './MessagesList'
import { Editor } from '~/editor/Editor'

let mainInputRef: Input | null = null
Expand Down Expand Up @@ -34,23 +34,23 @@ export const MessageInput = ({ inThread }: { inThread?: boolean }) => {
const key = e.key
switch (key) {
case 'ArrowUp': {
messagesListEmit({
messagesListEmitter.emit({
type: 'move',
value: 'up',
})
break
}

case 'ArrowDown': {
messagesListEmit({
messagesListEmitter.emit({
type: 'move',
value: 'down',
})
break
}

case 'Enter': {
messagesListEmit({
messagesListEmitter.emit({
type: 'select',
})
break
Expand Down
2 changes: 1 addition & 1 deletion apps/chat/src/interface/messages/MessageItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
YStack,
} from 'tamagui'
import type { Channel, Message, MessageWithRelations, Reaction, Thread, User } from '~/zero/schema'
import { useAuth } from '~/better-auth/useAuth'
import { useAuth } from '~/better-auth/authClient'
import { currentUser, updateUserOpenThread, useUserCurrentChannelState } from '~/state/user'
import { randomID } from '~/helpers/randomID'
import { mutate, useQuery } from '~/zero/zero'
Expand Down
Loading

0 comments on commit bd9143c

Please sign in to comment.