aboutsummaryrefslogtreecommitdiff
path: root/src/components/SearchBar.vue
blob: 6da0f3693a7c5648e227efb2369b8270b805c4d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<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>