From ce9de71f423eab6854f10d0302e0fe59397e8ba4 Mon Sep 17 00:00:00 2001 From: Mamalizz Date: Tue, 11 Feb 2025 23:25:17 +0330 Subject: [PATCH] added use object track --- frontend/composables/global/useObjectTrack.ts | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 frontend/composables/global/useObjectTrack.ts diff --git a/frontend/composables/global/useObjectTrack.ts b/frontend/composables/global/useObjectTrack.ts new file mode 100644 index 0000000..9a03a50 --- /dev/null +++ b/frontend/composables/global/useObjectTrack.ts @@ -0,0 +1,39 @@ +export const useObjectTrack = (object: Ref) => { + // state + + const isNotEqual = ref(false); + + const { history, reset, clear } = useRefHistory(object, { + deep: true, + }); + + // watch + + watch( + () => history.value, + (newHistory) => { + if (newHistory.length < 2) { + isNotEqual.value = false; + return; + } + + const initial = newHistory[0].snapshot; + const current = newHistory[newHistory.length - 1].snapshot; + + const hasChanges = Object.keys(initial).some( + (key) => + JSON.stringify(current[key]) !== + JSON.stringify(initial[key]) + ); + + isNotEqual.value = hasChanges; + }, + { deep: true } + ); + + return { + isNotEqual, + reset, + clear, + }; +};