reduce schema manager query log to debug, fix useSearch

This commit is contained in:
dswbx
2025-06-12 09:23:31 +02:00
parent 88419548c7
commit c75f8d0937
2 changed files with 12 additions and 9 deletions

View File

@@ -333,7 +333,7 @@ export class SchemaManager {
if (config.force) {
try {
$console.log("[SchemaManager]", sql);
$console.debug("[SchemaManager]", sql);
await qb.execute();
} catch (e) {
throw new Error(`Failed to execute query: ${sql}: ${(e as any).message}`);

View File

@@ -2,7 +2,7 @@ import { decodeSearch, encodeSearch, mergeObject } from "core/utils";
import { isEqual, transform } from "lodash-es";
import { useLocation, useSearch as useWouterSearch } from "wouter";
import { type s, parse } from "core/object/schema";
import { useEffect, useState } from "react";
import { useEffect, useMemo, useState } from "react";
export type UseSearchOptions<Schema extends s.TAnySchema = s.TAnySchema> = {
defaultValue?: Partial<s.StaticCoerced<Schema>>;
@@ -18,21 +18,24 @@ export function useSearch<Schema extends s.TAnySchema = s.TAnySchema>(
const [value, setValue] = useState<s.StaticCoerced<Schema>>(
options?.defaultValue ?? ({} as any),
);
const _defaults = mergeObject(
// @ts-ignore
schema.template({ withOptional: true }),
options?.defaultValue ?? {},
);
const defaults = useMemo(() => {
return mergeObject(
// @ts-ignore
schema.template({ withOptional: true }),
options?.defaultValue ?? {},
);
}, [JSON.stringify({ schema, dflt: options?.defaultValue })]);
useEffect(() => {
const initial =
searchString.length > 0 ? decodeSearch(searchString) : (options?.defaultValue ?? {});
const v = parse(schema, Object.assign({}, _defaults, initial)) as any;
const v = parse(schema, Object.assign({}, defaults, initial)) as any;
setValue(v);
}, [searchString, JSON.stringify(options?.defaultValue), location]);
function set<Update extends Partial<s.StaticCoerced<Schema>>>(update: Update): void {
const search = getWithoutDefaults(Object.assign({}, value, update), _defaults);
const search = getWithoutDefaults(Object.assign({}, value, update), defaults);
const prepared = options?.beforeEncode?.(search) ?? search;
const encoded = encodeSearch(prepared, { encode: false });
navigate(location + (encoded.length > 0 ? "?" + encoded : ""));