Remove non-null assertion in ConversationProvider
- Add `createOptionalContext` that is used by `AuthContext` and `ConversationContext` to create a context with a hook
that can be used to retrieve its value and throw an error if it's undefined.
- In `router.tsx`, put `Messenger` inside `ConversationProvider`.
- In `ConversationListItem`, use the conversationId from the `ConversationContext ` instead of the url params.
- Fix bug in `CallInterface` with fullscreen.
- Remove unecessary useEffect dependency in `NotificationManager`.
Change-Id: Ib5f0ae6a0a34cdbdb02f871e36194376d945230d
diff --git a/client/src/contexts/AuthProvider.tsx b/client/src/contexts/AuthProvider.tsx
index 06bf1bb..d0d88cc 100644
--- a/client/src/contexts/AuthProvider.tsx
+++ b/client/src/contexts/AuthProvider.tsx
@@ -17,10 +17,11 @@
*/
import axios, { AxiosInstance } from 'axios';
import { HttpStatusCode } from 'jami-web-common';
-import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react';
+import { useCallback, useEffect, useMemo, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import ProcessingRequest from '../components/ProcessingRequest';
+import { createOptionalContext } from '../hooks/createOptionalContext';
import { Account } from '../models/Account';
import { apiUrl } from '../utils/constants';
import { WithChildren } from '../utils/utils';
@@ -33,7 +34,9 @@
axiosInstance: AxiosInstance;
}
-const AuthContext = createContext<IAuthContext | undefined>(undefined);
+const optionalAuthContext = createOptionalContext<IAuthContext>('AuthContext');
+const AuthContext = optionalAuthContext.Context;
+export const useAuthContext = optionalAuthContext.useOptionalContext;
export default ({ children }: WithChildren) => {
const [token, setToken] = useState<string | undefined>();
@@ -109,13 +112,3 @@
</AuthContext.Provider>
);
};
-
-export function useAuthContext(dontThrowIfUndefined: true): IAuthContext | undefined;
-export function useAuthContext(): IAuthContext;
-export function useAuthContext(dontThrowIfUndefined?: true) {
- const authContext = useContext(AuthContext);
- if (!authContext && !dontThrowIfUndefined) {
- throw new Error('AuthContext is not provided');
- }
- return authContext;
-}