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

38 lines
894 B
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 { createSelector } from 'reselect';
import { assertDev } from '../../util/assert';
import { getDomain } from '../../types/LinkPreview';
2022-06-17 00:48:57 +00:00
import type { LinkPreviewSourceType } from '../../types/LinkPreview';
import type { StateType } from '../reducer';
2021-04-27 22:35:35 +00:00
export const getLinkPreview = createSelector(
2022-06-17 00:48:57 +00:00
({ linkPreviews }: StateType) => linkPreviews,
({ linkPreview, source }) => {
return (fromSource: LinkPreviewSourceType) => {
if (!linkPreview) {
return;
}
if (source !== fromSource) {
return;
}
const domain = getDomain(linkPreview.url);
assertDev(
domain !== undefined,
"Domain of linkPreview can't be undefined"
);
2021-04-27 22:35:35 +00:00
return {
...linkPreview,
domain,
2021-04-27 22:35:35 +00:00
isLoaded: true,
};
2022-06-17 00:48:57 +00:00
};
2021-04-27 22:35:35 +00:00
}
);