Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+71
-17
@@ -1,21 +1,75 @@
|
||||
import type { LoaderFunctionArgs } from "react-router";
|
||||
import { runGlobalSearch, type GlobalSearchEntityType } from "~/services/search/globalSearch.server";
|
||||
|
||||
const entityTypes = new Set<GlobalSearchEntityType>(["contact", "company", "lead", "project", "file", "task", "user"]);
|
||||
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
|
||||
import {
|
||||
GlobalSearchInputError,
|
||||
GlobalSearchUnavailableError,
|
||||
loadSearchContext,
|
||||
recordCommittedSearch,
|
||||
runGlobalSearch,
|
||||
} from "~/services/search/globalSearch.server";
|
||||
import { validateCSRFToken } from "~/utils/session.server";
|
||||
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
const url = new URL(request.url);
|
||||
const requestedTypes = url.searchParams
|
||||
.getAll("type")
|
||||
.filter((value): value is GlobalSearchEntityType => entityTypes.has(value as GlobalSearchEntityType));
|
||||
return Response.json(
|
||||
await runGlobalSearch({
|
||||
request,
|
||||
params,
|
||||
query: url.searchParams.get("q") ?? "",
|
||||
limit: Number(url.searchParams.get("limit") ?? 8),
|
||||
entityTypes: requestedTypes,
|
||||
recordRecent: false,
|
||||
})
|
||||
);
|
||||
try {
|
||||
return Response.json(
|
||||
await runGlobalSearch({
|
||||
request,
|
||||
params,
|
||||
query: url.searchParams.get("q") ?? "",
|
||||
limit: Number(url.searchParams.get("limit") ?? 8),
|
||||
entityTypes: url.searchParams.getAll("type"),
|
||||
requestId: url.searchParams.get("requestId"),
|
||||
})
|
||||
);
|
||||
} catch (error) {
|
||||
if (error instanceof GlobalSearchUnavailableError) {
|
||||
return Response.json({ error: "search_unavailable" }, { status: 503 });
|
||||
}
|
||||
return handleInputError(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
const ctx = await loadSearchContext({ request, params });
|
||||
if (!hasFormContentType(request)) {
|
||||
return Response.json({ error: "unsupported_media_type" }, { status: 415 });
|
||||
}
|
||||
|
||||
let formData: FormData;
|
||||
try {
|
||||
formData = await request.formData();
|
||||
} catch {
|
||||
return Response.json({ error: "invalid_form_data" }, { status: 400 });
|
||||
}
|
||||
await validateCSRFToken(request, { formData });
|
||||
if (formData.get("intent") !== "recordRecent") {
|
||||
return Response.json({ error: "invalid_intent" }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await recordCommittedSearch({
|
||||
ctx,
|
||||
query: formData.get("query"),
|
||||
entityTypes: formData.getAll("type"),
|
||||
resultCount: formData.get("resultCount"),
|
||||
selectedEntityType: formData.get("selectedEntityType"),
|
||||
selectedEntityId: formData.get("selectedEntityId"),
|
||||
});
|
||||
return Response.json({ ok: true, ...result });
|
||||
} catch (error) {
|
||||
return handleInputError(error);
|
||||
}
|
||||
};
|
||||
|
||||
function hasFormContentType(request: Request): boolean {
|
||||
const contentType = request.headers.get("content-type")?.toLowerCase() ?? "";
|
||||
const mediaType = contentType.split(";", 1)[0]?.trim();
|
||||
return mediaType === "application/x-www-form-urlencoded" || mediaType === "multipart/form-data";
|
||||
}
|
||||
|
||||
function handleInputError(error: unknown): Response {
|
||||
if (error instanceof GlobalSearchInputError) {
|
||||
return Response.json({ error: error.code }, { status: 400 });
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
Reference in new issue
Block a user