User can select from/to years when searching files. Indexing doc and docx files.

This commit is contained in:
Roland 2024-03-06 17:41:04 -07:00
parent 758ff8e810
commit 00fe799b8f
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
40 changed files with 880 additions and 159 deletions

View file

@ -1657,7 +1657,22 @@ export function getParentDirectory(filePath) {
export function MultilineText({text, ...props}) {
return <div {...props}>
{text.split('\n').map((line, index, array) =>
index === array.length - 1 ? line : <p>{line}</p>
index === array.length - 1 ? line : <p key={index}>{line}</p>
)}
</div>
}
export const monthNames = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];

View file

@ -0,0 +1,230 @@
import React from "react";
import {Button, Form, FormField, Modal, ModalActions, ModalContent, ModalHeader, Segment} from "./Theme";
import {Checkbox, Dropdown} from "semantic-ui-react";
import Grid from "semantic-ui-react/dist/commonjs/collections/Grid";
import {monthNames} from "./Common";
import Message from "semantic-ui-react/dist/commonjs/collections/Message";
export const dateRangeIsEmpty = (dateRange) => {
return dateRange[0] === null && dateRange[1] === null;
}
function MonthsForm({monthsSelected, setMonthsSelected}) {
monthsSelected = monthsSelected.map(i => parseInt(i));
const handleWinter = (e) => {
if (e) e.preventDefault();
setMonthsSelected([12, 1, 2]);
}
const handleSpring = (e) => {
if (e) e.preventDefault();
setMonthsSelected([3, 4, 5]);
}
const handleSummer = (e) => {
if (e) e.preventDefault();
setMonthsSelected([6, 7, 8]);
}
const handleFall = (e) => {
if (e) e.preventDefault();
setMonthsSelected([9, 10, 11]);
}
const monthCheckbox = (label) => {
// "January" == 1, etc.
const idx = monthNames.indexOf(label) + 1;
const isChecked = monthsSelected.indexOf(idx) >= 0;
return <Checkbox
label={label}
checked={isChecked}
size='large'
onChange={(e, {checked}) => {
// Add or remove month index if the respective checkbox is checked.
if (checked) {
setMonthsSelected([...monthsSelected, idx]);
} else {
setMonthsSelected(monthsSelected.filter(i => i !== idx));
}
}}
/>
}
return <Form>
<Grid>
<Grid.Row columns={3}>
<Grid.Column>{monthCheckbox('January')}</Grid.Column>
<Grid.Column>{monthCheckbox('February')}</Grid.Column>
<Grid.Column>{monthCheckbox('March')}</Grid.Column>
</Grid.Row>
<Grid.Row columns={3}>
<Grid.Column>{monthCheckbox('April')}</Grid.Column>
<Grid.Column>{monthCheckbox('May')}</Grid.Column>
<Grid.Column>{monthCheckbox('June')}</Grid.Column>
</Grid.Row>
<Grid.Row columns={3}>
<Grid.Column>{monthCheckbox('July')}</Grid.Column>
<Grid.Column>{monthCheckbox('August')}</Grid.Column>
<Grid.Column>{monthCheckbox('September')}</Grid.Column>
</Grid.Row>
<Grid.Row columns={3}>
<Grid.Column>{monthCheckbox('October')}</Grid.Column>
<Grid.Column>{monthCheckbox('November')}</Grid.Column>
<Grid.Column>{monthCheckbox('December')}</Grid.Column>
</Grid.Row>
<Grid.Row columns={4}>
<Grid.Column><Button onClick={handleWinter} color='blue' size='small'>Winter</Button></Grid.Column>
<Grid.Column><Button onClick={handleSpring} color='green' size='small'>Spring</Button></Grid.Column>
<Grid.Column><Button onClick={handleSummer} color='yellow' size='small'>Summer</Button></Grid.Column>
<Grid.Column><Button onClick={handleFall} color='red' size='small'>Fall</Button></Grid.Column>
</Grid.Row>
</Grid>
</Form>
}
function DateRangeForm({dateRange, setDateRange}) {
const currentYear = (new Date()).getFullYear();
const [error, setError] = React.useState('');
let yearRange = [];
for (let i = 1970; i <= currentYear; i++) {
yearRange = [...yearRange, {
key: i,
text: i,
value: i,
}];
}
const handleFromYear = (e, {value}) => {
const toYear = dateRange[1] || currentYear;
setDateRange([value, toYear]);
}
const handleToYear = (e, {value}) => {
setDateRange([dateRange[0], value]);
}
React.useEffect(() => {
if (dateRange[0] > dateRange[1]) {
setError('From Year must be greater than To Year');
} else {
setError('');
}
}, [dateRange])
const errorMessage = error && <Message error content={error}/>;
return <React.Fragment>
<Form>
<Grid>
<Grid.Row columns={2}>
<Grid.Column>
<FormField>
<label>From Year</label>
<Dropdown search scrolling fluid
options={yearRange}
value={dateRange[0]}
onChange={handleFromYear}
error={!!error}
/>
</FormField>
</Grid.Column>
<Grid.Column>
<FormField>
<label>To Year</label>
<Dropdown search scrolling fluid
options={yearRange.reverse()}
value={dateRange[1]}
onChange={handleToYear}
error={!!error}
/>
</FormField>
</Grid.Column>
</Grid.Row>
</Grid>
</Form>
{errorMessage}
</React.Fragment>
}
export function DateSelectorButton({
onMonthsChange,
onDateRangeChange,
defaultMonthsSelected,
defaultDateRange,
buttonProps
}) {
const emptyDateRange = [null, null];
const [open, setOpen] = React.useState(false);
const [monthsSelected, setMonthsSelected] = React.useState(defaultMonthsSelected || []);
const [dateRange, setDateRange] = React.useState(defaultDateRange || emptyDateRange);
const [color, setColor] = React.useState('grey');
React.useEffect(() => {
if (monthsSelected.length > 0 || (!dateRangeIsEmpty(dateRange))) {
setColor('violet');
} else {
setColor('grey');
}
}, [JSON.stringify(dateRange), JSON.stringify(monthsSelected)]);
const handleOpen = (e) => {
if (e) e.preventDefault();
setOpen(true);
}
const handleClose = (e) => {
if (e) e.preventDefault();
setOpen(false);
// Only submit selection when user has closed selector.
if (onDateRangeChange && dateRange[0] <= dateRange[1]) {
onDateRangeChange(dateRange);
} else if (onDateRangeChange && dateRangeIsEmpty(dateRange)) {
//Clear the date range.
onDateRangeChange(dateRange);
}
if (onMonthsChange) {
onMonthsChange(monthsSelected);
}
}
const localOnSetDateRange = (newDateRange) => {
setDateRange(newDateRange);
}
const handleClear = (e) => {
if (e) e.preventDefault();
setDateRange(emptyDateRange);
setMonthsSelected([]);
}
return <React.Fragment>
<Button
icon='calendar alternate'
onClick={handleOpen}
color={color}
{...buttonProps}
/>
<Modal closeIcon
open={open}
onClose={handleClose}
size='tiny'
>
<ModalHeader>Filter by Date</ModalHeader>
<ModalContent>
<Segment>
<DateRangeForm dateRange={dateRange} setDateRange={localOnSetDateRange}/>
</Segment>
<Segment>
<MonthsForm monthsSelected={monthsSelected} setMonthsSelected={setMonthsSelected}/>
</Segment>
</ModalContent>
<ModalActions>
<Button onClick={handleClear} secondary>Clear</Button>
<Button onClick={handleClose}>Close</Button>
</ModalActions>
</Modal>
</React.Fragment>
}

