55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
// hooks/useLatestMemo.js
|
|
|
|
import { useState, useEffect } from 'react';
|
|
|
|
function formatMemoContent(markdownContent) {
|
|
if (!markdownContent) return "";
|
|
return markdownContent.split('\n').map(line => {
|
|
const taskItemMatch = line.match(/^-\s*\[( |x|X)\]\s*/);
|
|
if (taskItemMatch) {
|
|
return '- ' + line.slice(taskItemMatch[0].length);
|
|
}
|
|
return line;
|
|
}).join('\n');
|
|
}
|
|
|
|
async function fetchLatestMemo() {
|
|
const url = "https://memos.couraud.xyz/api/v1/memos?page=1&perPage=1";
|
|
try {
|
|
const response = await fetch(url, {
|
|
headers: {
|
|
Authorization: "Bearer eyJhbGciOiJIUzI1NiIsImtpZCI6InYxIiwidHlwIjoiSldUIn0.eyJuYW1lIjoiZ3JvY2VyaWVzIiwiaXNzIjoibWVtb3MiLCJzdWIiOiIyIiwiYXVkIjpbInVzZXIuYWNjZXNzLXRva2VuIl0sImlhdCI6MTc2MDI3NzQwMX0.H8m6LSaav7cuiQgt_rrzB7Fx4UM7Un11M2S0L5JJfPc"
|
|
}
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error ${response.status}`);
|
|
}
|
|
const json = await response.json();
|
|
|
|
const memosArray = json.memos;
|
|
if (memosArray && memosArray.length > 0) {
|
|
return formatMemoContent(memosArray[0].content);
|
|
}
|
|
return "Aucune note trouvée.";
|
|
} catch (error) {
|
|
return `Erreur lors de la récupération de la note: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
export function useLatestMemo() {
|
|
const [latestMemo, setLatestMemo] = useState("Chargement de la dernière note...");
|
|
|
|
async function updateMemo() {
|
|
const memo = await fetchLatestMemo();
|
|
setLatestMemo(memo);
|
|
}
|
|
|
|
useEffect(() => {
|
|
updateMemo();
|
|
const interval = setInterval(updateMemo, 60000);
|
|
return () => clearInterval(interval);
|
|
}, []);
|
|
|
|
return { latestMemo };
|
|
}
|