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 React, { createContext, useContext, useMemo, useReducer } from 'react'
import type { Config } from '../types/config'
import type { Dispatch, Reducer } from 'react' import type { Dispatch, Reducer } from 'react'
type Context = { type Context = {
state: State state: State
dispatch: Dispatch<Action> dispatch: Dispatch<Action>
options: NonNullable<Config['options']>
} }
const AppContext = createContext<Context | undefined>(undefined) const AppContext = createContext<Context | undefined>(undefined)
@ -31,13 +33,27 @@ const initialState: State = {
interface Props { interface Props {
children: React.ReactNode children: React.ReactNode
config: Config
} }
export const AppProvider = (props: Props) => { export const AppProvider = (props: Props) => {
const { children } = props const { children, config } = props
const [state, dispatch] = useReducer(reducer, initialState) 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> return <AppContext.Provider value={value}>{children}</AppContext.Provider>
} }

View File

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

View File

@ -3,6 +3,7 @@ import React from 'react'
import { Route, Routes } from 'react-router-dom' import { Route, Routes } from 'react-router-dom'
import { MainSidebar } from '~/src/components/main-sidebar' import { MainSidebar } from '~/src/components/main-sidebar'
import { useAppState } from '~/src/contexts/app-context'
import { Chat } from '~/src/routes/chat' import { Chat } from '~/src/routes/chat'
import { NewChat } from '~/src/routes/new-chat' import { NewChat } from '~/src/routes/new-chat'
import { styled } from '~/src/styles/config' import { styled } from '~/src/styles/config'
@ -42,9 +43,11 @@ import { styled } from '~/src/styles/config'
// } // }
export function Messenger() { export function Messenger() {
const { options } = useAppState()
return ( return (
<Wrapper> <Wrapper>
<MainSidebar /> {options.enableMembers && <MainSidebar />}
<Routes> <Routes>
<Route path="/:id" element={<Chat />} /> <Route path="/:id" element={<Chat />} />
<Route path="/new" element={<NewChat />} /> <Route path="/new" element={<NewChat />} />

View File

@ -44,10 +44,10 @@ const Content = () => {
} }
export const Chat = () => { export const Chat = () => {
const { state } = useAppState() const { state, options } = useAppState()
// TODO: Update condition based on a chat type // 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 const showMembers = enableMembers && state.showMembers
return ( return (

View File

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