All files / src/pages/search SearchPage.vue

93.93% Statements 62/66
83.33% Branches 35/42
100% Functions 16/16
93.54% Lines 58/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356                                                                1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 2x 4x     3x             3x 3x 3x 1x 1x 1x 1x 1x 1x 1x   2x 2x 2x 2x 2x 2x 2x 2x         2x     2x 2x 2x 2x             2x 2x           1x 1x           2x 3x       2x         6x     4x         1x     1x                 2x                     1x     1x     1x         1x     1x   1x                         2x                     2x                                                                                                                                                                                                                                                                                                                                                  
<script setup lang="ts">
import { computed, onMounted, ref, watch } from "vue";
import {
  RouterLink,
  useRoute,
  useRouter,
  type RouteLocationRaw,
} from "vue-router";
 
import type {
  ProductSearchPage,
  ProductSummary,
} from "@plain-journal/foundation";
import {
  PjButton,
  PjField,
  PjPageContainer,
  PjStatusNotice,
} from "@plain-journal/ui";
 
import {
  catalogApi,
  CatalogAsyncState,
  CatalogPagination,
  ProductGrid,
} from "../../entities/catalog";
import {
  pageCount,
  pageFromQuery,
  queryWithPage,
} from "../../shared/lib";
 
const PAGE_SIZE = 12;
const route = useRoute();
const router = useRouter();
const input = ref("");
const products = ref<ProductSummary[]>([]);
const matchedTotal = ref(0);
const degraded = ref(false);
const source = ref<ProductSearchPage["source"] | null>(null);
const loading = ref(false);
const error = ref<string | null>(null);
let requestSequence = 0;
const query = computed(() => typeof route.query.q === "string" ? route.query.q.trim() : "");
const currentPage = computed(() => pageFromQuery(route.query.page));
const totalPages = computed(() => pageCount(matchedTotal.value, PAGE_SIZE));
 
function pageLocation(page: number): RouteLocationRaw {
  return {
    name: "search",
    query: queryWithPage(route.query, page),
  };
}
 
async function search() {
  const requestId = ++requestSequence;
  input.value = query.value;
  if (!query.value) {
    products.value = [];
    matchedTotal.value = 0;
    degraded.value = false;
    source.value = null;
    error.value = null;
    loading.value = false;
    return;
  }
  loading.value = true;
  error.value = null;
  products.value = [];
  matchedTotal.value = 0;
  degraded.value = false;
  source.value = null;
  try {
    const result = await catalogApi.searchProducts({
      q: query.value,
      page: currentPage.value,
      size: PAGE_SIZE,
    });
    Iif (requestId !== requestSequence) {
      return;
    }
    products.value = result.items;
    matchedTotal.value = result.matchedTotal;
    degraded.value = result.degraded;
    source.value = result.source;
  } catch (cause) {
    if (requestId !== requestSequence) {
      return;
    }
    error.value = cause instanceof Error ? cause.message : "暂时无法完成查找。";
  } finally {
    Eif (requestId === requestSequence) {
      loading.value = false;
    }
  }
}
 
function submit() {
  const normalized = input.value.trim();
  router.push({
    name: "search",
    query: normalized ? { q: normalized } : {},
  });
}
 
onMounted(search);
watch(() => route.fullPath, search);
</script>
 
<template>
  <PjPageContainer as="section" class="search-canvas">
    <header class="search-intro">
      <p>查找</p>
      <h1>你正在寻找什么?</h1>
      <p>用名称、用途或材料开始;查询会保存在地址中,刷新后仍可恢复。</p>
    </header>
    <form class="search-form" role="search" @submit.prevent="submit">
      <PjField
        v-slot="{ describedBy }"
        label="商品名称或用途"
        for-id="site-search"
        hint="最多 80 个字符;提交新的关键词会回到第一页。"
      >
        <div class="search-form__control">
          <input
            id="site-search"
            v-model="input"
            type="search"
            maxlength="80"
            autocomplete="off"
            placeholder="例如:通勤包、书写纸品"
            :aria-describedby="describedBy"
          />
          <PjButton type="submit" variant="secondary">
            显示结果
          </PjButton>
        </div>
      </PjField>
    </form>
 
    <div v-if="!query" class="search-suggestions">
      <div>
        <p>没有确定名称时</p>
        <h2>从用途开始。</h2>
      </div>
      <div>
        <RouterLink :to="{ name: 'search', query: { q: '通勤' } }">通勤随行</RouterLink>
        <RouterLink :to="{ name: 'products', query: { category: 'writing' } }">
          书写纸品
        </RouterLink>
        <RouterLink :to="{ name: 'products', query: { category: 'carry' } }">
          随身用品
        </RouterLink>
      </div>
    </div>
 
    <section v-else class="search-results" aria-labelledby="search-results-title">
      <div class="search-results__heading">
        <div>
          <p>查找结果</p>
          <h2 id="search-results-title">“{{ query }}”</h2>
        </div>
        <p aria-live="polite">
          <strong>{{ matchedTotal }}</strong>
          <span>件匹配</span>
          <span v-if="totalPages > 1">第 {{ currentPage }} / {{ totalPages }} 页</span>
        </p>
      </div>
      <PjStatusNotice
        v-if="degraded"
        class="search-degraded"
        tone="warning"
        title="查找范围暂时收窄"
        :data-search-source="source"
      >
        <p>搜索索引暂时不可用,当前结果来自商品事实库的基础匹配。</p>
        <p>商品事实仍然有效,但排序与召回范围可能较窄。</p>
      </PjStatusNotice>
      <CatalogAsyncState
        :loading="loading"
        :error="error"
        :empty="!loading && !error && products.length === 0"
        empty-title="没有找到匹配商品。"
        empty-message="尝试更短的关键词,或返回全部商品。"
        @retry="search"
      >
        <ProductGrid :products="products" :heading-level="2" />
        <CatalogPagination
          :current-page="currentPage"
          :total-pages="totalPages"
          :to-page="pageLocation"
          label="查找结果分页"
        />
      </CatalogAsyncState>
    </section>
  </PjPageContainer>
