feat(react): add components config options

This commit is contained in:
Pavel Prichodko 2022-03-31 16:05:59 +02:00
parent 6d2581c072
commit d6ed1d8496
No known key found for this signature in database
GPG Key ID: 0EB8D75C775AB6F1
5 changed files with 29 additions and 6 deletions

View File

@ -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>
}

View File

@ -31,7 +31,7 @@ export const Community = (props: Props) => {
return (
<Router>
<AppProvider>
<AppProvider config={props}>
<ThemeProvider theme={theme}>
<NarrowProvider myRef={ref}>
<ModalProvider>

View File

@ -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 />} />

View File

@ -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 (

View File

@ -8,4 +8,8 @@ export interface Config {
environment?: Environment
theme?: Theme
router?: typeof BrowserRouter | typeof MemoryRouter | typeof HashRouter
options?: {
enableSidebar: boolean
enableMembers: boolean
}
}