Conversation Colors

This commit is contained in:
Josh Perez
2021-05-28 12:15:17 -04:00
committed by GitHub
parent b63d8e908c
commit 28f016ce48
128 changed files with 3997 additions and 1207 deletions

View File

@@ -2,13 +2,21 @@
// SPDX-License-Identifier: AGPL-3.0-only
import { omit } from 'lodash';
import { v4 as getGuid } from 'uuid';
import { ThunkAction } from 'redux-thunk';
import { StateType as RootStateType } from '../reducer';
import * as storageShim from '../../shims/storage';
import { useBoundActions } from '../../util/hooks';
import { CustomColorType } from '../../types/Colors';
// State
export type ItemsStateType = {
readonly [key: string]: unknown;
readonly customColors?: {
readonly colors: Record<string, CustomColorType>;
readonly version: number;
};
};
// Actions
@@ -50,6 +58,9 @@ export type ItemsActionType =
// Action Creators
export const actions = {
addCustomColor,
editCustomColor,
removeCustomColor,
onSetSkinTone,
putItem,
putItemExternal,
@@ -103,6 +114,74 @@ function resetItems(): ItemsResetAction {
return { type: 'items/RESET' };
}
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));
};
}
// Reducer
function getEmptyState(): ItemsStateType {