Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@yourssu/logging-system): unload event 시 서버로 log 전송 #44

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 4 additions & 23 deletions packages/logging-system/src/Logger.ts
Original file line number Diff line number Diff line change
@@ -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 = '';
Expand All @@ -19,13 +19,13 @@ const createRandomId = () => {
};

const createHashedID = (userId: string) => {
let hashedId = '';
let hashedId: string;
let localHashedId = '';
const existLocalHashedId = window.localStorage.getItem('yls-web');
Hanna922 marked this conversation as resolved.
Show resolved Hide resolved

if (userId === '') {
if (existLocalHashedId) {
localHashedId = JSON.parse(window.localStorage.getItem('yls-web') as string).hashedId;
localHashedId = JSON.parse(existLocalHashedId).hashedId;
} else {
userId = createRandomId();
}
Expand Down Expand Up @@ -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');
}
});
2 changes: 1 addition & 1 deletion packages/logging-system/src/SetLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const SetLocalStorage = async (
SetLocalStorageClear();
}
} catch (e) {
console.error('Failed to post log');
console.error(e);
}
}
}
Expand Down
31 changes: 29 additions & 2 deletions packages/logging-system/src/contexts/YLSProvider.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
};
Comment on lines +55 to +59
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
window.addEventListener('unload', handleUnload);
return () => {
window.removeEventListener('unload', handleUnload);
};
window.addEventListener('unload', handleUnload, { once: true });

}, []);

return (
<YLSContext.Provider
value={{
Expand Down