Skip to content

Commit

Permalink
Update input type of useSetThreadUnreadStatus
Browse files Browse the repository at this point in the history
Summary: This differential updates input type of useSetThreadunreadStatus

Test Plan: Check that manually updating threads as read/unread works for both thick and thin threads/

Reviewers: tomek, kamil

Reviewed By: tomek

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D13303
  • Loading branch information
marcinwasowicz committed Sep 12, 2024
1 parent 6847b92 commit 0f616c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
28 changes: 19 additions & 9 deletions lib/actions/activity-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import type {
} from '../types/activity-types.js';
import type { DMChangeThreadReadStatusOperation } from '../types/dm-ops.js';
import type { ThreadInfo } from '../types/minimally-encoded-thread-permissions-types';
import { threadTypeIsThick } from '../types/thread-types-enum.js';
import { useSelector } from '../utils/redux-utils.js';

export type UpdateActivityInput = {
Expand Down Expand Up @@ -108,10 +107,19 @@ const setThreadUnreadStatus =
};
};

function useSetThreadUnreadStatus(
threadInfo: ThreadInfo,
): (
request: SetThreadUnreadStatusRequest,
export type UseSetThreadUnreadStatusInput = $ReadOnly<
| {
+thick: false,
...SetThreadUnreadStatusRequest,
}
| {
+thick: true,
+threadInfo: ThreadInfo,
...SetThreadUnreadStatusRequest,
},
>;
function useSetThreadUnreadStatus(): (
imput: UseSetThreadUnreadStatusInput,
) => Promise<SetThreadUnreadStatusPayload> {
const viewerID = useSelector(
state => state.currentUserInfo && state.currentUserInfo.id,
Expand All @@ -120,12 +128,14 @@ function useSetThreadUnreadStatus(
const keyserverCall = useKeyserverCall(setThreadUnreadStatus);

return React.useCallback(
async (input: SetThreadUnreadStatusRequest) => {
if (!threadTypeIsThick(threadInfo.type)) {
return await keyserverCall(input);
async (input: UseSetThreadUnreadStatusInput) => {
if (!input.thick) {
const { thick, ...rest } = input;
return await keyserverCall({ ...rest });
}

invariant(viewerID, 'viewerID must be set');
const { threadInfo } = input;
const op: DMChangeThreadReadStatusOperation = {
type: 'change_thread_read_status',
time: Date.now(),
Expand All @@ -149,7 +159,7 @@ function useSetThreadUnreadStatus(
};
},

[keyserverCall, threadInfo, viewerID, processAndSendDMOperation],
[keyserverCall, viewerID, processAndSendDMOperation],
);
}

Expand Down
22 changes: 14 additions & 8 deletions lib/hooks/toggle-unread-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import {
setThreadUnreadStatusActionTypes,
useSetThreadUnreadStatus,
} from '../actions/activity-actions.js';
import type {
SetThreadUnreadStatusPayload,
SetThreadUnreadStatusRequest,
} from '../types/activity-types.js';
import type { UseSetThreadUnreadStatusInput } from '../actions/activity-actions.js';
import type { SetThreadUnreadStatusPayload } from '../types/activity-types.js';
import type { ThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js';
import { threadTypeIsThick } from '../types/thread-types-enum.js';
import { useDispatchActionPromise } from '../utils/redux-promise-utils.js';

function useToggleUnreadStatus(
Expand All @@ -21,19 +20,26 @@ function useToggleUnreadStatus(
const dispatchActionPromise = useDispatchActionPromise();
const { currentUser } = threadInfo;
const boundSetThreadUnreadStatus: (
request: SetThreadUnreadStatusRequest,
) => Promise<SetThreadUnreadStatusPayload> =
useSetThreadUnreadStatus(threadInfo);
input: UseSetThreadUnreadStatusInput,
) => Promise<SetThreadUnreadStatusPayload> = useSetThreadUnreadStatus();

const toggleUnreadStatus = React.useCallback(() => {
const request = {
threadID: threadInfo.id,
unread: !currentUser.unread,
latestMessage: mostRecentNonLocalMessage,
};
const input = threadTypeIsThick(threadInfo.type)
? {
thick: true,
threadInfo,
...request,
}
: { thick: false, ...request };

void dispatchActionPromise(
setThreadUnreadStatusActionTypes,
boundSetThreadUnreadStatus(request),
boundSetThreadUnreadStatus(input),
undefined,
({
threadID: threadInfo.id,
Expand Down

0 comments on commit 0f616c5

Please sign in to comment.