subtracks/src/state/artists.ts
austinried f19cbabac4 tabs on tabs??
moving storage/model/stuff around
2021-06-19 11:32:23 +09:00

42 lines
1.1 KiB
TypeScript

import { atom, DefaultValue, selector, useRecoilValue, useSetRecoilState } from 'recoil';
import { SubsonicApiClient } from '../subsonic/api';
import { activeServer } from './settings'
import { Artist } from '../models/music';
import { musicDb } from '../clients';
export const artistsState = atom<Artist[]>({
key: 'artistsState',
default: selector({
key: 'artistsState/default',
get: () => musicDb.getArtists(),
}),
effects_UNSTABLE: [
({ onSet }) => {
onSet((newValue) => {
if (!(newValue instanceof DefaultValue)) {
musicDb.updateArtists(newValue);
}
});
}
],
});
export const useUpdateArtists = () => {
const setArtists = useSetRecoilState(artistsState);
const server = useRecoilValue(activeServer);
return async () => {
if (!server) {
return;
}
const client = new SubsonicApiClient(server.address, server.username, server.token, server.salt);
const response = await client.getArtists();
setArtists(response.data.artists.map(i => ({
id: i.id,
name: i.name,
})));
};
};