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