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 = {
|
|
|
|
readonly [key: string]: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
value: any;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
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-05-05 19:49:34 +00:00
|
|
|
export const useActions = () => useBoundActions(actions);
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
function putItem(key: string, value: any): 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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function putItemExternal(key: string, value: any): ItemPutExternalAction {
|
|
|
|
return {
|
|
|
|
type: 'items/PUT_EXTERNAL',
|
|
|
|
payload: {
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeItem(key: string): ItemRemoveAction {
|
2020-07-24 23:32:08 +00:00
|
|
|
// tslint:disable-next-line no-floating-promises
|
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(
|
|
|
|
state: ItemsStateType = getEmptyState(),
|
|
|
|
action: ItemsActionType
|
|
|
|
): 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)
|
|
|
|
);
|
|
|
|
|
|
|
|
export const useRecentEmojis = () => useSelector(selectRecentEmojis);
|