import React, { useEffect, useState } from 'react' import { __, sprintf } from '@wordpress/i18n' import ChannelInterface from 'Interfaces/ChannelInterface' export interface ChannelDropdownComponentProps { channels: ChannelInterface[] isOpen: boolean appliedIds: string[] onToggle: () => void onApply: (ids: string[]) => void } const SearchGlyph = () => ( ) const ChannelDropdownComponent = ({ channels, isOpen, appliedIds, onToggle, onApply }: ChannelDropdownComponentProps) => { const [filterText, setFilterText] = useState('') const [pending, setPending] = useState(appliedIds) const prevOpen = React.useRef(false) useEffect(() => { if (isOpen && !prevOpen.current) { setPending([...appliedIds]) setFilterText('') } prevOpen.current = isOpen }, [isOpen, appliedIds]) const filteredChannels = channels.filter((ch) => ch.screenname.toLowerCase().includes(filterText.toLowerCase())) const toggleChannel = (value: string) => { if (value === 'all') { setPending(pending.length === channels.length ? [] : channels.map((c) => c.id)) return } setPending( pending.includes(value) ? pending.filter((id) => id !== value) : [...pending, value] ) } const selectedCount = pending.length const countLabel = selectedCount === 0 ? __('No channels selected', 'textdomain') : sprintf( /* translators: %d: number of channels */ __('%d Channel Selected', 'textdomain'), selectedCount ) if (channels.length === 0) { return null } return ( <> {isOpen && (
{__('Show Channel in Results', 'textdomain')}

{__('Choose the Channels you would like to see in your search results.', 'textdomain')}

{countLabel}

setFilterText(e.target.value)} aria-label={__('Find channel', 'textdomain')} />
{filteredChannels.map((channel) => ( ))}
)} ) } export default ChannelDropdownComponent