Files
nimbus/resources/js/stores/request/useRequestsHistoryStore.ts
Mazen Touati e1b844cee0 feat(history): add history viewer and rewind (#38)
* feat(ui): add `input group` base component

* feat(history): add history viewer and rewind

* test: update selector snapshot

* test: add PW base page

* style: apply TS style fixes

* chore(history): request history wiki

* chore(history): remove unwanted symbol

* chore: fix type

* style: apply TS style fixes
2026-01-17 20:50:00 +01:00

71 lines
1.7 KiB
TypeScript

import { RequestLog } from '@/interfaces/history/logs';
import { useSettingsStore } from '@/stores';
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
export const useRequestsHistoryStore = defineStore(
'requestHistory',
() => {
/*
* Stores & dependencies.
*/
const settingsStore = useSettingsStore();
// State
const logs = ref<RequestLog[]>([]);
const activeLogIndex = ref<number | null>(null);
// Computed
const maxLogs = computed(() => settingsStore.preferences.maxHistoryLogs);
// Computed
const allLogs = computed(() => logs.value);
const lastLog = computed(() => {
if (activeLogIndex.value !== null && logs.value[activeLogIndex.value]) {
return logs.value[activeLogIndex.value];
}
return logs.value[logs.value.length - 1] ?? null;
});
// Actions
const addLog = (log: RequestLog) => {
logs.value.push(log);
// Maintain max logs limit
if (logs.value.length > maxLogs.value) {
logs.value = logs.value.slice(-maxLogs.value);
}
activeLogIndex.value = null;
};
const setActiveLog = (index: number | null) => {
activeLogIndex.value = index;
};
const clearLogs = () => {
logs.value = [];
};
return {
// State
logs,
maxLogs,
activeLogIndex,
// Getters
allLogs,
lastLog,
// Actions
addLog,
clearLogs,
setActiveLog,
};
},
{
persist: true,
},
);