summaryrefslogtreecommitdiff
path: root/src/components/jet/today-card/background-color-utils.ts
blob: c2c0fe6c175468ae0f6099f44bcb3c1c513ca147 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { type Optional, isSome } from '@jet/environment/types/optional';
import type {
    Color,
    TodayCardMedia,
    TodayCardMediaWithArtwork,
} from '@jet-app/app-store/api/models';

import { isTodayCardMediaBrandedSingleApp } from './media/TodayCardMediaBrandedSingleApp.svelte';
import { isTodayCardMediaAppEvent } from './media/TodayCardMediaAppEvent.svelte';
import { isTodayCardMediaWithArtwork } from './media/TodayCardMediaWithArtwork.svelte';

const DEFAULT_COLOR: Color = {
    type: 'named',
    name: 'defaultBackground',
};

function getBackgroundFromMediaWithArtwork(
    media: TodayCardMediaWithArtwork,
): Optional<Color> {
    return (
        media.videos[0]?.preview.backgroundColor ??
        media.artworks[0]?.backgroundColor
    );
}

/**
 * Onyx App Store alternative to the `bestBackgroundColor` method that exists on
 * the {@linkcode TodayCardMedia} type
 *
 * This is necessary because the functions on those class instances are not
 * carried over to the client when serializing the view-model, making them
 * impossible to call in a consistent way from our codebase
 */
export function bestBackgroundColor(media: Optional<TodayCardMedia>): Color {
    if (isSome(media)) {
        if (isTodayCardMediaAppEvent(media)) {
            return media.tintColor;
        }

        if (isTodayCardMediaBrandedSingleApp(media)) {
            return (
                getBackgroundFromMediaWithArtwork(media) ??
                media.icon.backgroundColor ??
                DEFAULT_COLOR
            );
        }

        if (isTodayCardMediaWithArtwork(media)) {
            return getBackgroundFromMediaWithArtwork(media) ?? DEFAULT_COLOR;
        }
    }

    return DEFAULT_COLOR;
}