import { Box, LinearProgress, Typography } from '@material-ui/core'; import React, { useCallback, useEffect, useReducer, useState } from 'react'; import { $enum } from 'ts-enum-util'; import { ItemType, QueryElemProperty, QueryResponseType } from '../../../api'; import { queryItems } from '../../../lib/backend/queries'; import { QueryElem, QueryLeafBy, QueryLeafOp } from '../../../lib/query/Query'; import StoreLinkIcon, { ExternalStore, StoreURLIdentifiers } from '../../common/StoreLinkIcon'; var _ = require('lodash'); export default function LinksStatusWidget(props: { }) { type Counts = { songs: number | undefined, albums: number | undefined, artists: number | undefined, }; let [totalCounts, setTotalCounts] = useState(undefined); let [linkedCounts, setLinkedCounts] = useState>({}); let queryStoreCount = async (store: ExternalStore, type: ItemType) => { let whichProp: any = { [ItemType.Song]: QueryLeafBy.SongStoreLinks, [ItemType.Artist]: QueryLeafBy.ArtistStoreLinks, [ItemType.Album]: QueryLeafBy.AlbumStoreLinks, } let whichElem: any = { [ItemType.Song]: 'songs', [ItemType.Artist]: 'artists', [ItemType.Album]: 'albums', } let r: any = await queryItems( [type], { a: whichProp[type], leafOp: QueryLeafOp.Like, b: `%${StoreURLIdentifiers[store]}%`, }, undefined, undefined, QueryResponseType.Count ); return r[whichElem[type]]; } // Start retrieving total counts useEffect(() => { (async () => { let counts: any = await queryItems( [ItemType.Song, ItemType.Artist, ItemType.Album], undefined, undefined, undefined, QueryResponseType.Count ); setTotalCounts(counts); } )(); }, []); // Start retrieving counts per store useEffect(() => { (async () => { let promises = $enum(ExternalStore).getValues().map((s: ExternalStore) => { let songsPromise: Promise = queryStoreCount(s, ItemType.Song); let albumsPromise: Promise = queryStoreCount(s, ItemType.Album); let artistsPromise: Promise = queryStoreCount(s, ItemType.Artist); let updatePromise = Promise.all([songsPromise, albumsPromise, artistsPromise]).then( (r: any[]) => { setLinkedCounts((prev: Record) => { return { ...prev, [s]: { songs: r[0], artists: r[2], albums: r[1], } } }); } ) console.log(s); return updatePromise; }) return Promise.all(promises); } )(); }, [setLinkedCounts]); let storeReady = (s: ExternalStore) => { return s in linkedCounts; } return {$enum(ExternalStore).getValues().map((s: ExternalStore) => { if (!totalCounts) { return <>; } if (!storeReady(s)) { return <>; } let tot = totalCounts; let lin = linkedCounts[s]; let perc = { songs: Math.round((lin.songs || 0) / (tot.songs || 1) * 100), artists: Math.round((lin.artists || 0) / (tot.artists || 1) * 100), albums: Math.round((lin.albums || 0) / (tot.albums || 1) * 100), } return <> {totalCounts && storeReady(s) && <> } ; })}
{s} Linked artists: {lin.artists} / {tot.artists}
Linked albums: {lin.albums} / {tot.albums}
Linked songs: {lin.songs} / {tot.songs}
 
}