Skip to content

Commit 5dffebf

Browse files
Chore: Convert AccountPreferencesPage to ts (#26096)
1 parent 2b90c48 commit 5dffebf

1 file changed

Lines changed: 51 additions & 12 deletions

File tree

apps/meteor/client/views/account/preferences/AccountPreferencesPage.js renamed to apps/meteor/client/views/account/preferences/AccountPreferencesPage.tsx

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ButtonGroup, Button, Box, Accordion } from '@rocket.chat/fuselage';
22
import { useToastMessageDispatch, useSetting, useMethod, useTranslation } from '@rocket.chat/ui-contexts';
3-
import React, { useState, useCallback, useRef } from 'react';
3+
import React, { useState, useCallback, useRef, ReactElement } from 'react';
44

55
import Page from '../../../components/Page';
66
import PreferencesGlobalSection from './PreferencesGlobalSection';
@@ -12,24 +12,63 @@ import PreferencesNotificationsSection from './PreferencesNotificationsSection';
1212
import PreferencesSoundSection from './PreferencesSoundSection';
1313
import PreferencesUserPresenceSection from './PreferencesUserPresenceSection';
1414

15-
const AccountPreferencesPage = () => {
15+
type CurrentData = {
16+
enableNewMessageTemplate: boolean;
17+
language: string;
18+
newRoomNotification: string;
19+
newMessageNotification: string;
20+
clockMode: number;
21+
useEmojis: boolean;
22+
convertAsciiEmoji: boolean;
23+
saveMobileBandwidth: boolean;
24+
collapseMediaByDefault: boolean;
25+
autoImageLoad: boolean;
26+
emailNotificationMode: string;
27+
unreadAlert: boolean;
28+
notificationsSoundVolume: number;
29+
desktopNotifications: string;
30+
pushNotifications: string;
31+
enableAutoAway: boolean;
32+
highlights: string;
33+
messageViewMode: number;
34+
hideUsernames: boolean;
35+
hideRoles: boolean;
36+
displayAvatars: boolean;
37+
hideFlexTab: boolean;
38+
sendOnEnter: string;
39+
idleTimeLimit: number;
40+
sidebarShowFavorites: boolean;
41+
sidebarShowUnread: boolean;
42+
sidebarSortby: string;
43+
sidebarViewMode: string;
44+
sidebarDisplayAvatar: boolean;
45+
sidebarGroupByType: boolean;
46+
muteFocusedConversations: boolean;
47+
dontAskAgainList: [action: string, label: string][];
48+
};
49+
50+
type FormatedData = Omit<Partial<CurrentData>, 'dontAskAgainList' | 'highlights'>;
51+
52+
const AccountPreferencesPage = (): ReactElement => {
1653
const t = useTranslation();
1754
const dispatchToastMessage = useToastMessageDispatch();
1855

1956
const [hasAnyChange, setHasAnyChange] = useState(false);
2057

21-
const saveData = useRef({});
58+
const saveData = useRef<Partial<CurrentData>>({});
2259
const commitRef = useRef({});
2360

2461
const dataDownloadEnabled = useSetting('UserData_EnableDownload');
2562

2663
const onChange = useCallback(
2764
({ initialValue, value, key }) => {
2865
const { current } = saveData;
29-
if (JSON.stringify(initialValue) !== JSON.stringify(value)) {
30-
current[key] = value;
31-
} else {
32-
delete current[key];
66+
if (current) {
67+
if (JSON.stringify(initialValue) !== JSON.stringify(value)) {
68+
current[key as keyof CurrentData] = value;
69+
} else {
70+
delete current[key as keyof CurrentData];
71+
}
3372
}
3473

3574
const anyChange = !!Object.values(current).length;
@@ -45,7 +84,7 @@ const AccountPreferencesPage = () => {
4584
const handleSave = useCallback(async () => {
4685
try {
4786
const { current: data } = saveData;
48-
if (data.highlights || data.highlights === '') {
87+
if (data?.highlights || data?.highlights === '') {
4988
Object.assign(data, {
5089
highlights: data.highlights
5190
.split(/,|\n/)
@@ -54,22 +93,22 @@ const AccountPreferencesPage = () => {
5493
});
5594
}
5695

57-
if (data.dontAskAgainList) {
96+
if (data?.dontAskAgainList) {
5897
const list =
5998
Array.isArray(data.dontAskAgainList) && data.dontAskAgainList.length > 0
6099
? data.dontAskAgainList.map(([action, label]) => ({ action, label }))
61100
: [];
62101
Object.assign(data, { dontAskAgainList: list });
63102
}
64103

65-
await saveFn(data);
104+
await saveFn(data as FormatedData);
66105
saveData.current = {};
67106
setHasAnyChange(false);
68-
Object.values(commitRef.current).forEach((fn) => fn());
107+
Object.values(commitRef.current).forEach((fn) => (fn as () => void)());
69108

70109
dispatchToastMessage({ type: 'success', message: t('Preferences_saved') });
71110
} catch (e) {
72-
dispatchToastMessage({ type: 'error', message: e });
111+
dispatchToastMessage({ type: 'error', message: String(e) });
73112
}
74113
}, [dispatchToastMessage, saveFn, t]);
75114

0 commit comments

Comments
 (0)