import React, { FC, useState } from 'react'; import { connect, MapStateToProps } from 'react-redux'; import { useDebounce } from 'react-use'; import { NavModel } from '@grafana/data'; import { ConfirmModal } from '@grafana/ui'; import Page from 'app/core/components/Page/Page'; import PageActionBar from 'app/core/components/PageActionBar/PageActionBar'; import { getNavModel } from 'app/core/selectors/navModel'; import { contextSrv } from 'app/core/services/context_srv'; import { StoreState } from 'app/types'; import EmptyListCTA from '../../core/components/EmptyListCTA/EmptyListCTA'; import { GrafanaRouteComponentProps } from '../../core/navigation/types'; import { EmptyQueryListBanner } from './EmptyQueryListBanner'; import { PlaylistPageList } from './PlaylistPageList'; import { StartModal } from './StartModal'; import { deletePlaylist, getAllPlaylist } from './api'; import { PlaylistDTO } from './types'; interface ConnectedProps { navModel: NavModel; } export interface PlaylistPageProps extends ConnectedProps, GrafanaRouteComponentProps {} export const PlaylistPage: FC = ({ navModel }) => { const [searchQuery, setSearchQuery] = useState(''); const [debouncedSearchQuery, setDebouncedSearchQuery] = useState(searchQuery); const [hasFetched, setHasFetched] = useState(false); const [startPlaylist, setStartPlaylist] = useState(); const [playlistToDelete, setPlaylistToDelete] = useState(); const [forcePlaylistsFetch, setForcePlaylistsFetch] = useState(0); const [playlists, setPlaylists] = useState([]); useDebounce( async () => { const playlists = await getAllPlaylist(searchQuery); if (!hasFetched) { setHasFetched(true); } setPlaylists(playlists); setDebouncedSearchQuery(searchQuery); }, 350, [forcePlaylistsFetch, searchQuery] ); const hasPlaylists = playlists && playlists.length > 0; const onDismissDelete = () => setPlaylistToDelete(undefined); const onDeletePlaylist = () => { if (!playlistToDelete) { return; } deletePlaylist(playlistToDelete.uid).finally(() => { setForcePlaylistsFetch(forcePlaylistsFetch + 1); setPlaylistToDelete(undefined); }); }; const emptyListBanner = ( ); const showSearch = playlists.length > 0 || searchQuery.length > 0 || debouncedSearchQuery.length > 0; return ( {showSearch && ( )} {!hasPlaylists && searchQuery ? ( ) : ( )} {!showSearch && emptyListBanner} {playlistToDelete && ( )} {startPlaylist && setStartPlaylist(undefined)} />} ); }; const mapStateToProps: MapStateToProps = (state: StoreState) => ({ navModel: getNavModel(state.navIndex, 'playlists'), }); export default connect(mapStateToProps)(PlaylistPage);