View file

@ -194,7 +194,7 @@ export function FilePreviewProvider({children}) {
if (previewQuery && !previewFile) {
initPreviewFile();
}
}, []);
}, [previewQuery]);
const localFetchFile = async () => {
// Get the file again with its Tags.

View file

@ -32,9 +32,9 @@ import {
useTitle
} from "./Common";
import {
useAllQuery,
useFilesProgressInterval,
usePages,
useQuery,
useSearchFiles,
useSearchFilter,
useSearchView,
@ -149,7 +149,7 @@ function FileCard({file}) {
</ExternalCardLink>
</CardHeader>
{author && <b {...s}>{author}</b>}
<p>{isoDatetimeToString(file.modified)}</p>
<p>{isoDatetimeToString(file.published_datetime) || isoDatetimeToString(file.modified)}</p>
<p>{size}</p>
</CardContent>
</Card>
@ -403,6 +403,9 @@ export function SearchFilter({filters = [], modalHeader, size = 'medium'}) {
</Form.Field>
);
// Use violet color when filter has been applied.
const buttonColor = filter ? 'violet' : 'grey';
if (filters && filters.length > 0) {
return <>
<Modal open={open} onOpen={() => handleOpen()} onClose={() => setOpen(false)} closeIcon>
@ -420,7 +423,7 @@ export function SearchFilter({filters = [], modalHeader, size = 'medium'}) {
<Button
icon='filter'
onClick={handleOpen}
primary={!!filter}
color={buttonColor}
size={size}
/>
</>
@ -430,12 +433,11 @@ export function SearchFilter({filters = [], modalHeader, size = 'medium'}) {
}
export function TagsQuerySelector({onChange}) {
// Creates dropdown that the User can use to manipulate the tag query.
const {searchParams, updateQuery} = useQuery();
const activeTags = searchParams.getAll('tag');
// Creates modal that the User can use to manipulate the tag query.
const [activeTags, setActiveTags] = useAllQuery('tag');
const localOnChange = (tagNames) => {
updateQuery({'tag': tagNames});
setActiveTags(tagNames);
if (onChange) {
onChange(tagNames);
}

View file

@ -1,7 +1,7 @@
import React, {useState} from "react";
import {createSearchParams, Route, Routes, useNavigate} from "react-router-dom";
import {Route, Routes, useNavigate} from "react-router-dom";
import {FilesSearchView} from "./Files";
import {useLatestRequest, usePages, useQuery, useSearchFilter} from "../hooks/customHooks";
import {useLatestRequest, usePages, useSearchDateRange, useSearchFilter, useSearchMonths} from "../hooks/customHooks";
import {ZimSearchView} from "./Zim";
import {searchEstimateZims, searchSuggestions} from "../api";
import {filterToMimetypes, fuzzyMatch, normalizeEstimate, SearchResultsInput, TabLinks} from "./Common";
@ -9,6 +9,7 @@ import _ from "lodash";
import {TagsContext} from "../Tags";
import {Button as SButton, Header as SHeader, Label} from "semantic-ui-react";
import {Modal, ModalContent} from "./Theme";
import {QueryContext} from "../contexts/contexts";
const SUGGESTED_APPS = [
{location: '/admin', title: 'Downloads', description: 'View and control your downloads'},
@ -37,7 +38,9 @@ const SUGGESTED_APPS = [
export const useSearch = (defaultLimit = 48, totalPages = 0, emptySearch = false, model) => {
const navigate = useNavigate();
const {searchParams, updateQuery} = useQuery();
const {months, setMonths} = useSearchMonths();
const {dateRange, setDateRange} = useSearchDateRange();
const {searchParams, updateQuery, getLocationStr} = React.useContext(QueryContext);
// `searchStr` means actually fetch the files/zims.
const searchStr = searchParams.get('q');
// User can search only by Tags, `searchStr` not required.
@ -53,11 +56,8 @@ export const useSearch = (defaultLimit = 48, totalPages = 0, emptySearch = false
if (filter) {
searchQuery['filter'] = filter;
}
navigate({
pathname: '/search',
// Start new search at offset 0.
search: createSearchParams(searchQuery).toString(),
});
const location = getLocationStr(searchQuery, '/search');
navigate(location);
}
const clearSearch = () => {
@ -90,6 +90,8 @@ export const useSearch = (defaultLimit = 48, totalPages = 0, emptySearch = false
setSearchStr,
clearSearch,
setTags,
months, setMonths,
dateRange, setDateRange,
}
}
@ -101,6 +103,8 @@ export function useSuggestions(searchStr, tagNames, filter) {
zimsEstimates: [],
}
const [suggestions, setSuggestions] = React.useState(defaultSuggestions);
const {months} = useSearchMonths();
const {dateRange} = useSearchDateRange();
// fileGroups/channels/domains.
const {data, sendRequest, loading} = useLatestRequest(500);
// Zims are slow, so they are separate.
@ -109,10 +113,17 @@ export function useSuggestions(searchStr, tagNames, filter) {
React.useEffect(() => {
if ((searchStr && searchStr.length > 0) || (tagNames && tagNames.length > 0)) {
const mimetypes = filterToMimetypes(filter);
sendRequest(async () => await searchSuggestions(searchStr, tagNames, mimetypes));
sendRequest(async () => await searchSuggestions(searchStr, tagNames, mimetypes, months, dateRange));
sendZimRequest(async () => await searchEstimateZims(searchStr, tagNames));
}
}, [searchStr, sendRequest, sendZimRequest, JSON.stringify(tagNames)]);
}, [
searchStr,
sendRequest,
sendZimRequest,
JSON.stringify(tagNames),
JSON.stringify(months),
JSON.stringify(dateRange),
]);
React.useEffect(() => {
if (!_.isEmpty(data)) {
@ -141,8 +152,10 @@ export function useSearchSuggestions(defaultSearchStr, defaultTagNames) {
const [searchStr, setSearchStr] = React.useState(defaultSearchStr || '');
const [searchTags, setSearchTags] = React.useState(defaultTagNames);
const {SingleTag, fuzzyMatchTagsByName} = React.useContext(TagsContext);
const {months, setMonths} = useSearchMonths();
const {dateRange, setDateRange} = useSearchDateRange();
const {suggestions, loading} = useSuggestions(searchStr, searchTags, filter);
const {getLocationStr} = useQuery();
const {getLocationStr} = React.useContext(QueryContext);
// The results that will be displayed by <Search>.
const [suggestionsResults, setSuggestionsResults] = useState({});
@ -275,12 +288,13 @@ export function useSearchSuggestions(defaultSearchStr, defaultTagNames) {
suggestions,
suggestionsResults,
suggestionsSums,
searchStr,
setSearchStr,
searchStr, setSearchStr,
setSearchTags,
months, setMonths,
handleResultSelect,
resultRenderer,
loading,
dateRange, setDateRange,
}
}

View file

@ -52,7 +52,7 @@ import Grid from "semantic-ui-react/dist/commonjs/collections/Grid";
import Icon from "semantic-ui-react/dist/commonjs/elements/Icon";
import {Button, Card, Header, Loader, Placeholder, Segment, Statistic, StatisticGroup} from "./Theme";
import {deleteVideos} from "../api";
import {Media, ThemeContext} from "../contexts/contexts";
import {Media, QueryContext, ThemeContext} from "../contexts/contexts";
import _ from "lodash";
export function VideoWrapper() {
@ -69,7 +69,7 @@ export function VideoWrapper() {
function VideosPage() {
const {channelId} = useParams();
const {searchParams} = useQuery();
const {searchParams} = React.useContext(QueryContext);
const [selectedVideos, setSelectedVideos] = useState([]);
let searchOrder = defaultVideoOrder;