125 lines
5.8 KiB
TypeScript
125 lines
5.8 KiB
TypeScript
import { Form, Link, useLoaderData, type LoaderFunctionArgs } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import { SearchIcon } from "~/components/ui/icons/vendor";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
import { runGlobalSearch, type GlobalSearchEntityType, type GlobalSearchResponse } from "~/services/search/globalSearch.server";
|
|
|
|
const SEARCH_MODULE_KEY = "search";
|
|
const entityTypes: GlobalSearchEntityType[] = ["contact", "company", "lead", "project", "file", "task", "user"];
|
|
type SearchLoaderData = GlobalSearchResponse & { selectedTypes: GlobalSearchEntityType[] };
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const url = new URL(request.url);
|
|
const selectedTypes = url.searchParams
|
|
.getAll("type")
|
|
.filter((value): value is GlobalSearchEntityType => entityTypes.includes(value as GlobalSearchEntityType));
|
|
const response = await runGlobalSearch({
|
|
request,
|
|
params,
|
|
query: url.searchParams.get("q") ?? "",
|
|
limit: 50,
|
|
entityTypes: selectedTypes,
|
|
});
|
|
return Response.json({ ...response, selectedTypes });
|
|
};
|
|
|
|
export default function GlobalSearchRoute() {
|
|
const data = useLoaderData<SearchLoaderData>();
|
|
const { t } = useTranslation();
|
|
const activeTypes = new Set(data.selectedTypes);
|
|
|
|
return (
|
|
<main className="mx-auto flex w-full max-w-5xl flex-col gap-6 px-6 py-6">
|
|
<header className="flex flex-col gap-2">
|
|
<h1 className="text-text-neutral-primary text-2xl font-semibold">{t("search.title")}</h1>
|
|
<p className="text-text-neutral-secondary text-sm">{t("search.latencyHint")}</p>
|
|
</header>
|
|
|
|
<Form method="get" className="flex flex-col gap-3 rounded-md border border-border-neutral bg-default-background px-4 py-4 shadow-sm">
|
|
<label className="text-text-neutral-secondary text-sm font-medium" htmlFor="global-search-q">
|
|
{t("search.form.label")}
|
|
</label>
|
|
<div className="flex items-center gap-2 rounded-md border border-border-neutral px-3 py-2 focus-within:border-border-brand focus-within:ring-2 focus-within:ring-brand-200">
|
|
<SearchIcon className="text-text-neutral-tertiary size-4" />
|
|
<input
|
|
id="global-search-q"
|
|
name="q"
|
|
defaultValue={data.query}
|
|
className="text-text-neutral-primary min-w-0 flex-1 bg-transparent text-sm outline-none"
|
|
placeholder={t("search.form.placeholder")}
|
|
/>
|
|
<button type="submit" className="rounded-md bg-neutral-900 px-3 py-1.5 text-sm font-medium text-white">
|
|
{t("search.form.submit")}
|
|
</button>
|
|
</div>
|
|
<div className="flex flex-wrap gap-2">
|
|
{entityTypes.map((type) => (
|
|
<label key={type} className="inline-flex items-center gap-2 rounded-md border border-border-neutral px-3 py-1.5 text-sm">
|
|
<input type="checkbox" name="type" value={type} defaultChecked={activeTypes.size === 0 || activeTypes.has(type)} />
|
|
{t(`search.entities.${type}`)}
|
|
</label>
|
|
))}
|
|
</div>
|
|
</Form>
|
|
|
|
{!data.query ? (
|
|
<section className="flex flex-col gap-3">
|
|
<h2 className="text-text-neutral-primary text-base font-semibold">{t("search.recent.title")}</h2>
|
|
{data.recentSearches.length === 0 ? (
|
|
<p className="text-text-neutral-secondary text-sm">{t("search.recent.empty")}</p>
|
|
) : (
|
|
<div className="flex flex-wrap gap-2">
|
|
{data.recentSearches.map((recent) => (
|
|
<Link key={recent.id} to={`?q=${encodeURIComponent(recent.queryText)}`} className="rounded-md border border-border-neutral px-3 py-1.5 text-sm">
|
|
{recent.queryText}
|
|
</Link>
|
|
))}
|
|
</div>
|
|
)}
|
|
</section>
|
|
) : (
|
|
<section className="flex flex-col gap-3">
|
|
<div className="flex items-center justify-between gap-3">
|
|
<h2 className="text-text-neutral-primary text-base font-semibold">{t("search.results.title", { count: data.results.length })}</h2>
|
|
{data.hiddenEntityTypes.length > 0 ? <p className="text-text-neutral-tertiary text-xs">{t("search.results.hidden")}</p> : null}
|
|
</div>
|
|
{data.results.length === 0 ? (
|
|
<p className="text-text-neutral-secondary rounded-md border border-border-neutral px-4 py-6 text-sm">{t("search.results.empty")}</p>
|
|
) : (
|
|
<div className="divide-y divide-border-neutral rounded-md border border-border-neutral bg-default-background">
|
|
{data.results.map((result) => (
|
|
<Link key={`${result.entityType}:${result.id}`} to={result.href} className="flex items-center justify-between gap-4 px-4 py-3 hover:bg-neutral-50">
|
|
<div className="min-w-0">
|
|
<div className="text-text-neutral-primary truncate text-sm font-medium">{highlightMatch(result.title, data.query)}</div>
|
|
<div className="text-text-neutral-secondary truncate text-xs">{highlightMatch(result.subtitle, data.query)}</div>
|
|
</div>
|
|
<span className="text-text-neutral-tertiary shrink-0 text-xs">{t(`search.entities.${result.entityType}`)}</span>
|
|
</Link>
|
|
))}
|
|
</div>
|
|
)}
|
|
</section>
|
|
)}
|
|
</main>
|
|
);
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: SEARCH_MODULE_KEY };
|
|
|
|
function highlightMatch(value: string, query: string) {
|
|
if (!query) return value;
|
|
const index = value.toLocaleLowerCase().indexOf(query.toLocaleLowerCase());
|
|
if (index < 0) return value;
|
|
return (
|
|
<>
|
|
{value.slice(0, index)}
|
|
<mark className="rounded bg-yellow-100 px-0.5 text-inherit">{value.slice(index, index + query.length)}</mark>
|
|
{value.slice(index + query.length)}
|
|
</>
|
|
);
|
|
}
|