sorted history yay

This commit is contained in:
AntoninoP 2024-09-06 19:25:05 +02:00
parent 0a0b5a7020
commit 89477505aa

View File

@ -23,7 +23,7 @@ const ServerHistory = () => {
const fetchServerHistory = async () => { const fetchServerHistory = async () => {
setError(null); setError(null);
setLoading(true); setLoading(true);
try { try {
const response = await serviiApi.fetchHistory(serverName); const response = await serviiApi.fetchHistory(serverName);
if (response.return_code === 200) { if (response.return_code === 200) {
@ -35,26 +35,31 @@ const ServerHistory = () => {
historyString = historyString.replace(/"{2,}/g, ''); historyString = historyString.replace(/"{2,}/g, '');
historyString = historyString.replace(/'{2,}/g, ''); historyString = historyString.replace(/'{2,}/g, '');
historyString = historyString.trim(); historyString = historyString.trim();
historyString = historyString.replace(/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})(type": ")/g, '$1", "$2'); historyString = historyString.replace(/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})(type": ")/g, '$1", "$2');
historyString = historyString.replace(/(type": "ServerCreate|ServerRun|Command|UpdateProperties)(details": )/g, '$1", "$2'); historyString = historyString.replace(/(type": "ServerCreate|ServerRun|Command|UpdateProperties)(details": )/g, '$1", "$2');
const historyArray = historyString.split('\n').map(entry => { const historyArray = historyString.split('\n').map(entry => {
const timestampMatch = entry.match(/"timestamp": "(.*?)"/); const timestampMatch = entry.match(/"timestamp": "(.*?)"/);
const typeMatch = entry.match(/"type": "(.*?)"/); const typeMatch = entry.match(/"type": "(.*?)"/);
const detailsMatch = entry.match(/"details": "(.*?)"/); const detailsMatch = entry.match(/"details": "(.*?)"/);
const parsedEntry = { const parsedEntry = {
timestamp: timestampMatch ? timestampMatch[1] : "Unknown", timestamp: timestampMatch ? timestampMatch[1] : "Unknown",
type: typeMatch ? typeMatch[1] : "Unknown", type: typeMatch ? typeMatch[1] : "Unknown",
details: detailsMatch ? detailsMatch[1] : null details: detailsMatch ? detailsMatch[1] : null
}; };
return parsedEntry; return parsedEntry;
}); });
const sortedHistoryArray = historyArray.sort((a, b) => {
setHistory(historyArray); const dateA = new Date(a.timestamp);
const dateB = new Date(b.timestamp);
return dateB - dateA;
});
setHistory(sortedHistoryArray);
} else { } else {
console.error("Failed to fetch history, response:", response); console.error("Failed to fetch history, response:", response);
} }
@ -65,6 +70,7 @@ const ServerHistory = () => {
setLoading(false); setLoading(false);
} }
}; };
useEffect(() => { useEffect(() => {
fetchServerHistory(); fetchServerHistory();