diff --git a/packages/logging-system/src/SetLocalStorage.ts b/packages/logging-system/src/SetLocalStorage.ts index 44b3435..583e89d 100644 --- a/packages/logging-system/src/SetLocalStorage.ts +++ b/packages/logging-system/src/SetLocalStorage.ts @@ -1,7 +1,7 @@ import { LogRequestList, LogResponse, LogType } from './types/LogType'; export const SetLocalStorageClear = () => { - const list: any[] = []; + const list: LogType[] = []; localStorage.setItem('yls-web', JSON.stringify(list)); }; @@ -9,28 +9,24 @@ export const SetLocalStorage = async ( logger: LogType, putLog: (data: LogRequestList) => Promise ) => { - if (window.localStorage.getItem('yls-web') == undefined) { - const list: any[] = []; - list.push(logger); - localStorage.setItem('yls-web', JSON.stringify(list)); - } else { - const remainList: any[] = JSON.parse(localStorage.getItem('yls-web') as string) || []; - if (remainList.length < 10) { - const updateList = [...remainList, logger]; - localStorage.setItem('yls-web', JSON.stringify(updateList)); - } else { - const req: LogRequestList = { - logRequestList: remainList, - }; + const list: LogType[] = JSON.parse(localStorage.getItem('yls-web') as string) || []; + list.push(logger); + localStorage.setItem('yls-web', JSON.stringify(list)); + + if (list.length >= 10) { + const req: LogRequestList = { + logRequestList: list, + }; + + SetLocalStorageClear(); - try { - const res = await putLog(req); - if (res.success) { - SetLocalStorageClear(); - } - } catch (e) { - console.error('Failed to post log'); - } + try { + await putLog(req); + } catch (e) { + console.error('Failed to post log'); + const remainList: LogType[] = JSON.parse(localStorage.getItem('yls-web') as string) || []; + remainList.unshift(...list); + localStorage.setItem('yls-web', JSON.stringify(remainList)); } } };