Files
frontend/apps/st/src/shared/lib/use-statistics-input-toolbar-group-gate.ts
T

69 lines
2.2 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import {
checkStatisticsInputStatusConfirmGroupMembership,
checkStatisticsInputToolbarGroupMembership,
} from "@/entities/group-user/lib/check-statistics-input-toolbar-group-membership";
import { useUserStore } from "./use-user-store";
/** 통계 입력 상단 툴바(저장·제출·검증결과 등) — 그룹 멤버십 만족 시에만 true */
export function useStatisticsInputToolbarGroupGate(): boolean {
const userInfo = useUserStore((s) => s.userInfo);
const [allowed, setAllowed] = useState(false);
useEffect(() => {
let cancelled = false;
const name = userInfo?.userName?.trim() ?? "";
const uid = userInfo?.userUnqId?.trim() ?? "";
if (!name || !uid) {
setAllowed(false);
return;
}
void (async () => {
try {
const ok = await checkStatisticsInputToolbarGroupMembership(name, uid);
if (!cancelled) setAllowed(ok);
} catch {
if (!cancelled) setAllowed(false);
}
})();
return () => {
cancelled = true;
};
}, [userInfo?.userName, userInfo?.userUnqId]);
return allowed;
}
/** 통계 입력 현황 — `GRP_ST_CONFIRM` / `GRP_ADMIN` 멤버일 때만 true */
export function useStatisticsInputStatusConfirmGroupGate(): boolean {
const userInfo = useUserStore((s) => s.userInfo);
const [allowed, setAllowed] = useState(false);
useEffect(() => {
let cancelled = false;
const name = userInfo?.userName?.trim() ?? "";
const uid = userInfo?.userUnqId?.trim() ?? "";
if (!name || !uid) {
setAllowed(false);
return;
}
void (async () => {
try {
const ok = await checkStatisticsInputStatusConfirmGroupMembership(name, uid);
if (!cancelled) setAllowed(ok);
} catch {
if (!cancelled) setAllowed(false);
}
})();
return () => {
cancelled = true;
};
}, [userInfo?.userName, userInfo?.userUnqId]);
return allowed;
}