import React, { useCallback, useContext, useState } from 'react'; import { __ } from '@wordpress/i18n'; import { ReactComponent as HourglassIcon } from '../assets/images/icons/icon-hourglass.svg'; import { ReactComponent as CloseIcon } from '../assets/images/icons/icon-close.svg'; import { Button, InputField, Tooltip } from '../elements/JSXElements'; import { AppContext } from '../app/context'; import { InfoTooltipIcon } from '../elements/InfoTooltipIcon'; import { checkAddedUrl } from '../app/api'; import { UrlStatus, UrlsListItem } from '../app/types'; import '../assets/styles/components/_UrlsList.scss'; type UpdateUrlsAction = 'add' | 'remove'; const UrlsList: React.FC<{ urls: UrlsListItem[] }> = ( { urls } ) => { const { state, dispatch } = useContext( AppContext ); const updateSelectedUrls = useCallback( ( action: UpdateUrlsAction, url: string ) => { dispatch( { type: 'selected_urls', payload: action === 'add' ? [ ...state.selected_urls, url ] : state.selected_urls.filter( ( item ) => item !== url ), } ); }, [ dispatch, state.selected_urls ] ); return (
{ urls.length > 0 &&
{ urls.map( ( item ) => { const checked = state.selected_urls.includes( item.url ); return (
{ item.status === 'active' ? :
{ item.url }
}
{ item.status === 'active' &&
} { item.status === 'pending' &&
{ __( 'URL is waiting to be loaded', 'urlslab' ) }
} { item.status === 'error' &&
{ __( 'URL cannot be fetched', 'urlslab' ) }
}
); } ) }
}
); }; const AddNewUrl: React.FC = React.memo( () => { const [ newUrl, setNewUrl ] = useState( '' ); const { dispatch } = useContext( AppContext ); // const addUrl = async ( url: string ) => { // if ( url !== '' ) { // dispatch( { type: 'url_filter', payload: { url, status: 'pending' } } ); // setNewUrl( '' ); // const currentStatus: UrlStatus | null = await checkAddedUrl( url, true ); // if ( currentStatus !== null ) { // dispatch( { type: 'url_filter', payload: { url, status: currentStatus } } ); // } // } // }; return
{ __( 'Add new url', 'urlslab' ) }
setNewUrl( value as string ) } />
; } ); AddNewUrl.displayName = 'AddNewUrl'; type UrlCheckboxType = { item: UrlsListItem, checked: boolean, updateSelectedUrls: ( action: UpdateUrlsAction, url: string ) => void } const UrlCheckbox:React.FC = React.memo( ( { item, checked, updateSelectedUrls }:UrlCheckboxType ) => { return ( ) ; } ); UrlCheckbox.displayName = 'UrlCheckbox'; export default React.memo( UrlsList );