feat(search): improve Search component with conditional rendering and debugging logs

- Added conditional rendering for the "No results found" message using the Show component.
- Introduced debugging logs in getUserImages and Search component to track data flow.
- Cleaned up the data mapping process in getUserImages for better readability.
This commit is contained in:
2025-04-14 10:54:18 +02:00
parent 0bc556f47c
commit a1369719d7
2 changed files with 24 additions and 15 deletions

View File

@ -6,6 +6,7 @@ import { listen } from "@tauri-apps/api/event";
import Fuse from "fuse.js"; import Fuse from "fuse.js";
import { import {
For, For,
Show,
createEffect, createEffect,
createResource, createResource,
createSignal, createSignal,
@ -55,12 +56,13 @@ export const Search = () => {
); );
const [data] = createResource(() => const [data] = createResource(() =>
getUserImages().then((data) => getUserImages().then((data) => {
data.map((d) => ({ console.log("DBG: ", data);
return data.map((d) => ({
...d, ...d,
rawData: getAllValues(d), rawData: getAllValues(d),
})), }));
), }),
); );
let fuze = new Fuse<UserImage>(data() ?? [], { let fuze = new Fuse<UserImage>(data() ?? [], {
@ -72,7 +74,9 @@ export const Search = () => {
}); });
createEffect(() => { createEffect(() => {
console.log("DBG: ", data());
setSearchResults(data() ?? []); setSearchResults(data() ?? []);
fuze = new Fuse<UserImage>(data() ?? [], { fuze = new Fuse<UserImage>(data() ?? [], {
keys: [ keys: [
{ name: "data.Name", weight: 2 }, { name: "data.Name", weight: 2 },
@ -157,7 +161,14 @@ export const Search = () => {
<div class="px-4 mt-4 bg-white rounded-t-2xl"> <div class="px-4 mt-4 bg-white rounded-t-2xl">
<div class="h-[254px] mt-4 overflow-scroll scrollbar-hide"> <div class="h-[254px] mt-4 overflow-scroll scrollbar-hide">
{searchResults().length > 0 ? ( <Show
when={searchResults().length > 0}
fallback={
<div class="text-center text-lg m-auto mt-6 text-neutral-700">
No results found
</div>
}
>
<div class="w-full grid grid-cols-9 gap-2 grid-flow-row-dense py-4"> <div class="w-full grid grid-cols-9 gap-2 grid-flow-row-dense py-4">
<For each={searchResults()}> <For each={searchResults()}>
{(item) => ( {(item) => (
@ -180,11 +191,7 @@ export const Search = () => {
)} )}
</For> </For>
</div> </div>
) : ( </Show>
<div class="text-center text-lg m-auto mt-6 text-neutral-700">
No results found
</div>
)}
</div> </div>
</div> </div>

View File

@ -3,13 +3,13 @@ import { fetch } from "@tauri-apps/plugin-http";
import { import {
type InferOutput, type InferOutput,
array, array,
literal,
nullable, nullable,
strictObject,
parse, parse,
pipe, pipe,
strictObject,
string, string,
uuid, uuid,
literal,
variant, variant,
} from "valibot"; } from "valibot";
@ -19,9 +19,7 @@ type BaseRequestParams = Partial<{
method: "GET" | "POST"; method: "GET" | "POST";
}>; }>;
export const base = import.meta.env.DEV export const base = "https://haystack.johncosta.tech";
? "http://localhost:3040"
: "https://haystack.johncosta.tech";
const getBaseRequest = ({ path, body, method }: BaseRequestParams): Request => { const getBaseRequest = ({ path, body, method }: BaseRequestParams): Request => {
return new Request(`${base}/${path}`, { return new Request(`${base}/${path}`, {
@ -134,8 +132,12 @@ export type UserImage = InferOutput<typeof dataTypeValidator>;
export const getUserImages = async (): Promise<UserImage[]> => { export const getUserImages = async (): Promise<UserImage[]> => {
const request = getBaseAuthorizedRequest({ path: "image" }); const request = getBaseAuthorizedRequest({ path: "image" });
console.log("DBG: ", request);
const res = await fetch(request).then((res) => res.json()); const res = await fetch(request).then((res) => res.json());
console.log("DBG: ", res);
return parse(getUserImagesResponseValidator, res); return parse(getUserImagesResponseValidator, res);
}; };