-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathTimerNotification.tsx
More file actions
215 lines (193 loc) · 7.25 KB
/
TimerNotification.tsx
File metadata and controls
215 lines (193 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import styled from 'styled-components';
import { PubkeyType } from 'libsession_util_nodejs';
import { isNil } from 'lodash';
import { getAppDispatch } from '../../state/dispatch';
import {
useSelectedConversationDisappearingMode,
useSelectedConversationKey,
useSelectedExpireTimer,
useSelectedIsGroupOrCommunity,
useSelectedIsGroupV2,
useSelectedIsPrivateFriend,
useSelectedIsPublic,
} from '../../state/selectors/selectedConversation';
import { Flex } from '../basic/Flex';
import { SpacerMD, TextWithChildren } from '../basic/Text';
import { ExpirableReadableMessage } from './message/message-item/ExpirableReadableMessage';
// eslint-disable-next-line import/order
import { ConversationInteraction } from '../../interactions';
import { ConvoHub } from '../../session/conversations';
import { updateConfirmModal } from '../../state/ducks/modalDialog';
import { Localizer } from '../basic/Localizer';
import { SessionButtonColor } from '../basic/SessionButton';
import { SessionIcon } from '../icon';
import { getTimerNotificationStr } from '../../models/timerNotifications';
import type { WithContextMenuId, WithMessageId } from '../../session/types/with';
import {
useMessageAuthor,
useMessageAuthorIsUs,
useMessageExpirationUpdateDisabled,
useMessageExpirationUpdateMode,
useMessageExpirationUpdateTimespanSeconds,
useMessageExpirationUpdateTimespanText,
} from '../../state/selectors';
import { tr, type TrArgs } from '../../localization/localeTools';
import type { WithPopoverPosition, WithSetPopoverPosition } from '../SessionTooltip';
const FollowSettingButton = styled.button`
color: var(--primary-color);
`;
function useFollowSettingsButtonClick({ messageId }: WithMessageId) {
const selectedConvoKey = useSelectedConversationKey();
const timespanSeconds = useMessageExpirationUpdateTimespanSeconds(messageId);
const expirationMode = useMessageExpirationUpdateMode(messageId);
const disabled = useMessageExpirationUpdateDisabled(messageId);
const timespanText = useMessageExpirationUpdateTimespanText(messageId);
const dispatch = getAppDispatch();
const onExit = () => dispatch(updateConfirmModal(null));
const doIt = () => {
const localizedMode =
expirationMode === 'deleteAfterRead'
? tr('disappearingMessagesTypeRead')
: tr('disappearingMessagesTypeSent');
const i18nMessage: TrArgs = disabled
? {
token: 'disappearingMessagesFollowSettingOff',
}
: {
token: 'disappearingMessagesFollowSettingOn',
time: timespanText,
disappearing_messages_type: localizedMode,
};
dispatch(
updateConfirmModal({
title: { token: 'disappearingMessagesFollowSetting' },
i18nMessage,
okText: { token: 'confirm' },
okTheme: SessionButtonColor.Danger,
onClickOk: async () => {
if (!selectedConvoKey) {
throw new Error('no selected convo key');
}
const convo = ConvoHub.use().get(selectedConvoKey);
if (!convo) {
throw new Error('no selected convo');
}
if (!convo.isPrivate()) {
throw new Error('follow settings only work for private chats');
}
if (expirationMode !== 'off' && !timespanSeconds) {
throw new Error('non-off mode requires seconds arg to be given');
}
await ConversationInteraction.setDisappearingMessagesByConvoId(
selectedConvoKey,
expirationMode,
timespanSeconds ?? undefined
);
onExit();
},
onClickClose: onExit,
})
);
};
return { doIt };
}
function useOurExpirationMatches({ messageId }: WithMessageId) {
const timespanSeconds = useMessageExpirationUpdateTimespanSeconds(messageId);
const expirationMode = useMessageExpirationUpdateMode(messageId);
const disabled = useMessageExpirationUpdateDisabled(messageId);
const selectedMode = useSelectedConversationDisappearingMode();
const selectedTimespan = useSelectedExpireTimer();
if (disabled && (selectedMode === 'off' || selectedMode === undefined)) {
return true;
}
if (expirationMode === selectedMode && timespanSeconds === selectedTimespan) {
return true;
}
return false;
}
// NOTE: [react-compiler] this convinces the compiler the hook is static
const useSelectedIsPrivateFriendInternal = useSelectedIsPrivateFriend;
const useMessageAuthorIsUsInternal = useMessageAuthorIsUs;
const FollowSettingsButton = ({ messageId }: WithMessageId) => {
const isPrivateAndFriend = useSelectedIsPrivateFriendInternal();
const authorIsUs = useMessageAuthorIsUsInternal(messageId);
const click = useFollowSettingsButtonClick({
messageId,
});
const areSameThanOurs = useOurExpirationMatches({ messageId });
if (!isPrivateAndFriend) {
return null;
}
if (authorIsUs || areSameThanOurs) {
return null;
}
return (
<FollowSettingButton
// eslint-disable-next-line @typescript-eslint/no-misused-promises
onClick={() => click.doIt()}
>
{tr('disappearingMessagesFollowSetting')}
</FollowSettingButton>
);
};
export const TimerNotification = (
props: WithMessageId & WithPopoverPosition & WithSetPopoverPosition & WithContextMenuId
) => {
const { messageId } = props;
const timespanSeconds = useMessageExpirationUpdateTimespanSeconds(messageId);
const expirationMode = useMessageExpirationUpdateMode(messageId);
const disabled = useMessageExpirationUpdateDisabled(messageId);
const pubkey = useMessageAuthor(messageId);
const convoId = useSelectedConversationKey();
const isGroupOrCommunity = useSelectedIsGroupOrCommunity();
const isGroupV2 = useSelectedIsGroupV2();
const isPublic = useSelectedIsPublic();
if (!convoId || !messageId || isNil(timespanSeconds) || isNil(expirationMode)) {
return null;
}
const i18nProps = getTimerNotificationStr({
convoId,
author: pubkey as PubkeyType,
expirationMode,
isGroup: isGroupOrCommunity,
timespanSeconds,
});
// renderOff is true when the update is put to off, or when we have a legacy group control message (as they are not expiring at all)
const renderOffIcon = disabled || (isGroupOrCommunity && isPublic && !isGroupV2);
return (
<ExpirableReadableMessage
contextMenuId={props.contextMenuId}
setTriggerPosition={props.setTriggerPosition}
messageId={messageId}
key={`readable-message-${messageId}`}
dataTestId={'disappear-control-message'}
>
<Flex
$container={true}
$flexDirection="column"
$alignItems="center"
$justifyContent="center"
width="90%"
$maxWidth="700px"
$margin="5px auto 10px auto" // top margin is smaller that bottom one to make the stopwatch icon of expirable message closer to its content
$padding="5px 10px"
style={{ textAlign: 'center' }}
>
{renderOffIcon && (
<>
<SessionIcon
iconType="timerFixed"
iconSize={'small'}
iconColor="var(--text-secondary-color)"
/>
<SpacerMD />
</>
)}
<TextWithChildren $subtle={true}>
<Localizer {...i18nProps} />
</TextWithChildren>
<FollowSettingsButton {...props} />
</Flex>
</ExpirableReadableMessage>
);
};