usePlaylist.tsx 654 B

123456789101112131415161718192021222324
  1. import { useEffect, useState } from 'react';
  2. import { getPlaylist } from './api';
  3. import { Playlist } from './types';
  4. export function usePlaylist(playlistUid?: string) {
  5. const [playlist, setPlaylist] = useState<Playlist>({ items: [], interval: '5m', name: '', uid: '' });
  6. const [loading, setLoading] = useState<boolean>(true);
  7. useEffect(() => {
  8. const initPlaylist = async () => {
  9. if (!playlistUid) {
  10. setLoading(false);
  11. return;
  12. }
  13. const list = await getPlaylist(playlistUid);
  14. setPlaylist(list);
  15. setLoading(false);
  16. };
  17. initPlaylist();
  18. }, [playlistUid]);
  19. return { playlist, loading };
  20. }