diff --git a/packages/logging-system/src/Logger.ts b/packages/logging-system/src/Logger.ts index 2761bf0..23ffb14 100644 --- a/packages/logging-system/src/Logger.ts +++ b/packages/logging-system/src/Logger.ts @@ -1,8 +1,8 @@ import { sha256, base64Encode, hexToUtf8 } from '@yourssu/crypto'; -import { SetLocalStorage, SetLocalStorageClear } from './SetLocalStorage'; +import { SetLocalStorage } from './SetLocalStorage'; import { useYLSContext } from './hooks/useYLSContext'; -import { LogPayloadParams, LogRequestList, LogResponse, LogType } from './types/LogType'; +import { LogPayloadParams, LogRequestList, LogResponse } from './types/LogType'; const createRandomId = () => { let randomId = ''; @@ -19,13 +19,13 @@ const createRandomId = () => { }; const createHashedID = (userId: string) => { - let hashedId = ''; + let hashedId: string; let localHashedId = ''; const existLocalHashedId = window.localStorage.getItem('yls-web'); if (userId === '') { if (existLocalHashedId) { - localHashedId = JSON.parse(window.localStorage.getItem('yls-web') as string).hashedId; + localHashedId = JSON.parse(existLocalHashedId).hashedId; } else { userId = createRandomId(); } @@ -87,22 +87,3 @@ export const Logger = ({ userId, version, event }: LogPayloadParams) => { }, }; }; - -window.addEventListener('unload', async (event) => { - event.preventDefault(); - - const { postLog } = useYLSContext(); - const logList: LogType[] = JSON.parse(localStorage.getItem('yls-web') as string) || []; - const req: LogRequestList = { - logRequestList: logList, - }; - - try { - const res = await postLog(req); - if (res.success) { - SetLocalStorageClear(); - } - } catch (e) { - console.error('Failed to post log'); - } -}); diff --git a/packages/logging-system/src/SetLocalStorage.ts b/packages/logging-system/src/SetLocalStorage.ts index b6c047c..05f0eb9 100644 --- a/packages/logging-system/src/SetLocalStorage.ts +++ b/packages/logging-system/src/SetLocalStorage.ts @@ -29,7 +29,7 @@ export const SetLocalStorage = async ( SetLocalStorageClear(); } } catch (e) { - console.error('Failed to post log'); + console.error(e); } } } diff --git a/packages/logging-system/src/contexts/YLSProvider.tsx b/packages/logging-system/src/contexts/YLSProvider.tsx index bda7713..e12be51 100644 --- a/packages/logging-system/src/contexts/YLSProvider.tsx +++ b/packages/logging-system/src/contexts/YLSProvider.tsx @@ -1,7 +1,8 @@ -import { createContext } from 'react'; +import { createContext, useEffect } from 'react'; import { createAxiosInstance } from '@/apis/createAxiosInstance'; -import { LogRequestList, LogResponse } from '@/types/LogType'; +import { LogRequestList, LogResponse, LogType } from '@/types/LogType'; +import { SetLocalStorageClear } from '@/SetLocalStorage'; interface YLSProviderProps { children: React.ReactNode; @@ -32,6 +33,32 @@ export const YLSProvider = ({ children, baseURL }: YLSProviderProps) => { } }; + useEffect(() => { + const handleUnload = async (event: Event) => { + event.preventDefault(); + + const logList: LogType[] = JSON.parse(localStorage.getItem('yls-web') as string) || []; + const req: LogRequestList = { + logRequestList: logList, + }; + + try { + const res = await postLog(req); + if (res.success) { + SetLocalStorageClear(); + } + } catch (e) { + console.error(e); + } + }; + + window.addEventListener('unload', handleUnload); + + return () => { + window.removeEventListener('unload', handleUnload); + }; + }, []); + return (