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

78 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-11-02 23:01:13 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { createSelector } from 'reselect';
import { mapValues } from 'lodash';
import * as log from '../../logging/log';
import type { StateType } from '../reducer';
import type { BadgesStateType } from '../ducks/badges';
import type { BadgeType } from '../../badges/types';
import { getOwn } from '../../util/getOwn';
const getBadgeState = (state: Readonly<StateType>): BadgesStateType =>
state.badges;
export const getBadgesById = createSelector(getBadgeState, state =>
mapValues(state.byId, badge => ({
...badge,
images: badge.images.map(image =>
mapValues(image, imageFile =>
imageFile.localPath
? {
...imageFile,
localPath: window.Signal.Migrations.getAbsoluteBadgeImageFilePath(
imageFile.localPath
),
}
: imageFile
)
),
}))
);
export const getBadgesSelector = createSelector(
getBadgesById,
2021-11-11 22:43:05 +00:00
badgesById =>
(
conversationBadges: ReadonlyArray<Pick<BadgeType, 'id'>>
): Array<BadgeType> => {
const result: Array<BadgeType> = [];
2021-11-02 23:01:13 +00:00
2021-11-11 22:43:05 +00:00
for (const { id } of conversationBadges) {
const badge = getOwn(badgesById, id);
if (!badge) {
log.error('getBadgesSelector: conversation badge was not found');
continue;
}
result.push(badge);
2021-11-02 23:01:13 +00:00
}
2021-11-11 22:43:05 +00:00
return result;
}
2021-11-02 23:01:13 +00:00
);
export type PreferredBadgeSelectorType = (
conversationBadges: ReadonlyArray<Pick<BadgeType, 'id'>>
) => undefined | BadgeType;
2021-11-02 23:01:13 +00:00
export const getPreferredBadgeSelector = createSelector(
getBadgesById,
(badgesById): PreferredBadgeSelectorType =>
conversationBadges => {
2021-11-11 22:43:05 +00:00
const firstId: undefined | string = conversationBadges[0]?.id;
if (!firstId) {
return undefined;
}
2021-11-02 23:01:13 +00:00
2021-11-11 22:43:05 +00:00
const badge = getOwn(badgesById, firstId);
if (!badge) {
log.error(
'getPreferredBadgeSelector: conversation badge was not found'
);
return undefined;
}
2021-11-02 23:01:13 +00:00
2021-11-11 22:43:05 +00:00
return badge;
}
2021-11-02 23:01:13 +00:00
);