2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2019-2020 Signal Messenger, LLC
|
|
|
|
// 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';
|
|
|
|
import { ThunkAction } from 'redux-thunk';
|
|
|
|
import { StateType as RootStateType } from '../reducer';
|
2019-05-16 22:32:11 +00:00
|
|
|
import * as storageShim from '../../shims/storage';
|
2020-05-05 19:49:34 +00:00
|
|
|
import { useBoundActions } from '../../util/hooks';
|
2021-06-02 21:05:09 +00:00
|
|
|
import {
|
|
|
|
ConversationColors,
|
|
|
|
ConversationColorType,
|
|
|
|
CustomColorType,
|
|
|
|
} from '../../types/Colors';
|
|
|
|
import { reloadSelectedConversation } from '../../shims/reloadSelectedConversation';
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
// State
|
|
|
|
|
|
|
|
export type ItemsStateType = {
|
2021-06-01 20:45:43 +00:00
|
|
|
readonly universalExpireTimer?: number;
|
|
|
|
|
2020-09-14 21:56:35 +00:00
|
|
|
readonly [key: string]: unknown;
|
2021-06-02 21:05:09 +00:00
|
|
|
|
|
|
|
// This property should always be set and this is ensured in background.ts
|
|
|
|
readonly defaultConversationColor?: {
|
|
|
|
color: ConversationColorType;
|
|
|
|
customColorData?: {
|
|
|
|
id: string;
|
|
|
|
value: CustomColorType;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-05-28 16:15:17 +00:00
|
|
|
readonly customColors?: {
|
|
|
|
readonly colors: Record<string, CustomColorType>;
|
|
|
|
readonly version: number;
|
|
|
|
};
|
2019-05-16 22:32:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
|
|
|
type ItemPutAction = {
|
|
|
|
type: 'items/PUT';
|
2020-03-31 20:03:38 +00:00
|
|
|
payload: null;
|
2019-05-16 22:32:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type ItemPutExternalAction = {
|
|
|
|
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
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
type ItemRemoveAction = {
|
|
|
|
type: 'items/REMOVE';
|
2020-03-31 20:03:38 +00:00
|
|
|
payload: null;
|
2019-05-16 22:32:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type ItemRemoveExternalAction = {
|
|
|
|
type: 'items/REMOVE_EXTERNAL';
|
|
|
|
payload: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type ItemsResetAction = {
|
|
|
|
type: 'items/RESET';
|
|
|
|
};
|
|
|
|
|
|
|
|
export type ItemsActionType =
|
|
|
|
| ItemPutAction
|
|
|
|
| ItemPutExternalAction
|
|
|
|
| ItemRemoveAction
|
|
|
|
| ItemRemoveExternalAction
|
|
|
|
| ItemsResetAction;
|
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
|
|
|
|
export const actions = {
|
2021-05-28 16:15:17 +00:00
|
|
|
addCustomColor,
|
|
|
|
editCustomColor,
|
|
|
|
removeCustomColor,
|
2021-06-02 21:05:09 +00:00
|
|
|
resetDefaultChatColor,
|
|
|
|
setGlobalDefaultConversationColor,
|
2021-04-27 22:35:35 +00:00
|
|
|
onSetSkinTone,
|
2019-05-16 22:32:11 +00:00
|
|
|
putItem,
|
|
|
|
putItemExternal,
|
|
|
|
removeItem,
|
|
|
|
removeItemExternal,
|
|
|
|
resetItems,
|
|
|
|
};
|
|
|
|
|
2020-09-14 21:56:35 +00:00
|
|
|
export const useActions = (): typeof actions => useBoundActions(actions);
|
2020-05-05 19:49:34 +00:00
|
|
|
|
2020-09-14 21:56:35 +00:00
|
|
|
function putItem(key: string, value: unknown): ItemPutAction {
|
2020-03-31 20:03:38 +00:00
|
|
|
storageShim.put(key, value);
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
return {
|
|
|
|
type: 'items/PUT',
|
2020-03-31 20:03:38 +00:00
|
|
|
payload: null,
|
2019-05-16 22:32:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-27 22:35:35 +00:00
|
|
|
function onSetSkinTone(tone: number): ItemPutAction {
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeItem(key: string): ItemRemoveAction {
|
2020-03-31 20:03:38 +00:00
|
|
|
storageShim.remove(key);
|
|
|
|
|
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 {
|
|
|
|
colors: {},
|
|
|
|
version: 1,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function addCustomColor(
|
|
|
|
payload: CustomColorType
|
|
|
|
): 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,
|
|
|
|
[uuid]: payload,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
dispatch(putItem('customColors', nextCustomColors));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
// Reducer
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|