260702_01_ENH: 리스트 작업 도중 공용 ui를 적용하기 위한 중간 커밋
This commit is contained in:
139
src/lib/utils.ts
139
src/lib/utils.ts
@@ -1,137 +1,2 @@
|
||||
import { clsx, type ClassValue } from "clsx"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
import type {NextRequest} from "next/server";
|
||||
|
||||
export type ValueOf<T> = T[keyof T];
|
||||
|
||||
export type EnumLikeKey<T extends Record<string, unknown>> = keyof T;
|
||||
export type EnumLikeValue<T extends Record<string, unknown>> = ValueOf<T>;
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
}
|
||||
|
||||
export function getKeyByValue<
|
||||
const T extends Record<PropertyKey, string>
|
||||
>(map: T, value: T[keyof T]) {
|
||||
const entry = Object.entries(map).find(([, v]) => v === value);
|
||||
return entry ? (entry[0] as keyof T) : undefined;
|
||||
}
|
||||
|
||||
export function getNumberKeyByValue(
|
||||
map: Record<number, string>,
|
||||
value: string
|
||||
): number | undefined {
|
||||
const entry = Object.entries(map).find(([, v]) => v === value);
|
||||
return entry ? Number(entry[0]) : undefined;
|
||||
}
|
||||
|
||||
export function buildUrlWithParams(
|
||||
targetPath: string,
|
||||
from: Record<string, string | string[] | undefined>,
|
||||
extra?: Record<string, string | undefined>,
|
||||
) {
|
||||
const qs = new URLSearchParams();
|
||||
for (const [k, v] of Object.entries(from)) {
|
||||
if (Array.isArray(v)) v.forEach((val) => qs.append(k, val));
|
||||
else if (v !== undefined) qs.set(k, v);
|
||||
}
|
||||
if (extra) for (const [k, v] of Object.entries(extra)) if (v) qs.set(k, v);
|
||||
return `${targetPath}${qs.toString() ? `?${qs}` : ""}`;
|
||||
}
|
||||
|
||||
export const downloadFile = (blob: Blob, filename: string) => {
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
URL.revokeObjectURL(url);
|
||||
};
|
||||
|
||||
export const downloadBase64File = (base64: string, filename: string) => {
|
||||
const byteString = atob(base64);
|
||||
const arrayBuffer = new ArrayBuffer(byteString.length);
|
||||
const uint8Array = new Uint8Array(arrayBuffer);
|
||||
|
||||
for (let i = 0; i < byteString.length; i++) {
|
||||
uint8Array[i] = byteString.charCodeAt(i);
|
||||
}
|
||||
|
||||
const blob = new Blob([arrayBuffer], {
|
||||
type:
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
});
|
||||
|
||||
downloadFile(blob, filename);
|
||||
};
|
||||
|
||||
export type DateTimeFormatOption =
|
||||
| "date" // 1-1. 날짜만
|
||||
| "dateHour" // 1-2. 날짜 + 시
|
||||
| "dateHourMin" // 1-3. 날짜 + 시 분
|
||||
| "dateTime"; // 1-4. 날짜 + 시 분 초
|
||||
|
||||
/**
|
||||
* 서버에서 받은 시간(ISO 문자열/Date/number)을 한국시간 기준으로 포맷팅
|
||||
* - 예: "2025-11-28T03:15:50Z" -> "2025-11-28 12:15:50" (KST)
|
||||
*/
|
||||
export const formatKst = (
|
||||
input: string | number | Date | null | undefined,
|
||||
opt: DateTimeFormatOption = "dateTime"
|
||||
): string => {
|
||||
if (!input) return "";
|
||||
|
||||
const d = input instanceof Date ? input : new Date(input);
|
||||
if (Number.isNaN(d.getTime())) return "";
|
||||
|
||||
// UTC -> KST(UTC+9) 변환
|
||||
const kst = new Date(d.getTime() + 9 * 60 * 60 * 1000);
|
||||
|
||||
const pad2 = (n: number) => String(n).padStart(2, "0");
|
||||
|
||||
const yyyy = kst.getUTCFullYear();
|
||||
const MM = pad2(kst.getUTCMonth() + 1);
|
||||
const dd = pad2(kst.getUTCDate());
|
||||
const HH = pad2(kst.getUTCHours());
|
||||
const mm = pad2(kst.getUTCMinutes());
|
||||
const ss = pad2(kst.getUTCSeconds());
|
||||
|
||||
if (opt === "date") return `${yyyy}-${MM}-${dd}`;
|
||||
if (opt === "dateHour") return `${yyyy}-${MM}-${dd} ${HH}`;
|
||||
if (opt === "dateHourMin") return `${yyyy}-${MM}-${dd} ${HH}:${mm}`;
|
||||
return `${yyyy}-${MM}-${dd} ${HH}:${mm}:${ss}`;
|
||||
};
|
||||
|
||||
export const isBlank = (v?: string | null) => !v || v.trim().length === 0;
|
||||
|
||||
export const getClientIp = (req: NextRequest): string | null => {
|
||||
const h = req.headers;
|
||||
|
||||
// 가장 흔한 케이스: 프록시/호스팅이 넣어줌
|
||||
const xff = h.get("x-forwarded-for");
|
||||
if (xff) {
|
||||
// "client, proxy1, proxy2" 형태. 보통 첫 번째가 client IP로 쓰이지만,
|
||||
// 프록시가 헤더를 덮어쓰지 않고 append만 하는 환경이면 spoof 가능성 있음.
|
||||
return xff.split(",")[0].trim();
|
||||
}
|
||||
|
||||
// 일부 인프라/프록시에서 쓰는 헤더들
|
||||
const xRealIp = h.get("x-real-ip");
|
||||
if (xRealIp) return xRealIp.trim();
|
||||
|
||||
// (Cloudflare 등) 사용 환경이면
|
||||
const cf = h.get("cf-connecting-ip");
|
||||
if (cf) return cf.trim();
|
||||
|
||||
// RFC 7239 Forwarded: for=1.2.3.4;proto=https;by=...
|
||||
const forwarded = h.get("forwarded");
|
||||
if (forwarded) {
|
||||
const m = forwarded.match(/for="?([^;,\s"]+)"?/i);
|
||||
if (m?.[1]) return m[1];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
//shadcn에서 이 경로의 cn을 사용하기 때문에 생성
|
||||
export { cn } from "@kri/common/ui";
|
||||
|
||||
Reference in New Issue
Block a user