48 lines
1.6 KiB
TypeScript
48 lines
1.6 KiB
TypeScript
import {
|
|
createCoverPaletteSource,
|
|
type CoverPaletteResult,
|
|
} from "@/hooks/cover-palette-source"
|
|
import {
|
|
prefetchLegacyCoverPalette,
|
|
useLegacyCoverPalette,
|
|
} from "@/hooks/use-legacy-cover-palette"
|
|
import {
|
|
loadArtworkColorsEnabled,
|
|
useArtworkColorsEnabled,
|
|
} from "@/lib/artwork-color-preference"
|
|
import { extractCoverSwatches } from "@/lib/artwork-colors"
|
|
import { deriveCoverPalette, type CoverSwatches } from "@/lib/cover-palette"
|
|
|
|
export { COVER_PALETTE_WAIT_MS } from "@/hooks/cover-palette-source"
|
|
|
|
const region = createCoverPaletteSource<CoverSwatches>({
|
|
cacheKey: (colorSourceUri) => `palette:v2:${colorSourceUri}`,
|
|
extract: extractCoverSwatches,
|
|
derive: (swatches) => deriveCoverPalette(swatches),
|
|
})
|
|
|
|
/** Warm visible artwork colors before a detail page is pushed. */
|
|
export function prefetchCoverPalette(uri: string) {
|
|
void loadArtworkColorsEnabled().then((enabled) =>
|
|
enabled ? region.prefetch(uri) : prefetchLegacyCoverPalette(uri)
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Colors for a screen tinted by `uri`'s cover art, from whichever pipeline the
|
|
* device preference selects. Nothing is extracted until the preference loads,
|
|
* and the screen stays unsettled meanwhile so it doesn't flash theme colors.
|
|
*/
|
|
export function useCoverPalette(
|
|
uri: string,
|
|
hasCover: boolean | undefined
|
|
): CoverPaletteResult {
|
|
const enabled = useArtworkColorsEnabled()
|
|
const current = region.usePalette(enabled === true ? uri : "", hasCover)
|
|
const legacy = useLegacyCoverPalette(enabled === false ? uri : "", hasCover)
|
|
if (enabled === undefined) {
|
|
return { palette: current.palette, settled: !uri || hasCover === false }
|
|
}
|
|
return enabled ? current : legacy
|
|
}
|