diff --git a/native/components/auto-join-community-handler.react.js b/lib/components/base-auto-join-community-handler.react.js copy from native/components/auto-join-community-handler.react.js copy to lib/components/base-auto-join-community-handler.react.js --- a/native/components/auto-join-community-handler.react.js +++ b/lib/components/base-auto-join-community-handler.react.js @@ -4,41 +4,44 @@ import _pickBy from 'lodash/fp/pickBy.js'; import * as React from 'react'; -import { NeynarClientContext } from 'lib/components/neynar-client-provider.react.js'; -import blobService from 'lib/facts/blob-service.js'; -import { extractKeyserverIDFromID } from 'lib/keyserver-conn/keyserver-call-utils.js'; -import { isLoggedInToIdentityAndAuthoritativeKeyserver } from 'lib/selectors/user-selectors.js'; +import { NeynarClientContext } from '../components/neynar-client-provider.react.js'; +import blobService from '../facts/blob-service.js'; +import { extractKeyserverIDFromID } from '../keyserver-conn/keyserver-call-utils.js'; +import { isLoggedInToIdentityAndAuthoritativeKeyserver } from '../selectors/user-selectors.js'; import { farcasterChannelTagBlobHash, useJoinCommunity, -} from 'lib/shared/community-utils.js'; -import type { AuthMetadata } from 'lib/shared/identity-client-context.js'; -import { IdentityClientContext } from 'lib/shared/identity-client-context.js'; -import type { KeyserverOverride } from 'lib/shared/invite-links.js'; +} from '../shared/community-utils.js'; +import type { AuthMetadata } from '../shared/identity-client-context.js'; +import { IdentityClientContext } from '../shared/identity-client-context.js'; +import type { KeyserverOverride } from '../shared/invite-links.js'; import type { OngoingJoinCommunityData, JoinCommunityStep, -} from 'lib/types/community-types.js'; -import { defaultThreadSubscription } from 'lib/types/subscription-types.js'; -import { getBlobFetchableURL } from 'lib/utils/blob-service.js'; -import { useCurrentUserFID } from 'lib/utils/farcaster-utils.js'; -import { values } from 'lib/utils/objects.js'; -import { promiseAll } from 'lib/utils/promises.js'; +} from '../types/community-types.js'; +import type { CalendarQuery } from '../types/entry-types.js'; +import { defaultThreadSubscription } from '../types/subscription-types.js'; +import { getBlobFetchableURL } from '../utils/blob-service.js'; +import { useCurrentUserFID } from '../utils/farcaster-utils.js'; +import { values } from '../utils/objects.js'; +import { promiseAll } from '../utils/promises.js'; +import { useSelector } from '../utils/redux-utils.js'; import { usingCommServicesAccessToken, createDefaultHTTPRequestHeaders, -} from 'lib/utils/services-utils.js'; - -import { nonThreadCalendarQuery } from '../navigation/nav-selectors.js'; -import { NavContext } from '../navigation/navigation-context.js'; -import { useSelector } from '../redux/redux-utils.js'; +} from '../utils/services-utils.js'; type CommunityToAutoJoin = { +communityID: string, +keyserverOverride: ?KeyserverOverride, }; -function AutoJoinCommunityHandler(): React.Node { +type Props = { + +calendarQuery: () => CalendarQuery, +}; +function BaseAutoJoinCommunityHandler(props: Props): React.Node { + const { calendarQuery } = props; + const isActive = useSelector(state => state.lifecycleState !== 'background'); const loggedIn = useSelector(isLoggedInToIdentityAndAuthoritativeKeyserver); @@ -156,9 +159,10 @@ key={index} communityID={communityToAutoJoin.communityID} keyserverOverride={communityToAutoJoin.keyserverOverride} + calendarQuery={calendarQuery} /> )), - [communitiesToAuoJoin], + [calendarQuery, communitiesToAuoJoin], ); return joinHandlers; @@ -167,18 +171,10 @@ type JoinHandlerProps = { +communityID: string, +keyserverOverride: ?KeyserverOverride, + +calendarQuery: () => CalendarQuery, }; function JoinHandler(props: JoinHandlerProps) { - const { communityID, keyserverOverride } = props; - - const navContext = React.useContext(NavContext); - - const calendarQuery = useSelector(state => - nonThreadCalendarQuery({ - redux: state, - navContext, - }), - ); + const { communityID, keyserverOverride, calendarQuery } = props; const [ongoingJoinData, setOngoingJoinData] = React.useState(null); @@ -205,4 +201,4 @@ return null; } -export { AutoJoinCommunityHandler }; +export { BaseAutoJoinCommunityHandler }; diff --git a/native/components/auto-join-community-handler.react.js b/native/components/auto-join-community-handler.react.js --- a/native/components/auto-join-community-handler.react.js +++ b/native/components/auto-join-community-handler.react.js @@ -1,176 +1,14 @@ // @flow -import invariant from 'invariant'; -import _pickBy from 'lodash/fp/pickBy.js'; import * as React from 'react'; -import { NeynarClientContext } from 'lib/components/neynar-client-provider.react.js'; -import blobService from 'lib/facts/blob-service.js'; -import { extractKeyserverIDFromID } from 'lib/keyserver-conn/keyserver-call-utils.js'; -import { isLoggedInToIdentityAndAuthoritativeKeyserver } from 'lib/selectors/user-selectors.js'; -import { - farcasterChannelTagBlobHash, - useJoinCommunity, -} from 'lib/shared/community-utils.js'; -import type { AuthMetadata } from 'lib/shared/identity-client-context.js'; -import { IdentityClientContext } from 'lib/shared/identity-client-context.js'; -import type { KeyserverOverride } from 'lib/shared/invite-links.js'; -import type { - OngoingJoinCommunityData, - JoinCommunityStep, -} from 'lib/types/community-types.js'; -import { defaultThreadSubscription } from 'lib/types/subscription-types.js'; -import { getBlobFetchableURL } from 'lib/utils/blob-service.js'; -import { useCurrentUserFID } from 'lib/utils/farcaster-utils.js'; -import { values } from 'lib/utils/objects.js'; -import { promiseAll } from 'lib/utils/promises.js'; -import { - usingCommServicesAccessToken, - createDefaultHTTPRequestHeaders, -} from 'lib/utils/services-utils.js'; +import { BaseAutoJoinCommunityHandler } from 'lib/components/base-auto-join-community-handler.react.js'; import { nonThreadCalendarQuery } from '../navigation/nav-selectors.js'; import { NavContext } from '../navigation/navigation-context.js'; import { useSelector } from '../redux/redux-utils.js'; -type CommunityToAutoJoin = { - +communityID: string, - +keyserverOverride: ?KeyserverOverride, -}; - function AutoJoinCommunityHandler(): React.Node { - const isActive = useSelector(state => state.lifecycleState !== 'background'); - - const loggedIn = useSelector(isLoggedInToIdentityAndAuthoritativeKeyserver); - - const fid = useCurrentUserFID(); - - const neynarClient = React.useContext(NeynarClientContext)?.client; - - const identityClientContext = React.useContext(IdentityClientContext); - invariant(identityClientContext, 'IdentityClientContext should be set'); - const { getAuthMetadata } = identityClientContext; - - const threadInfos = useSelector(state => state.threadStore.threadInfos); - - const keyserverInfos = useSelector( - state => state.keyserverStore.keyserverInfos, - ); - - const [communitiesToAuoJoin, setCommunitiesToAutoJoin] = - React.useState>(); - - React.useEffect(() => { - if (!loggedIn || !isActive || !fid || !neynarClient || !threadInfos) { - return; - } - - void (async () => { - const authMetadataPromise: Promise = (async () => { - if (!usingCommServicesAccessToken) { - return undefined; - } - return await getAuthMetadata(); - })(); - - const followedFarcasterChannelsPromise = - neynarClient.fetchFollowedFarcasterChannels(fid); - - const [authMetadata, followedFarcasterChannels] = await Promise.all([ - authMetadataPromise, - followedFarcasterChannelsPromise, - ]); - - const headers = authMetadata - ? createDefaultHTTPRequestHeaders(authMetadata) - : {}; - - const followedFarcasterChannelIDs = followedFarcasterChannels.map( - channel => channel.id, - ); - - const promises: { [string]: Promise } = {}; - - for (const channelID of followedFarcasterChannelIDs) { - promises[channelID] = (async () => { - const blobHash = farcasterChannelTagBlobHash(channelID); - const blobURL = getBlobFetchableURL(blobHash); - - const blobResult = await fetch(blobURL, { - method: blobService.httpEndpoints.GET_BLOB.method, - headers, - }); - - if (blobResult.status !== 200) { - return null; - } - - const { commCommunityID, keyserverURL } = await blobResult.json(); - const keyserverID = extractKeyserverIDFromID(commCommunityID); - - // The user is already in the community - if (threadInfos[commCommunityID]) { - return null; - } - - const keyserverOverride = !keyserverInfos[keyserverID] - ? { - keyserverID, - keyserverURL: keyserverURL.replace(/\/$/, ''), - } - : null; - - return { - communityID: commCommunityID, - keyserverOverride, - }; - })(); - } - - const communitiesObj = await promiseAll(promises); - - const filteredCommunitiesObj = _pickBy(Boolean)(communitiesObj); - - const communities = values(filteredCommunitiesObj); - - if (communities.length === 0) { - return; - } - - setCommunitiesToAutoJoin(communities); - })(); - }, [ - threadInfos, - fid, - isActive, - loggedIn, - neynarClient, - getAuthMetadata, - keyserverInfos, - ]); - - const joinHandlers = React.useMemo( - () => - communitiesToAuoJoin?.map((communityToAutoJoin, index) => ( - - )), - [communitiesToAuoJoin], - ); - - return joinHandlers; -} - -type JoinHandlerProps = { - +communityID: string, - +keyserverOverride: ?KeyserverOverride, -}; -function JoinHandler(props: JoinHandlerProps) { - const { communityID, keyserverOverride } = props; - const navContext = React.useContext(NavContext); const calendarQuery = useSelector(state => @@ -180,29 +18,7 @@ }), ); - const [ongoingJoinData, setOngoingJoinData] = - React.useState(null); - - const [step, setStep] = React.useState('inactive'); - - const joinCommunity = useJoinCommunity({ - communityID, - keyserverOverride, - calendarQuery, - ongoingJoinData, - setOngoingJoinData, - step, - setStep, - defaultSubscription: defaultThreadSubscription, - }); - - React.useEffect(() => { - void (async () => { - await joinCommunity(); - })(); - }, [joinCommunity]); - - return null; + return ; } export { AutoJoinCommunityHandler };