Changeset View
Changeset View
Standalone View
Standalone View
web/grpc/identity-service-context-provider.react.js
// @flow | // @flow | ||||
import * as React from 'react'; | import * as React from 'react'; | ||||
import { | import { | ||||
IdentityClientContext, | IdentityClientContext, | ||||
type AuthMetadata, | type AuthMetadata, | ||||
} from 'lib/shared/identity-client-context.js'; | } from 'lib/shared/identity-client-context.js'; | ||||
import { getConfig } from 'lib/utils/config.js'; | import { getConfig } from 'lib/utils/config.js'; | ||||
import { IdentityServiceClientSharedProxy } from './identity-service-client-proxy.js'; | import { IdentityServiceClientSharedProxy } from './identity-service-client-proxy.js'; | ||||
import { IdentityServiceClientWrapper } from './identity-service-client-wrapper.js'; | import { IdentityServiceClientWrapper } from './identity-service-client-wrapper.js'; | ||||
import { useGetDeviceKeyUpload } from '../account/account-hooks.js'; | import { useGetNewDeviceKeyUpload } from '../account/account-hooks.js'; | ||||
import { usingSharedWorker } from '../crypto/olm-api.js'; | import { usingSharedWorker } from '../crypto/olm-api.js'; | ||||
import { useSelector } from '../redux/redux-utils.js'; | import { useSelector } from '../redux/redux-utils.js'; | ||||
type Props = { | type Props = { | ||||
+children: React.Node, | +children: React.Node, | ||||
}; | }; | ||||
function IdentityServiceContextProvider(props: Props): React.Node { | function IdentityServiceContextProvider(props: Props): React.Node { | ||||
const { children } = props; | const { children } = props; | ||||
const userID = useSelector(state => state.currentUserInfo?.id); | const userID = useSelector(state => state.currentUserInfo?.id); | ||||
const accessToken = useSelector(state => state.commServicesAccessToken); | const accessToken = useSelector(state => state.commServicesAccessToken); | ||||
const deviceID = useSelector( | const deviceID = useSelector( | ||||
state => state.cryptoStore?.primaryIdentityKeys.ed25519, | state => state.cryptoStore?.primaryIdentityKeys.ed25519, | ||||
); | ); | ||||
const getDeviceKeyUpload = useGetDeviceKeyUpload(); | const getNewDeviceKeyUpload = useGetNewDeviceKeyUpload(); | ||||
const client = React.useMemo(() => { | const client = React.useMemo(() => { | ||||
let authLayer = null; | let authLayer = null; | ||||
if (userID && deviceID && accessToken) { | if (userID && deviceID && accessToken) { | ||||
authLayer = { | authLayer = { | ||||
userID, | userID, | ||||
deviceID, | deviceID, | ||||
commServicesAccessToken: accessToken, | commServicesAccessToken: accessToken, | ||||
}; | }; | ||||
} | } | ||||
if (usingSharedWorker) { | if (usingSharedWorker) { | ||||
return new IdentityServiceClientSharedProxy(authLayer); | return new IdentityServiceClientSharedProxy(authLayer); | ||||
} else { | } else { | ||||
return new IdentityServiceClientWrapper( | return new IdentityServiceClientWrapper( | ||||
getConfig().platformDetails, | getConfig().platformDetails, | ||||
null, | null, | ||||
authLayer, | authLayer, | ||||
getDeviceKeyUpload, | getNewDeviceKeyUpload, | ||||
); | ); | ||||
} | } | ||||
}, [accessToken, deviceID, getDeviceKeyUpload, userID]); | }, [accessToken, deviceID, getNewDeviceKeyUpload, userID]); | ||||
const getAuthMetadata = React.useCallback<() => Promise<AuthMetadata>>( | const getAuthMetadata = React.useCallback<() => Promise<AuthMetadata>>( | ||||
async () => ({ | async () => ({ | ||||
userID, | userID, | ||||
deviceID, | deviceID, | ||||
accessToken, | accessToken, | ||||
}), | }), | ||||
[accessToken, deviceID, userID], | [accessToken, deviceID, userID], | ||||
Show All 18 Lines |