Global search uses Semantic SearchResultsInput component, other components use SearchInput.

This commit is contained in:
Roland 2023-11-21 13:01:52 -07:00
parent 2bd39fd0c7
commit 1b92d1e580
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
12 changed files with 205 additions and 93 deletions

View file

@ -224,3 +224,16 @@ textarea.otp {
overflow: hidden;
text-overflow: ellipsis;
}
.search-container {
display: flex;
justify-content: flex-start;
}
.search-input {
flex-grow: 1;
}
.search-clear {
margin: 10px;
}

View file

@ -1,4 +1,4 @@
import {LoadStatistic, PageContainer, SearchInput, useTitle} from "./components/Common";
import {LoadStatistic, PageContainer, SearchResultsInput, useTitle} from "./components/Common";
import React, {useContext, useState} from "react";
import {Media, StatusContext} from "./contexts/contexts";
import {DownloadMenu} from "./components/Download";
@ -158,7 +158,7 @@ export function DashboardPage() {
const navigate = useNavigate();
const {searchStr, setSearchStr, activeTags} = useSearch();
const {suggestions, handleResultSelect} = useSearchSuggestions();
const {suggestions, handleResultSelect, resultRenderer} = useSearchSuggestions();
const {status} = useContext(StatusContext);
let title = 'Dashboard';
@ -188,17 +188,18 @@ export function DashboardPage() {
<Grid>
<Grid.Row columns={2}>
<Grid.Column width={13}>
<SearchInput clearable
searchStr={searchStr}
onChange={setSearchStr}
onSubmit={setSearchStr}
size='large'
placeholder='Search everywhere...'
actionIcon='search'
onClear={() => navigate('/')}
style={{marginBottom: '2em'}}
results={suggestions}
handleResultSelect={handleResultSelect}
<SearchResultsInput clearable
searchStr={searchStr}
onChange={setSearchStr}
onSubmit={setSearchStr}
size='large'
placeholder='Search everywhere...'
actionIcon='search'
onClear={() => navigate('/')}
style={{marginBottom: '2em'}}
results={suggestions}
handleResultSelect={handleResultSelect}
resultRenderer={resultRenderer}
/>
</Grid.Column>
<Grid.Column width={3} textAlign='right'>
@ -211,17 +212,18 @@ export function DashboardPage() {
<Grid>
<Grid.Row columns={2}>
<Grid.Column mobile={14}>
<SearchInput clearable
searchStr={searchStr}
onChange={setSearchStr}
onSubmit={setSearchStr}
size='big'
placeholder='Search everywhere...'
actionIcon='search'
onClear={() => navigate('/')}
style={{marginBottom: '2em'}}
results={suggestions}
handleResultSelect={handleResultSelect}
<SearchResultsInput clearable
searchStr={searchStr}
onChange={setSearchStr}
onSubmit={setSearchStr}
size='big'
placeholder='Search everywhere...'
actionIcon='search'
onClear={() => navigate('/')}
style={{marginBottom: '2em'}}
results={suggestions}
handleResultSelect={handleResultSelect}
resultRenderer={resultRenderer}
/>
</Grid.Column>
<Grid.Column textAlign='right' width={2}>

View file

@ -1,17 +1,6 @@
import React, {useEffect} from "react";
import {deleteTag, getTags, saveTag} from "./api";
import {
Dimmer,
Divider,
Form,
Grid,
GridColumn,
GridRow,
Label,
LabelGroup,
TableCell,
TableRow,
} from "semantic-ui-react";
import {Dimmer, Divider, Form, Grid, GridColumn, GridRow, Label, TableCell, TableRow,} from "semantic-ui-react";
import {APIButton, contrastingColor, ErrorMessage, scrollToTopOfElement} from "./components/Common";
import {
Button,
@ -36,6 +25,7 @@ export const TagsContext = React.createContext({
NameToTagLabel: null,
TagsGroup: null,
TagsLinkGroup: null,
SingleTag: null,
fetchTags: null,
findTagByName: null,
tagNames: [],
@ -103,7 +93,7 @@ export function useTags() {
}
const TagLabelLink = ({name, props}) => {
const to = `/search?tag=${name}`;
const to = `/search?tag=${encodeURIComponent(name)}`;
const style = {marginLeft: '0.3em', marginRight: '0.3em'};
try {
// We prefer to use Link to avoid reloading the page, check if React Router is available, so we can use it.
@ -129,12 +119,16 @@ export function useTags() {
</Label.Group>
}
const SingleTag = ({name}) => {
return <Label.Group tag><NameToTagLabel name={name}/></Label.Group>
}
useEffect(() => {
setTags([]);
fetchTags();
}, []);
return {tags, tagNames, NameToTagLabel, TagsGroup, TagsLinkGroup, fetchTags, findTagByName}
return {tags, tagNames, NameToTagLabel, TagsGroup, TagsLinkGroup, fetchTags, findTagByName, SingleTag}
}
export const useTagsInterval = () => {
@ -147,7 +141,7 @@ export const useTagsInterval = () => {
}
function EditTagRow({tag, onDelete, onEdit}) {
const {NameToTagLabel} = React.useContext(TagsContext);
const {SingleTag} = React.useContext(TagsContext);
const {name, color, id, file_group_count, zim_entry_count} = tag;
const deleteConfirm = <>
@ -160,16 +154,13 @@ function EditTagRow({tag, onDelete, onEdit}) {
/>
</>;
const editButton = <Button primary onClick={() => onEdit(name, color, id)} icon='edit'/>;
const nameLabel = <LabelGroup tag>
<NameToTagLabel name={name}/>
</LabelGroup>;
const countColor = (file_group_count + zim_entry_count) > 0 ? 'black' : 'grey';
const countLabel = <Label color={countColor}>{file_group_count + zim_entry_count}</Label>;
return <TableRow>
<TableCell>{deleteConfirm}</TableCell>
<TableCell>{editButton}</TableCell>
<TableCell>{nameLabel}</TableCell>
<TableCell><SingleTag name={name}/></TableCell>
<Media greaterThanOrEqual='tablet'>
{(className, renderChildren) => {
return renderChildren ? <TableCell className={className}>

View file

@ -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;
}

View file

@ -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) => {

View file

@ -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>

View file

@ -536,7 +536,7 @@ def search_archives(search_str: str, domain: str, limit: int, offset: int, order
@optional_session
def search_domains_by_name(name: str, limit: int = 5, session: Session = None) -> List[Domain]:
async def search_domains_by_name(name: str, limit: int = 5, session: Session = None) -> List[Domain]:
domains = session.query(Domain) \
.filter(Domain.domain.ilike(f'%{name}%')) \
.order_by(asc(Domain.domain)) \

View file

@ -202,7 +202,7 @@ def download_channel(id_: int, reset_attempts: bool = False):
@optional_session
def search_channels_by_name(name: str, limit: int = 5, session: Session = None) -> List[Channel]:
async def search_channels_by_name(name: str, limit: int = 5, session: Session = None) -> List[Channel]:
channels = session.query(Channel) \
.filter(Channel.name.ilike(f'%{name}%')) \
.order_by(asc(Channel.name)) \

View file

@ -10,7 +10,7 @@ from sqlalchemy.orm.collections import InstrumentedList
from modules.videos.errors import UnknownVideo, UnknownChannel
from wrolpi.captions import read_captions
from wrolpi.common import Base, ModelHelper, logger, get_media_directory, background_task
from wrolpi.db import get_db_curs, get_db_session, optional_session, mogrify
from wrolpi.db import get_db_curs, get_db_session, optional_session
from wrolpi.downloader import Download, download_manager
from wrolpi.files.lib import refresh_files, split_path_stem_and_suffix
from wrolpi.files.models import FileGroup

View file

@ -130,12 +130,12 @@ async def post_search_directories(_, body: schema.DirectoriesSearchRequest):
return response.empty()
from modules.videos.channel import lib as channels_lib
channels = channels_lib.search_channels_by_name(name=body.name)
channels = await channels_lib.search_channels_by_name(name=body.name)
channel_directories = [dict(path=i.directory, name=i.name) for i in channels]
channel_paths = [i['path'] for i in channel_directories]
from modules.archive import lib as archives_lib
domains = archives_lib.search_domains_by_name(name=body.name)
domains = await archives_lib.search_domains_by_name(name=body.name)
domain_directories = [dict(path=i.directory, domain=i.domain) for i in domains]
domain_paths = [i['path'] for i in domain_directories]

View file

@ -1,3 +1,4 @@
import asyncio
import json
import re
from datetime import datetime, date, timezone
@ -551,9 +552,11 @@ async def post_search_suggestions(_: Request, body: schema.SearchSuggestionsRequ
from modules.videos.channel.lib import search_channels_by_name
from modules.archive.lib import search_domains_by_name
channels = sorted(search_channels_by_name(body.search_str), key=lambda i: i.name)
domains = sorted(search_domains_by_name(body.search_str), key=lambda i: i.domain)
tags_ = tags.search_tags_by_name(body.search_str)
channels, domains, tags_ = await asyncio.gather(
search_channels_by_name(body.search_str),
search_domains_by_name(body.search_str),
tags.search_tags_by_name(body.search_str),
)
ret = dict(
channels=channels,
domains=domains,

View file

@ -1,7 +1,7 @@
import contextlib
import pathlib
from datetime import datetime
from typing import List, Dict, Tuple
from typing import List, Dict, Tuple, Sequence
from sqlalchemy import Column, Integer, String, ForeignKey, BigInteger, asc
from sqlalchemy.exc import IntegrityError
@ -464,7 +464,7 @@ def tag_names_to_zim_sub_select(tag_names: List[str], zim_id: int = None) -> Tup
@optional_session
def search_tags_by_name(name: str, limit: int = 5, session: Session = None) -> List[Tag]:
async def search_tags_by_name(name: str, limit: int = 5, session: Session = None) -> Sequence[Tag]:
tags = session.query(Tag) \
.filter(Tag.name.ilike(f'%{name}%')) \
.order_by(asc(Tag.name)) \