Open History to see the last commit
1<script setup lang="ts">
2import { computed, ref, watch } from 'vue'
3import {
4 Link,
5 Outlet,
6 useMatchRoute,
7 useNavigate,
8 useRouterState,
9 useSearch,
10} from '@tanstack/vue-router'
11import { useDebouncer } from '@tanstack/vue-pacer/debouncer'
12import { useIsFetching, useQuery } from '@tanstack/vue-query'
13import { contactsQueries } from '../services/contacts/queries'
14import type { Ref } from 'vue'
15β
16const navigate = useNavigate()
17const matchRoute = useMatchRoute()
18const loading: Ref<boolean> = useRouterState({
19 select: (state) => state.isLoading,
20})
21β
22const q = useSearch({ from: '__root__', select: (search) => search.q })
23const search = ref(q.value ?? '')
24watch(q, (value) => {
25 search.value = value ?? ''
26})
27β
28const { data: contacts } = useQuery(() => contactsQueries.list(q.value))
29const fetchingContacts = useIsFetching({ queryKey: contactsQueries.lists() })
30const searching = computed(() => fetchingContacts.value > 0)
31β
32const searchDebouncer = useDebouncer(
33 (value: string) => {
34 navigate({ to: '.', search: { q: value || undefined } })
35 },
36 { wait: 500 },
37)
38</script>
39β
40<template>
41 <div id="sidebar">
42 <h1>TanStack Router Contacts</h1>
43 <div>
44 <form id="search-form" role="search" @submit.prevent>
45 <input
46 id="q"
47 v-model="search"
48 aria-label="Search contacts"
49 placeholder="Search"
50 type="search"
51 name="q"
52 autofocus
53 :class="searching ? 'loading' : ''"
54 @input="searchDebouncer.maybeExecute(search)"
55 />
56 <div id="search-spinner" aria-hidden="true" :hidden="!searching" />
57 <div class="sr-only" aria-live="polite" />
58 </form>
59 <Link to="/contacts/new" :search="{ q }" class="button">New</Link>
60 </div>
61 <nav>
62 <ul v-if="contacts?.length">
63 <li v-for="contact in contacts" :key="contact.id">
64 <Link
65 to="/contacts/$contactId"
66 :params="{ contactId: contact.id }"
67 :search="{ q }"
68 :active-props="{ class: 'active' }"
69 :class="
70 matchRoute({
71 to: '/contacts/$contactId',
72 params: { contactId: contact.id },
73 pending: true,
74 }).value
75 ? 'pending'
76 : ''
77 "
78 >
79 <template v-if="contact.first || contact.last">
80 {{ contact.first }} {{ contact.last }}
81 </template>
82 <i v-else>No Name</i>
83 {{ ' ' }}
84 <span v-if="contact.favorite">β
</span>
85 </Link>
86 </li>
87 </ul>
88 <p v-else>
89 <i>No contacts</i>
90 </p>
91 </nav>
92 </div>
93 <div id="detail" :class="loading ? 'loading' : ''">
94 <Outlet />
95 </div>
96</template>