2023-03-06 15:31:58 +01:00
|
|
|
<script lang="ts">
|
2023-07-01 00:50:47 -04:00
|
|
|
import { AppRoute } from '$lib/constants';
|
|
|
|
|
import { goto } from '$app/navigation';
|
2024-02-21 16:50:50 +01:00
|
|
|
import { isSearchEnabled, preventRaceConditionSearchBar, savedSearchTerms } from '$lib/stores/search.store';
|
2024-02-02 13:30:40 -06:00
|
|
|
import { mdiClose, mdiMagnify, mdiTune } from '@mdi/js';
|
|
|
|
|
import SearchHistoryBox from './search-history-box.svelte';
|
2024-09-10 00:12:26 -04:00
|
|
|
import SearchFilterModal from './search-filter-modal.svelte';
|
2024-02-17 11:00:55 -06:00
|
|
|
import type { MetadataSearchDto, SmartSearchDto } from '@immich/sdk';
|
2024-02-19 21:55:54 +01:00
|
|
|
import { getMetadataSearchQuery } from '$lib/utils/metadata-search';
|
2024-02-27 08:37:37 -08:00
|
|
|
import { handlePromiseError } from '$lib/utils';
|
2024-05-23 19:56:48 +02:00
|
|
|
import { shortcuts } from '$lib/actions/shortcut';
|
|
|
|
|
import { focusOutside } from '$lib/actions/focus-outside';
|
2024-04-29 21:17:22 +00:00
|
|
|
import CircleIconButton from '$lib/components/elements/buttons/circle-icon-button.svelte';
|
2024-06-04 21:53:00 +02:00
|
|
|
import { t } from 'svelte-i18n';
|
2024-07-26 17:45:15 -04:00
|
|
|
import { generateId } from '$lib/utils/generate-id';
|
|
|
|
|
import { tick } from 'svelte';
|
2024-02-21 16:50:50 +01:00
|
|
|
|
2024-11-14 08:43:25 -06:00
|
|
|
interface Props {
|
|
|
|
|
value?: string;
|
|
|
|
|
grayTheme: boolean;
|
|
|
|
|
searchQuery?: MetadataSearchDto | SmartSearchDto;
|
|
|
|
|
}
|
2023-03-06 15:31:58 +01:00
|
|
|
|
2025-03-11 17:23:25 -05:00
|
|
|
let { value = $bindable(''), grayTheme, searchQuery = {} }: Props = $props();
|
2024-07-26 17:45:15 -04:00
|
|
|
|
2024-11-14 08:43:25 -06:00
|
|
|
let showClearIcon = $derived(value.length > 0);
|
2023-07-28 20:03:23 +02:00
|
|
|
|
2024-11-14 08:43:25 -06:00
|
|
|
let input = $state<HTMLInputElement>();
|
|
|
|
|
let searchHistoryBox = $state<ReturnType<typeof SearchHistoryBox>>();
|
|
|
|
|
let showSuggestions = $state(false);
|
|
|
|
|
let showFilter = $state(false);
|
|
|
|
|
let isSearchSuggestions = $state(false);
|
|
|
|
|
let selectedId: string | undefined = $state();
|
2025-03-10 03:20:25 +00:00
|
|
|
let isFocus = $state(false);
|
2024-07-26 17:45:15 -04:00
|
|
|
|
|
|
|
|
const listboxId = generateId();
|
2023-03-06 15:31:58 +01:00
|
|
|
|
2024-11-14 08:43:25 -06:00
|
|
|
const handleSearch = async (payload: SmartSearchDto | MetadataSearchDto) => {
|
2024-02-19 21:55:54 +01:00
|
|
|
const params = getMetadataSearchQuery(payload);
|
2023-03-06 15:31:58 +01:00
|
|
|
|
2024-07-26 17:45:15 -04:00
|
|
|
closeDropdown();
|
2024-02-17 11:00:55 -06:00
|
|
|
showFilter = false;
|
2023-09-26 04:53:26 +02:00
|
|
|
$isSearchEnabled = false;
|
2024-02-27 08:37:37 -08:00
|
|
|
await goto(`${AppRoute.SEARCH}?${params}`);
|
2024-02-17 11:00:55 -06:00
|
|
|
};
|
2023-03-23 17:57:49 -05:00
|
|
|
|
2023-07-28 20:03:23 +02:00
|
|
|
const clearSearchTerm = (searchTerm: string) => {
|
2024-11-14 08:43:25 -06:00
|
|
|
input?.focus();
|
2023-07-28 20:03:23 +02:00
|
|
|
$savedSearchTerms = $savedSearchTerms.filter((item) => item !== searchTerm);
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-01 00:50:47 -04:00
|
|
|
const saveSearchTerm = (saveValue: string) => {
|
2024-07-26 17:45:15 -04:00
|
|
|
const filteredSearchTerms = $savedSearchTerms.filter((item) => item.toLowerCase() !== saveValue.toLowerCase());
|
|
|
|
|
$savedSearchTerms = [saveValue, ...filteredSearchTerms];
|
2023-03-23 17:57:49 -05:00
|
|
|
|
2023-07-01 00:50:47 -04:00
|
|
|
if ($savedSearchTerms.length > 5) {
|
|
|
|
|
$savedSearchTerms = $savedSearchTerms.slice(0, 5);
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-03-23 17:57:49 -05:00
|
|
|
|
2023-07-28 20:03:23 +02:00
|
|
|
const clearAllSearchTerms = () => {
|
2024-11-14 08:43:25 -06:00
|
|
|
input?.focus();
|
2023-07-01 00:50:47 -04:00
|
|
|
$savedSearchTerms = [];
|
|
|
|
|
};
|
2023-07-19 11:03:23 -05:00
|
|
|
|
|
|
|
|
const onFocusIn = () => {
|
|
|
|
|
$isSearchEnabled = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onFocusOut = () => {
|
2024-10-23 18:21:17 +05:30
|
|
|
const focusOutTimer = setTimeout(() => {
|
|
|
|
|
if ($isSearchEnabled) {
|
|
|
|
|
$preventRaceConditionSearchBar = true;
|
|
|
|
|
}
|
2023-08-05 20:05:24 +02:00
|
|
|
|
2024-10-23 18:21:17 +05:30
|
|
|
closeDropdown();
|
|
|
|
|
$isSearchEnabled = false;
|
|
|
|
|
showFilter = false;
|
|
|
|
|
}, 100);
|
|
|
|
|
|
|
|
|
|
clearTimeout(focusOutTimer);
|
2024-02-17 11:00:55 -06:00
|
|
|
};
|
|
|
|
|
|
2024-02-27 08:37:37 -08:00
|
|
|
const onHistoryTermClick = async (searchTerm: string) => {
|
2024-07-26 17:45:15 -04:00
|
|
|
value = searchTerm;
|
2024-02-17 11:00:55 -06:00
|
|
|
const searchPayload = { query: searchTerm };
|
2024-11-14 08:43:25 -06:00
|
|
|
await handleSearch(searchPayload);
|
2024-02-17 11:00:55 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onFilterClick = () => {
|
|
|
|
|
showFilter = !showFilter;
|
|
|
|
|
value = '';
|
|
|
|
|
|
|
|
|
|
if (showFilter) {
|
2024-07-26 17:45:15 -04:00
|
|
|
closeDropdown();
|
2024-02-17 11:00:55 -06:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSubmit = () => {
|
2025-03-10 03:20:25 +00:00
|
|
|
const searchType = getSearchType();
|
|
|
|
|
let payload: SmartSearchDto | MetadataSearchDto = {} as SmartSearchDto | MetadataSearchDto;
|
|
|
|
|
|
|
|
|
|
switch (searchType) {
|
|
|
|
|
case 'smart': {
|
|
|
|
|
payload = { query: value } as SmartSearchDto;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'metadata': {
|
|
|
|
|
payload = { originalFileName: value } as MetadataSearchDto;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'description': {
|
|
|
|
|
payload = { description: value } as MetadataSearchDto;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handlePromiseError(handleSearch(payload));
|
2024-02-17 11:00:55 -06:00
|
|
|
saveSearchTerm(value);
|
2023-07-19 11:03:23 -05:00
|
|
|
};
|
2024-07-26 17:45:15 -04:00
|
|
|
|
|
|
|
|
const onClear = () => {
|
|
|
|
|
value = '';
|
2024-11-14 08:43:25 -06:00
|
|
|
input?.focus();
|
2024-07-26 17:45:15 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onEscape = () => {
|
|
|
|
|
closeDropdown();
|
|
|
|
|
showFilter = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onArrow = async (direction: 1 | -1) => {
|
|
|
|
|
openDropdown();
|
|
|
|
|
await tick();
|
2024-11-14 08:43:25 -06:00
|
|
|
searchHistoryBox?.moveSelection(direction);
|
2024-07-26 17:45:15 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onEnter = (event: KeyboardEvent) => {
|
|
|
|
|
if (selectedId) {
|
|
|
|
|
event.preventDefault();
|
2024-11-14 08:43:25 -06:00
|
|
|
searchHistoryBox?.selectActiveOption();
|
2024-07-26 17:45:15 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onInput = () => {
|
|
|
|
|
openDropdown();
|
2024-11-14 08:43:25 -06:00
|
|
|
searchHistoryBox?.clearSelection();
|
2024-07-26 17:45:15 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const openDropdown = () => {
|
|
|
|
|
showSuggestions = true;
|
2025-03-10 03:20:25 +00:00
|
|
|
isFocus = true;
|
2024-07-26 17:45:15 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const closeDropdown = () => {
|
|
|
|
|
showSuggestions = false;
|
2025-03-10 03:20:25 +00:00
|
|
|
isFocus = false;
|
2024-11-14 08:43:25 -06:00
|
|
|
searchHistoryBox?.clearSelection();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onsubmit = (event: Event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
onSubmit();
|
2024-07-26 17:45:15 -04:00
|
|
|
};
|
2025-03-10 03:20:25 +00:00
|
|
|
|
|
|
|
|
function getSearchType(): 'smart' | 'metadata' | 'description' {
|
|
|
|
|
const t = localStorage.getItem('searchQueryType');
|
|
|
|
|
return t === 'smart' || t === 'description' ? t : 'metadata';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getSearchTypeText(): string {
|
|
|
|
|
const searchType = getSearchType();
|
|
|
|
|
switch (searchType) {
|
|
|
|
|
case 'smart': {
|
|
|
|
|
return $t('context');
|
|
|
|
|
}
|
|
|
|
|
case 'metadata': {
|
|
|
|
|
return $t('filename');
|
|
|
|
|
}
|
|
|
|
|
case 'description': {
|
|
|
|
|
return $t('description');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-06 15:31:58 +01:00
|
|
|
</script>
|
|
|
|
|
|
2024-03-19 12:56:41 +00:00
|
|
|
<svelte:window
|
2024-04-08 19:20:24 +02:00
|
|
|
use:shortcuts={[
|
2024-07-26 17:45:15 -04:00
|
|
|
{ shortcut: { key: 'Escape' }, onShortcut: onEscape },
|
2024-11-14 08:43:25 -06:00
|
|
|
{ shortcut: { ctrl: true, key: 'k' }, onShortcut: () => input?.select() },
|
2024-04-08 19:20:24 +02:00
|
|
|
{ shortcut: { ctrl: true, shift: true, key: 'k' }, onShortcut: onFilterClick },
|
|
|
|
|
]}
|
2024-03-19 12:56:41 +00:00
|
|
|
/>
|
|
|
|
|
|
2024-08-23 12:34:12 -04:00
|
|
|
<div class="w-full relative" use:focusOutside={{ onFocusOut }} tabindex="-1">
|
2023-07-28 05:06:42 +02:00
|
|
|
<form
|
|
|
|
|
draggable="false"
|
|
|
|
|
autocomplete="off"
|
2024-02-21 16:50:50 +01:00
|
|
|
class="select-text text-sm"
|
2023-07-28 05:06:42 +02:00
|
|
|
action={AppRoute.SEARCH}
|
2024-11-14 08:43:25 -06:00
|
|
|
onreset={() => (value = '')}
|
|
|
|
|
{onsubmit}
|
|
|
|
|
onfocusin={onFocusIn}
|
2024-07-26 17:45:15 -04:00
|
|
|
role="search"
|
2023-07-28 05:06:42 +02:00
|
|
|
>
|
2024-08-23 12:34:12 -04:00
|
|
|
<div use:focusOutside={{ onFocusOut: closeDropdown }} tabindex="-1">
|
2024-07-26 17:45:15 -04:00
|
|
|
<label for="main-search-bar" class="sr-only">{$t('search_your_photos')}</label>
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
name="q"
|
|
|
|
|
id="main-search-bar"
|
2025-03-24 17:36:36 -04:00
|
|
|
class="w-full transition-all border-2 px-14 py-4 max-md:py-2 text-immich-fg/75 dark:text-immich-dark-fg
|
2024-07-26 17:45:15 -04:00
|
|
|
{grayTheme ? 'dark:bg-immich-dark-gray' : 'dark:bg-immich-dark-bg'}
|
2024-09-10 00:12:26 -04:00
|
|
|
{showSuggestions && isSearchSuggestions ? 'rounded-t-3xl' : 'rounded-3xl bg-gray-200'}
|
|
|
|
|
{$isSearchEnabled && !showFilter ? 'border-gray-200 dark:border-gray-700 bg-white' : 'border-transparent'}"
|
2024-07-26 17:45:15 -04:00
|
|
|
placeholder={$t('search_your_photos')}
|
|
|
|
|
required
|
|
|
|
|
pattern="^(?!m:$).*$"
|
|
|
|
|
bind:value
|
|
|
|
|
bind:this={input}
|
2024-11-14 08:43:25 -06:00
|
|
|
onfocus={openDropdown}
|
|
|
|
|
oninput={onInput}
|
2024-07-26 17:45:15 -04:00
|
|
|
disabled={showFilter}
|
|
|
|
|
role="combobox"
|
|
|
|
|
aria-controls={listboxId}
|
|
|
|
|
aria-activedescendant={selectedId ?? ''}
|
|
|
|
|
aria-expanded={showSuggestions && isSearchSuggestions}
|
|
|
|
|
aria-autocomplete="list"
|
|
|
|
|
use:shortcuts={[
|
|
|
|
|
{ shortcut: { key: 'Escape' }, onShortcut: onEscape },
|
|
|
|
|
{ shortcut: { ctrl: true, shift: true, key: 'k' }, onShortcut: onFilterClick },
|
|
|
|
|
{ shortcut: { key: 'ArrowUp' }, onShortcut: () => onArrow(-1) },
|
|
|
|
|
{ shortcut: { key: 'ArrowDown' }, onShortcut: () => onArrow(1) },
|
|
|
|
|
{ shortcut: { key: 'Enter' }, onShortcut: onEnter, preventDefault: false },
|
|
|
|
|
{ shortcut: { key: 'ArrowDown', alt: true }, onShortcut: openDropdown },
|
|
|
|
|
]}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<!-- SEARCH HISTORY BOX -->
|
|
|
|
|
<SearchHistoryBox
|
2024-11-14 08:43:25 -06:00
|
|
|
bind:this={searchHistoryBox}
|
|
|
|
|
bind:isSearchSuggestions
|
2024-07-26 17:45:15 -04:00
|
|
|
id={listboxId}
|
|
|
|
|
searchQuery={value}
|
|
|
|
|
isOpen={showSuggestions}
|
|
|
|
|
onClearAllSearchTerms={clearAllSearchTerms}
|
|
|
|
|
onClearSearchTerm={(searchTerm) => clearSearchTerm(searchTerm)}
|
|
|
|
|
onSelectSearchTerm={(searchTerm) => handlePromiseError(onHistoryTermClick(searchTerm))}
|
|
|
|
|
onActiveSelectionChange={(id) => (selectedId = id)}
|
|
|
|
|
/>
|
2024-04-29 21:17:22 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="absolute inset-y-0 {showClearIcon ? 'right-14' : 'right-2'} flex items-center pl-6 transition-all">
|
2024-11-14 08:43:25 -06:00
|
|
|
<CircleIconButton title={$t('show_search_options')} icon={mdiTune} onclick={onFilterClick} size="20" />
|
2024-04-29 21:17:22 +00:00
|
|
|
</div>
|
2025-03-10 03:20:25 +00:00
|
|
|
|
|
|
|
|
{#if isFocus}
|
|
|
|
|
<div
|
|
|
|
|
class="absolute inset-y-0 flex items-center"
|
|
|
|
|
class:right-16={isFocus}
|
|
|
|
|
class:right-28={isFocus && value.length > 0}
|
|
|
|
|
>
|
|
|
|
|
<p
|
|
|
|
|
class="bg-immich-primary text-white dark:bg-immich-dark-primary/90 dark:text-black/75 rounded-full px-3 py-1 text-xs z-10"
|
|
|
|
|
>
|
|
|
|
|
{getSearchTypeText()}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
|
|
|
|
|
2023-07-28 05:06:42 +02:00
|
|
|
{#if showClearIcon}
|
2024-04-29 21:17:22 +00:00
|
|
|
<div class="absolute inset-y-0 right-0 flex items-center pr-2">
|
2024-11-14 08:43:25 -06:00
|
|
|
<CircleIconButton onclick={onClear} icon={mdiClose} title={$t('clear')} size="20" />
|
2023-07-28 05:06:42 +02:00
|
|
|
</div>
|
|
|
|
|
{/if}
|
2024-07-26 17:45:15 -04:00
|
|
|
<div class="absolute inset-y-0 left-0 flex items-center pl-2">
|
2024-11-14 08:43:25 -06:00
|
|
|
<CircleIconButton
|
|
|
|
|
type="submit"
|
|
|
|
|
disabled={showFilter}
|
|
|
|
|
title={$t('search')}
|
|
|
|
|
icon={mdiMagnify}
|
|
|
|
|
size="20"
|
|
|
|
|
onclick={() => {}}
|
|
|
|
|
/>
|
2024-07-26 17:45:15 -04:00
|
|
|
</div>
|
2023-07-28 05:06:42 +02:00
|
|
|
</form>
|
2024-02-21 16:50:50 +01:00
|
|
|
|
|
|
|
|
{#if showFilter}
|
2024-11-14 08:43:25 -06:00
|
|
|
<SearchFilterModal
|
|
|
|
|
{searchQuery}
|
|
|
|
|
onSearch={(payload) => handleSearch(payload)}
|
|
|
|
|
onClose={() => (showFilter = false)}
|
|
|
|
|
/>
|
2024-02-21 16:50:50 +01:00
|
|
|
{/if}
|
2023-08-05 20:05:24 +02:00
|
|
|
</div>
|