2023-03-06 15:31:58 +01:00
|
|
|
<script lang="ts">
|
2023-07-01 00:50:47 -04:00
|
|
|
import { AppRoute } from '$lib/constants';
|
2023-10-25 09:48:25 -04:00
|
|
|
import Icon from '$lib/components/elements/icon.svelte';
|
2023-07-01 00:50:47 -04:00
|
|
|
import { goto } from '$app/navigation';
|
2024-02-21 16:50:50 +01:00
|
|
|
import { isSearchEnabled, preventRaceConditionSearchBar, savedSearchTerms } from '$lib/stores/search.store';
|
2023-07-28 05:06:42 +02:00
|
|
|
import { clickOutside } from '$lib/utils/click-outside';
|
2024-02-02 13:30:40 -06:00
|
|
|
import { mdiClose, mdiMagnify, mdiTune } from '@mdi/js';
|
|
|
|
|
import IconButton from '$lib/components/elements/buttons/icon-button.svelte';
|
|
|
|
|
import SearchHistoryBox from './search-history-box.svelte';
|
|
|
|
|
import SearchFilterBox from './search-filter-box.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-21 16:50:50 +01:00
|
|
|
|
2023-07-01 00:50:47 -04:00
|
|
|
export let value = '';
|
|
|
|
|
export let grayTheme: boolean;
|
2024-02-21 16:50:50 +01:00
|
|
|
export let searchQuery: MetadataSearchDto | SmartSearchDto = {};
|
2023-03-06 15:31:58 +01:00
|
|
|
|
2023-07-28 20:03:23 +02:00
|
|
|
let input: HTMLInputElement;
|
|
|
|
|
|
2024-02-02 13:30:40 -06:00
|
|
|
let showHistory = false;
|
|
|
|
|
let showFilter = false;
|
2023-07-01 00:50:47 -04:00
|
|
|
$: showClearIcon = value.length > 0;
|
2023-03-06 15:31:58 +01:00
|
|
|
|
2024-02-17 11:00:55 -06:00
|
|
|
const onSearch = (payload: SmartSearchDto | MetadataSearchDto) => {
|
2024-02-19 21:55:54 +01:00
|
|
|
const params = getMetadataSearchQuery(payload);
|
2023-03-06 15:31:58 +01:00
|
|
|
|
2024-02-02 13:30:40 -06:00
|
|
|
showHistory = false;
|
2024-02-17 11:00:55 -06:00
|
|
|
showFilter = false;
|
2023-09-26 04:53:26 +02:00
|
|
|
$isSearchEnabled = false;
|
2024-02-21 16:50:50 +01:00
|
|
|
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) => {
|
|
|
|
|
input.focus();
|
|
|
|
|
$savedSearchTerms = $savedSearchTerms.filter((item) => item !== searchTerm);
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-01 00:50:47 -04:00
|
|
|
const saveSearchTerm = (saveValue: string) => {
|
|
|
|
|
$savedSearchTerms = [saveValue, ...$savedSearchTerms];
|
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 = () => {
|
|
|
|
|
input.focus();
|
2023-07-01 00:50:47 -04:00
|
|
|
$savedSearchTerms = [];
|
|
|
|
|
};
|
2023-07-19 11:03:23 -05:00
|
|
|
|
|
|
|
|
const onFocusIn = () => {
|
2024-02-02 13:30:40 -06:00
|
|
|
showHistory = true;
|
2023-07-19 11:03:23 -05:00
|
|
|
$isSearchEnabled = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onFocusOut = () => {
|
2023-08-05 20:05:24 +02:00
|
|
|
if ($isSearchEnabled) {
|
|
|
|
|
$preventRaceConditionSearchBar = true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-02 13:30:40 -06:00
|
|
|
showHistory = false;
|
2023-07-19 11:03:23 -05:00
|
|
|
$isSearchEnabled = false;
|
2024-02-17 11:00:55 -06:00
|
|
|
showFilter = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onHistoryTermClick = (searchTerm: string) => {
|
|
|
|
|
const searchPayload = { query: searchTerm };
|
|
|
|
|
onSearch(searchPayload);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onFilterClick = () => {
|
|
|
|
|
showFilter = !showFilter;
|
|
|
|
|
value = '';
|
|
|
|
|
|
|
|
|
|
if (showFilter) {
|
|
|
|
|
showHistory = false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSubmit = () => {
|
|
|
|
|
onSearch({ query: value });
|
|
|
|
|
saveSearchTerm(value);
|
2023-07-19 11:03:23 -05:00
|
|
|
};
|
2023-03-06 15:31:58 +01:00
|
|
|
</script>
|
|
|
|
|
|
2024-02-21 16:50:50 +01:00
|
|
|
<div class="w-full relative" use:clickOutside on:outclick={onFocusOut} on:escape={onFocusOut}>
|
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}
|
|
|
|
|
on:reset={() => (value = '')}
|
2024-02-17 11:00:55 -06:00
|
|
|
on:submit|preventDefault={onSubmit}
|
2023-07-28 05:06:42 +02:00
|
|
|
>
|
|
|
|
|
<label>
|
|
|
|
|
<div class="absolute inset-y-0 left-0 flex items-center pl-6">
|
2023-07-28 20:03:23 +02:00
|
|
|
<div class="dark:text-immich-dark-fg/75">
|
|
|
|
|
<button class="flex items-center">
|
2023-10-25 09:48:25 -04:00
|
|
|
<Icon path={mdiMagnify} size="1.5em" />
|
2023-07-28 20:03:23 +02:00
|
|
|
</button>
|
2023-07-28 05:06:42 +02:00
|
|
|
</div>
|
2023-07-01 00:50:47 -04:00
|
|
|
</div>
|
2023-07-28 05:06:42 +02:00
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
name="q"
|
2023-09-03 03:31:12 +02:00
|
|
|
class="w-full {grayTheme
|
2023-07-28 05:06:42 +02:00
|
|
|
? 'dark:bg-immich-dark-gray'
|
2024-02-02 13:30:40 -06:00
|
|
|
: 'dark:bg-immich-dark-bg'} px-14 py-4 text-immich-fg/75 dark:text-immich-dark-fg {showHistory || showFilter
|
2023-07-28 05:06:42 +02:00
|
|
|
? 'rounded-t-3xl border border-gray-200 bg-white dark:border-gray-800'
|
|
|
|
|
: 'rounded-3xl border border-transparent bg-gray-200'}"
|
|
|
|
|
placeholder="Search your photos"
|
|
|
|
|
required
|
|
|
|
|
pattern="^(?!m:$).*$"
|
|
|
|
|
bind:value
|
2023-07-28 20:03:23 +02:00
|
|
|
bind:this={input}
|
|
|
|
|
on:click={onFocusIn}
|
2024-02-02 13:30:40 -06:00
|
|
|
disabled={showFilter}
|
2023-07-28 05:06:42 +02:00
|
|
|
/>
|
2024-02-02 13:30:40 -06:00
|
|
|
|
2024-02-17 11:00:55 -06:00
|
|
|
<div class="absolute inset-y-0 {showClearIcon ? 'right-14' : 'right-5'} flex items-center pl-6 transition-all">
|
2024-02-02 13:30:40 -06:00
|
|
|
<div class="dark:text-immich-dark-fg/75">
|
2024-02-17 11:00:55 -06:00
|
|
|
<IconButton on:click={onFilterClick} title="Show search options">
|
2024-02-02 13:30:40 -06:00
|
|
|
<Icon path={mdiTune} size="1.5em" />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2023-07-28 05:06:42 +02:00
|
|
|
</label>
|
|
|
|
|
{#if showClearIcon}
|
|
|
|
|
<div class="absolute inset-y-0 right-0 flex items-center pr-4">
|
|
|
|
|
<button
|
|
|
|
|
type="reset"
|
|
|
|
|
class="rounded-full p-2 hover:bg-immich-primary/5 active:bg-immich-primary/10 dark:text-immich-dark-fg/75 dark:hover:bg-immich-dark-primary/25 dark:active:bg-immich-dark-primary/[.35]"
|
|
|
|
|
>
|
2023-10-25 09:48:25 -04:00
|
|
|
<Icon path={mdiClose} size="1.5em" />
|
2023-07-28 05:06:42 +02:00
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
|
|
|
|
|
2024-02-02 13:30:40 -06:00
|
|
|
<!-- SEARCH HISTORY BOX -->
|
|
|
|
|
{#if showHistory}
|
|
|
|
|
<SearchHistoryBox
|
|
|
|
|
on:clearAllSearchTerms={clearAllSearchTerms}
|
|
|
|
|
on:clearSearchTerm={({ detail: searchTerm }) => clearSearchTerm(searchTerm)}
|
2024-02-17 11:00:55 -06:00
|
|
|
on:selectSearchTerm={({ detail: searchTerm }) => onHistoryTermClick(searchTerm)}
|
2024-02-02 13:30:40 -06:00
|
|
|
/>
|
|
|
|
|
{/if}
|
2023-07-28 05:06:42 +02:00
|
|
|
</form>
|
2024-02-21 16:50:50 +01:00
|
|
|
|
|
|
|
|
{#if showFilter}
|
|
|
|
|
<SearchFilterBox {searchQuery} on:search={({ detail }) => onSearch(detail)} />
|
|
|
|
|
{/if}
|
2023-08-05 20:05:24 +02:00
|
|
|
</div>
|