260506_01_fst: First Commit
This commit is contained in:
230
src/components/chart/chart-options.ts
Normal file
230
src/components/chart/chart-options.ts
Normal file
@@ -0,0 +1,230 @@
|
||||
import {ChartData, NewNumber} from "@/components/chart/types";
|
||||
import {AXIS_HIDE_Y_STYLE, BAR_WIDTHS, CHART_GRID_DEFAULTS} from "@/components/chart/chart.config";
|
||||
|
||||
export function reverseCategories<T extends ChartData>(data: T): T {
|
||||
const categories = [...data.categories].reverse();
|
||||
|
||||
const series = data.series.map((s) => ({
|
||||
...s,
|
||||
data: [...s.data].reverse(),
|
||||
}));
|
||||
|
||||
return { ...data, categories, series };
|
||||
}
|
||||
|
||||
export function reverseSeries<T extends ChartData>(data: T): T {
|
||||
return {
|
||||
...data,
|
||||
series: [...data.series].reverse(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 기본 Tooltip 옵션 생성
|
||||
* @param valueFormatter - 값 포맷터 함수 (기본값: 정수는 그대로, 소수는 toLocaleString)
|
||||
* @param overrides - 추가/덮어쓸 옵션
|
||||
*/
|
||||
export function createDefaultTooltip(
|
||||
valueFormatter?: (v: number) => string,
|
||||
overrides?: any
|
||||
): any {
|
||||
const defaultFmt = (v: number) =>
|
||||
Number.isInteger(v) ? String(v) : v.toLocaleString();
|
||||
|
||||
return {
|
||||
trigger: "axis",
|
||||
axisPointer: { type: "shadow" },
|
||||
valueFormatter: (v: unknown) =>
|
||||
typeof v === "number" ? (valueFormatter ?? defaultFmt)(v) : String(v),
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 기본 Legend 옵션 생성
|
||||
* @param overrides - 추가/덮어쓸 옵션
|
||||
*/
|
||||
export function createDefaultLegend(
|
||||
overrides?: any
|
||||
): any {
|
||||
return {
|
||||
...overrides,
|
||||
top: 0,
|
||||
};
|
||||
}
|
||||
|
||||
export function createReverseLegend(
|
||||
originalData: ChartData,
|
||||
overrides?: any
|
||||
): any {
|
||||
const order = originalData.series.map((s) => s.name);
|
||||
|
||||
return createDefaultLegend({
|
||||
...overrides,
|
||||
data: order,
|
||||
});
|
||||
}
|
||||
|
||||
export function createReverseTooltip(
|
||||
originalData: ChartData,
|
||||
valueFormatter?: (v: number) => string,
|
||||
overrides?: any
|
||||
): any {
|
||||
const order = originalData.series.map((s) => s.name);
|
||||
|
||||
// 기본 tooltip 생성(내부 defaultFmt 포함)
|
||||
const base = createDefaultTooltip(valueFormatter, overrides);
|
||||
const fmt = base.valueFormatter ?? ((v: unknown) => String(v));
|
||||
|
||||
const extractVal = (p: any): unknown => {
|
||||
const v = p?.value;
|
||||
if (typeof v === "number") return v;
|
||||
if (Array.isArray(v)) return v[v.length - 1]; // dataset/encode 케이스 대비
|
||||
if (typeof p?.data === "number") return p.data;
|
||||
if (p?.data && typeof p.data.value === "number") return p.data.value;
|
||||
return v;
|
||||
};
|
||||
|
||||
return {
|
||||
...base,
|
||||
formatter: (params: any) => {
|
||||
const arr = Array.isArray(params) ? params : [params];
|
||||
if (arr.length === 0) return "";
|
||||
|
||||
const title = arr[0]?.axisValueLabel ?? arr[0]?.name ?? "";
|
||||
|
||||
const byName = new Map<string, any>();
|
||||
for (const p of arr) byName.set(p.seriesName, p);
|
||||
|
||||
const lines = order
|
||||
.map((name) => byName.get(name))
|
||||
.filter(Boolean)
|
||||
.map((p) => {
|
||||
const v = extractVal(p);
|
||||
const printed =
|
||||
typeof v === "number" ? fmt(v) : v == null ? "-" : String(v);
|
||||
return `${p.marker ?? ""}${p.seriesName} ${printed}`;
|
||||
});
|
||||
|
||||
return [title, ...lines].join("<br/>");
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 기본 Grid 옵션 생성
|
||||
* @param preset - 프리셋 타입 (default | compact)
|
||||
* @param overrides - 추가/덮어쓸 옵션
|
||||
*/
|
||||
export function createDefaultGrid(
|
||||
preset: "default" | "compact" = "default",
|
||||
overrides?: any
|
||||
): any {
|
||||
const base =
|
||||
preset === "compact"
|
||||
? CHART_GRID_DEFAULTS.COMPACT
|
||||
: CHART_GRID_DEFAULTS.DEFAULT;
|
||||
|
||||
return {
|
||||
...base,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Category Axis 옵션 생성
|
||||
* @param data - 카테고리 데이터 배열
|
||||
* @param hideStyle - 축 스타일 숨김 여부 (라벨, 틱, 라인 등)
|
||||
* @param overrides - 추가/덮어쓸 옵션
|
||||
*/
|
||||
export function createCategoryAxis(
|
||||
data: string[],
|
||||
hideStyle?: boolean,
|
||||
overrides?: any
|
||||
): any {
|
||||
const base: any = {
|
||||
type: "category",
|
||||
data,
|
||||
};
|
||||
|
||||
if (hideStyle) {
|
||||
return {
|
||||
...base,
|
||||
axisLabel: { show: false },
|
||||
axisTick: { show: false },
|
||||
axisLine: { show: false },
|
||||
splitLine: { show: false },
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...base,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Value Axis 옵션 생성
|
||||
* @param hideStyle - 축 스타일 숨김 여부 (라벨, 틱, 라인 등)
|
||||
* @param overrides - 추가/덮어쓸 옵션
|
||||
*/
|
||||
export function createValueAxis(
|
||||
hideStyle?: boolean,
|
||||
overrides?: any
|
||||
): any {
|
||||
const base: any = {
|
||||
type: "value",
|
||||
};
|
||||
|
||||
if (hideStyle) {
|
||||
return {
|
||||
...base,
|
||||
...AXIS_HIDE_Y_STYLE,
|
||||
splitLine: { show: false },
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...base,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Bar 시리즈 기본 옵션 생성
|
||||
* @param name - 시리즈 이름
|
||||
* @param data - 데이터 배열
|
||||
* @param options - 추가 옵션 (stack, barMaxWidth 등)
|
||||
*/
|
||||
export function createBarSeries(
|
||||
name: string,
|
||||
data: NewNumber[],
|
||||
options?: {
|
||||
stack?: string;
|
||||
barMaxWidth?: number;
|
||||
emphasis?: boolean;
|
||||
label?: object;
|
||||
}
|
||||
): {
|
||||
name: string;
|
||||
type: "bar";
|
||||
data: NewNumber[];
|
||||
stack?: string;
|
||||
barMaxWidth?: number;
|
||||
emphasis?: { disabled: boolean };
|
||||
label?: object;
|
||||
} {
|
||||
const { stack, barMaxWidth, emphasis = false, label } = options ?? {};
|
||||
|
||||
return {
|
||||
name,
|
||||
type: "bar",
|
||||
data,
|
||||
...(stack && { stack }),
|
||||
barMaxWidth: barMaxWidth ?? BAR_WIDTHS.DEFAULT,
|
||||
emphasis: { disabled: !emphasis },
|
||||
...(label && { label }),
|
||||
};
|
||||
}
|
||||
70
src/components/chart/chart.config.ts
Normal file
70
src/components/chart/chart.config.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* 차트 기본 높이 설정
|
||||
*/
|
||||
export const CHART_HEIGHTS = {
|
||||
DEFAULT: "100%",
|
||||
COMPACT: 200,
|
||||
EXPANDED: 320,
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* Bar 차트의 Bar 너비 설정
|
||||
*/
|
||||
export const BAR_WIDTHS = {
|
||||
DEFAULT: 42,
|
||||
NARROW: 30,
|
||||
WIDE: 60,
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* 차트 Grid 기본 패딩 설정
|
||||
*/
|
||||
export const CHART_GRID_DEFAULTS = {
|
||||
DEFAULT: {
|
||||
left: 16,
|
||||
right: 16,
|
||||
top: 36,
|
||||
bottom: 12,
|
||||
containLabel: true,
|
||||
},
|
||||
COMPACT: {
|
||||
left: 10,
|
||||
right: 10,
|
||||
top: 20,
|
||||
bottom: 8,
|
||||
containLabel: true,
|
||||
},
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* Legend 아이템 크기 설정 (GroupedOverlayBar, GroupedStackBar에서 사용)
|
||||
*/
|
||||
export const LEGEND_ITEM_SIZE = {
|
||||
WIDTH: 25,
|
||||
HEIGHT: 14,
|
||||
RADIUS: 4,
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* 테이블 크기 설정
|
||||
*/
|
||||
export const TABLE_DIMENSIONS = {
|
||||
CELL_HEIGHT: {
|
||||
DEFAULT: 26,
|
||||
COMPACT: 20,
|
||||
EXPANDED: 32,
|
||||
},
|
||||
MIN_WIDTH: {
|
||||
STANDARD: 1600,
|
||||
COMPACT: 1200,
|
||||
WIDE: 2000,
|
||||
},
|
||||
} as const;
|
||||
|
||||
/** X 축 패딩 없앤 GridOption*/
|
||||
export const GRID_X_NOT_PADDING = { left: 0, right: 0, top: 50, bottom: 10, containLabel: false } as const;
|
||||
/** T 축 패딩 없앤 GridOption*/
|
||||
export const GRID_Y_NOT_PADDING = { left: 10, right: 10, top: 0, bottom: 0, containLabel: false } as const;
|
||||
/** 축 스타일 감춤 */
|
||||
export const AXIS_HIDE_STYLE = { axisLabel: { show: false }, axisTick: { show: false }, axisLine: { show: false }} as const;
|
||||
export const AXIS_HIDE_Y_STYLE = { min:0, max:100, axisLabel: { show: false }, axisTick: { show: false }, axisLine: { show: false }} as const;
|
||||
17
src/components/chart/types.ts
Normal file
17
src/components/chart/types.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
export type NewNumber = number | null | undefined;
|
||||
|
||||
export type ChartSeries = {
|
||||
/** legend에 노출될 이름 */
|
||||
name: string;
|
||||
/** categories와 동일 길이 */
|
||||
data: NewNumber[];
|
||||
/** stacked bar 용 */
|
||||
stack?: string; // 같은 stack 값끼리 누적
|
||||
};
|
||||
|
||||
export type ChartData = {
|
||||
/** x(세로바)/y(가로바) 카테고리 */
|
||||
categories: string[];
|
||||
/** 시리즈(막대/스택 등) */
|
||||
series: ChartSeries[];
|
||||
};
|
||||
17
src/components/common/LoadingOverlay.tsx
Normal file
17
src/components/common/LoadingOverlay.tsx
Normal file
@@ -0,0 +1,17 @@
|
||||
"use client";
|
||||
|
||||
import { useAtomValue } from "jotai";
|
||||
import { loadingAtom } from "@/store/jotai/loading";
|
||||
|
||||
export function LoadingOverlay({ force = false }: { force?: boolean }) {
|
||||
const isLoading = useAtomValue(loadingAtom);
|
||||
|
||||
if (!force && !isLoading) return null;
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 z-[9999] bg-black/70 flex items-center justify-center">
|
||||
{/* 심플 로딩 인디케이터 */}
|
||||
<div className="h-12 w-12 rounded-full border-4 border-gray-400 border-t-white animate-spin" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
19
src/components/common/ReactQueryLoadingSync.tsx
Normal file
19
src/components/common/ReactQueryLoadingSync.tsx
Normal file
@@ -0,0 +1,19 @@
|
||||
"use client";
|
||||
|
||||
import {useIsFetching} from "@tanstack/react-query";
|
||||
import {useLoading} from "@/hooks/useLoading";
|
||||
import {useEffect} from "react";
|
||||
|
||||
const ReactQueryLoadingSync = () => {
|
||||
const fetching = useIsFetching();
|
||||
const { showLoading, hideLoading } = useLoading();
|
||||
|
||||
useEffect(() => {
|
||||
if (fetching > 0) showLoading();
|
||||
else hideLoading();
|
||||
}, [fetching, showLoading, hideLoading]);
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
export default ReactQueryLoadingSync;
|
||||
164
src/components/dialog/loginDialog.tsx
Normal file
164
src/components/dialog/loginDialog.tsx
Normal file
@@ -0,0 +1,164 @@
|
||||
'use client'
|
||||
|
||||
import {useEffect, useRef, useState} from "react";
|
||||
import {apiError} from "@/lib/http-util";
|
||||
import {NONE_HTTP_ERROR} from "@/const/commonConst";
|
||||
import {DialogId} from "@/store/jotai/dialog";
|
||||
import {useDialog} from "@/hooks/useDialog";
|
||||
import {useLoginMutation} from "@/query/auth";
|
||||
import {Input} from "@/components/ui/input";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle
|
||||
} from "@/components/ui/dialog";
|
||||
import {Button} from "@/components/ui/button";
|
||||
|
||||
export type LoginDialogProps = {
|
||||
/** Jotai로 관리할 다이얼로그 ID */
|
||||
dialogId?: DialogId
|
||||
|
||||
/** 필요하면 외부에서 open 상태를 제어할 수 있게 함 */
|
||||
open?: boolean
|
||||
onOpenChange?: (open: boolean) => void
|
||||
}
|
||||
|
||||
const LoginDialog = ({
|
||||
dialogId = 'loginDialog',
|
||||
open,
|
||||
onOpenChange,
|
||||
}: LoginDialogProps) => {
|
||||
const { isDialogOpen, showDialog, hideDialog } = useDialog();
|
||||
const resolvedOpen = open ?? isDialogOpen(dialogId);
|
||||
|
||||
const loginMutation = useLoginMutation();
|
||||
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const [userId, setUserId] = useState<string>("");
|
||||
const [userPw, setUserPw] = useState<string>("");
|
||||
|
||||
const passwordInputRef = useRef<HTMLInputElement | null>(null);
|
||||
|
||||
const handleOpenChange = (nextOpen: boolean) => {
|
||||
onOpenChange?.(nextOpen);
|
||||
|
||||
if (nextOpen) {
|
||||
showDialog(dialogId);
|
||||
setUserId("");
|
||||
setUserPw("");
|
||||
setError(null);
|
||||
} else {
|
||||
hideDialog(dialogId);
|
||||
}
|
||||
}
|
||||
|
||||
const handleIdKeyDown: React.KeyboardEventHandler<HTMLInputElement> = (e) => {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
passwordInputRef.current?.focus();
|
||||
}
|
||||
};
|
||||
|
||||
const handlePwKeyDown: React.KeyboardEventHandler<HTMLInputElement> = (e) => {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
tryLogin();
|
||||
}
|
||||
};
|
||||
|
||||
const tryLogin = async () => {
|
||||
if (!userId) return alert("아이디를 입력해 주세요.");
|
||||
if (!userPw) return alert("비밀번호를 입력해 주세요.");
|
||||
|
||||
setError(null);
|
||||
try {
|
||||
await loginMutation.mutateAsync({ userId, userPw });
|
||||
handleOpenChange(false);
|
||||
} catch (e) {
|
||||
const err = apiError(e);
|
||||
if (err.status === 401 || err.status === 403) {
|
||||
setError("아이디 또는 비밀번호를 다시 확인해 주세요.");
|
||||
} else if (err.code === NONE_HTTP_ERROR) {
|
||||
setError("알 수 없는 오류가 발생했습니다. 잠시 후 다시 시도해 주세요.");
|
||||
} else {
|
||||
setError(`로그인에 실패했습니다. (${err.message})`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
setUserId("");
|
||||
setUserPw("");
|
||||
setError(null);
|
||||
}, [resolvedOpen])
|
||||
|
||||
return (
|
||||
<Dialog open={resolvedOpen} onOpenChange={handleOpenChange}>
|
||||
<DialogContent className="sm:max-w-[500px] content-text-sm">
|
||||
<DialogHeader>
|
||||
<DialogTitle className="content-text-sxl">
|
||||
로그인
|
||||
</DialogTitle>
|
||||
</DialogHeader>
|
||||
<div className="grid gap-6 py-4">
|
||||
<div className="grid gap-2">
|
||||
<label className="text-sm font-medium">ID</label>
|
||||
<Input
|
||||
id="id"
|
||||
name="id"
|
||||
type="text"
|
||||
className="mt-1"
|
||||
required
|
||||
placeholder="아이디를 입력하세요."
|
||||
value={userId}
|
||||
onChange={(e) => setUserId(e.currentTarget.value)}
|
||||
onKeyDown={handleIdKeyDown}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<label className="text-sm font-medium">Password</label>
|
||||
<Input
|
||||
id="password"
|
||||
name="password"
|
||||
type="password"
|
||||
className="mt-1"
|
||||
autoComplete="current-password"
|
||||
required
|
||||
placeholder="비밀번호를 입력하세요."
|
||||
value={userPw}
|
||||
onChange={(e) => setUserPw(e.currentTarget.value)}
|
||||
onKeyDown={handlePwKeyDown}
|
||||
ref={passwordInputRef}
|
||||
/>
|
||||
</div>
|
||||
{error && (
|
||||
<p className="text-sm text-destructive">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => handleOpenChange(false)}
|
||||
>
|
||||
취소
|
||||
</Button>
|
||||
<Button
|
||||
type="submit"
|
||||
onClick={tryLogin}
|
||||
disabled={loginMutation.isPending}
|
||||
>
|
||||
로그인
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
export default LoginDialog
|
||||
99
src/components/layout/Header.tsx
Normal file
99
src/components/layout/Header.tsx
Normal file
@@ -0,0 +1,99 @@
|
||||
'use client'
|
||||
|
||||
import Image from "next/image";
|
||||
import {Button} from "@/components/ui/button";
|
||||
import {LogIn, LogOut} from "lucide-react";
|
||||
import {cn} from "@/lib/utils";
|
||||
import {SITE_TITLE} from "@/config/kfpaConfig";
|
||||
import {TopMenuItem} from "@/config/menu.type";
|
||||
import {usePathname} from "next/navigation";
|
||||
|
||||
interface HeaderProps {
|
||||
topMenus: TopMenuItem[];
|
||||
userName: string;
|
||||
isAuthenticated: boolean;
|
||||
onNavigate: (path: string) => void;
|
||||
onLogin: () => void;
|
||||
onLogout: () => void;
|
||||
}
|
||||
|
||||
const Header = ({
|
||||
topMenus,
|
||||
userName,
|
||||
isAuthenticated,
|
||||
onNavigate,
|
||||
onLogin,
|
||||
onLogout,
|
||||
}: HeaderProps) => {
|
||||
const pathname = usePathname();
|
||||
|
||||
return (
|
||||
<header className="app-header sticky top-0 z-40">
|
||||
<div className="grid grid-cols-[minmax(0,1fr)_auto_minmax(0,1fr)] items-center gap-4 px-4 h-16">
|
||||
<div className="flex min-w-0 items-center gap-4">
|
||||
<div className="flex items-center gap-2 cursor-pointer w-37 h-15 justify-center">
|
||||
<Image
|
||||
src="/head_logo.png"
|
||||
alt="logo"
|
||||
width={150}
|
||||
height={90}
|
||||
className="object-contain"
|
||||
onClick={() => onNavigate("/safety-inspection")}
|
||||
priority
|
||||
/>
|
||||
</div>
|
||||
|
||||
<nav className="flex min-w-0 items-center gap-3 ml-5 overflow-hidden">
|
||||
{topMenus.map((topMenu) => {
|
||||
const active = topMenu.match?.(pathname) ?? pathname === topMenu.path;
|
||||
return (
|
||||
<button
|
||||
key={topMenu.id}
|
||||
type="button"
|
||||
onClick={() => onNavigate(topMenu.path)}
|
||||
className={cn(
|
||||
"relative h-8 inline-flex items-center font-bold transition-colors cursor-pointer",
|
||||
"content-text-lg bg-transparent px-1",
|
||||
active
|
||||
? "text-header-primary"
|
||||
: "text-header-secondary-hover"
|
||||
)}
|
||||
>
|
||||
{topMenu.label}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</nav>
|
||||
</div>
|
||||
<div className="flex items-center justify-center">
|
||||
<h1 className="font-bold content-text-2xl text-header-color">{SITE_TITLE}</h1>
|
||||
</div>
|
||||
<div className="flex min-w-0 items-center justify-end gap-4">
|
||||
{isAuthenticated && (
|
||||
<span className="content-text-base text-header-color hidden max-w-40 truncate sm:inline font-bold">
|
||||
{userName}
|
||||
</span>
|
||||
)}
|
||||
<Button
|
||||
className="gap-2 border border-transparent bg-green-600 text-white w-25 shadow-sm hover:bg-green-500 hover:text-white"
|
||||
onClick={isAuthenticated ? onLogout : onLogin}
|
||||
>
|
||||
{isAuthenticated ? (
|
||||
<>
|
||||
<LogOut className="w-4 h-4" />
|
||||
<span className="hidden sm:inline">{"로그아웃"}</span>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<LogIn className="w-4 h-4" />
|
||||
<span className="hidden sm:inline">{"로그인"}</span>
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
);
|
||||
}
|
||||
|
||||
export default Header;
|
||||
54
src/components/layout/LayoutCategoryContext.tsx
Normal file
54
src/components/layout/LayoutCategoryContext.tsx
Normal file
@@ -0,0 +1,54 @@
|
||||
"use client";
|
||||
|
||||
import React, { createContext, useCallback, useContext, useMemo, useRef } from "react";
|
||||
import {DivKey} from "@/types/kfpa";
|
||||
|
||||
type ExcelExporter = (ctx: DivKey) => void | Promise<void>;
|
||||
|
||||
type LayoutCategoryContextValue = {
|
||||
selectedDiv: DivKey;
|
||||
registerExcelExporter: (exporter: ExcelExporter | null) => void;
|
||||
requestExcelExport: () => Promise<void>;
|
||||
};
|
||||
|
||||
const LayoutCategoryContext = createContext<LayoutCategoryContextValue | null>(null);
|
||||
|
||||
export const LayoutCategoryProvider = ({
|
||||
selectedDiv,
|
||||
children,
|
||||
}: {
|
||||
selectedDiv: DivKey;
|
||||
children: React.ReactNode;
|
||||
}) => {
|
||||
const exporterRef = useRef<ExcelExporter | null>(null);
|
||||
|
||||
const registerExcelExporter = useCallback((exporter: ExcelExporter | null) => {
|
||||
exporterRef.current = exporter;
|
||||
}, []);
|
||||
|
||||
const requestExcelExport = useCallback(async () => {
|
||||
const exporter = exporterRef.current;
|
||||
if (!exporter) {
|
||||
console.warn("Excel exporter is not registered.");
|
||||
return;
|
||||
}
|
||||
await exporter(selectedDiv);
|
||||
}, [selectedDiv]);
|
||||
|
||||
const ctxValue = useMemo<LayoutCategoryContextValue>(
|
||||
() => ({
|
||||
selectedDiv,
|
||||
registerExcelExporter,
|
||||
requestExcelExport,
|
||||
}),
|
||||
[selectedDiv, registerExcelExporter, requestExcelExport]
|
||||
);
|
||||
|
||||
return <LayoutCategoryContext.Provider value={ctxValue}>{children}</LayoutCategoryContext.Provider>;
|
||||
};
|
||||
|
||||
export const useLayoutFilter = () => {
|
||||
const ctx = useContext(LayoutCategoryContext);
|
||||
if (!ctx) throw new Error("useLayoutFilter must be used within LayoutCategoryProvider");
|
||||
return ctx;
|
||||
};
|
||||
86
src/components/layout/MainLayout.tsx
Normal file
86
src/components/layout/MainLayout.tsx
Normal file
@@ -0,0 +1,86 @@
|
||||
"use client"
|
||||
|
||||
import {ReactNode, useEffect, useState} from "react";
|
||||
import {useRouter} from "next/navigation";
|
||||
import {DivKey} from "@/types/kfpa";
|
||||
import {useAuth} from "@/hooks/useAuth";
|
||||
import {LayoutCategoryProvider} from "./LayoutCategoryContext";
|
||||
import Header from "@/components/layout/Header";
|
||||
import {useMounted} from "@/hooks/useMounted";
|
||||
import {Sidebar} from "@/components/layout/Sidebar";
|
||||
import {TOP_MENUS, SIDE_MENUS} from "@/config/menu.data";
|
||||
import {useLogoutMutation} from "@/query/auth";
|
||||
import {useDialog} from "@/hooks/useDialog";
|
||||
import LoginDialog from "@/components/dialog/loginDialog";
|
||||
|
||||
type MainLayoutProps = {
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
const getDivKey = () => {
|
||||
return "-1";
|
||||
}
|
||||
|
||||
const MainLayout = ({ children }: MainLayoutProps) => {
|
||||
const { auth, logout } = useAuth();
|
||||
const { showDialog } = useDialog();
|
||||
const logoutMutation = useLogoutMutation();
|
||||
const router = useRouter();
|
||||
const isMounted = useMounted();
|
||||
|
||||
const [menuOpen, setMenuOpen] = useState(true);
|
||||
const [selectedDiv, setSelectedDiv] = useState<DivKey>(getDivKey());
|
||||
|
||||
const movePage = (path: string) => {
|
||||
router.push(path);
|
||||
};
|
||||
|
||||
const openLoginDialog = () => {
|
||||
showDialog("loginDialog");
|
||||
};
|
||||
|
||||
const handleLogout = () => {
|
||||
logoutMutation.mutate();
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const handleAuthExpired = () => {
|
||||
logout();
|
||||
};
|
||||
|
||||
window.addEventListener("auth:expired", handleAuthExpired);
|
||||
return () => window.removeEventListener("auth:expired", handleAuthExpired);
|
||||
}, [logout]);
|
||||
|
||||
if(!isMounted) return null;
|
||||
|
||||
return (
|
||||
<div className="h-screen flex flex-col bg-transparent overflow-hidden">
|
||||
<LayoutCategoryProvider selectedDiv={selectedDiv}>
|
||||
<Header
|
||||
topMenus={TOP_MENUS}
|
||||
userName={auth.user?.userName || ""}
|
||||
isAuthenticated={auth.user != null}
|
||||
onNavigate={movePage}
|
||||
onLogin={openLoginDialog}
|
||||
onLogout={handleLogout}
|
||||
/>
|
||||
<div className="flex flex-1">
|
||||
<Sidebar
|
||||
sideMenus={SIDE_MENUS}
|
||||
isOpen={menuOpen}
|
||||
selectedDiv={selectedDiv}
|
||||
onChangeDiv={setSelectedDiv}
|
||||
/>
|
||||
<main className="flex-1 p-4">
|
||||
{children}
|
||||
</main>
|
||||
</div>
|
||||
<LoginDialog />
|
||||
|
||||
</LayoutCategoryProvider>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default MainLayout;
|
||||
72
src/components/layout/Sidebar.tsx
Normal file
72
src/components/layout/Sidebar.tsx
Normal file
@@ -0,0 +1,72 @@
|
||||
"use client";
|
||||
|
||||
import { cn } from "@/lib/utils";
|
||||
import {SideMenuItem} from "@/config/menu.type";
|
||||
import {DivKey} from "@/types/kfpa";
|
||||
|
||||
interface SidebarProps {
|
||||
sideMenus: SideMenuItem[];
|
||||
isOpen: boolean;
|
||||
selectedDiv: DivKey;
|
||||
onChangeDiv: (div: DivKey) => void;
|
||||
}
|
||||
|
||||
export const Sidebar = ({
|
||||
sideMenus,
|
||||
isOpen,
|
||||
selectedDiv,
|
||||
onChangeDiv,
|
||||
}: SidebarProps) => {
|
||||
const activeMenuClassName =
|
||||
"text-foreground bg-muted shadow-[inset_4px_0_0_#ed1c24]";
|
||||
|
||||
return (
|
||||
<aside
|
||||
className={cn(
|
||||
"fixed lg:static top-16 lg:top-0 left-0",
|
||||
"h-[calc(100vh-4rem)] app-sidebar",
|
||||
"transition-all duration-300 z-30 overflow-y-auto",
|
||||
"lg:translate-x-0",
|
||||
isOpen
|
||||
? "w-45 translate-x-0"
|
||||
: "w-45 -translate-x-full lg:w-0 lg:border-r-0"
|
||||
)}
|
||||
>
|
||||
<div className="py-4 overflow-y-auto h-full">
|
||||
<nav className="space-y-0.5 pl-2">
|
||||
{sideMenus.map((item) => {
|
||||
const active = selectedDiv === item.id;
|
||||
|
||||
return (
|
||||
<button
|
||||
key={item.id}
|
||||
type="button"
|
||||
onClick={() => onChangeDiv(item.id as DivKey)}
|
||||
aria-current={active ? "true" : undefined}
|
||||
className={cn(
|
||||
"w-full flex items-center gap-2.5 px-3 py-2 rounded-md",
|
||||
"content-text-base font-bold transition-all duration-200 cursor-pointer",
|
||||
active
|
||||
? activeMenuClassName
|
||||
: "text-header-secondary-hover hover:bg-muted"
|
||||
)}
|
||||
>
|
||||
<item.icon
|
||||
strokeWidth={active ? 2.4 : 2}
|
||||
className={cn(
|
||||
"shrink-0 transition-all duration-200",
|
||||
active ? "text-primary" : "text-muted-foreground",
|
||||
active ? "w-[18px] h-[18px]" : "w-4 h-4"
|
||||
)}
|
||||
/>
|
||||
<span className={cn("transition-all duration-200", active ? "text-foreground text-[15px]" : "text-[14px]")}>
|
||||
{item.label}
|
||||
</span>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</nav>
|
||||
</div>
|
||||
</aside>
|
||||
);
|
||||
};
|
||||
33
src/components/providers/AppProvider.tsx
Normal file
33
src/components/providers/AppProvider.tsx
Normal file
@@ -0,0 +1,33 @@
|
||||
'use client'
|
||||
|
||||
import { Provider as JotaiProvider } from "jotai";
|
||||
import {LoadingOverlay} from "@/components/common/LoadingOverlay";
|
||||
import ReactQueryLoadingSync from "@/components/common/ReactQueryLoadingSync";
|
||||
import {useState} from "react";
|
||||
import {QueryClient} from "@tanstack/query-core";
|
||||
import { QueryClientProvider } from "@tanstack/react-query";
|
||||
|
||||
export function AppProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
const [queryClient] = useState(
|
||||
() =>
|
||||
new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
refetchOnWindowFocus: false,
|
||||
retry: 1,
|
||||
},
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
return (
|
||||
<JotaiProvider>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<ReactQueryLoadingSync />
|
||||
<LoadingOverlay />
|
||||
{children}
|
||||
</QueryClientProvider>
|
||||
</JotaiProvider>
|
||||
);
|
||||
}
|
||||
67
src/components/ui/button.tsx
Normal file
67
src/components/ui/button.tsx
Normal file
@@ -0,0 +1,67 @@
|
||||
import * as React from "react"
|
||||
import { cva, type VariantProps } from "class-variance-authority"
|
||||
import { Slot } from "radix-ui"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
const buttonVariants = cva(
|
||||
"group/button inline-flex shrink-0 items-center justify-center rounded-lg border border-transparent bg-clip-padding text-sm font-medium whitespace-nowrap transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 active:not-aria-[haspopup]:translate-y-px disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 cursor-pointer",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "bg-primary text-primary-foreground [a]:hover:bg-primary/80",
|
||||
outline:
|
||||
"border-border bg-background hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:border-input dark:bg-input/30 dark:hover:bg-input/50",
|
||||
secondary:
|
||||
"bg-secondary text-secondary-foreground hover:bg-secondary/80 aria-expanded:bg-secondary aria-expanded:text-secondary-foreground",
|
||||
ghost:
|
||||
"hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:hover:bg-muted/50",
|
||||
destructive:
|
||||
"bg-destructive/10 text-destructive hover:bg-destructive/20 focus-visible:border-destructive/40 focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:hover:bg-destructive/30 dark:focus-visible:ring-destructive/40",
|
||||
link: "text-primary underline-offset-4 hover:underline",
|
||||
},
|
||||
size: {
|
||||
default:
|
||||
"h-8 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
|
||||
xs: "h-6 gap-1 rounded-[min(var(--radius-md),10px)] px-2 text-xs in-data-[slot=button-group]:rounded-lg has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3",
|
||||
sm: "h-7 gap-1 rounded-[min(var(--radius-md),12px)] px-2.5 text-[0.8rem] in-data-[slot=button-group]:rounded-lg has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3.5",
|
||||
lg: "h-9 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
|
||||
icon: "size-8",
|
||||
"icon-xs":
|
||||
"size-6 rounded-[min(var(--radius-md),10px)] in-data-[slot=button-group]:rounded-lg [&_svg:not([class*='size-'])]:size-3",
|
||||
"icon-sm":
|
||||
"size-7 rounded-[min(var(--radius-md),12px)] in-data-[slot=button-group]:rounded-lg",
|
||||
"icon-lg": "size-9",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
size: "default",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
function Button({
|
||||
className,
|
||||
variant = "default",
|
||||
size = "default",
|
||||
asChild = false,
|
||||
...props
|
||||
}: React.ComponentProps<"button"> &
|
||||
VariantProps<typeof buttonVariants> & {
|
||||
asChild?: boolean
|
||||
}) {
|
||||
const Comp = asChild ? Slot.Root : "button"
|
||||
|
||||
return (
|
||||
<Comp
|
||||
data-slot="button"
|
||||
data-variant={variant}
|
||||
data-size={size}
|
||||
className={cn(buttonVariants({ variant, size, className }))}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Button, buttonVariants }
|
||||
168
src/components/ui/dialog.tsx
Normal file
168
src/components/ui/dialog.tsx
Normal file
@@ -0,0 +1,168 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import { Dialog as DialogPrimitive } from "radix-ui"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { XIcon } from "lucide-react"
|
||||
|
||||
function Dialog({
|
||||
...props
|
||||
}: React.ComponentProps<typeof DialogPrimitive.Root>) {
|
||||
return <DialogPrimitive.Root data-slot="dialog" {...props} />
|
||||
}
|
||||
|
||||
function DialogTrigger({
|
||||
...props
|
||||
}: React.ComponentProps<typeof DialogPrimitive.Trigger>) {
|
||||
return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />
|
||||
}
|
||||
|
||||
function DialogPortal({
|
||||
...props
|
||||
}: React.ComponentProps<typeof DialogPrimitive.Portal>) {
|
||||
return <DialogPrimitive.Portal data-slot="dialog-portal" {...props} />
|
||||
}
|
||||
|
||||
function DialogClose({
|
||||
...props
|
||||
}: React.ComponentProps<typeof DialogPrimitive.Close>) {
|
||||
return <DialogPrimitive.Close data-slot="dialog-close" {...props} />
|
||||
}
|
||||
|
||||
function DialogOverlay({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof DialogPrimitive.Overlay>) {
|
||||
return (
|
||||
<DialogPrimitive.Overlay
|
||||
data-slot="dialog-overlay"
|
||||
className={cn(
|
||||
"fixed inset-0 isolate z-50 bg-black/10 duration-100 supports-backdrop-filter:backdrop-blur-xs data-open:animate-in data-open:fade-in-0 data-closed:animate-out data-closed:fade-out-0",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function DialogContent({
|
||||
className,
|
||||
children,
|
||||
showCloseButton = true,
|
||||
...props
|
||||
}: React.ComponentProps<typeof DialogPrimitive.Content> & {
|
||||
showCloseButton?: boolean
|
||||
}) {
|
||||
return (
|
||||
<DialogPortal>
|
||||
<DialogOverlay />
|
||||
<DialogPrimitive.Content
|
||||
data-slot="dialog-content"
|
||||
className={cn(
|
||||
"fixed top-1/2 left-1/2 z-50 grid w-full max-w-[calc(100%-2rem)] -translate-x-1/2 -translate-y-1/2 gap-4 rounded-xl bg-popover p-4 text-sm text-popover-foreground ring-1 ring-foreground/10 duration-100 outline-none sm:max-w-sm data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
{showCloseButton && (
|
||||
<DialogPrimitive.Close data-slot="dialog-close" asChild>
|
||||
<Button
|
||||
variant="ghost"
|
||||
className="absolute top-2 right-2 cursor-pointer"
|
||||
size="icon-sm"
|
||||
>
|
||||
<XIcon
|
||||
/>
|
||||
<span className="sr-only">Close</span>
|
||||
</Button>
|
||||
</DialogPrimitive.Close>
|
||||
)}
|
||||
</DialogPrimitive.Content>
|
||||
</DialogPortal>
|
||||
)
|
||||
}
|
||||
|
||||
function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="dialog-header"
|
||||
className={cn("flex flex-col gap-2", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function DialogFooter({
|
||||
className,
|
||||
showCloseButton = false,
|
||||
children,
|
||||
...props
|
||||
}: React.ComponentProps<"div"> & {
|
||||
showCloseButton?: boolean
|
||||
}) {
|
||||
return (
|
||||
<div
|
||||
data-slot="dialog-footer"
|
||||
className={cn(
|
||||
"-mx-4 -mb-4 flex flex-col-reverse gap-2 rounded-b-xl border-t bg-muted/50 p-4 sm:flex-row sm:justify-end",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
{showCloseButton && (
|
||||
<DialogPrimitive.Close asChild>
|
||||
<Button variant="outline">Close</Button>
|
||||
</DialogPrimitive.Close>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function DialogTitle({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof DialogPrimitive.Title>) {
|
||||
return (
|
||||
<DialogPrimitive.Title
|
||||
data-slot="dialog-title"
|
||||
className={cn(
|
||||
"font-heading text-base leading-none font-medium",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function DialogDescription({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof DialogPrimitive.Description>) {
|
||||
return (
|
||||
<DialogPrimitive.Description
|
||||
data-slot="dialog-description"
|
||||
className={cn(
|
||||
"text-sm text-muted-foreground *:[a]:underline *:[a]:underline-offset-3 *:[a]:hover:text-foreground",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export {
|
||||
Dialog,
|
||||
DialogClose,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogOverlay,
|
||||
DialogPortal,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
}
|
||||
19
src/components/ui/input.tsx
Normal file
19
src/components/ui/input.tsx
Normal file
@@ -0,0 +1,19 @@
|
||||
import * as React from "react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Input({ className, type, ...props }: React.ComponentProps<"input">) {
|
||||
return (
|
||||
<input
|
||||
type={type}
|
||||
data-slot="input"
|
||||
className={cn(
|
||||
"h-8 w-full min-w-0 rounded-lg border border-input bg-transparent px-2.5 py-1 text-base transition-colors outline-none file:inline-flex file:h-6 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed disabled:bg-input/50 disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:disabled:bg-input/80 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Input }
|
||||
Reference in New Issue
Block a user