A decentralized music tracking and discovery platform built on AT Protocol 馃幍
at feat/feed-generator 115 lines 2.9 kB view raw
1import { useQuery } from "@tanstack/react-query"; 2import useSWR from "swr"; 3import { client } from "../api"; 4import { 5 getAlbumChart, 6 getArtistChart, 7 getProfileChart, 8 getSongChart, 9} from "../api/charts"; 10import { API_URL } from "../consts"; 11 12export const useScrobblesChartQuery = () => 13 useQuery({ 14 queryKey: ["scrobblesChart"], 15 queryFn: () => client.get("/xrpc/app.rocksky.charts.getScrobblesChart"), 16 select: ({ data }) => data.scrobbles || [], 17 }); 18 19export const useSongChartQuery = (uri: string) => 20 useQuery({ 21 queryKey: ["songChart", uri], 22 queryFn: () => getSongChart(uri), 23 select: (data) => data.scrobbles || [], 24 }); 25 26export const useArtistChartQuery = (uri: string) => 27 useQuery({ 28 queryKey: ["artistChart", uri], 29 queryFn: () => getArtistChart(uri), 30 select: (data) => data.scrobbles || [], 31 }); 32 33export const useAlbumChartQuery = (uri: string) => 34 useQuery({ 35 queryKey: ["albumChart", uri], 36 queryFn: () => getAlbumChart(uri), 37 select: (data) => data.scrobbles || [], 38 }); 39 40export const useProfileChartQuery = (did: string) => 41 useQuery({ 42 queryKey: ["profileChart", did], 43 queryFn: () => getProfileChart(did), 44 select: (data) => data.scrobbles || [], 45 }); 46 47function useChart() { 48 const fetcher = (path: string) => 49 fetch(`${API_URL}${path}`, { 50 method: "GET", 51 }).then((res) => res.json()); 52 53 const { data: scrobblesChart } = useSWR( 54 "/xrpc/app.rocksky.charts.getScrobblesChart", 55 fetcher, 56 ); 57 58 const getScrobblesChart = () => { 59 return scrobblesChart?.scrobbles || []; 60 }; 61 62 const getSongChart = async (uri: string) => { 63 const response = await client.get( 64 "/xrpc/app.rocksky.charts.getScrobblesChart", 65 { params: { songuri: uri } }, 66 ); 67 if (response.status !== 200) { 68 return []; 69 } 70 return response.data.scrobbles; 71 }; 72 73 const getArtistChart = async (uri: string) => { 74 const response = await client.get( 75 "/xrpc/app.rocksky.charts.getScrobblesChart", 76 { params: { artisturi: uri } }, 77 ); 78 if (response.status !== 200) { 79 return []; 80 } 81 return response.data.scrobbles; 82 }; 83 84 const getAlbumChart = async (uri: string) => { 85 const response = await client.get( 86 "/xrpc/app.rocksky.charts.getScrobblesChart", 87 { params: { albumuri: uri } }, 88 ); 89 if (response.status !== 200) { 90 return []; 91 } 92 return response.data.scrobbles; 93 }; 94 95 const getProfileChart = async (did: string) => { 96 const response = await client.get( 97 "/xrpc/app.rocksky.charts.getScrobblesChart", 98 { params: { did } }, 99 ); 100 if (response.status !== 200) { 101 return []; 102 } 103 return response.data.scrobbles; 104 }; 105 106 return { 107 getScrobblesChart, 108 getSongChart, 109 getArtistChart, 110 getAlbumChart, 111 getProfileChart, 112 }; 113} 114 115export default useChart;