|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { isEqual } from "lodash"; |
| 18 | +import React, { |
| 19 | + createContext, Dispatch, |
| 20 | + Fragment, |
| 21 | + PropsWithChildren, |
| 22 | + ReactNode, |
| 23 | + Reducer, |
| 24 | + ReducerState, |
| 25 | + useContext, |
| 26 | + useEffect, |
| 27 | + useReducer, |
| 28 | +} from "react"; |
| 29 | + |
| 30 | +interface AuthHeaderContextValue { |
| 31 | + title: ReactNode; |
| 32 | + icon?: ReactNode; |
| 33 | + hideServerPicker?: boolean; |
| 34 | +} |
| 35 | + |
| 36 | +enum AuthHeaderContextActionType { |
| 37 | + ADD, |
| 38 | + REMOVE |
| 39 | +} |
| 40 | + |
| 41 | +interface AuthHeaderContextAction { |
| 42 | + type: AuthHeaderContextActionType; |
| 43 | + value: AuthHeaderContextValue; |
| 44 | +} |
| 45 | + |
| 46 | +type AuthContextReducer = Reducer<AuthHeaderContextValue[], AuthHeaderContextAction>; |
| 47 | + |
| 48 | +interface AuthHeaderContextType { |
| 49 | + state: ReducerState<AuthContextReducer>; |
| 50 | + dispatch: Dispatch<AuthHeaderContextAction>; |
| 51 | +} |
| 52 | + |
| 53 | +const AuthHeaderContext = createContext<AuthHeaderContextType>(undefined); |
| 54 | + |
| 55 | +export function AuthHeader(content: AuthHeaderContextValue) { |
| 56 | + const context = useContext(AuthHeaderContext); |
| 57 | + const dispatch = context ? context.dispatch : null; |
| 58 | + useEffect(() => { |
| 59 | + if (!dispatch) { |
| 60 | + return; |
| 61 | + } |
| 62 | + dispatch({ type: AuthHeaderContextActionType.ADD, value: content }); |
| 63 | + return () => dispatch({ type: AuthHeaderContextActionType.REMOVE, value: content }); |
| 64 | + }, [content, dispatch]); |
| 65 | + return null; |
| 66 | +} |
| 67 | + |
| 68 | +interface Props { |
| 69 | + title: ReactNode; |
| 70 | + icon?: ReactNode; |
| 71 | + serverPicker: ReactNode; |
| 72 | +} |
| 73 | + |
| 74 | +export function AuthHeaderDisplay({ title, icon, serverPicker, children }: PropsWithChildren<Props>) { |
| 75 | + const context = useContext(AuthHeaderContext); |
| 76 | + if (!context) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + const current = context.state.length ? context.state[0] : null; |
| 80 | + return ( |
| 81 | + <Fragment> |
| 82 | + { current?.icon ?? icon } |
| 83 | + <h2>{ current?.title ?? title }</h2> |
| 84 | + { children } |
| 85 | + { current?.hideServerPicker !== true && serverPicker } |
| 86 | + </Fragment> |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +export function AuthHeaderProvider({ children }: PropsWithChildren<{}>) { |
| 91 | + const [state, dispatch] = useReducer<AuthContextReducer>( |
| 92 | + (state: AuthHeaderContextValue[], action: AuthHeaderContextAction) => { |
| 93 | + switch (action.type) { |
| 94 | + case AuthHeaderContextActionType.ADD: |
| 95 | + return [action.value, ...state]; |
| 96 | + case AuthHeaderContextActionType.REMOVE: |
| 97 | + return (state.length && isEqual(state[0], action.value)) ? state.slice(1) : state; |
| 98 | + } |
| 99 | + }, |
| 100 | + [] as AuthHeaderContextValue[], |
| 101 | + ); |
| 102 | + return ( |
| 103 | + <AuthHeaderContext.Provider value={{ state, dispatch }}> |
| 104 | + { children } |
| 105 | + </AuthHeaderContext.Provider> |
| 106 | + ); |
| 107 | +} |
0 commit comments