Skip to content

Commit

Permalink
ActionSheet [nfc]: Update signature of actionsheet to not use
Browse files Browse the repository at this point in the history
`streamName`.

Also involves necessary changes at call sites, and some actionsheet
functions.
  • Loading branch information
AkashDhiman committed Jul 1, 2021
1 parent 18c04bd commit 67f4878
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 39 deletions.
36 changes: 14 additions & 22 deletions src/message/__tests__/messageActionSheet-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @flow strict-local
import deepFreeze from 'deep-freeze';
import { HOME_NARROW } from '../../utils/narrow';
import { streamNameOfStreamMessage } from '../../utils/recipient';

import * as eg from '../../__tests__/lib/exampleData';
import { constructMessageActionButtons, constructTopicActionButtons } from '../messageActionSheet';
Expand Down Expand Up @@ -46,10 +45,11 @@ describe('constructActionButtons', () => {
});

describe('constructTopicActionButtons', () => {
const streamMessage = eg.streamMessage();
const streamName = streamNameOfStreamMessage(streamMessage);
const stream = eg.makeStream();
const streamMessage = eg.streamMessage({ stream });
const topic = streamMessage.subject;
const streamId = streamMessage.stream_id;
const streams = deepFreeze(new Map([[stream.stream_id, stream]]));

const baseState = (() => {
const r = (state, action) => reducer(state, action, eg.plusReduxState);
Expand All @@ -61,8 +61,7 @@ describe('constructTopicActionButtons', () => {
test('show mark as read if topic is unread', () => {
const unread = baseState;
const buttons = constructTopicActionButtons({
backgroundData: { ...eg.backgroundData, unread },
streamName,
backgroundData: { ...eg.backgroundData, streams, unread },
streamId,
topic,
});
Expand All @@ -71,19 +70,17 @@ describe('constructTopicActionButtons', () => {

test('do not show mark as read if topic is read', () => {
const buttons = constructTopicActionButtons({
backgroundData: eg.backgroundData,
streamName,
backgroundData: { ...eg.backgroundData, streams },
streamId,
topic,
});
expect(buttonTitles(buttons)).not.toContain('Mark topic as read');
});

test('show Unmute topic option if topic is muted', () => {
const mute = deepFreeze([[streamName, topic]]);
const mute = deepFreeze([[stream.name, topic]]);
const buttons = constructTopicActionButtons({
backgroundData: { ...eg.backgroundData, mute },
streamName,
backgroundData: { ...eg.backgroundData, streams, mute },
streamId,
topic,
});
Expand All @@ -92,30 +89,27 @@ describe('constructTopicActionButtons', () => {

test('show mute topic option if topic is not muted', () => {
const buttons = constructTopicActionButtons({
backgroundData: { ...eg.backgroundData, mute: [] },
streamName,
backgroundData: { ...eg.backgroundData, streams, mute: [] },
streamId,
topic,
});
expect(buttonTitles(buttons)).toContain('Mute topic');
});

test('show Unmute stream option if stream is not in home view', () => {
const subscriptions = [{ ...eg.subscription, in_home_view: false }];
const subscriptions = [{ ...eg.subscription, in_home_view: false, ...stream }];
const buttons = constructTopicActionButtons({
backgroundData: { ...eg.backgroundData, subscriptions },
streamName,
backgroundData: { ...eg.backgroundData, subscriptions, streams },
streamId,
topic,
});
expect(buttonTitles(buttons)).toContain('Unmute stream');
});

test('show mute stream option if stream is in home view', () => {
const subscriptions = [{ ...eg.subscription, in_home_view: true }];
const subscriptions = [{ ...eg.subscription, in_home_view: true, ...stream }];
const buttons = constructTopicActionButtons({
backgroundData: { ...eg.backgroundData, subscriptions },
streamName,
backgroundData: { ...eg.backgroundData, subscriptions, streams },
streamId,
topic,
});
Expand All @@ -125,8 +119,7 @@ describe('constructTopicActionButtons', () => {
test('show delete topic option if current user is an admin', () => {
const ownUser = { ...eg.selfUser, is_admin: true };
const buttons = constructTopicActionButtons({
backgroundData: { ...eg.backgroundData, ownUser },
streamName,
backgroundData: { ...eg.backgroundData, ownUser, streams },
streamId,
topic,
});
Expand All @@ -135,8 +128,7 @@ describe('constructTopicActionButtons', () => {

test('do not show delete topic option if current user is not an admin', () => {
const buttons = constructTopicActionButtons({
backgroundData: eg.backgroundData,
streamName,
backgroundData: { ...eg.backgroundData, streams },
streamId,
topic,
});
Expand Down
31 changes: 17 additions & 14 deletions src/message/messageActionSheet.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* @flow strict-local */
import { Clipboard, Share, Alert } from 'react-native';
import invariant from 'invariant';

import * as NavigationService from '../nav/NavigationService';
import type {
Expand Down Expand Up @@ -40,10 +41,10 @@ export type ShowActionSheetWithOptions = (

type TopicArgs = {
auth: Auth,
streamName: string,
streamId: number,
topic: string,
subscriptions: Subscription[],
streams: Map<number, Stream>,
dispatch: Dispatch,
_: GetText,
...
Expand Down Expand Up @@ -128,14 +129,18 @@ const markTopicAsRead = async ({ auth, streamId, topic }) => {
markTopicAsRead.title = 'Mark topic as read';
markTopicAsRead.errorMessage = 'Failed to mark topic as read';

const unmuteTopic = async ({ auth, streamName, topic }) => {
await api.setTopicMute(auth, streamName, topic, false);
const unmuteTopic = async ({ auth, streamId, topic, streams }) => {
const stream = streams.get(streamId);
invariant(stream !== undefined, 'Stream with provided streamId must exist.');
await api.setTopicMute(auth, stream.name, topic, false);
};
unmuteTopic.title = 'Unmute topic';
unmuteTopic.errorMessage = 'Failed to unmute topic';

const muteTopic = async ({ auth, streamName, topic }) => {
await api.setTopicMute(auth, streamName, topic, true);
const muteTopic = async ({ auth, streamId, topic, streams }) => {
const stream = streams.get(streamId);
invariant(stream !== undefined, 'Stream with provided streamId must exist.');
await api.setTopicMute(auth, stream.name, topic, true);
};
muteTopic.title = 'Mute topic';
muteTopic.errorMessage = 'Failed to mute topic';
Expand Down Expand Up @@ -229,7 +234,6 @@ cancel.errorMessage = 'Failed to hide menu';

export const constructTopicActionButtons = ({
backgroundData: { mute, ownUser, streams, subscriptions, unread },
streamName,
streamId,
topic,
}: {|
Expand All @@ -241,7 +245,6 @@ export const constructTopicActionButtons = ({
ownUser: User,
...
}>,
streamName: string,
streamId: number,
topic: string,
|}): Button<TopicArgs>[] => {
Expand All @@ -253,12 +256,14 @@ export const constructTopicActionButtons = ({
if (unreadCount > 0) {
buttons.push(markTopicAsRead);
}
if (isTopicMuted(streamName, topic, mute)) {
const stream = streams.get(streamId);
invariant(stream !== undefined, 'Stream with provided streamId not found.');
if (isTopicMuted(stream.name, topic, mute)) {
buttons.push(unmuteTopic);
} else {
buttons.push(muteTopic);
}
const sub = subscriptions.find(x => x.name === streamName);
const sub = subscriptions.find(x => x.stream_id === streamId);
if (sub && !sub.in_home_view) {
buttons.push(unmuteStream);
} else {
Expand Down Expand Up @@ -403,7 +408,6 @@ export const showTopicActionSheet = ({
callbacks,
backgroundData,
topic,
streamName,
streamId,
}: {|
showActionSheetWithOptions: ShowActionSheetWithOptions,
Expand All @@ -421,26 +425,25 @@ export const showTopicActionSheet = ({
flags: FlagsState,
...
}>,
streamName: string,
streamId: number,
topic: string,
|}): void => {
const buttonList = constructTopicActionButtons({
backgroundData,
streamName,
streamId,
topic,
});
const stream = backgroundData.streams.get(streamId);
invariant(stream !== undefined, 'Stream with provided streamId not found.');
showActionSheetWithOptions(
{
title: `#${streamName} > ${topic}`,
title: `#${stream.name} > ${topic}`,
options: buttonList.map(button => callbacks._(button.title)),
cancelButtonIndex: buttonList.length - 1,
},
makeButtonCallback(buttonList, {
...backgroundData,
...callbacks,
streamName,
streamId,
topic,
}),
Expand Down
1 change: 0 additions & 1 deletion src/streams/TopicItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export default function TopicItem(props: Props) {
showActionSheetWithOptions,
callbacks: { dispatch, _ },
backgroundData,
streamName,
streamId: stream[0],
topic: name,
});
Expand Down
1 change: 0 additions & 1 deletion src/title/TitleStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ const TitleStream = (props: Props) => {
showActionSheetWithOptions,
callbacks: { dispatch, _ },
backgroundData,
streamName: stream.name,
streamId: stream.stream_id,
topic: topicOfNarrow(narrow),
});
Expand Down
1 change: 0 additions & 1 deletion src/webview/handleOutboundEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ const handleLongPress = (
showActionSheetWithOptions,
callbacks: { dispatch, _ },
backgroundData,
streamName,
streamId: stream[0],
topic: message.subject,
});
Expand Down

0 comments on commit 67f4878

Please sign in to comment.