Skip to content

Modular logistics and supply chain operating system (LSOS)

4.4 KB
Raw Blame
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { computed } from '@ember/object';
import { isArray } from '@ember/array';
import { isBlank } from '@ember/utils';
import { task } from 'ember-concurrency';
import { storageFor } from 'ember-local-storage';
import { add, isPast } from 'date-fns';
import fetch from 'fetch';
import { faGithub } from '@fortawesome/free-brands-svg-icons';

const REPOSITORY_URL = 'https://github.com/fleetbase/fleetbase';
const compactNumber = new Intl.NumberFormat('en', { notation: 'compact', maximumFractionDigits: 1 });

export default class GithubCardComponent extends Component {
    @storageFor('local-cache') localCache;
    @tracked data = {
        owner: {
            avatar_url: 'https://avatars.githubusercontent.com/u/38091894?v=4',
        },
    };
    @tracked tags = [];
    githubIcon = faGithub;
    repositoryUrl = REPOSITORY_URL;

    /**
     * The repository counters shown in the card's footer, compacted (e.g. 3.9K).
     */
    get stats() {
        const stat = (label, icon, count, path) => ({
            label,
            icon,
            url: `${REPOSITORY_URL}${path}`,
            value: typeof count === 'number' ? compactNumber.format(count) : '-',
            title: typeof count === 'number' ? `${count.toLocaleString()} ${label.toLowerCase()}` : label,
        });

        return [
            stat('Stars', 'star', this.data.stargazers_count, '/stargazers'),
            stat('Watchers', 'eye', this.data.subscribers_count, '/watchers'),
            stat('Forks', 'code-fork', this.data.forks_count, '/forks'),
            stat('Issues', 'circle-dot', this.data.open_issues_count, '/issues'),
        ];
    }

    @computed('tags.length') get latestRelease() {
        if (isArray(this.tags) && this.tags.length) {
            return this.tags[0];
        }

        return { name: 'v0.0.1' };
    }

    @computed('data.releases_url', 'latestRelease.name') get releaseUrl() {
        let url = `${REPOSITORY_URL}/releases`;

        if (!isBlank(this.latestRelease?.name)) {
            url += '/tag/' + this.latestRelease.name;
        }

        return url;
    }

    constructor() {
        super(...arguments);
        this.getRepositoryData.perform();
        this.getRepositoryTags.perform();
    }

    @task *getRepositoryData() {
        // Check if cached data and expiration are available
        const cachedData = this.localCache.get('fleetbase-github-data');
        const expiration = this.localCache.get('fleetbase-github-data-expiration');

        // Check if the cached data is still valid
        if (cachedData && expiration && !isPast(new Date(expiration))) {
            // Use cached data
            this.data = cachedData;
        } else {
            // Fetch new data
            const response = yield fetch('https://api.github.com/repos/fleetbase/fleetbase', { cache: 'default' });

            // The component can be torn down mid-request; writing tracked state afterwards
            // throws "Cannot create a new tag ... after it has been destroyed".
            if (this.isDestroying || this.isDestroyed) {
                return;
            }

            if (response.ok) {
                this.data = yield response.json();
                this.localCache.set('fleetbase-github-data', this.data);
                this.localCache.set('fleetbase-github-data-expiration', add(new Date(), { hours: 6 }));
            }
        }
    }

    @task *getRepositoryTags() {
        // Check if cached tags and expiration are available
        const cachedTags = this.localCache.get('fleetbase-github-tags');
        const expiration = this.localCache.get('fleetbase-github-tags-expiration');

        // Check if the cached tags are still valid
        if (cachedTags && expiration && !isPast(new Date(expiration))) {
            // Use cached tags
            this.tags = cachedTags;
        } else {
            // Fetch new tags
            const response = yield fetch('https://api.github.com/repos/fleetbase/fleetbase/tags', { cache: 'default' });

            if (this.isDestroying || this.isDestroyed) {
                return;
            }

            if (response.ok) {
                this.tags = yield response.json();
                this.localCache.set('fleetbase-github-tags', this.tags);
                this.localCache.set('fleetbase-github-tags-expiration', add(new Date(), { hours: 6 }));
            }
        }
    }
}