Improve sidebar, new chat, and share dialog (#190)

This commit is contained in:
Jared Palmer 2023-12-04 12:42:53 -05:00 committed by GitHub
parent 35e83dc87e
commit be90a40427
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 598 additions and 217 deletions

60
lib/hooks/use-sidebar.tsx Normal file
View file

@ -0,0 +1,60 @@
'use client'
import * as React from 'react'
const LOCAL_STORAGE_KEY = 'sidebar'
interface SidebarContext {
isSidebarOpen: boolean
toggleSidebar: () => void
isLoading: boolean
}
const SidebarContext = React.createContext<SidebarContext | undefined>(
undefined
)
export function useSidebar() {
const context = React.useContext(SidebarContext)
if (!context) {
throw new Error('useSidebarContext must be used within a SidebarProvider')
}
return context
}
interface SidebarProviderProps {
children: React.ReactNode
}
export function SidebarProvider({ children }: SidebarProviderProps) {
const [isSidebarOpen, setSidebarOpen] = React.useState(true)
const [isLoading, setLoading] = React.useState(true)
React.useEffect(() => {
const value = localStorage.getItem(LOCAL_STORAGE_KEY)
if (value) {
setSidebarOpen(JSON.parse(value))
}
setLoading(false)
}, [])
const toggleSidebar = () => {
setSidebarOpen(value => {
const newState = !value
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(newState))
return newState
})
}
if (isLoading) {
return null
}
return (
<SidebarContext.Provider
value={{ isSidebarOpen, toggleSidebar, isLoading }}
>
{children}
</SidebarContext.Provider>
)
}