260714_01_ENH: 테스트 서버에서 기동 후 중간 테스트를 위한 커밋
This commit is contained in:
@@ -1,23 +1,18 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback } from "react";
|
||||
import { createAuthHook, type AuthState as CommonAuthState } from "@kri/common/hooks";
|
||||
import { getLocalStorage, removeLocalStorage, setLocalStorage } from "@/store/storage/secure-storage";
|
||||
import { StorageKeys } from "@/store/storage/storage-keys";
|
||||
import type { LoginUser } from "@/types/auth";
|
||||
import {useCallback} from "react";
|
||||
import {type AuthState as CommonAuthState, createAuthHook} from "@kri/common/hooks";
|
||||
import {getLocalStorage, removeLocalStorage, setLocalStorage} from "@/store/storage/secure-storage";
|
||||
import {StorageKeys} from "@/store/storage/storage-keys";
|
||||
import type {LoginUser} from "@kri/common";
|
||||
|
||||
type ContactAuthExtra = {
|
||||
seatNumber: string | null;
|
||||
};
|
||||
|
||||
export type AuthState = CommonAuthState<LoginUser, ContactAuthExtra>;
|
||||
export type AuthState = CommonAuthState<LoginUser>;
|
||||
|
||||
const initialAuthState: AuthState = {
|
||||
user: null,
|
||||
seatNumber: null,
|
||||
};
|
||||
|
||||
const loadInitialAuthState = (): AuthState => {
|
||||
const loadStoredAuthState = (): AuthState => {
|
||||
const stored = getLocalStorage(StorageKeys.AuthState);
|
||||
|
||||
if (stored && typeof stored === "object" && "user" in stored) {
|
||||
@@ -30,14 +25,24 @@ const loadInitialAuthState = (): AuthState => {
|
||||
return initialAuthState;
|
||||
};
|
||||
|
||||
const { authAtom, useAuth: useCommonAuth } = createAuthHook<LoginUser, ContactAuthExtra>({
|
||||
initialState: loadInitialAuthState(),
|
||||
const { authAtom, useAuth: useCommonAuth } = createAuthHook<LoginUser>({
|
||||
initialState: initialAuthState,
|
||||
});
|
||||
|
||||
export const authStateAtom = authAtom;
|
||||
|
||||
/*
|
||||
* localStorage는 서버에 없기 때문에, 모듈 로드 시점에 동기로 읽어 초기값에 반영하면
|
||||
* 서버 렌더링 결과(항상 null)와 브라우저의 첫 렌더링 결과(저장된 값)가 달라져
|
||||
* 하이드레이션 불일치가 발생한다. onMount에서 읽어와야 첫 렌더링은 항상 initialAuthState로
|
||||
* 서버와 동일하게 그려지고, 마운트 이후에만 실제 값으로 갱신된다.
|
||||
*/
|
||||
authAtom.onMount = (setAtom) => {
|
||||
setAtom(loadStoredAuthState());
|
||||
};
|
||||
|
||||
const persistAuth = (next: AuthState) => {
|
||||
if (next.user === null && next.seatNumber === null) {
|
||||
if (next.user === null) {
|
||||
removeLocalStorage(StorageKeys.AuthState);
|
||||
return;
|
||||
}
|
||||
@@ -60,10 +65,9 @@ export const useAuth = () => {
|
||||
);
|
||||
|
||||
const setFromLoginResponse = useCallback(
|
||||
(res: LoginUser, loginSeatNumber: string | null = null) => {
|
||||
(res: LoginUser) => {
|
||||
setAuth({
|
||||
user: res,
|
||||
seatNumber: loginSeatNumber,
|
||||
});
|
||||
},
|
||||
[setAuth],
|
||||
|
||||
8
src/hooks/useCtiAgentStatus.ts
Normal file
8
src/hooks/useCtiAgentStatus.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
"use client";
|
||||
|
||||
import { useSyncExternalStore } from "react";
|
||||
import { ctiClient } from "@/lib/cti/ctiClient";
|
||||
|
||||
export function useCtiAgentStatus() {
|
||||
return useSyncExternalStore(ctiClient.subscribe, ctiClient.getAgentStatus, () => null);
|
||||
}
|
||||
42
src/hooks/useCtiConnection.ts
Normal file
42
src/hooks/useCtiConnection.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect } from "react";
|
||||
import { useAuth } from "@/hooks/useAuth";
|
||||
import { useInternalInfoQuery } from "@/query/internal";
|
||||
import { ctiClient } from "@/lib/cti/ctiClient";
|
||||
|
||||
// TEMP_CTI_LOG: 배포 후 CTI 연동 오류를 콘솔에서 바로 확인하기 위한 임시 로그.
|
||||
// 안정화되면 "TEMP_CTI_LOG"로 검색해서 관련 로그를 전부 제거할 것.
|
||||
|
||||
export function useCtiConnection() {
|
||||
const { auth } = useAuth();
|
||||
const hasUser = !!auth.user;
|
||||
|
||||
// 로그인 시 IP 기반으로 사내 서버에서 내부 정보를 조회한다.
|
||||
// 이 정보가 정상적으로 내려오면(clientId 존재) CTI 웹소켓을 사용하고, 아니면 사용하지 않는다.
|
||||
const internalInfoQuery = useInternalInfoQuery(hasUser);
|
||||
|
||||
const clientId = internalInfoQuery.data?.clientId ?? "";
|
||||
const internalNo = internalInfoQuery.data?.internalNo ?? "";
|
||||
const externalNo = internalInfoQuery.data?.externalNo ?? "";
|
||||
const agentName = auth.user?.userName ?? "";
|
||||
|
||||
useEffect(() => {
|
||||
if (!hasUser) {
|
||||
console.log("[CTI][TEMP_LOG] useCtiConnection skip, not logged in"); // TEMP_CTI_LOG
|
||||
ctiClient.logout();
|
||||
return;
|
||||
}
|
||||
if (!clientId) {
|
||||
console.log("[CTI][TEMP_LOG] useCtiConnection skip, internalInfo.clientId is empty", internalInfoQuery.data); // TEMP_CTI_LOG
|
||||
return;
|
||||
}
|
||||
|
||||
ctiClient.connectAndLogin({
|
||||
agentLoginId: clientId,
|
||||
agentPhone: internalNo,
|
||||
aniNumber: externalNo,
|
||||
agentName,
|
||||
});
|
||||
}, [hasUser, clientId, internalNo, externalNo, agentName]);
|
||||
}
|
||||
8
src/hooks/useCtiConnectionStatus.ts
Normal file
8
src/hooks/useCtiConnectionStatus.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
"use client";
|
||||
|
||||
import { useSyncExternalStore } from "react";
|
||||
import { ctiClient } from "@/lib/cti/ctiClient";
|
||||
|
||||
export function useCtiConnectionStatus() {
|
||||
return useSyncExternalStore(ctiClient.subscribe, ctiClient.getConnectionState, () => "idle" as const);
|
||||
}
|
||||
@@ -13,13 +13,13 @@ import { StorageKeys, StorageType } from "@/store/storage/storage-keys";
|
||||
import type { AuthState } from "@/hooks/useAuth";
|
||||
import type { SelectedProject } from "@/store/jotai/project";
|
||||
import type { ItrntUserInfo } from "@/types/user";
|
||||
import type { LastListVisit } from "@/types/list";
|
||||
|
||||
type ContactStorageSchema = {
|
||||
[StorageKeys.AuthState]: AuthState;
|
||||
[StorageKeys.SeatNumber]: string;
|
||||
[StorageKeys.LoginType]: string;
|
||||
[StorageKeys.SelectedProject]: SelectedProject;
|
||||
[StorageKeys.ItrntUserList]: ItrntUserInfo[];
|
||||
[StorageKeys.LastListVisit]: LastListVisit;
|
||||
};
|
||||
|
||||
type ContactStorageKey = Extract<keyof ContactStorageSchema, string>;
|
||||
|
||||
Reference in New Issue
Block a user