feat(react): add components config options
This commit is contained in:
parent
6d2581c072
commit
d6ed1d8496
|
@ -1,10 +1,12 @@
|
|||
import React, { createContext, useContext, useMemo, useReducer } from 'react'
|
||||
|
||||
import type { Config } from '../types/config'
|
||||
import type { Dispatch, Reducer } from 'react'
|
||||
|
||||
type Context = {
|
||||
state: State
|
||||
dispatch: Dispatch<Action>
|
||||
options: NonNullable<Config['options']>
|
||||
}
|
||||
|
||||
const AppContext = createContext<Context | undefined>(undefined)
|
||||
|
@ -31,13 +33,27 @@ const initialState: State = {
|
|||
|
||||
interface Props {
|
||||
children: React.ReactNode
|
||||
config: Config
|
||||
}
|
||||
|
||||
export const AppProvider = (props: Props) => {
|
||||
const { children } = props
|
||||
const { children, config } = props
|
||||
|
||||
const [state, dispatch] = useReducer(reducer, initialState)
|
||||
const value = useMemo(() => ({ state, dispatch }), [state])
|
||||
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
state,
|
||||
dispatch,
|
||||
options: {
|
||||
enableSidebar: true,
|
||||
enableMembers: true,
|
||||
...config.options,
|
||||
},
|
||||
}),
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[state]
|
||||
)
|
||||
|
||||
return <AppContext.Provider value={value}>{children}</AppContext.Provider>
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ export const Community = (props: Props) => {
|
|||
|
||||
return (
|
||||
<Router>
|
||||
<AppProvider>
|
||||
<AppProvider config={props}>
|
||||
<ThemeProvider theme={theme}>
|
||||
<NarrowProvider myRef={ref}>
|
||||
<ModalProvider>
|
||||
|
|
|
@ -3,6 +3,7 @@ import React from 'react'
|
|||
import { Route, Routes } from 'react-router-dom'
|
||||
|
||||
import { MainSidebar } from '~/src/components/main-sidebar'
|
||||
import { useAppState } from '~/src/contexts/app-context'
|
||||
import { Chat } from '~/src/routes/chat'
|
||||
import { NewChat } from '~/src/routes/new-chat'
|
||||
import { styled } from '~/src/styles/config'
|
||||
|
@ -42,9 +43,11 @@ import { styled } from '~/src/styles/config'
|
|||
// }
|
||||
|
||||
export function Messenger() {
|
||||
const { options } = useAppState()
|
||||
|
||||
return (
|
||||
<Wrapper>
|
||||
<MainSidebar />
|
||||
{options.enableMembers && <MainSidebar />}
|
||||
<Routes>
|
||||
<Route path="/:id" element={<Chat />} />
|
||||
<Route path="/new" element={<NewChat />} />
|
||||
|
|
|
@ -44,10 +44,10 @@ const Content = () => {
|
|||
}
|
||||
|
||||
export const Chat = () => {
|
||||
const { state } = useAppState()
|
||||
const { state, options } = useAppState()
|
||||
|
||||
// TODO: Update condition based on a chat type
|
||||
const enableMembers = true
|
||||
const enableMembers = options.enableMembers // && (chat.type === 'group' || chat.type === 'channel')
|
||||
const showMembers = enableMembers && state.showMembers
|
||||
|
||||
return (
|
||||
|
|
|
@ -8,4 +8,8 @@ export interface Config {
|
|||
environment?: Environment
|
||||
theme?: Theme
|
||||
router?: typeof BrowserRouter | typeof MemoryRouter | typeof HashRouter
|
||||
options?: {
|
||||
enableSidebar: boolean
|
||||
enableMembers: boolean
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue