parent
c28db21b18
commit
e257c135be
17 changed files with 465 additions and 230 deletions
@ -0,0 +1,111 @@ |
||||
import { Box, 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 { 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<Counts | undefined>(undefined); |
||||
let [linkedCounts, setLinkedCounts] = useState<Record<string, Counts>>({}); |
||||
|
||||
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<number> = queryStoreCount(s, ItemType.Song); |
||||
let albumsPromise: Promise<number> = queryStoreCount(s, ItemType.Album); |
||||
let artistsPromise: Promise<number> = queryStoreCount(s, ItemType.Artist); |
||||
let updatePromise = Promise.all([songsPromise, albumsPromise, artistsPromise]).then( |
||||
(r: any[]) => { |
||||
setLinkedCounts((prev: Record<string, Counts>) => { |
||||
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) => { |
||||
return <Box> |
||||
{totalCounts && storeReady(s) && <Box> |
||||
<Typography> |
||||
{s}:<br /> |
||||
{linkedCounts[s].songs} / {totalCounts.songs} songs linked<br /> |
||||
{linkedCounts[s].artists} / {totalCounts.artists} artists linked<br /> |
||||
{linkedCounts[s].albums} / {totalCounts.albums} albums linked<br /> |
||||
<br /> |
||||
</Typography> |
||||
</Box>} |
||||
</Box> |
||||
})} |
||||
</> |
||||
} |
Loading…
Reference in new issue