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