Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: broken query in simple index: #1568

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import {
type AssetIndexLoaderData,
} from "~/routes/_layout+/assets._index";
import { getCustomFieldDisplayValue } from "~/utils/custom-fields";
import { markSubstring } from "~/utils/mark-substring";
import { isLink } from "~/utils/misc";
import {
PermissionAction,
Expand Down Expand Up @@ -149,7 +148,7 @@ export function AdvancedIndexColumn({
className="font-medium underline hover:text-gray-600"
title={item.title}
>
{markSubstring(item.title)}
{item.title}
</Link>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/modules/asset/data.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export async function simpleModeLoader({
searchFieldLabel: "Search assets",
searchFieldTooltip: {
title: "Search your asset database",
text: "Search assets based on asset name or description, category, tag, location, custodian name. Simply separate your keywords by a space: 'Laptop lenovo 2020'.",
text: "Search assets based on asset name or description, category, tag, location, custodian name. Separate your keywords by a comma(,) to search with OR condition. For example: searching 'Laptop, lenovo, 2020' will find assets matching any of these terms.",
},
totalCategories,
totalTags,
Expand Down Expand Up @@ -357,7 +357,7 @@ export async function advancedModeLoader({
searchFieldLabel: "Search by asset name",
searchFieldTooltip: {
title: "Search your asset database",
text: "Search assets based on asset name Simply separate your keywords by a space: 'Laptop lenovo 2020'.",
text: "Search assets based on asset name. Separate your keywords by a comma(,) to search with OR condition. For example: searching 'Laptop, lenovo, 2020' will find assets matching any of these terms.",
},
filters,
organizationId,
Expand Down
19 changes: 16 additions & 3 deletions app/modules/asset/query.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,23 @@ export function generateWhereClause(
}

if (search) {
const words = search.trim().split(/\s+/).filter(Boolean);
const words = search
.trim()
.split(",")
.map((term) => term.trim())
.filter(Boolean);

if (words.length > 0) {
const searchPattern = `%${words.join("%")}%`;
whereClause = Prisma.sql`${whereClause} AND a."title" ILIKE ${searchPattern}`;
// Create OR conditions for each search term
const searchConditions = words.map(
(term) => Prisma.sql`a.title ILIKE ${`%${term}%`}`
);

// Combine all search terms with OR
whereClause = Prisma.sql`${whereClause} AND (${Prisma.join(
searchConditions,
" OR "
)})`;
}
}

Expand Down
3 changes: 2 additions & 1 deletion app/modules/asset/service.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ async function getAssets(params: {
const searchTerms = search
.toLowerCase()
.trim()
.split(/\s+/)
.split(",")
.map((term) => term.trim())
.filter(Boolean);

where.OR = searchTerms.map((term) => ({
Expand Down
3 changes: 1 addition & 2 deletions app/routes/_layout+/assets._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ import { checkExhaustiveSwitch } from "~/utils/check-exhaustive-switch";
import { sendNotification } from "~/utils/emitter/send-notification.server";
import { ShelfError, makeShelfError } from "~/utils/error";
import { data, error, parseData } from "~/utils/http.server";
import { markSubstring } from "~/utils/mark-substring";
import {
PermissionAction,
PermissionEntity,
Expand Down Expand Up @@ -416,7 +415,7 @@ const ListAssetContent = ({ item }: { item: AssetsFromViewItem }) => {
</div>
<div className="min-w-[130px]">
<span className="word-break mb-1 block font-medium">
{markSubstring(item.title)}
{item.title}
</span>
<div>
<AssetStatusBadge
Expand Down
17 changes: 16 additions & 1 deletion app/utils/mark-substring.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
/**
* This is temp deprecated until we find a better way to implement it.
*/

export function markSubstring(string: string) {
const searchParams = new URLSearchParams(window.location.search);

let content = <>{string}</>;

if (searchParams.has("s")) {
const searchQuery = searchParams.get("s")?.toLowerCase() as string;
const searchIndex = string.toLowerCase().indexOf(searchQuery);
const searchTerms = searchQuery.split(",").map((term) => term.trim());
let searchIndex = -1;
let currentSearchTerm = "";

for (const term of searchTerms) {
const index = string.toLowerCase().indexOf(term);
if (index !== -1 && (searchIndex === -1 || index < searchIndex)) {
searchIndex = index;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
currentSearchTerm = term;
}
}
if (searchIndex !== -1) {
const searchLength = searchQuery.length;
content = (
Expand Down
Loading
Loading