Initial commit: Horux Despachos project
This commit is contained in:
45
apps/web/stores/periodo-store.ts
Normal file
45
apps/web/stores/periodo-store.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { create } from 'zustand';
|
||||
import { persist } from 'zustand/middleware';
|
||||
|
||||
interface PeriodoState {
|
||||
fechaInicio: string;
|
||||
fechaFin: string;
|
||||
setRango: (fechaInicio: string, fechaFin: string) => void;
|
||||
resetActual: () => void;
|
||||
}
|
||||
|
||||
/** Devuelve el primer y último día del mes en curso como ISO strings. */
|
||||
function actual(): { fechaInicio: string; fechaFin: string } {
|
||||
const d = new Date();
|
||||
const año = d.getFullYear();
|
||||
const mes = d.getMonth();
|
||||
const inicio = new Date(año, mes, 1);
|
||||
const fin = new Date(año, mes + 1, 0);
|
||||
return {
|
||||
fechaInicio: inicio.toISOString().split('T')[0],
|
||||
fechaFin: fin.toISOString().split('T')[0],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Selector global de periodo (rango de fechas) usado en `/despachos/*` para
|
||||
* filtrar métricas. Persistido en localStorage. Compatible con el componente
|
||||
* `<PeriodSelector />` de @horux/shared-ui (recibe fechaInicio/fechaFin como
|
||||
* strings YYYY-MM-DD).
|
||||
*/
|
||||
export const usePeriodoStore = create<PeriodoState>()(
|
||||
persist(
|
||||
(set) => ({
|
||||
...actual(),
|
||||
setRango: (fechaInicio, fechaFin) => set({ fechaInicio, fechaFin }),
|
||||
resetActual: () => set(actual()),
|
||||
}),
|
||||
{ name: 'periodo-despacho' },
|
||||
),
|
||||
);
|
||||
|
||||
/** Helper: extrae año y mes del fechaInicio para queries que usan ese formato. */
|
||||
export function añoMesFromFechaInicio(fechaInicio: string): { año: number; mes: number } {
|
||||
const d = new Date(fechaInicio + 'T00:00:00');
|
||||
return { año: d.getFullYear(), mes: d.getMonth() + 1 };
|
||||
}
|
||||
Reference in New Issue
Block a user