2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2019 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
import { omit } from 'lodash';
|
2021-05-28 16:15:17 +00:00
|
|
|
import { v4 as getGuid } from 'uuid';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ThunkAction } from 'redux-thunk';
|
2023-01-13 20:07:26 +00:00
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { StateType as RootStateType } from '../reducer';
|
2019-05-16 22:32:11 +00:00
|
|
|
import * as storageShim from '../../shims/storage';
|
2022-12-09 17:35:34 +00:00
|
|
|
import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions';
|
2021-09-17 22:24:21 +00:00
|
|
|
import { useBoundActions } from '../../hooks/useBoundActions';
|
2022-12-21 18:41:48 +00:00
|
|
|
import { drop } from '../../util/drop';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type {
|
2021-06-02 21:05:09 +00:00
|
|
|
ConversationColorType,
|
|
|
|
CustomColorType,
|
2021-06-15 00:09:37 +00:00
|
|
|
CustomColorsItemType,
|
2021-06-08 21:31:35 +00:00
|
|
|
DefaultConversationColorType,
|
2021-06-02 21:05:09 +00:00
|
|
|
} from '../../types/Colors';
|
2021-10-26 19:15:33 +00:00
|
|
|
import { ConversationColors } from '../../types/Colors';
|
2021-06-02 21:05:09 +00:00
|
|
|
import { reloadSelectedConversation } from '../../shims/reloadSelectedConversation';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { StorageAccessType } from '../../types/Storage.d';
|
2021-08-18 20:08:14 +00:00
|
|
|
import { actions as conversationActions } from './conversations';
|
2021-11-01 19:13:35 +00:00
|
|
|
import type { ConfigMapType as RemoteConfigType } from '../../RemoteConfig';
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
// State
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type ItemsStateType = ReadonlyDeep<{
|
|
|
|
universalExpireTimer?: number;
|
2021-06-01 20:45:43 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
[key: string]: unknown;
|
2021-06-02 21:05:09 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
remoteConfig?: RemoteConfigType;
|
2023-07-13 19:06:42 +00:00
|
|
|
serverTimeSkew?: number;
|
2021-11-01 19:13:35 +00:00
|
|
|
|
2021-06-02 21:05:09 +00:00
|
|
|
// This property should always be set and this is ensured in background.ts
|
2023-01-13 20:07:26 +00:00
|
|
|
defaultConversationColor?: DefaultConversationColorType;
|
2021-06-02 21:05:09 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
customColors?: CustomColorsItemType;
|
2021-09-09 16:29:01 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
preferredLeftPaneWidth?: number;
|
2021-10-12 23:59:08 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
preferredReactionEmoji?: Array<string>;
|
2021-11-30 16:29:57 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
areWeASubscriber?: boolean;
|
|
|
|
}>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type ItemPutAction = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
type: 'items/PUT';
|
2020-03-31 20:03:38 +00:00
|
|
|
payload: null;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type ItemPutExternalAction = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
type: 'items/PUT_EXTERNAL';
|
|
|
|
payload: {
|
|
|
|
key: string;
|
2020-09-14 21:56:35 +00:00
|
|
|
value: unknown;
|
2019-05-16 22:32:11 +00:00
|
|
|
};
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type ItemRemoveAction = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
type: 'items/REMOVE';
|
2020-03-31 20:03:38 +00:00
|
|
|
payload: null;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type ItemRemoveExternalAction = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
type: 'items/REMOVE_EXTERNAL';
|
|
|
|
payload: string;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type ItemsResetAction = ReadonlyDeep<{
|
2019-05-16 22:32:11 +00:00
|
|
|
type: 'items/RESET';
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type ItemsActionType = ReadonlyDeep<
|
2019-05-16 22:32:11 +00:00
|
|
|
| ItemPutAction
|
|
|
|
| ItemPutExternalAction
|
|
|
|
| ItemRemoveAction
|
|
|
|
| ItemRemoveExternalAction
|
2023-01-13 20:07:26 +00:00
|
|
|
| ItemsResetAction
|
|
|
|
>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
|
|
|
|
export const actions = {
|
2021-05-28 16:15:17 +00:00
|
|
|
addCustomColor,
|
|
|
|
editCustomColor,
|
2023-07-13 19:06:42 +00:00
|
|
|
markHasCompletedSafetyNumberOnboarding,
|
2021-05-28 16:15:17 +00:00
|
|
|
removeCustomColor,
|
2021-06-02 21:05:09 +00:00
|
|
|
resetDefaultChatColor,
|
2021-10-12 23:59:08 +00:00
|
|
|
savePreferredLeftPaneWidth,
|
2021-06-02 21:05:09 +00:00
|
|
|
setGlobalDefaultConversationColor,
|
2021-04-27 22:35:35 +00:00
|
|
|
onSetSkinTone,
|
2019-05-16 22:32:11 +00:00
|
|
|
putItem,
|
|
|
|
putItemExternal,
|
|
|
|
removeItem,
|
|
|
|
removeItemExternal,
|
|
|
|
resetItems,
|
|
|
|
};
|
|
|
|
|
2022-12-09 17:35:34 +00:00
|
|
|
export const useActions = (): BoundActionCreatorsMapObject<typeof actions> =>
|
|
|
|
useBoundActions(actions);
|
2020-05-05 19:49:34 +00:00
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
function putItem<K extends keyof StorageAccessType>(
|
|
|
|
key: K,
|
|
|
|
value: StorageAccessType[K]
|
2022-12-21 18:41:48 +00:00
|
|
|
): ThunkAction<void, RootStateType, unknown, ItemPutAction> {
|
|
|
|
return async dispatch => {
|
|
|
|
dispatch({
|
|
|
|
type: 'items/PUT',
|
|
|
|
payload: null,
|
|
|
|
});
|
|
|
|
await storageShim.put(key, value);
|
2019-05-16 22:32:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
function onSetSkinTone(
|
|
|
|
tone: number
|
|
|
|
): ThunkAction<void, RootStateType, unknown, ItemPutAction> {
|
2021-04-27 22:35:35 +00:00
|
|
|
return putItem('skinTone', tone);
|
|
|
|
}
|
|
|
|
|
2020-09-14 21:56:35 +00:00
|
|
|
function putItemExternal(key: string, value: unknown): ItemPutExternalAction {
|
2019-05-16 22:32:11 +00:00
|
|
|
return {
|
|
|
|
type: 'items/PUT_EXTERNAL',
|
|
|
|
payload: {
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
function removeItem(key: keyof StorageAccessType): ItemRemoveAction {
|
2022-12-21 18:41:48 +00:00
|
|
|
drop(storageShim.remove(key));
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
return {
|
|
|
|
type: 'items/REMOVE',
|
2020-03-31 20:03:38 +00:00
|
|
|
payload: null,
|
2019-05-16 22:32:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeItemExternal(key: string): ItemRemoveExternalAction {
|
|
|
|
return {
|
|
|
|
type: 'items/REMOVE_EXTERNAL',
|
|
|
|
payload: key,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetItems(): ItemsResetAction {
|
|
|
|
return { type: 'items/RESET' };
|
|
|
|
}
|
|
|
|
|
2021-05-28 16:15:17 +00:00
|
|
|
function getDefaultCustomColorData() {
|
|
|
|
return {
|
2023-03-20 19:58:07 +00:00
|
|
|
colors: {} as Record<string, CustomColorType>,
|
2021-05-28 16:15:17 +00:00
|
|
|
version: 1,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function addCustomColor(
|
2021-06-03 21:34:36 +00:00
|
|
|
customColor: CustomColorType,
|
2021-08-18 20:08:14 +00:00
|
|
|
conversationId?: string
|
2021-05-28 16:15:17 +00:00
|
|
|
): ThunkAction<void, RootStateType, unknown, ItemPutAction> {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const { customColors = getDefaultCustomColorData() } = getState().items;
|
|
|
|
|
|
|
|
let uuid = getGuid();
|
|
|
|
while (customColors.colors[uuid]) {
|
|
|
|
uuid = getGuid();
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextCustomColors = {
|
|
|
|
...customColors,
|
|
|
|
colors: {
|
|
|
|
...customColors.colors,
|
2021-06-03 21:34:36 +00:00
|
|
|
[uuid]: customColor,
|
2021-05-28 16:15:17 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
dispatch(putItem('customColors', nextCustomColors));
|
2021-08-18 20:08:14 +00:00
|
|
|
|
|
|
|
const customColorData = {
|
|
|
|
id: uuid,
|
|
|
|
value: customColor,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (conversationId) {
|
|
|
|
conversationActions.colorSelected({
|
|
|
|
conversationId,
|
|
|
|
conversationColor: 'custom',
|
|
|
|
customColorData,
|
|
|
|
})(dispatch, getState, null);
|
|
|
|
} else {
|
|
|
|
setGlobalDefaultConversationColor('custom', customColorData)(
|
|
|
|
dispatch,
|
|
|
|
getState,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
}
|
2021-05-28 16:15:17 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function editCustomColor(
|
|
|
|
colorId: string,
|
|
|
|
color: CustomColorType
|
|
|
|
): ThunkAction<void, RootStateType, unknown, ItemPutAction> {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const { customColors = getDefaultCustomColorData() } = getState().items;
|
|
|
|
|
|
|
|
if (!customColors.colors[colorId]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextCustomColors = {
|
|
|
|
...customColors,
|
|
|
|
colors: {
|
|
|
|
...customColors.colors,
|
|
|
|
[colorId]: color,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
dispatch(putItem('customColors', nextCustomColors));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeCustomColor(
|
|
|
|
payload: string
|
|
|
|
): ThunkAction<void, RootStateType, unknown, ItemPutAction> {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const { customColors = getDefaultCustomColorData() } = getState().items;
|
|
|
|
|
|
|
|
const nextCustomColors = {
|
|
|
|
...customColors,
|
|
|
|
colors: omit(customColors.colors, payload),
|
|
|
|
};
|
|
|
|
|
|
|
|
dispatch(putItem('customColors', nextCustomColors));
|
2021-08-25 00:15:12 +00:00
|
|
|
resetDefaultChatColor()(dispatch, getState, null);
|
2021-05-28 16:15:17 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-02 21:05:09 +00:00
|
|
|
function resetDefaultChatColor(): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
ItemPutAction
|
|
|
|
> {
|
|
|
|
return dispatch => {
|
|
|
|
dispatch(
|
|
|
|
putItem('defaultConversationColor', {
|
|
|
|
color: ConversationColors[0],
|
|
|
|
})
|
|
|
|
);
|
|
|
|
reloadSelectedConversation();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function setGlobalDefaultConversationColor(
|
|
|
|
color: ConversationColorType,
|
|
|
|
customColorData?: {
|
|
|
|
id: string;
|
|
|
|
value: CustomColorType;
|
|
|
|
}
|
|
|
|
): ThunkAction<void, RootStateType, unknown, ItemPutAction> {
|
|
|
|
return dispatch => {
|
|
|
|
dispatch(
|
|
|
|
putItem('defaultConversationColor', {
|
|
|
|
color,
|
|
|
|
customColorData,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
reloadSelectedConversation();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-12 23:59:08 +00:00
|
|
|
function savePreferredLeftPaneWidth(
|
|
|
|
preferredWidth: number
|
|
|
|
): ThunkAction<void, RootStateType, unknown, ItemPutAction> {
|
|
|
|
return dispatch => {
|
|
|
|
dispatch(putItem('preferredLeftPaneWidth', preferredWidth));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-07-13 19:06:42 +00:00
|
|
|
function markHasCompletedSafetyNumberOnboarding(): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
ItemPutAction
|
|
|
|
> {
|
|
|
|
return dispatch => {
|
|
|
|
dispatch(putItem('hasCompletedSafetyNumberOnboarding', true));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
// Reducer
|
|
|
|
|
2022-02-23 18:48:40 +00:00
|
|
|
export function getEmptyState(): ItemsStateType {
|
2021-06-02 21:05:09 +00:00
|
|
|
return {
|
|
|
|
defaultConversationColor: {
|
|
|
|
color: ConversationColors[0],
|
|
|
|
},
|
|
|
|
};
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function reducer(
|
2020-12-14 19:47:21 +00:00
|
|
|
state: Readonly<ItemsStateType> = getEmptyState(),
|
|
|
|
action: Readonly<ItemsActionType>
|
2019-05-16 22:32:11 +00:00
|
|
|
): ItemsStateType {
|
|
|
|
if (action.type === 'items/PUT_EXTERNAL') {
|
|
|
|
const { payload } = action;
|
|
|
|
|
2022-12-19 22:33:55 +00:00
|
|
|
if (state[payload.key] === payload.value) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
[payload.key]: payload.value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'items/REMOVE_EXTERNAL') {
|
|
|
|
const { payload } = action;
|
|
|
|
|
|
|
|
return omit(state, payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'items/RESET') {
|
|
|
|
return getEmptyState();
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|