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

79 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-04-27 22:35:35 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { LinkPreviewType } from '../../types/message/LinkPreviews';
2021-08-18 13:34:22 +00:00
import { assignWithNoUnnecessaryAllocation } from '../../util/assignWithNoUnnecessaryAllocation';
2021-04-27 22:35:35 +00:00
// State
export type LinkPreviewsStateType = {
readonly linkPreview?: LinkPreviewType;
};
// Actions
const ADD_PREVIEW = 'linkPreviews/ADD_PREVIEW';
2021-08-18 13:34:22 +00:00
export const REMOVE_PREVIEW = 'linkPreviews/REMOVE_PREVIEW';
2021-04-27 22:35:35 +00:00
type AddLinkPreviewActionType = {
type: 'linkPreviews/ADD_PREVIEW';
payload: LinkPreviewType;
};
2021-08-18 13:34:22 +00:00
export type RemoveLinkPreviewActionType = {
2021-04-27 22:35:35 +00:00
type: 'linkPreviews/REMOVE_PREVIEW';
};
type LinkPreviewsActionType =
| AddLinkPreviewActionType
| RemoveLinkPreviewActionType;
// Action Creators
export const actions = {
addLinkPreview,
removeLinkPreview,
};
function addLinkPreview(payload: LinkPreviewType): AddLinkPreviewActionType {
return {
type: ADD_PREVIEW,
payload,
};
}
function removeLinkPreview(): RemoveLinkPreviewActionType {
return {
type: REMOVE_PREVIEW,
};
}
// Reducer
export function getEmptyState(): LinkPreviewsStateType {
return {
linkPreview: undefined,
};
}
export function reducer(
state: Readonly<LinkPreviewsStateType> = getEmptyState(),
action: Readonly<LinkPreviewsActionType>
): LinkPreviewsStateType {
if (action.type === ADD_PREVIEW) {
const { payload } = action;
return {
linkPreview: payload,
};
}
if (action.type === REMOVE_PREVIEW) {
2021-08-18 13:34:22 +00:00
return assignWithNoUnnecessaryAllocation(state, {
2021-04-27 22:35:35 +00:00
linkPreview: undefined,
2021-08-18 13:34:22 +00:00
});
2021-04-27 22:35:35 +00:00
}
return state;
}