2019-02-18 18:24:56 +01:00
// @flow
2021-03-26 09:33:30 +01:00
import { SEARCH _OPTIONS , SEARCH _PAGE _SIZE } from 'constants/search' ;
2019-02-18 18:24:56 +01:00
import * as ICONS from 'constants/icons' ;
2021-02-16 17:59:16 +01:00
import React , { useMemo } from 'react' ;
2019-02-18 18:24:56 +01:00
import { Form , FormField } from 'component/common/form' ;
import Button from 'component/button' ;
2021-03-24 06:55:46 +01:00
import Icon from 'component/common/icon' ;
import classnames from 'classnames' ;
const CLAIM _TYPES = {
[ SEARCH _OPTIONS . INCLUDE _FILES ] : 'Files' ,
[ SEARCH _OPTIONS . INCLUDE _CHANNELS ] : 'Channels' ,
[ SEARCH _OPTIONS . INCLUDE _FILES _AND _CHANNELS ] : 'Everything' ,
} ;
const TYPES _ADVANCED = {
[ SEARCH _OPTIONS . MEDIA _VIDEO ] : 'Video' ,
[ SEARCH _OPTIONS . MEDIA _AUDIO ] : 'Audio' ,
[ SEARCH _OPTIONS . MEDIA _IMAGE ] : 'Image' ,
[ SEARCH _OPTIONS . MEDIA _TEXT ] : 'Text' ,
[ SEARCH _OPTIONS . MEDIA _APPLICATION ] : 'Other' ,
} ;
const TIME _FILTER = {
'' : 'None' ,
2021-03-30 15:43:50 +02:00
// [SEARCH_OPTIONS.TIME_FILTER_LAST_HOUR]: 'Last Hour', -- disable (doesn't work)
2021-03-24 06:55:46 +01:00
[ SEARCH _OPTIONS . TIME _FILTER _TODAY ] : 'Last 24 Hours' ,
[ SEARCH _OPTIONS . TIME _FILTER _THIS _WEEK ] : 'This Week' ,
[ SEARCH _OPTIONS . TIME _FILTER _THIS _MONTH ] : 'This Month' ,
[ SEARCH _OPTIONS . TIME _FILTER _THIS _YEAR ] : 'This Year' ,
} ;
const SORT _BY = {
'' : 'Relevance' ,
[ SEARCH _OPTIONS . SORT _DESCENDING ] : 'Newest first' ,
[ SEARCH _OPTIONS . SORT _ACCENDING ] : 'Oldest first' ,
} ;
2019-02-18 18:24:56 +01:00
type Props = {
setSearchOption : ( string , boolean | string | number ) => void ,
options : { } ,
2021-03-24 06:55:46 +01:00
simple : boolean ,
2019-02-21 23:45:17 +01:00
expanded : boolean ,
toggleSearchExpanded : ( ) => void ,
2021-04-07 12:52:15 +02:00
onSearchOptionsChanged : ( string ) => void ,
2019-02-18 18:24:56 +01:00
} ;
const SearchOptions = ( props : Props ) => {
2021-04-07 12:52:15 +02:00
const { options , simple , setSearchOption , expanded , toggleSearchExpanded , onSearchOptionsChanged } = props ;
2021-03-24 06:55:46 +01:00
2021-02-18 19:41:46 +01:00
const stringifiedOptions = JSON . stringify ( options ) ;
2019-02-18 18:24:56 +01:00
2021-02-16 17:59:16 +01:00
const isFilteringByChannel = useMemo ( ( ) => {
2021-02-18 19:41:46 +01:00
const jsonOptions = JSON . parse ( stringifiedOptions ) ;
const claimType = String ( jsonOptions [ SEARCH _OPTIONS . CLAIM _TYPE ] || '' ) ;
2021-02-16 17:59:16 +01:00
return claimType . includes ( SEARCH _OPTIONS . INCLUDE _CHANNELS ) ;
2021-02-18 19:41:46 +01:00
} , [ stringifiedOptions ] ) ;
2021-02-16 17:59:16 +01:00
2021-03-24 06:55:46 +01:00
if ( simple ) {
delete TYPES _ADVANCED [ SEARCH _OPTIONS . MEDIA _APPLICATION ] ;
2021-04-07 20:13:40 +02:00
delete TYPES _ADVANCED [ SEARCH _OPTIONS . MEDIA _IMAGE ] ;
delete TYPES _ADVANCED [ SEARCH _OPTIONS . MEDIA _AUDIO ] ;
2021-03-24 06:55:46 +01:00
}
2021-03-26 09:33:30 +01:00
React . useEffect ( ( ) => {
// We no longer let the user set the search results count, but the value
// will be in local storage for existing users. Override that.
if ( options [ SEARCH _OPTIONS . RESULT _COUNT ] !== SEARCH _PAGE _SIZE ) {
setSearchOption ( SEARCH _OPTIONS . RESULT _COUNT , SEARCH _PAGE _SIZE ) ;
}
} , [ ] ) ;
2021-04-07 12:52:15 +02:00
function updateSearchOptions ( option , value ) {
setSearchOption ( option , value ) ;
if ( onSearchOptionsChanged ) {
onSearchOptionsChanged ( option ) ;
}
}
2021-03-24 06:55:46 +01:00
function addRow ( label : string , value : any ) {
return (
< tr >
< td >
< legend className = "search__legend" > { label } < / legend >
< / td >
< td > { value } < / td >
< / tr >
) ;
}
const OBJ _TO _OPTION _ELEM = ( obj ) => {
return Object . entries ( obj ) . map ( ( x ) => {
return (
< option key = { x [ 0 ] } value = { x [ 0 ] } >
{ _ _ ( String ( x [ 1 ] ) ) }
< / option >
) ;
} ) ;
} ;
const typeElem = (
< >
< div className = "filter-values" >
< div >
{ Object . entries ( CLAIM _TYPES ) . map ( ( t ) => {
const option = t [ 0 ] ;
if ( option === SEARCH _OPTIONS . INCLUDE _FILES _AND _CHANNELS ) {
return null ;
}
return (
< Button
2019-09-26 18:07:11 +02:00
key = { option }
2021-03-24 06:55:46 +01:00
button = "alt"
label = { t [ 1 ] }
className = { classnames ( ` button-toggle ` , {
'button-toggle--active' : options [ SEARCH _OPTIONS . CLAIM _TYPE ] === option ,
} ) }
onClick = { ( ) => setSearchOption ( SEARCH _OPTIONS . CLAIM _TYPE , option ) }
2019-09-26 18:07:11 +02:00
/ >
2021-03-24 06:55:46 +01:00
) ;
} ) }
< / div >
< Button
button = "close"
className = { classnames ( 'close-button' , {
'close-button--visible' : options [ SEARCH _OPTIONS . CLAIM _TYPE ] !== SEARCH _OPTIONS . INCLUDE _FILES _AND _CHANNELS ,
} ) }
icon = { ICONS . REMOVE }
2021-04-07 12:52:15 +02:00
onClick = { ( ) => updateSearchOptions ( SEARCH _OPTIONS . CLAIM _TYPE , SEARCH _OPTIONS . INCLUDE _FILES _AND _CHANNELS ) }
2021-03-24 06:55:46 +01:00
/ >
< / div >
2021-04-07 07:40:34 +02:00
{ options [ SEARCH _OPTIONS . CLAIM _TYPE ] === SEARCH _OPTIONS . INCLUDE _FILES && (
< div className = "media-types" >
{ Object . entries ( TYPES _ADVANCED ) . map ( ( t ) => {
2021-03-24 06:55:46 +01:00
const option = t [ 0 ] ;
return (
2019-02-18 18:24:56 +01:00
< FormField
2019-09-26 18:07:11 +02:00
key = { option }
name = { option }
type = "checkbox"
2019-02-18 18:24:56 +01:00
blockWrap = { false }
2021-03-24 06:55:46 +01:00
disabled = { options [ SEARCH _OPTIONS . CLAIM _TYPE ] !== SEARCH _OPTIONS . INCLUDE _FILES }
label = { t [ 1 ] }
2021-02-16 17:59:16 +01:00
checked = { ! isFilteringByChannel && options [ option ] }
2021-04-07 12:52:15 +02:00
onChange = { ( ) => updateSearchOptions ( option , ! options [ option ] ) }
2019-09-26 18:07:11 +02:00
/ >
2021-03-24 06:55:46 +01:00
) ;
} ) }
2021-04-07 07:40:34 +02:00
< / div >
) }
2021-03-24 06:55:46 +01:00
< / >
) ;
const otherOptionsElem = (
< >
< div className = "filter-values" >
< FormField
type = "checkbox"
name = "exact-match"
checked = { options [ SEARCH _OPTIONS . EXACT ] }
2021-04-07 12:52:15 +02:00
onChange = { ( ) => updateSearchOptions ( SEARCH _OPTIONS . EXACT , ! options [ SEARCH _OPTIONS . EXACT ] ) }
2021-03-24 06:55:46 +01:00
label = { _ _ ( 'Exact match' ) }
/ >
< Icon
className = "icon--help"
icon = { ICONS . HELP }
tooltip
size = { 16 }
customTooltipText = { _ _ (
'Find results that include all the given words in the exact order.\nThis can also be done by surrounding the search query with quotation marks (e.g. "hello world").'
) }
/ >
< / div >
< / >
) ;
const uploadDateElem = (
< div className = "filter-values" >
< FormField
type = "select"
name = "upload-date"
value = { options [ SEARCH _OPTIONS . TIME _FILTER ] }
2021-04-07 12:52:15 +02:00
onChange = { ( e ) => updateSearchOptions ( SEARCH _OPTIONS . TIME _FILTER , e . target . value ) }
2021-03-24 06:55:46 +01:00
blockWrap = { false }
>
{ OBJ _TO _OPTION _ELEM ( TIME _FILTER ) }
< / FormField >
< Button
button = "close"
className = { classnames ( 'close-button' , {
'close-button--visible' : options [ SEARCH _OPTIONS . TIME _FILTER ] !== '' ,
} ) }
icon = { ICONS . REMOVE }
2021-04-07 12:52:15 +02:00
onClick = { ( ) => updateSearchOptions ( SEARCH _OPTIONS . TIME _FILTER , '' ) }
2021-03-24 06:55:46 +01:00
/ >
< / div >
) ;
const sortByElem = (
< div className = "filter-values" >
< FormField
type = "select"
name = "sort-by"
blockWrap = { false }
value = { options [ SEARCH _OPTIONS . SORT ] }
2021-04-07 12:52:15 +02:00
onChange = { ( e ) => updateSearchOptions ( SEARCH _OPTIONS . SORT , e . target . value ) }
2021-03-24 06:55:46 +01:00
>
{ OBJ _TO _OPTION _ELEM ( SORT _BY ) }
< / FormField >
< / div >
) ;
return (
< div >
< Button
button = "alt"
label = { _ _ ( 'Filter' ) }
icon = { ICONS . FILTER }
iconRight = { expanded ? ICONS . UP : ICONS . DOWN }
onClick = { toggleSearchExpanded }
/ >
< Form
className = { classnames ( 'search__options' , {
'search__options--expanded' : expanded ,
} ) }
>
< table className = "table table--condensed" >
< tbody >
{ addRow ( _ _ ( 'Type' ) , typeElem ) }
{ addRow ( _ _ ( 'Upload Date' ) , uploadDateElem ) }
{ addRow ( _ _ ( 'Sort By' ) , sortByElem ) }
{ addRow ( _ _ ( 'Other Options' ) , otherOptionsElem ) }
< / tbody >
< / table >
< / Form >
2019-02-18 18:24:56 +01:00
< / div >
) ;
} ;
export default SearchOptions ;