</template>
 
<style scoped>
.search-canvas {
  padding-block: var(--pj-space-7) var(--pj-space-8);
}
 
.search-intro {
  max-width: 52rem;
  padding-block: var(--pj-space-5) var(--pj-space-7);
}
 
.search-intro > p:first-child,
.search-suggestions > div:first-child > p,
.search-results__heading > div > p {
  margin: 0 0 var(--pj-space-3);
  color: var(--pj-text-secondary);
  font-size: var(--pj-font-size-xs);
  font-weight: 650;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}
 
.search-intro h1 {
  margin: 0;
  max-width: 12ch;
  font-size: clamp(2.6rem, 6vw, 6rem);
  font-weight: 520;
  letter-spacing: -0.055em;
  line-height: 0.98;
}
 
.search-intro > p:last-child {
  max-width: 38rem;
  margin: var(--pj-space-5) 0 0;
  color: var(--pj-text-secondary);
  font-size: var(--pj-font-size-md);
}
 
.search-form {
  margin-bottom: var(--pj-space-8);
}
 
.search-form__control {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: var(--pj-space-4);
  border-bottom: 1px solid var(--pj-border-strong);
}
 
.search-form__control input {
  min-width: 0;
  padding: var(--pj-space-4) 0;
  border: 0;
  background: transparent;
  font-size: clamp(1.25rem, 2.5vw, 2rem);
}
 
.search-form__control input:focus {
  outline: none;
}
 
.search-form__control:focus-within {
  border-color: var(--pj-focus-ring);
  box-shadow: 0 2px var(--pj-focus-ring);
}
 
.search-suggestions {
  display: grid;
  grid-template-columns: minmax(12rem, 0.7fr) minmax(0, 1.3fr);
  gap: var(--pj-space-7);
  padding-block: var(--pj-space-7);
  border-top: 1px solid var(--pj-color-line);
}
 
.search-suggestions h2 {
  margin: 0;
  font-size: clamp(1.6rem, 3vw, 2.8rem);
  font-weight: 520;
  line-height: var(--pj-line-height-tight);
}
 
.search-suggestions > div:last-child {
  display: grid;
  align-content: start;
}
 
.search-suggestions a {
  display: flex;
  justify-content: space-between;
  gap: var(--pj-space-4);
  padding-block: var(--pj-space-4);
  border-bottom: 1px solid var(--pj-border-subtle);
  color: var(--pj-text-primary);
  text-decoration: none;
}
 
.search-suggestions a::after {
  content: "→";
}
 
.search-degraded {
  margin-bottom: var(--pj-space-6);
}
 
.search-results__heading {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  gap: var(--pj-space-6);
  margin-bottom: var(--pj-space-6);
}
 
.search-results__heading h2 {
  margin: 0;
  font-size: clamp(2rem, 4vw, 4rem);
  font-weight: 520;
  letter-spacing: -0.045em;
  line-height: 1;
}
 
.search-results__heading > p {
  display: grid;
  justify-items: end;
  gap: var(--pj-space-1);
  margin: 0;
  color: var(--pj-text-secondary);
  font-size: var(--pj-font-size-sm);
}
 
.search-results__heading > p strong {
  color: var(--pj-text-primary);
  font-size: var(--pj-font-size-xl);
  font-weight: 520;
}
 
@media (max-width: 32rem) {
  .search-form__control {
    grid-template-columns: 1fr;
    padding-bottom: var(--pj-space-3);
  }
 
  .search-form__control :deep(.pj-button) {
    width: 100%;
  }
 
  .search-suggestions,
  .search-results__heading {
    grid-template-columns: 1fr;
  }
 
  .search-results__heading > p {
    grid-template-columns: auto auto 1fr;
    justify-items: start;
    align-items: baseline;
  }
}
</style>