2021-04-27 22:35:35 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-06-17 00:48:57 +00:00
|
|
|
import type { ThunkAction } from 'redux-thunk';
|
|
|
|
|
|
|
|
import type { NoopActionType } from './noop';
|
|
|
|
import type { StateType as RootStateType } from '../reducer';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LinkPreviewType } from '../../types/message/LinkPreviews';
|
2022-06-17 00:48:57 +00:00
|
|
|
import type { LinkPreviewSourceType } from '../../types/LinkPreview';
|
2021-08-18 13:34:22 +00:00
|
|
|
import { assignWithNoUnnecessaryAllocation } from '../../util/assignWithNoUnnecessaryAllocation';
|
2022-06-17 00:48:57 +00:00
|
|
|
import { maybeGrabLinkPreview } from '../../services/LinkPreview';
|
|
|
|
import { useBoundActions } from '../../hooks/useBoundActions';
|
2021-04-27 22:35:35 +00:00
|
|
|
|
|
|
|
// State
|
|
|
|
|
|
|
|
export type LinkPreviewsStateType = {
|
|
|
|
readonly linkPreview?: LinkPreviewType;
|
2022-06-17 00:48:57 +00:00
|
|
|
readonly source?: LinkPreviewSourceType;
|
2021-04-27 22:35:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
2022-06-17 00:48:57 +00:00
|
|
|
export 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
|
|
|
|
2022-06-17 00:48:57 +00:00
|
|
|
export type AddLinkPreviewActionType = {
|
2021-04-27 22:35:35 +00:00
|
|
|
type: 'linkPreviews/ADD_PREVIEW';
|
2022-06-17 00:48:57 +00:00
|
|
|
payload: {
|
|
|
|
linkPreview: LinkPreviewType;
|
|
|
|
source: LinkPreviewSourceType;
|
|
|
|
};
|
2021-04-27 22:35:35 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
2022-06-17 00:48:57 +00:00
|
|
|
function debouncedMaybeGrabLinkPreview(
|
|
|
|
message: string,
|
|
|
|
source: LinkPreviewSourceType
|
|
|
|
): ThunkAction<void, RootStateType, unknown, NoopActionType> {
|
|
|
|
return dispatch => {
|
|
|
|
maybeGrabLinkPreview(message, source);
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'NOOP',
|
|
|
|
payload: null,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2021-04-27 22:35:35 +00:00
|
|
|
|
2022-06-17 00:48:57 +00:00
|
|
|
function addLinkPreview(
|
|
|
|
linkPreview: LinkPreviewType,
|
|
|
|
source: LinkPreviewSourceType
|
|
|
|
): AddLinkPreviewActionType {
|
2021-04-27 22:35:35 +00:00
|
|
|
return {
|
|
|
|
type: ADD_PREVIEW,
|
2022-06-17 00:48:57 +00:00
|
|
|
payload: {
|
|
|
|
linkPreview,
|
|
|
|
source,
|
|
|
|
},
|
2021-04-27 22:35:35 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeLinkPreview(): RemoveLinkPreviewActionType {
|
|
|
|
return {
|
|
|
|
type: REMOVE_PREVIEW,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-06-17 00:48:57 +00:00
|
|
|
export const actions = {
|
|
|
|
addLinkPreview,
|
|
|
|
debouncedMaybeGrabLinkPreview,
|
|
|
|
removeLinkPreview,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useLinkPreviewActions = (): typeof actions =>
|
|
|
|
useBoundActions(actions);
|
|
|
|
|
2021-04-27 22:35:35 +00:00
|
|
|
// 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 {
|
2022-06-17 00:48:57 +00:00
|
|
|
linkPreview: payload.linkPreview,
|
|
|
|
source: payload.source,
|
2021-04-27 22:35:35 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2022-06-17 00:48:57 +00:00
|
|
|
source: undefined,
|
2021-08-18 13:34:22 +00:00
|
|
|
});
|
2021-04-27 22:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|