From 1b92d1e5805bc95870b7d595f1ef54e6cbd36c48 Mon Sep 17 00:00:00 2001 From: Roland Date: Tue, 21 Nov 2023 13:01:52 -0700 Subject: [PATCH] Global search uses Semantic SearchResultsInput component, other components use SearchInput. --- app/src/App.css | 13 ++++ app/src/DashboardPage.js | 50 ++++++------ app/src/Tags.js | 29 +++---- app/src/components/Common.js | 138 +++++++++++++++++++++++++++------- app/src/components/Search.js | 41 +++++++--- app/src/components/Videos.js | 4 +- modules/archive/lib.py | 2 +- modules/videos/channel/lib.py | 2 +- modules/videos/models.py | 2 +- wrolpi/files/api.py | 4 +- wrolpi/root_api.py | 9 ++- wrolpi/tags.py | 4 +- 12 files changed, 205 insertions(+), 93 deletions(-) diff --git a/app/src/App.css b/app/src/App.css index 83ecefd5..3d6ee109 100644 --- a/app/src/App.css +++ b/app/src/App.css @@ -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; +} diff --git a/app/src/DashboardPage.js b/app/src/DashboardPage.js index edfa85da..2474ebf7 100644 --- a/app/src/DashboardPage.js +++ b/app/src/DashboardPage.js @@ -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() { - navigate('/')} - style={{marginBottom: '2em'}} - results={suggestions} - handleResultSelect={handleResultSelect} + navigate('/')} + style={{marginBottom: '2em'}} + results={suggestions} + handleResultSelect={handleResultSelect} + resultRenderer={resultRenderer} /> @@ -211,17 +212,18 @@ export function DashboardPage() { - navigate('/')} - style={{marginBottom: '2em'}} - results={suggestions} - handleResultSelect={handleResultSelect} + navigate('/')} + style={{marginBottom: '2em'}} + results={suggestions} + handleResultSelect={handleResultSelect} + resultRenderer={resultRenderer} /> diff --git a/app/src/Tags.js b/app/src/Tags.js index c0423b04..99c76ba2 100644 --- a/app/src/Tags.js +++ b/app/src/Tags.js @@ -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() { } + const SingleTag = ({name}) => { + return + } + 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 = ; - } - - return
- + + {clearable === true &&
{clearButton}
} } @@ -1526,3 +1581,32 @@ export function ErrorMessage({children, size = null}) { {children} } + +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; +} diff --git a/app/src/components/Search.js b/app/src/components/Search.js index 833dc252..34906fee 100644 --- a/app/src/components/Search.js +++ b/app/src/components/Search.js @@ -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 ? {description} : null; + if (type === 'tag') { + return ; + } + + // No specific renderer, use the generic. + return <> + {title} + {description} + + }; + + return {suggestions, searchStr, handleResultSelect, resultRenderer} } export const useSearch = (defaultLimit = 48, totalPages = 0, emptySearch = false, model) => { diff --git a/app/src/components/Videos.js b/app/src/components/Videos.js index 4bf6300b..7871ecf6 100644 --- a/app/src/components/Videos.js +++ b/app/src/components/Videos.js @@ -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() {
- {searchInput} + {searchInput}
diff --git a/modules/archive/lib.py b/modules/archive/lib.py index d4cec58f..ea35a5a8 100644 --- a/modules/archive/lib.py +++ b/modules/archive/lib.py @@ -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)) \ diff --git a/modules/videos/channel/lib.py b/modules/videos/channel/lib.py index 3ff416b3..07bb32ae 100644 --- a/modules/videos/channel/lib.py +++ b/modules/videos/channel/lib.py @@ -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)) \ diff --git a/modules/videos/models.py b/modules/videos/models.py index b0b2f790..8bdbfc61 100644 --- a/modules/videos/models.py +++ b/modules/videos/models.py @@ -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 diff --git a/wrolpi/files/api.py b/wrolpi/files/api.py index a7d8fab5..adde7cf0 100644 --- a/wrolpi/files/api.py +++ b/wrolpi/files/api.py @@ -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] diff --git a/wrolpi/root_api.py b/wrolpi/root_api.py index 3b9771d4..2626609f 100644 --- a/wrolpi/root_api.py +++ b/wrolpi/root_api.py @@ -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, diff --git a/wrolpi/tags.py b/wrolpi/tags.py index b6a012c4..49b5345d 100644 --- a/wrolpi/tags.py +++ b/wrolpi/tags.py @@ -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)) \