<commit_before>/**
 * TagSpaces - universal file and folder organizer
 * Copyright (C) 2017-present TagSpaces UG (haftungsbeschraenkt)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License (version 3) as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

import React from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { withStyles } from '@material-ui/core/styles';
import classNames from 'classnames';
import Typography from '@material-ui/core/Typography';
import MenuItem from '@material-ui/core/MenuItem';
import PictureIcon from '@material-ui/icons/Panorama';
import DocumentIcon from '@material-ui/icons/PictureAsPdf';
import NoteIcon from '@material-ui/icons/Note';
import AudioIcon from '@material-ui/icons/MusicVideo';
import VideoIcon from '@material-ui/icons/OndemandVideo';
import ArchiveIcon from '@material-ui/icons/Archive';
import FolderIcon from '@material-ui/icons/FolderOpen';
import UntaggedIcon from '@material-ui/icons/LabelOffOutlined';
import FileIcon from '@material-ui/icons/InsertDriveFileOutlined';
import ClearSearchIcon from '@material-ui/icons/Clear';
import BookmarkIcon from '@material-ui/icons/BookmarkBorder';
import BookIcon from '@material-ui/icons/LocalLibraryOutlined';
import PlaceIcon from '@material-ui/icons/Place';
import DateIcon from '@material-ui/icons/DateRange';
import Button from '@material-ui/core/Button';
import Input from '@material-ui/core/Input';
import OutlinedInput from '@material-ui/core/OutlinedInput';
import TextField from '@material-ui/core/TextField';
import InputLabel from '@material-ui/core/InputLabel';
import InputAdornment from '@material-ui/core/InputAdornment';
import IconButton from '@material-ui/core/IconButton';
import Select from '@material-ui/core/Select';
import FormControl from '@material-ui/core/FormControl';
import MoreVertIcon from '@material-ui/icons/MoreVert';
import ButtonGroup from '@material-ui/core/ButtonGroup';
import ToggleButton from '@material-ui/lab/ToggleButton';
import ToggleButtonGroup from '@material-ui/lab/ToggleButtonGroup';
import OpenLocationCode from 'open-location-code-typescript';
import { FormControlLabel, Switch } from '@material-ui/core';
import TagsSelect from './TagsSelect';
import CustomLogo from './CustomLogo';
import { actions as AppActions, getDirectoryPath } from '../reducers/app';
import {
  actions as LocationIndexActions,
  getIndexedEntriesCount,
  isIndexing,
  getSearchQuery
} from '../reducers/location-index';
import { getMaxSearchResults } from '../reducers/settings';
import styles from './SidePanels.css';
import i18n from '../services/i18n';
import { FileTypeGroups, SearchQuery } from '../services/search';
import { Pro } from '../pro';
import SearchMenu from './menus/SearchMenu';
import { Tag } from '../reducers/taglibrary';
import { formatDateTime, extractTimePeriod } from '../utils/dates';
import { isPlusCode, parseLatLon } from '../utils/misc';
import PlatformIO from '../services/platform-io';

interface Props {
  classes: any;
  style: any;
  searchLocationIndex: (searchQuery: SearchQuery) => void;
  createLocationsIndexes: () => void;
  searchAllLocations: (searchQuery: SearchQuery) => void;
  loadDirectoryContent: (path: string) => void;
  openURLExternally: (url: string) => void;
  hideDrawer?: () => void;
  searchQuery: () => any;
  setSearchResults: (entries: Array<any>) => void;
  setSearchQuery: (searchQuery: SearchQuery) => void;
  currentDirectory: string;
  indexedEntriesCount: number;
  maxSearchResults: number;
  indexing: boolean;
}

interface State {
  textQuery: string;
  tagsAND: Array<Tag>;
  tagsOR: Array<Tag>;
  tagsNOT: Array<Tag>;
  fileTypes: Array<string>;
  searchBoxing: 'location' | 'folder' | 'global';
  lastModified: string;
  tagTimePeriod: string;
  tagTimePeriodHelper: string;
  tagPlace: string;
  tagPlaceHelper: string;
  tagTimePeriodFrom: Date | null;
  tagTimePeriodTo: Date | null;
  tagPlaceLat: number | null;
  tagPlaceLong: number | null;
  tagPlaceRadius: number;
  fileSize: string;
  forceIndexing: boolean;
  searchMenuOpened: boolean;
  searchMenuAnchorEl: Element | null;
}

class Search extends React.Component<Props, State> {
  // @ts-ignore
  state = {
    textQuery: '',
    tagsAND: [],
    tagsOR: [],
    tagsNOT: [],
    fileTypes: FileTypeGroups.any,
    searchBoxing: 'location',
    lastModified: '',
    tagTimePeriod: '',
    tagTimePeriodHelper: ' ',
    tagPlace: '',
    tagPlaceHelper: ' ',
    tagTimePeriodFrom: null,
    tagTimePeriodTo: null,
    tagPlaceLat: null,
    tagPlaceLong: null,
    tagPlaceRadius: 0,
    forceIndexing: false,
    fileSize: '',
    searchMenuOpened: false,
    searchMenuAnchorEl: null
  };

  static getDerivedStateFromProps(nextProps, prevState) {
    let { searchBoxing } = prevState;
    if (
      Pro &&
      (!nextProps.currentDirectory || nextProps.currentDirectory.length < 1)
    ) {
      searchBoxing = 'global';
    }
    if (
      prevState.tagsAND.length < 1 &&
      nextProps.searchQuery &&
      nextProps.searchQuery.tagsAND
    ) {
      return {
        ...prevState,
        searchBoxing,
        tagsAND: nextProps.searchQuery.tagsAND<commit_msg>fix: disabling logic for selecting the global search in some cases<commit_after>/**
 * TagSpaces - universal file and folder organizer
 * Copyright (C) 2017-present TagSpaces UG (haftungsbeschraenkt)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License (version 3) as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

import React from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { withStyles } from '@material-ui/core/styles';
import classNames from 'classnames';
import Typography from '@material-ui/core/Typography';
import MenuItem from '@material-ui/core/MenuItem';
import PictureIcon from '@material-ui/icons/Panorama';
import DocumentIcon from '@material-ui/icons/PictureAsPdf';
import NoteIcon from '@material-ui/icons/Note';
import AudioIcon from '@material-ui/icons/MusicVideo';
import VideoIcon from '@material-ui/icons/OndemandVideo';
import ArchiveIcon from '@material-ui/icons/Archive';
import FolderIcon from '@material-ui/icons/FolderOpen';
import UntaggedIcon from '@material-ui/icons/LabelOffOutlined';
import FileIcon from '@material-ui/icons/InsertDriveFileOutlined';
import ClearSearchIcon from '@material-ui/icons/Clear';
import BookmarkIcon from '@material-ui/icons/BookmarkBorder';
import BookIcon from '@material-ui/icons/LocalLibraryOutlined';
import PlaceIcon from '@material-ui/icons/Place';
import DateIcon from '@material-ui/icons/DateRange';
import Button from '@material-ui/core/Button';
import Input from '@material-ui/core/Input';
import OutlinedInput from '@material-ui/core/OutlinedInput';
import TextField from '@material-ui/core/TextField';
import InputLabel from '@material-ui/core/InputLabel';
import InputAdornment from '@material-ui/core/InputAdornment';
import IconButton from '@material-ui/core/IconButton';
import Select from '@material-ui/core/Select';
import FormControl from '@material-ui/core/FormControl';
import MoreVertIcon from '@material-ui/icons/MoreVert';
import ButtonGroup from '@material-ui/core/ButtonGroup';
import ToggleButton from '@material-ui/lab/ToggleButton';
import ToggleButtonGroup from '@material-ui/lab/ToggleButtonGroup';
import OpenLocationCode from 'open-location-code-typescript';
import { FormControlLabel, Switch } from '@material-ui/core';
import TagsSelect from './TagsSelect';
import CustomLogo from './CustomLogo';
import { actions as AppActions, getDirectoryPath } from '../reducers/app';
import {
  actions as LocationIndexActions,
  getIndexedEntriesCount,
  isIndexing,
  getSearchQuery
} from '../reducers/location-index';
import { getMaxSearchResults } from '../reducers/settings';
import styles from './SidePanels.css';
import i18n from '../services/i18n';
import { FileTypeGroups, SearchQuery } from '../services/search';
import { Pro } from '../pro';
import SearchMenu from './menus/SearchMenu';
import { Tag } from '../reducers/taglibrary';
import { formatDateTime, extractTimePeriod } from '../utils/dates';
import { isPlusCode, parseLatLon } from '../utils/misc';
import PlatformIO from '../services/platform-io';

interface Props {
  classes: any;
  style: any;
  searchLocationIndex: (searchQuery: SearchQuery) => void;
  createLocationsIndexes: () => void;
  searchAllLocations: (searchQuery: SearchQuery) => void;
  loadDirectoryContent: (path: string) => void;
  openURLExternally: (url: string) => void;
  hideDrawer?: () => void;
  searchQuery: () => any;
  setSearchResults: (entries: Array<any>) => void;
  setSearchQuery: (searchQuery: SearchQuery) => void;
  currentDirectory: string;
  indexedEntriesCount: number;
  maxSearchResults: number;
  indexing: boolean;
}

interface State {
  textQuery: string;
  tagsAND: Array<Tag>;
  tagsOR: Array<Tag>;
  tagsNOT: Array<Tag>;
  fileTypes: Array<string>;
  searchBoxing: 'location' | 'folder' | 'global';
  lastModified: string;
  tagTimePeriod: string;
  tagTimePeriodHelper: string;
  tagPlace: string;
  tagPlaceHelper: string;
  tagTimePeriodFrom: Date | null;
  tagTimePeriodTo: Date | null;
  tagPlaceLat: number | null;
  tagPlaceLong: number | null;
  tagPlaceRadius: number;
  fileSize: string;
  forceIndexing: boolean;
  searchMenuOpened: boolean;
  searchMenuAnchorEl: Element | null;
}

class Search extends React.Component<Props, State> {
  // @ts-ignore
  state = {
    textQuery: '',
    tagsAND: [],
    tagsOR: [],
    tagsNOT: [],
    fileTypes: FileTypeGroups.any,
    searchBoxing: 'location',
    lastModified: '',
    tagTimePeriod: '',
    tagTimePeriodHelper: ' ',
    tagPlace: '',
    tagPlaceHelper: ' ',
    tagTimePeriodFrom: null,
    tagTimePeriodTo: null,
    tagPlaceLat: null,
    tagPlaceLong: null,
    tagPlaceRadius: 0,
    forceIndexing: false,
    fileSize: '',
    searchMenuOpened: false,
    searchMenuAnchorEl: null
  };

  static getDerivedStateFromProps(nextProps, prevState) {
    const { searchBoxing } = prevState;
    // console.log('Path: ' + nextProps.currentDirectory);
    // if (
    //   Pro &&
    //   (!nextProps.currentDirectory || nextProps.currentDirectory.length < 1)
    // ) {
    //   searchBoxing = 'global';
    // }
    if (
      prevState.tagsAND.length < 1 &&
      nextProps.searchQuery &&
      nextProps.searchQuery.tagsAND
    ) {
      return {
        ...prevState,
        searchBoxing,
        tagsAND: nextProps.searchQuery.tagsAND
{'fraction_non_alphanumeric': 0.11924213453850165, 'fraction_numerical': 0.0020858682426560054, 'mean_word_length': 4.484747378455672, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 35, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4194478', 'n_tokens_mistral': 3363, 'n_tokens_neox': 3313, 'n_words': 949}