Initial commit - Horux Despachos NL

This commit is contained in:
2026-05-03 16:47:53 -06:00
commit b00b677c54
647 changed files with 133843 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
export * from './use-debounce';
export * from './use-table-sort';

View File

@@ -0,0 +1,17 @@
import { useState, useEffect } from 'react';
export function useDebounce<T>(value: T, delay: number): T {
const [debouncedValue, setDebouncedValue] = useState<T>(value);
useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(handler);
};
}, [value, delay]);
return debouncedValue;
}

View File

@@ -0,0 +1,56 @@
'use client';
import { useMemo, useState } from 'react';
export type SortDir = 'asc' | 'desc';
/**
* Ordenamiento client-side de una tabla.
*
* Uso:
* const { sortedData, sortKey, sortDir, toggleSort, getSortIndicator } = useTableSort(
* data,
* {
* fecha: (row) => new Date(row.fechaEmision).getTime(),
* total: (row) => Number(row.totalMxn || 0),
* },
* 'fecha',
* );
*/
export function useTableSort<T, K extends string>(
data: T[] | undefined,
accessors: Record<K, (row: T) => number | string>,
initialKey: K,
initialDir: SortDir = 'desc',
) {
const [sortKey, setSortKey] = useState<K>(initialKey);
const [sortDir, setSortDir] = useState<SortDir>(initialDir);
const sortedData = useMemo(() => {
if (!data) return data;
const accessor = accessors[sortKey];
const sign = sortDir === 'asc' ? 1 : -1;
return [...data].sort((a, b) => {
const va = accessor(a);
const vb = accessor(b);
if (typeof va === 'number' && typeof vb === 'number') {
return (va - vb) * sign;
}
return String(va).localeCompare(String(vb)) * sign;
});
}, [data, sortKey, sortDir, accessors]);
const toggleSort = (key: K) => {
if (sortKey === key) {
setSortDir(sortDir === 'asc' ? 'desc' : 'asc');
} else {
setSortKey(key);
setSortDir('desc');
}
};
/** Retorna 'asc' | 'desc' | null según el header sea activo o no */
const getSortIndicator = (key: K): SortDir | null => (sortKey === key ? sortDir : null);
return { sortedData, sortKey, sortDir, toggleSort, getSortIndicator };
}