Improve sidebar, new chat, and share dialog (#190)
This commit is contained in:
parent
35e83dc87e
commit
be90a40427
23 changed files with 598 additions and 217 deletions
60
lib/hooks/use-sidebar.tsx
Normal file
60
lib/hooks/use-sidebar.tsx
Normal 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>
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue