Files
sam-react-prod/src/hooks/useDateRange.ts
유병철 71352923c8 refactor(WEB): 회계/차량/결재 등 코드 중복 제거 및 공통 훅 추출
- useAccountingListPage, useDateRange 공통 훅 추출
- accounting/shared/ 공통 컴포넌트 분리
- 회계 모듈(입금/출금/매출/매입/청구 등) 중복 로직 통합
- 차량관리 page.tsx 패턴 간소화
- 건설/결재/자재/출하/단가 등 날짜 관련 코드 공통화
- 코드 중복 제거 체크리스트 문서 추가

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 19:01:41 +09:00

61 lines
1.9 KiB
TypeScript

'use client';
import { useState } from 'react';
import { getLocalDateString } from '@/lib/utils/date';
// ===== 프리셋 타입 =====
export type DateRangePreset = 'currentYear' | 'currentMonth' | 'today' | 'none';
// ===== 반환 타입 =====
export interface UseDateRangeReturn {
startDate: string;
endDate: string;
setStartDate: React.Dispatch<React.SetStateAction<string>>;
setEndDate: React.Dispatch<React.SetStateAction<string>>;
}
// ===== 프리셋별 초기값 계산 =====
function getInitialDates(preset: DateRangePreset): { start: string; end: string } {
const now = new Date();
switch (preset) {
case 'currentYear': {
const year = now.getFullYear();
return { start: `${year}-01-01`, end: `${year}-12-31` };
}
case 'currentMonth': {
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const lastDay = new Date(year, now.getMonth() + 1, 0).getDate();
return {
start: `${year}-${month}-01`,
end: `${year}-${month}-${String(lastDay).padStart(2, '0')}`,
};
}
case 'today': {
const today = getLocalDateString(now);
return { start: today, end: today };
}
case 'none':
return { start: '', end: '' };
}
}
/**
* 날짜 범위 상태 관리 훅
*
* @param preset - 초기 날짜 범위 프리셋
* - 'currentYear': 올해 1/1 ~ 12/31 (회계 관리 기본값)
* - 'currentMonth': 이번 달 1일 ~ 말일
* - 'today': 오늘 ~ 오늘 (로컬 시간대 기준)
* - 'none': 빈 문자열
*
* @example
* const { startDate, endDate, setStartDate, setEndDate } = useDateRange('currentYear');
*/
export function useDateRange(preset: DateRangePreset = 'currentYear'): UseDateRangeReturn {
const [startDate, setStartDate] = useState(() => getInitialDates(preset).start);
const [endDate, setEndDate] = useState(() => getInitialDates(preset).end);
return { startDate, endDate, setStartDate, setEndDate };
}