mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-03 21:29:59 -05:00
Global search uses Semantic SearchResultsInput component, other components use SearchInput.
This commit is contained in:
parent
2bd39fd0c7
commit
1b92d1e580
12 changed files with 205 additions and 93 deletions
|
|
@ -401,28 +401,16 @@ export function scrollToTopOfElement(element, smooth = true) {
|
|||
});
|
||||
}
|
||||
|
||||
export function SearchInput({
|
||||
searchStr,
|
||||
onSubmit,
|
||||
size = 'small',
|
||||
placeholder,
|
||||
action,
|
||||
actionIcon,
|
||||
clearable,
|
||||
autoFocus = false,
|
||||
onClear = null,
|
||||
onChange = null,
|
||||
results = null,
|
||||
handleResultSelect = null,
|
||||
...props
|
||||
}) {
|
||||
const useClearableInput = (searchStr, onChange, onClear, onSubmit, size = 'small', placeholder = 'Search...', icon = 'search', withAction = true) => {
|
||||
const [value, setValue] = useState(searchStr || '');
|
||||
const [submitted, setSubmitted] = useState(false);
|
||||
|
||||
const handleChange = (e) => {
|
||||
if (e) {
|
||||
e.preventDefault();
|
||||
}
|
||||
setValue(e.target.value);
|
||||
setSubmitted(false);
|
||||
if (onChange) {
|
||||
// Try to call the remote function, don't let its failure break this.
|
||||
try {
|
||||
|
|
@ -437,6 +425,7 @@ export function SearchInput({
|
|||
e.preventDefault();
|
||||
// Clear the input when the "clear" button is clicked, search again.
|
||||
setValue('');
|
||||
setSubmitted(false);
|
||||
if (onSubmit) {
|
||||
onSubmit('');
|
||||
}
|
||||
|
|
@ -448,6 +437,7 @@ export function SearchInput({
|
|||
const localOnSubmit = (e) => {
|
||||
// Send the value up when submitting.
|
||||
e.preventDefault();
|
||||
setSubmitted(true);
|
||||
if (onSubmit) {
|
||||
onSubmit(value);
|
||||
} else {
|
||||
|
|
@ -455,35 +445,100 @@ export function SearchInput({
|
|||
}
|
||||
}
|
||||
|
||||
const clearButton = <Button
|
||||
icon='close'
|
||||
size={size}
|
||||
onClick={handleClearSearch}
|
||||
type='button'
|
||||
disabled={!!!value}
|
||||
className='search-clear'
|
||||
/>;
|
||||
|
||||
let action;
|
||||
if (withAction) {
|
||||
// Can only clear after submitting.
|
||||
action = submitted ? clearButton : <Button type='button' icon={icon} size='big'/>;
|
||||
}
|
||||
|
||||
const input = <Input fluid
|
||||
placeholder={placeholder}
|
||||
type='text'
|
||||
onChange={handleChange}
|
||||
value={value}
|
||||
size={size}
|
||||
className='search-input'
|
||||
action={action}
|
||||
/>;
|
||||
|
||||
return {value, submitted, clearButton, input, localOnSubmit, handleChange}
|
||||
}
|
||||
|
||||
export function SearchInput({
|
||||
searchStr,
|
||||
onSubmit,
|
||||
onChange,
|
||||
onClear,
|
||||
size = 'small',
|
||||
placeholder = 'Search...',
|
||||
icon = 'search',
|
||||
}) {
|
||||
let {input, localOnSubmit} = useClearableInput(searchStr, onChange, onClear, onSubmit, size, placeholder, icon);
|
||||
|
||||
return <Form onSubmit={localOnSubmit} className='search-container'>
|
||||
{input}
|
||||
</Form>
|
||||
}
|
||||
|
||||
export function SearchResultsInput({
|
||||
searchStr,
|
||||
onSubmit,
|
||||
onClear = null,
|
||||
onChange = null,
|
||||
size = 'small',
|
||||
placeholder = 'Search...',
|
||||
icon = 'search',
|
||||
action,
|
||||
actionIcon,
|
||||
clearable = false,
|
||||
autoFocus = false,
|
||||
results = undefined,
|
||||
handleResultSelect = null,
|
||||
resultRenderer = undefined,
|
||||
...props
|
||||
}) {
|
||||
let {
|
||||
value,
|
||||
clearButton,
|
||||
localOnSubmit,
|
||||
handleChange,
|
||||
} = useClearableInput(searchStr, onChange, onClear, onSubmit, size, placeholder, undefined, false);
|
||||
|
||||
const localHandleResultSelect = (e, data) => {
|
||||
if (e) {
|
||||
e.preventDefault();
|
||||
}
|
||||
console.debug(`Selected result`, data);
|
||||
if (handleResultSelect) {
|
||||
console.debug(`Selected result`, data);
|
||||
handleResultSelect(data);
|
||||
} else {
|
||||
console.error('No handleResultSelect defined!');
|
||||
}
|
||||
}
|
||||
|
||||
if ((action || actionIcon) && searchStr && searchStr === value && clearable) {
|
||||
action = <Button icon='close' size={size} onClick={handleClearSearch} type='button'/>;
|
||||
} else if (actionIcon) {
|
||||
action = <Button icon={actionIcon} size={size}/>;
|
||||
} else if (action) {
|
||||
action = <Button size={size}>{action}</Button>;
|
||||
}
|
||||
|
||||
return <Form onSubmit={localOnSubmit} {...props}>
|
||||
<Search fluid category
|
||||
return <Form onSubmit={localOnSubmit} {...props} className='search-container'>
|
||||
<Search category
|
||||
input={{fluid: true}}
|
||||
placeholder={placeholder}
|
||||
type='text'
|
||||
onSearchChange={handleChange}
|
||||
onResultSelect={localHandleResultSelect}
|
||||
value={value}
|
||||
size={size}
|
||||
action={action}
|
||||
results={results}
|
||||
resultRenderer={resultRenderer}
|
||||
className='search-input'
|
||||
/>
|
||||
{clearable === true && <div style={{marginLeft: '1em'}}>{clearButton}</div>}
|
||||
</Form>
|
||||
}
|
||||
|
||||
|
|
@ -1526,3 +1581,32 @@ export function ErrorMessage({children, size = null}) {
|
|||
<Message.Content>{children}</Message.Content>
|
||||
</Message>
|
||||
}
|
||||
|
||||
function levenshteinDistance(a, b) {
|
||||
const matrix = [];
|
||||
|
||||
// Initialize the matrix
|
||||
for (let i = 0; i <= b.length; i++) {
|
||||
matrix[i] = [i];
|
||||
}
|
||||
for (let j = 0; j <= a.length; j++) {
|
||||
matrix[0][j] = j;
|
||||
}
|
||||
|
||||
// Populate the matrix
|
||||
for (let i = 1; i <= b.length; i++) {
|
||||
for (let j = 1; j <= a.length; j++) {
|
||||
if (b.charAt(i - 1) === a.charAt(j - 1)) {
|
||||
matrix[i][j] = matrix[i - 1][j - 1];
|
||||
} else {
|
||||
matrix[i][j] = Math.min(matrix[i - 1][j - 1], matrix[i][j - 1], matrix[i - 1][j]) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return matrix[b.length][a.length];
|
||||
}
|
||||
|
||||
export function fuzzyMatch(a, b, threshold = 3) {
|
||||
return levenshteinDistance(a, b) <= threshold;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,8 +4,10 @@ import {FilesSearchView} from "./Files";
|
|||
import {usePages, useQuery} from "../hooks/customHooks";
|
||||
import {ZimSearchView} from "./Zim";
|
||||
import {searchEstimate, searchSuggestions} from "../api";
|
||||
import {normalizeEstimate, TabLinks} from "./Common";
|
||||
import {fuzzyMatch, normalizeEstimate, TabLinks} from "./Common";
|
||||
import _ from "lodash";
|
||||
import {TagsContext} from "../Tags";
|
||||
import {Header as SHeader} from "semantic-ui-react";
|
||||
|
||||
const suggestedApps = [
|
||||
{location: '/more/otp', title: 'One Time Pad', description: 'Encrypt and Decrypt messages'},
|
||||
|
|
@ -19,6 +21,7 @@ const suggestedApps = [
|
|||
export function useSearchSuggestions() {
|
||||
const navigate = useNavigate();
|
||||
const {searchParams} = useQuery();
|
||||
const {SingleTag} = React.useContext(TagsContext);
|
||||
|
||||
const searchStr = searchParams.get('q');
|
||||
|
||||
|
|
@ -26,29 +29,32 @@ export function useSearchSuggestions() {
|
|||
|
||||
const fetchSuggestions = async () => {
|
||||
if (searchStr && searchStr.length > 0) {
|
||||
const lowerSearchStr = searchStr.toLowerCase();
|
||||
try {
|
||||
const i = await searchSuggestions(searchStr);
|
||||
const matchingApps = suggestedApps.filter(i => i.title.toLowerCase().includes(searchStr.toLowerCase()));
|
||||
const newSuggestions = {
|
||||
const matchingApps = suggestedApps.filter(i =>
|
||||
i.title.toLowerCase().includes(lowerSearchStr)
|
||||
|| fuzzyMatch(i.title.toLowerCase(), lowerSearchStr));
|
||||
const matchingSuggestions = {
|
||||
channels: {
|
||||
name: 'Channels', results: i['channels'].map(i => {
|
||||
name: 'Channels', results: i.channels.map(i => {
|
||||
return {title: i['name'], id: i['id'], type: 'channel'}
|
||||
})
|
||||
},
|
||||
domains: {
|
||||
name: 'Domains', results: i['domains'].map(i => {
|
||||
name: 'Domains', results: i.domains.map(i => {
|
||||
return {title: i['domain'], id: i['id'], type: 'domain'}
|
||||
})
|
||||
},
|
||||
tags: {
|
||||
name: 'Tags', results: i['tags'].map(i => {
|
||||
name: 'Tags', results: i.tags.map(i => {
|
||||
return {title: i['name'], type: 'tag'}
|
||||
})
|
||||
},
|
||||
apps: {name: 'Apps', results: matchingApps},
|
||||
};
|
||||
console.debug('newSuggestions', newSuggestions);
|
||||
setSuggestions(newSuggestions);
|
||||
console.debug('newSuggestions', matchingSuggestions);
|
||||
setSuggestions(matchingSuggestions);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
console.error('Failed to get search suggestions');
|
||||
|
|
@ -75,11 +81,24 @@ export function useSearchSuggestions() {
|
|||
} else if (result['type'] === 'domain') {
|
||||
navigate(`/archive?domain=${result['domain']}`);
|
||||
} else if (result['type'] === 'tag') {
|
||||
navigate(`/search?tag=${result['title']}`);
|
||||
navigate(`/search?tag=${encodeURIComponent(result['title'])}`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return {suggestions, searchStr, handleResultSelect}
|
||||
const resultRenderer = ({type, title, description}) => {
|
||||
description = description !== null ? <SHeader as='h5'>{description}</SHeader> : null;
|
||||
if (type === 'tag') {
|
||||
return <SingleTag name={title}/>;
|
||||
}
|
||||
|
||||
// No specific renderer, use the generic.
|
||||
return <>
|
||||
<SHeader as='h4'>{title}</SHeader>
|
||||
{description}
|
||||
</>
|
||||
};
|
||||
|
||||
return {suggestions, searchStr, handleResultSelect, resultRenderer}
|
||||
}
|
||||
|
||||
export const useSearch = (defaultLimit = 48, totalPages = 0, emptySearch = false, model) => {
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ import {
|
|||
StatisticValue,
|
||||
TableCell
|
||||
} from "semantic-ui-react";
|
||||
import {ChannelsPage, ChannelEditPage, ChannelNewPage} from "./Channels";
|
||||
import {ChannelEditPage, ChannelNewPage, ChannelsPage} from "./Channels";
|
||||
import {
|
||||
useChannel,
|
||||
useQuery,
|
||||
|
|
@ -200,7 +200,7 @@ function VideosPage() {
|
|||
<Grid.Column width={6}><SortButton sorts={videoOrders}/></Grid.Column>
|
||||
</Grid.Row>
|
||||
<Grid.Row>
|
||||
<Grid.Column width={10}>{searchInput}</Grid.Column>
|
||||
<Grid.Column width={16}>{searchInput}</Grid.Column>
|
||||
</Grid.Row>
|
||||
</Grid>
|
||||
</Media>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue