From 11672637a55cb0bc12c489ac2e3148a2758fafb0 Mon Sep 17 00:00:00 2001 From: Hanna922 Date: Fri, 9 Feb 2024 22:09:51 +0900 Subject: [PATCH] rename: setLocalStorage -> SetLocalStorage --- src/Logger.ts | 4 ++-- src/setLocalStorage.ts | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Logger.ts b/src/Logger.ts index 8071c7e..07a1bd3 100644 --- a/src/Logger.ts +++ b/src/Logger.ts @@ -1,4 +1,4 @@ -import { setLocalStorage } from './setLocalStorage'; +import { SetLocalStorage } from './SetLocalStorage'; import { LogPayloadParams, ServiceNameType } from './types/LogType'; import CryptoJS from 'crypto-js'; @@ -57,7 +57,7 @@ const initialLog = ( logger.event.name = name; logger.event.path = path; - setLocalStorage(logger); + SetLocalStorage(logger); }; export const useYLSLogger = () => { diff --git a/src/setLocalStorage.ts b/src/setLocalStorage.ts index e69de29..a96331d 100644 --- a/src/setLocalStorage.ts +++ b/src/setLocalStorage.ts @@ -0,0 +1,24 @@ +import { postLog } from './apis/postLog'; +import { LogType } from './types/LogType'; + +const SetLocalStorageClear = () => { + const list: any[] = []; + localStorage.setItem('yls-web', JSON.stringify(list)); +}; + +export const SetLocalStorage = async (logger: LogType) => { + 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 { + SetLocalStorageClear(); + const res = await postLog(); + } + } +};