Skip to content

Commit

Permalink
[flow] Update useResolvedThreadInfo and fix subsequent flow issues
Browse files Browse the repository at this point in the history
Summary:
As mentioned in D9896, there were quite a few places where refactoring code to accept `MinimallyEncodedThreadInfo` required the `useResolvedThreadInfo` hook to be updated. Updating that hook required a bunch of changes, which in turn required even more changes, etc.

There wasn't a super obvious way to break down the refactoring going into this since I was responding to `flow` issues as they surfaced, so apologize for the large diff. I **think** that the changes are straightforward enough that this is fine, but I can go back and chunk changes into separate diffs if that would make things easier to review.

---

Depends on D9896

Test Plan: Trusting `flow`, but also based on feedback here: https://phab.comm.dev/D9896#inline-61685... will make sure to read through this carefully a couple times and ensure the "base type(s)" and "minimally encoded type(s)" match up.

Reviewers: ashoat, ginsu, rohan, tomek

Reviewed By: ashoat

Differential Revision: https://phab.comm.dev/D9907
  • Loading branch information
atulsmadhugiri committed Nov 16, 2023
1 parent 97e284e commit 7c75580
Show file tree
Hide file tree
Showing 70 changed files with 406 additions and 221 deletions.
4 changes: 2 additions & 2 deletions lib/selectors/chat-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export type RobotextChatMessageInfoItem = {
+startsCluster: boolean,
endsCluster: boolean,
+robotext: EntityText,
+threadCreatedFromMessage: ?ThreadInfo,
+threadCreatedFromMessage: ?ThreadInfo | ?MinimallyEncodedThreadInfo,
+reactions: ReactionInfo,
};
export type ChatMessageInfoItem =
Expand All @@ -307,7 +307,7 @@ export type ChatMessageInfoItem =
+startsConversation: boolean,
+startsCluster: boolean,
endsCluster: boolean,
+threadCreatedFromMessage: ?ThreadInfo,
+threadCreatedFromMessage: ?ThreadInfo | ?MinimallyEncodedThreadInfo,
+reactions: ReactionInfo,
+hasBeenEdited: boolean,
+isPinned: boolean,
Expand Down
18 changes: 12 additions & 6 deletions lib/shared/mention-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { oldValidUsernameRegexString } from './account-utils.js';
import SentencePrefixSearchIndex from './sentence-prefix-search-index.js';
import { threadOtherMembers } from './thread-utils.js';
import { stringForUserExplicit } from './user-utils.js';
import type { MinimallyEncodedResolvedThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js';
import type {
MinimallyEncodedRelativeMemberInfo,
MinimallyEncodedResolvedThreadInfo,
MinimallyEncodedThreadInfo,
} from '../types/minimally-encoded-thread-permissions-types.js';
import { threadTypes } from '../types/thread-types-enum.js';
import type {
ThreadInfo,
Expand All @@ -26,7 +30,7 @@ export type Selection = {

type MentionTypeaheadUserSuggestionItem = {
+type: 'user',
+userInfo: RelativeMemberInfo,
+userInfo: RelativeMemberInfo | MinimallyEncodedRelativeMemberInfo,
};

type MentionTypeaheadChatSuggestionItem = {
Expand Down Expand Up @@ -105,7 +109,9 @@ function getTypeaheadRegexMatches(

function getMentionTypeaheadUserSuggestions(
userSearchIndex: SentencePrefixSearchIndex,
threadMembers: $ReadOnlyArray<RelativeMemberInfo>,
threadMembers: $ReadOnlyArray<
RelativeMemberInfo | MinimallyEncodedRelativeMemberInfo,
>,
viewerID: ?string,
usernamePrefix: string,
): $ReadOnlyArray<MentionTypeaheadUserSuggestionItem> {
Expand Down Expand Up @@ -161,9 +167,9 @@ function getNewTextAndSelection(
}

function getUserMentionsCandidates(
threadInfo: ThreadInfo,
parentThreadInfo: ?ThreadInfo,
): $ReadOnlyArray<RelativeMemberInfo> {
threadInfo: ThreadInfo | MinimallyEncodedThreadInfo,
parentThreadInfo: ?ThreadInfo | ?MinimallyEncodedThreadInfo,
): $ReadOnlyArray<RelativeMemberInfo | MinimallyEncodedRelativeMemberInfo> {
if (threadInfo.type !== threadTypes.SIDEBAR) {
return threadInfo.members;
}
Expand Down
10 changes: 5 additions & 5 deletions lib/shared/message-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ function messageID(messageInfo: MessageInfo | RawMessageInfo): string {

function robotextForMessageInfo(
messageInfo: RobotextMessageInfo,
threadInfo: ?ThreadInfo,
parentThreadInfo: ?ThreadInfo,
threadInfo: ?ThreadInfo | ?MinimallyEncodedThreadInfo,
parentThreadInfo: ?ThreadInfo | ?MinimallyEncodedThreadInfo,
): EntityText {
const messageSpec = messageSpecs[messageInfo.type];
invariant(
Expand Down Expand Up @@ -413,8 +413,8 @@ function getMessageTitle(
| RobotextMessageInfo
| ReactionMessageInfo
| EditMessageInfo,
threadInfo: ThreadInfo,
parentThreadInfo: ?ThreadInfo,
threadInfo: ThreadInfo | MinimallyEncodedThreadInfo,
parentThreadInfo: ?ThreadInfo | ?MinimallyEncodedThreadInfo,
markdownRules: ParserRules,
): EntityText {
const { messageTitle } = messageSpecs[messageInfo.type];
Expand Down Expand Up @@ -500,7 +500,7 @@ export type MessagePreviewResult = {
};
function useMessagePreview(
originalMessageInfo: ?MessageInfo,
threadInfo: ThreadInfo,
threadInfo: ThreadInfo | MinimallyEncodedThreadInfo,
markdownRules: ParserRules,
): ?MessagePreviewResult {
let messageInfo;
Expand Down
11 changes: 6 additions & 5 deletions lib/shared/messages/message-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
RawRobotextMessageInfo,
} from '../../types/message-types.js';
import type { RawUnsupportedMessageInfo } from '../../types/messages/unsupported.js';
import type { MinimallyEncodedThreadInfo } from '../../types/minimally-encoded-thread-permissions-types.js';
import type { NotifTexts } from '../../types/notif-types.js';
import type { ThreadInfo } from '../../types/thread-types.js';
import type { RelativeUserInfo, UserInfo } from '../../types/user-types.js';
Expand All @@ -20,7 +21,7 @@ import { type ParserRules } from '../markdown.js';

export type MessageTitleParam<Info> = {
+messageInfo: Info,
+threadInfo: ThreadInfo,
+threadInfo: ThreadInfo | MinimallyEncodedThreadInfo,
+markdownRules: ParserRules,
};

Expand All @@ -42,8 +43,8 @@ export type CreateMessageInfoParams = {
};

export type RobotextParams = {
+threadInfo: ?ThreadInfo,
+parentThreadInfo: ?ThreadInfo,
+threadInfo: ?ThreadInfo | ?MinimallyEncodedThreadInfo,
+parentThreadInfo: ?ThreadInfo | ?MinimallyEncodedThreadInfo,
};

export type NotificationTextsParams = {
Expand All @@ -65,8 +66,8 @@ export type PushType = $Values<typeof pushTypes>;

export type CreationSideEffectsFunc<RawInfo> = (
messageInfo: RawInfo,
threadInfo: ThreadInfo,
parentThreadInfo: ?ThreadInfo,
threadInfo: ThreadInfo | MinimallyEncodedThreadInfo,
parentThreadInfo: ?ThreadInfo | ?MinimallyEncodedThreadInfo,
) => Promise<mixed>;

export type MessageSpec<Data, RawInfo, Info> = {
Expand Down
5 changes: 3 additions & 2 deletions lib/shared/messages/text-message-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
type TextMessageInfo,
rawTextMessageInfoValidator,
} from '../../types/messages/text.js';
import type { MinimallyEncodedThreadInfo } from '../../types/minimally-encoded-thread-permissions-types.js';
import type { NotifTexts } from '../../types/notif-types.js';
import { threadTypes } from '../../types/thread-types-enum.js';
import type { ThreadInfo } from '../../types/thread-types.js';
Expand Down Expand Up @@ -285,8 +286,8 @@ export const textMessageSpec: MessageSpec<
const callChangeThreadSettings = useChangeThreadSettings();
return async (
messageInfo: RawTextMessageInfo,
threadInfo: ThreadInfo,
parentThreadInfo: ?ThreadInfo,
threadInfo: ThreadInfo | MinimallyEncodedThreadInfo,
parentThreadInfo: ?ThreadInfo | ?MinimallyEncodedThreadInfo,
) => {
if (threadInfo.type !== threadTypes.SIDEBAR) {
return;
Expand Down
41 changes: 28 additions & 13 deletions lib/shared/thread-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ type CreatePendingThreadArgs = {
+viewerID: string,
+threadType: ThreadType,
+members: $ReadOnlyArray<UserIDAndUsername>,
+parentThreadInfo?: ?ThreadInfo,
+parentThreadInfo?: ?ThreadInfo | ?MinimallyEncodedThreadInfo,
+threadColor?: ?string,
+name?: ?string,
+sourceMessageID?: string,
Expand Down Expand Up @@ -590,7 +590,7 @@ function extractNewMentionedParentMembers(
type SharedCreatePendingSidebarInput = {
+sourceMessageInfo: ComposableMessageInfo | RobotextMessageInfo,
+parentThreadInfo: ThreadInfo,
+parentThreadInfo: ThreadInfo | MinimallyEncodedThreadInfo,
+loggedInUserInfo: LoggedInUserInfo,
};
Expand Down Expand Up @@ -1319,9 +1319,10 @@ type ExistingThreadInfoFinderParams = {
};
type ExistingThreadInfoFinder = (
params: ExistingThreadInfoFinderParams,
) => ?ThreadInfo;
) => ?ThreadInfo | ?MinimallyEncodedThreadInfo;
// TODO (atul): Parameterize function once `createPendingThread` is updated.
function useExistingThreadInfoFinder(
baseThreadInfo: ?ThreadInfo,
baseThreadInfo: ?ThreadInfo | ?MinimallyEncodedThreadInfo,
): ExistingThreadInfoFinder {
const threadInfos = useSelector(threadInfoSelector);
const loggedInUserInfo = useLoggedInUserInfo();
Expand All @@ -1331,7 +1332,9 @@ function useExistingThreadInfoFinder(
pendingToRealizedThreadIDsSelector(state.threadStore.threadInfos),
);
return React.useCallback(
(params: ExistingThreadInfoFinderParams): ?ThreadInfo => {
(
params: ExistingThreadInfoFinderParams,
): ?ThreadInfo | ?MinimallyEncodedThreadInfo => {
if (!baseThreadInfo) {
return null;
}
Expand Down Expand Up @@ -1379,10 +1382,22 @@ function useExistingThreadInfoFinder(
members: [loggedInUserInfo, ...userInfoInputArray],
})
: baseThreadInfo;
return {
...updatedThread,
currentUser: getCurrentUser(updatedThread, viewerID, userInfos),
};

if (updatedThread.minimallyEncoded) {
return {
...updatedThread,
currentUser: getMinimallyEncodedCurrentUser(
updatedThread,
viewerID,
userInfos,
),
};
} else {
return {
...updatedThread,
currentUser: getCurrentUser(updatedThread, viewerID, userInfos),
};
}
},
[
baseThreadInfo,
Expand Down Expand Up @@ -1632,7 +1647,7 @@ function useThreadListSearch(

function removeMemberFromThread(
threadInfo: ThreadInfo | MinimallyEncodedThreadInfo,
memberInfo: RelativeMemberInfo,
memberInfo: RelativeMemberInfo | MinimallyEncodedRelativeMemberInfo,
dispatchActionPromise: DispatchActionPromise,
removeUserFromThreadServerCall: (
input: RemoveUsersFromThreadInput,
Expand Down Expand Up @@ -1718,11 +1733,11 @@ function getAvailableThreadMemberActions(
}
function patchThreadInfoToIncludeMentionedMembersOfParent(
threadInfo: ThreadInfo,
parentThreadInfo: ThreadInfo,
threadInfo: ThreadInfo | MinimallyEncodedThreadInfo,
parentThreadInfo: ThreadInfo | MinimallyEncodedThreadInfo,
messageText: string,
viewerID: string,
): ThreadInfo {
): ThreadInfo | MinimallyEncodedThreadInfo {
const members: UserIDAndUsername[] = threadInfo.members
.map(({ id, username }) => (username ? { id, username } : null))
.filter(Boolean);
Expand Down
32 changes: 20 additions & 12 deletions lib/utils/drawer-utils.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { values } from './objects.js';
import { threadInFilterList, threadIsChannel } from '../shared/thread-utils.js';
import type {
MinimallyEncodedRawThreadInfo,
MinimallyEncodedResolvedThreadInfo,
MinimallyEncodedThreadInfo,
} from '../types/minimally-encoded-thread-permissions-types.js';
import { communitySubthreads } from '../types/thread-types-enum.js';
Expand All @@ -14,15 +15,19 @@ import type {
} from '../types/thread-types.js';

export type CommunityDrawerItemData<T> = {
+threadInfo: ThreadInfo,
+threadInfo: ThreadInfo | MinimallyEncodedThreadInfo,
+itemChildren: $ReadOnlyArray<CommunityDrawerItemData<T>>,
+hasSubchannelsButton: boolean,
+labelStyle: T,
};

function createRecursiveDrawerItemsData<LabelStyleType>(
childThreadInfosMap: { +[id: string]: $ReadOnlyArray<ThreadInfo> },
communities: $ReadOnlyArray<ResolvedThreadInfo>,
childThreadInfosMap: {
+[id: string]: $ReadOnlyArray<ThreadInfo | MinimallyEncodedThreadInfo>,
},
communities: $ReadOnlyArray<
ResolvedThreadInfo | MinimallyEncodedResolvedThreadInfo,
>,
labelStyles: $ReadOnlyArray<LabelStyleType>,
maxDepth: number,
): $ReadOnlyArray<CommunityDrawerItemData<LabelStyleType>> {
Expand Down Expand Up @@ -57,12 +62,10 @@ function createRecursiveDrawerItemsData<LabelStyleType>(
return result;
}

function threadHasSubchannels(
threadInfo: ThreadInfo | MinimallyEncodedThreadInfo,
function threadHasSubchannels<T: ThreadInfo | MinimallyEncodedThreadInfo>(
threadInfo: T,
childThreadInfosMap: {
+[id: string]:
| $ReadOnlyArray<ThreadInfo>
| $ReadOnlyArray<MinimallyEncodedThreadInfo>,
+[id: string]: $ReadOnlyArray<T>,
},
): boolean {
if (!childThreadInfosMap[threadInfo.id]?.length) {
Expand All @@ -73,9 +76,9 @@ function threadHasSubchannels(
);
}

function appendSuffix(
chats: $ReadOnlyArray<ResolvedThreadInfo>,
): ResolvedThreadInfo[] {
function appendSuffix<
T: ResolvedThreadInfo | MinimallyEncodedResolvedThreadInfo,
>(chats: $ReadOnlyArray<T>): T[] {
const result = [];
const names = new Map<string, number>();

Expand All @@ -86,7 +89,12 @@ function appendSuffix(
if (numberOfOccurrences) {
name = `${name} (${numberOfOccurrences.toString()})`;
}
result.push({ ...chat, uiName: name });
// Branching to appease `flow`.
if (chat.minimallyEncoded) {
result.push({ ...chat, uiName: name });
} else {
result.push({ ...chat, uiName: name });
}
}
return result;
}
Expand Down
32 changes: 23 additions & 9 deletions lib/utils/entity-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import {
entityTextToRawString,
} from './entity-text.js';
import type { UseENSNamesOptions } from '../hooks/ens-cache.js';
import type {
MinimallyEncodedResolvedThreadInfo,
MinimallyEncodedThreadInfo,
} from '../types/minimally-encoded-thread-permissions-types.js';
import type { ThreadInfo, ResolvedThreadInfo } from '../types/thread-types.js';
import { values } from '../utils/objects.js';

function useResolvedThreadInfos(
threadInfos: $ReadOnlyArray<ThreadInfo>,
threadInfos: $ReadOnlyArray<ThreadInfo | MinimallyEncodedThreadInfo>,
options?: ?UseENSNamesOptions,
): $ReadOnlyArray<ResolvedThreadInfo> {
): $ReadOnlyArray<ResolvedThreadInfo | MinimallyEncodedResolvedThreadInfo> {
const entityText = React.useMemo(
() => threadInfos.map(threadInfo => threadInfo.uiName),
[threadInfos],
Expand All @@ -34,10 +38,18 @@ function useResolvedThreadInfos(
return (threadInfo: any);
}
const resolvedThreadEntity = withENSNames[i];
return {
...threadInfo,
uiName: entityTextToRawString([resolvedThreadEntity]),
};
// Branching to appease `flow`.
if (threadInfo.minimallyEncoded) {
return {
...threadInfo,
uiName: entityTextToRawString([resolvedThreadEntity]),
};
} else {
return {
...threadInfo,
uiName: entityTextToRawString([resolvedThreadEntity]),
};
}
}),
[threadInfos, withENSNames],
);
Expand Down Expand Up @@ -99,15 +111,17 @@ function useResolvedThreadInfosObj(
}, [resolvedThreadInfosArray]);
}

function useResolvedThreadInfo(threadInfo: ThreadInfo): ResolvedThreadInfo {
function useResolvedThreadInfo(
threadInfo: ThreadInfo | MinimallyEncodedThreadInfo,
): ResolvedThreadInfo | MinimallyEncodedResolvedThreadInfo {
const resolutionInput = React.useMemo(() => [threadInfo], [threadInfo]);
const [resolvedThreadInfo] = useResolvedThreadInfos(resolutionInput);
return resolvedThreadInfo;
}

function useResolvedOptionalThreadInfo(
threadInfo: ?ThreadInfo,
): ?ResolvedThreadInfo {
threadInfo: ?ThreadInfo | ?MinimallyEncodedThreadInfo,
): ?ResolvedThreadInfo | ?MinimallyEncodedResolvedThreadInfo {
const resolutionInput = React.useMemo(
() => (threadInfo ? [threadInfo] : []),
[threadInfo],
Expand Down
Loading

0 comments on commit 7c75580

Please sign in to comment.