Loheide-eu-mitarbeiterplatt.../resources/js/components/GlobalSearchBox.vue
Friederich Loheide b276e1c6d0
Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m24s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m26s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m41s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m46s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m31s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m16s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m18s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m14s
Globale Live-Suche, Kontaktkarten-Personensuche, neue Beitragsansicht, Termine mit Seitenleiste, Demo-Inhalte, Plugin-Upload-Fix
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 19:07:13 +00:00

162 lines
6.5 KiB
Vue

<script setup>
import { computed, ref, watch } from 'vue';
import { Link, router } from '@inertiajs/vue3';
import { onClickOutside, useDebounceFn } from '@vueuse/core';
import { ArrowRight, LoaderCircle, Search } from '@lucide/vue';
import AppIcon from '@/components/AppIcon.vue';
import FileIcon from '@/components/FileIcon.vue';
import { http } from '@/lib/http';
import { highlight } from '@/lib/highlight';
/**
* Globale Live-Suche in der Kopfzeile (Neuigkeiten, Termine, Seiten, Downloads, QM …).
* Enter ohne Auswahl öffnet die Ergebnisseite /suche.
*/
const props = defineProps({
initial: { type: String, default: '' },
});
const root = ref(null);
const input = ref(null);
const query = ref(props.initial);
const groups = ref([]);
const loading = ref(false);
const open = ref(false);
const active = ref(-1);
let latest = 0;
const items = computed(() => groups.value.flatMap((g) => g.items));
const indexOf = (group, item) => items.value.indexOf(item);
const fetchResults = useDebounceFn(async (value) => {
const ticket = ++latest;
try {
const data = await http.get(`/suche/live?q=${encodeURIComponent(value)}`);
if (ticket === latest) {
groups.value = data.groups;
active.value = -1;
}
} catch {
if (ticket === latest) groups.value = [];
} finally {
if (ticket === latest) loading.value = false;
}
}, 220);
watch(query, (value) => {
if (value.trim().length < 2) {
latest++;
groups.value = [];
loading.value = false;
return;
}
loading.value = true;
open.value = true;
fetchResults(value.trim());
});
onClickOutside(root, () => (open.value = false));
function move(step) {
if (!items.value.length) return;
open.value = true;
active.value = (active.value + step + items.value.length) % items.value.length;
}
function go(item) {
open.value = false;
if (item.external) {
window.location.href = item.url;
} else {
router.visit(item.url);
}
}
function submit() {
const item = items.value[active.value];
if (item) return go(item);
if (query.value.trim().length < 2) return;
open.value = false;
router.get('/suche', { q: query.value.trim() });
}
const showPanel = computed(() => open.value && query.value.trim().length >= 2);
</script>
<template>
<div ref="root" class="relative">
<form role="search" @submit.prevent="submit">
<label class="relative block">
<span class="sr-only">Portal durchsuchen</span>
<Search class="pointer-events-none absolute top-1/2 left-3 size-4 -translate-y-1/2 text-muted-foreground" />
<input
ref="input"
v-model="query"
type="search"
placeholder="Portal durchsuchen …"
autocomplete="off"
role="combobox"
:aria-expanded="showPanel"
aria-controls="global-search-results"
class="h-10 w-full rounded-md border border-input bg-card pr-9 pl-9 text-sm shadow-xs placeholder:text-muted-foreground focus-visible:border-brand-400 focus-visible:ring-3 focus-visible:ring-ring/30 focus-visible:outline-none"
@focus="open = true"
@keydown.down.prevent="move(1)"
@keydown.up.prevent="move(-1)"
@keydown.esc="open = false"
/>
<LoaderCircle v-if="loading" class="absolute top-1/2 right-3 size-4 -translate-y-1/2 animate-spin text-muted-foreground" />
</label>
</form>
<div
v-if="showPanel"
id="global-search-results"
class="absolute top-full right-0 z-50 mt-2 max-h-[min(70vh,560px)] w-[min(92vw,30rem)] overflow-y-auto rounded-lg border bg-popover text-popover-foreground shadow-lg"
>
<template v-if="groups.length">
<section v-for="group in groups" :key="group.key" class="border-b last:border-b-0">
<h3 class="flex items-center gap-2 px-4 pt-3 pb-1 text-[11px] font-semibold tracking-wider text-muted-foreground uppercase">
<AppIcon :name="group.icon" class="size-3.5" /> {{ group.label }}
</h3>
<ul class="pb-2">
<li v-for="item in group.items" :key="item.url">
<component
:is="item.external ? 'a' : Link"
:href="item.url"
class="flex items-start gap-3 px-4 py-2 transition-colors"
:class="indexOf(group, item) === active ? 'bg-accent' : 'hover:bg-accent/60'"
@click="open = false"
@mouseenter="active = indexOf(group, item)"
>
<img v-if="item.image" :src="item.image" alt="" class="mt-0.5 size-10 shrink-0 rounded-md object-cover" />
<FileIcon v-else-if="item.file" :extension="item.file" size="sm" class="mt-0.5" />
<span class="min-w-0 flex-1">
<span class="block truncate text-sm font-medium" v-html="highlight(item.title, query)" />
<span v-if="item.meta" class="block truncate text-xs text-muted-foreground">{{ item.meta }}</span>
<span v-if="item.excerpt" class="mt-0.5 line-clamp-1 text-xs text-muted-foreground" v-html="highlight(item.excerpt, query)" />
</span>
</component>
</li>
</ul>
</section>
</template>
<p v-else-if="!loading" class="px-4 py-6 text-center text-sm text-muted-foreground">Keine Treffer für „{{ query.trim() }}“.</p>
<p v-else class="px-4 py-6 text-center text-sm text-muted-foreground">Suche läuft …</p>
<Link
:href="`/suche?q=${encodeURIComponent(query.trim())}`"
class="sticky bottom-0 flex items-center justify-between gap-2 border-t bg-muted/60 px-4 py-2.5 text-sm font-medium text-brand-text backdrop-blur hover:bg-muted"
@click="open = false"
>
Alle Ergebnisse für „{{ query.trim() }}“ <ArrowRight class="size-4" />
</Link>
</div>
</div>
</template>