signal-desktop/ts/state/ducks/emojis.ts

76 lines
1.4 KiB
TypeScript
Raw Normal View History

2019-05-24 23:58:27 +00:00
import { take, uniq } from 'lodash';
import { ThunkAction } from 'redux-thunk';
import { EmojiPickDataType } from '../../components/emoji/EmojiPicker';
import dataInterface from '../../sql/Client';
import { useBoundActions } from '../../util/hooks';
const { updateEmojiUsage } = dataInterface;
2019-05-24 23:58:27 +00:00
// State
export type EmojisStateType = {
readonly recents: Array<string>;
};
// Actions
type UseEmojiAction = {
2019-05-24 23:58:27 +00:00
type: 'emojis/USE_EMOJI';
payload: string;
2019-05-24 23:58:27 +00:00
};
type EmojisActionType = UseEmojiAction;
2019-05-24 23:58:27 +00:00
// Action Creators
export const actions = {
onUseEmoji,
useEmoji,
2019-05-24 23:58:27 +00:00
};
export const useActions = (): typeof actions => useBoundActions(actions);
function onUseEmoji({
shortName,
}: EmojiPickDataType): ThunkAction<void, unknown, unknown, UseEmojiAction> {
return async dispatch => {
try {
await updateEmojiUsage(shortName);
dispatch(useEmoji(shortName));
} catch (err) {
// Errors are ignored.
}
2019-05-24 23:58:27 +00:00
};
}
function useEmoji(payload: string): UseEmojiAction {
return {
type: 'emojis/USE_EMOJI',
payload,
};
2019-05-24 23:58:27 +00:00
}
// Reducer
function getEmptyState(): EmojisStateType {
return {
recents: [],
};
}
export function reducer(
state: EmojisStateType = getEmptyState(),
action: EmojisActionType
): EmojisStateType {
if (action.type === 'emojis/USE_EMOJI') {
2019-05-24 23:58:27 +00:00
const { payload } = action;
return {
...state,
recents: take(uniq([payload, ...state.recents]), 32),
};
}
return state;
}