aboutsummaryrefslogtreecommitdiff
path: root/src/components/SearchBar.vue
diff options
context:
space:
mode:
authorGravatar Corentin THOMASSET <corentin.thomasset74@gmail.com> 2023-06-19 00:21:36 +0200
committerGravatar Corentin Thomasset <corentin.thomasset74@gmail.com> 2023-06-19 00:35:50 +0200
commitbcb98b359c1a7fa39ee2bd72776dbb0434de150e (patch)
treeb17f8970d34fd7cdb5393a3703fb5dcc8d7b375c /src/components/SearchBar.vue
parent732da08157a1bec9e203d40bba06cc67e28f29b4 (diff)
downloadit-tools-bcb98b359c1a7fa39ee2bd72776dbb0434de150e.tar.gz
it-tools-bcb98b359c1a7fa39ee2bd72776dbb0434de150e.tar.zst
it-tools-bcb98b359c1a7fa39ee2bd72776dbb0434de150e.zip
refactor(search): command palette design (#463)
Diffstat (limited to 'src/components/SearchBar.vue')
-rw-r--r--src/components/SearchBar.vue109
1 files changed, 0 insertions, 109 deletions
diff --git a/src/components/SearchBar.vue b/src/components/SearchBar.vue
deleted file mode 100644
index 6da0f36..0000000
--- a/src/components/SearchBar.vue
+++ /dev/null
@@ -1,109 +0,0 @@
-<script lang="ts" setup>
-import { SearchRound } from '@vicons/material';
-import { useMagicKeys, whenever } from '@vueuse/core';
-import { NInput } from 'naive-ui';
-import { useRouter } from 'vue-router';
-import SearchBarItem from './SearchBarItem.vue';
-import type { Tool } from '@/tools/tools.types';
-import { tools } from '@/tools';
-import { useTracker } from '@/modules/tracker/tracker.services';
-import { useFuzzySearch } from '@/composable/fuzzySearch';
-
-const toolToOption = (tool: Tool) => ({ label: tool.name, value: tool.path, tool });
-
-const router = useRouter();
-const { tracker } = useTracker();
-
-const queryString = ref('');
-const inputEl = ref<HTMLElement>();
-const displayDropDown = ref(true);
-const isMac = computed(() => window.navigator.userAgent.toLowerCase().includes('mac'));
-
-const { searchResult } = useFuzzySearch({
- search: queryString,
- data: tools,
- options: { keys: [{ name: 'name', weight: 2 }, 'description', 'keywords'] },
-});
-
-const options = computed(() => {
- if (queryString.value === '') {
- return tools.map(toolToOption);
- }
-
- return searchResult.value.map(toolToOption);
-});
-
-const keys = useMagicKeys({
- passive: false,
- onEventFired(e) {
- if (e.ctrlKey && e.key === 'k' && e.type === 'keydown') {
- e.preventDefault();
- }
-
- if (e.metaKey && e.key === 'k' && e.type === 'keydown') {
- e.preventDefault();
- }
- },
-});
-
-whenever(keys.ctrl_k, claimFocus);
-whenever(keys.meta_k, claimFocus);
-whenever(keys.escape, releaseFocus);
-
-function renderOption({ tool }: { tool: Tool }) {
- return h(SearchBarItem, { tool });
-}
-
-function onSelect(path: string) {
- router.push(path);
- queryString.value = '';
-}
-
-function claimFocus() {
- displayDropDown.value = true;
-
- inputEl.value?.focus();
-}
-
-function releaseFocus() {
- displayDropDown.value = false;
-}
-
-function onFocus() {
- tracker.trackEvent({ eventName: 'Search-bar focused' });
- displayDropDown.value = true;
-}
-</script>
-
-<template>
- <div class="search-bar">
- <n-auto-complete
- v-model:value="queryString"
- :options="options"
- :on-select="(value: string | number) => onSelect(String(value))"
- :render-label="renderOption"
- default-value="aa"
- :get-show="() => displayDropDown"
- :on-focus="onFocus"
- @update:value="() => (displayDropDown = true)"
- >
- <template #default="{ handleInput, handleBlur, handleFocus, value: slotValue }">
- <NInput
- ref="inputEl"
- round
- clearable
- :placeholder="`Search a tool (use ${isMac ? 'Cmd' : 'Ctrl'} + K to focus)`"
- :value="slotValue"
- :input-props="{ autocomplete: 'disabled' }"
- @input="handleInput"
- @focus="handleFocus"
- @blur="handleBlur"
- >
- <template #prefix>
- <n-icon :component="SearchRound" />
- </template>
- </NInput>
- </template>
- </n-auto-complete>
- </div>
-</template>