From 4bf50a0b461fd554421e1ffa90f141c290b2d417 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 23 Mar 2023 17:57:49 -0500 Subject: [PATCH] feat(web): better search bar (#2062) --- .../search-bar/search-bar.svelte | 85 +++++++++++++++++-- web/src/lib/stores/search.store.ts | 5 ++ 2 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 web/src/lib/stores/search.store.ts diff --git a/web/src/lib/components/shared-components/search-bar/search-bar.svelte b/web/src/lib/components/shared-components/search-bar/search-bar.svelte index 13201fb887..4f77c40dd8 100644 --- a/web/src/lib/components/shared-components/search-bar/search-bar.svelte +++ b/web/src/lib/components/shared-components/search-bar/search-bar.svelte @@ -3,7 +3,9 @@ import Magnify from 'svelte-material-icons/Magnify.svelte'; import Close from 'svelte-material-icons/Close.svelte'; import { goto } from '$app/navigation'; - + import { savedSearchTerms } from '$lib/stores/search.store'; + import { clickOutside } from '$lib/utils/click-outside'; + import { fly } from 'svelte/transition'; export let value = ''; export let grayTheme: boolean; @@ -11,24 +13,53 @@ // of having to go through every search query. export let replaceHistoryState = false; + let showBigSearchBar = false; $: showClearIcon = value.length > 0; - function onSearch() { + function onSearch(saveSearch: boolean) { + let clipSearch = 'true'; + let searchValue = value; + + if (value.slice(0, 2) == 'm:') { + clipSearch = 'false'; + searchValue = value.slice(2); + } + + if (saveSearch) { + saveSearchTerm(value); + } + const params = new URLSearchParams({ q: value, - clip: 'true' + clip: clipSearch }); goto(`${AppRoute.SEARCH}?${params}`, { replaceState: replaceHistoryState }); } + + const saveSearchTerm = (saveValue: string) => { + $savedSearchTerms = [saveValue, ...$savedSearchTerms]; + + if ($savedSearchTerms.length > 5) { + $savedSearchTerms = $savedSearchTerms.slice(0, 5); + } + }; + + const clearSearchTerm = () => { + $savedSearchTerms = []; + };
(value = '')} - on:submit|preventDefault={onSearch} + on:submit|preventDefault={() => onSearch(true)} + on:focusin={() => (showBigSearchBar = true)} + use:clickOutside + on:outclick={() => (showBigSearchBar = false)} >