refactor(Index.tsx): extract meilisearch headers and mapHitToNote function to improve code readability

This commit is contained in:
2025-08-11 12:05:31 +02:00
parent e54efe011b
commit c192f10d77

View File

@@ -34,6 +34,20 @@ const MEILISEARCH_API_KEY = '31qXgGbQ3lT4DYHQ0TOKpMzh7wcigs7agHyxP5Fz6T6D61xsvNr
const NOTE_INDEX = 'notes';
const SCRATCH_INDEX = 'scratch';
const SETTINGS_INDEX = 'settings';
const meiliHeaders = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${MEILISEARCH_API_KEY}`,
} as const;
const mapHitToNote = (hit: any): Note => ({
id: hit.id,
epochTime: hit.date,
dateISO: hit.dateISO,
content: hit.content,
topLetter: hit.topLetter,
topLetterFrequency: hit.topLetterFrequency,
letterCount: hit.letterCount || 0,
});
const Index = () => {
const [currentNote, setCurrentNote] = useState('');
@@ -414,15 +428,7 @@ const Index = () => {
}
const data = await response.json();
const notes: Note[] = data.hits.map((hit: any) => ({
id: hit.id,
epochTime: hit.date,
dateISO: hit.dateISO,
content: hit.content,
topLetter: hit.topLetter,
topLetterFrequency: hit.topLetterFrequency,
letterCount: hit.letterCount || 0,
}));
const notes: Note[] = data.hits.map((hit: any) => mapHitToNote(hit));
if (offset === 0) {
setNoteCache(notes);
@@ -470,15 +476,7 @@ const Index = () => {
}
const data = await response.json();
const notes: Note[] = data.hits.map((hit: any) => ({
id: hit.id,
epochTime: hit.date,
dateISO: hit.dateISO,
content: hit.content,
topLetter: hit.topLetter,
topLetterFrequency: hit.topLetterFrequency,
letterCount: hit.letterCount || 0,
}));
const notes: Note[] = data.hits.map((hit: any) => mapHitToNote(hit));
const targetIndex = notes.findIndex(n => n.id === targetNote.id);
if (targetIndex >= 0) {
@@ -505,20 +503,16 @@ const Index = () => {
const loadNotesAroundDate = async (targetMs: number, beforeLimit = 600, afterLimit = 600) => {
try {
setIsLoading(true);
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${MEILISEARCH_API_KEY}`,
} as const;
const [beforeRes, afterRes] = await Promise.all([
fetch(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/search`, {
method: 'POST',
headers,
headers: meiliHeaders,
body: JSON.stringify({ q: '', filter: `date <= ${targetMs}`, sort: ['date:desc'], limit: beforeLimit }),
}),
fetch(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/search`, {
method: 'POST',
headers,
headers: meiliHeaders,
body: JSON.stringify({ q: '', filter: `date >= ${targetMs}`, sort: ['date:asc'], limit: afterLimit }),
}),
]);
@@ -528,25 +522,9 @@ const Index = () => {
afterRes.ok ? afterRes.json() : Promise.resolve({ hits: [] }),
]);
const before: Note[] = (beforeData.hits || []).map((hit: any) => ({
id: hit.id,
epochTime: hit.date,
dateISO: hit.dateISO,
content: hit.content,
topLetter: hit.topLetter,
topLetterFrequency: hit.topLetterFrequency,
letterCount: hit.letterCount || 0,
}));
const before: Note[] = (beforeData.hits || []).map((hit: any) => mapHitToNote(hit));
const after: Note[] = (afterData.hits || []).map((hit: any) => ({
id: hit.id,
epochTime: hit.date,
dateISO: hit.dateISO,
content: hit.content,
topLetter: hit.topLetter,
topLetterFrequency: hit.topLetterFrequency,
letterCount: hit.letterCount || 0,
}));
const after: Note[] = (afterData.hits || []).map((hit: any) => mapHitToNote(hit));
// Combine, remove duplicates by id, and sort desc by date
const combinedMap = new Map<string, Note>();
@@ -603,13 +581,7 @@ const Index = () => {
const data = await response.json();
const results: Note[] = data.hits.map((hit: any) => ({
id: hit.id,
epochTime: hit.date,
dateISO: hit.dateISO,
content: hit.content,
topLetter: hit.topLetter,
topLetterFrequency: hit.topLetterFrequency,
letterCount: hit.letterCount || 0,
...mapHitToNote(hit),
snippet: hit._formatted?.content || hit.content,
}));
@@ -701,13 +673,7 @@ const Index = () => {
}
return {
id: hit.id,
epochTime: hit.date,
dateISO: hit.dateISO,
content: hit.content,
topLetter: hit.topLetter,
topLetterFrequency: hit.topLetterFrequency,
letterCount: hit.letterCount || 0,
...mapHitToNote(hit),
isProblematic: true,
problemReason,
};