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';
|
2020-05-05 19:49:34 +00:00
|
|
|
import { createSelector } from 'reselect';
|
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
import { StateType } 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 { isShortName } from '../../components/emoji/lib';
|
|
|
|
import { useBoundActions } from '../../util/hooks';
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
// State
|
|
|
|
|
|
|
|
export type ItemsStateType = {
|
2020-09-14 21:56:35 +00:00
|
|
|
readonly [key: string]: unknown;
|
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 = {
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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' };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reducer
|
|
|
|
|
|
|
|
function getEmptyState(): ItemsStateType {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-05-05 19:49:34 +00:00
|
|
|
|
|
|
|
// Selectors
|
|
|
|
|
|
|
|
const selectRecentEmojis = createSelector(
|
|
|
|
({ emojis }: StateType) => emojis.recents,
|
|
|
|
recents => recents.filter(isShortName)
|
|
|
|
);
|
|
|
|
|
2020-09-14 21:56:35 +00:00
|
|
|
export const useRecentEmojis = (): Array<string> =>
|
|
|
|
useSelector(selectRecentEmojis);
|