Spaces:
Sleeping

lucas-dev / client /src /services /formatService.js
lemachinarbo
fix: text.json path
7087ddd
Raw History Blame Contribute Delete
3.04 kB
import { sortEntriesByDate } from "./storageService.js";
export const formatGroupedEntries = async (groupedEntries) => {
const template = await loadTemplate("/templates/group-entry.html");
const formattedSections = await Promise.all(
Object.entries(groupedEntries).map(async ([date, entries]) => {
const sortedEntries = sortEntriesByDate(entries);
const formattedEntries = await Promise.all(
sortedEntries.map(formatEntry)
);
return template
.replace("{{date}}", date)
.replace("{{entries}}", formattedEntries.join(""));
})
);
return formattedSections.join("");
};
const templateCache = {};
const loadTemplate = async (path) => {
if (templateCache[path]) {
return templateCache[path];
}
const fetchPromise = fetch(path).then((res) => res.text());
templateCache[path] = fetchPromise; // Store the promise to avoid duplicate fetches
const text = await fetchPromise;
templateCache[path] = text; // Replace with resolved text
return text;
};
const formatEntry = async (entry) => {
let template = await loadTemplate("/templates/log-entry.html");
const emotionTemplate = await loadTemplate("/templates/emotion.html");
const time = new Date(entry.date).toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
});
const emotionsWithEmojis = entry.emotions
.map((e) =>
emotionTemplate
.replace("{{emoji}}", getEmojiForEmotion(e.emotion))
.replace("{{emotion}}", e.emotion)
.replace(
"{{score}}",
e.score !== undefined ? e.score.toFixed(2) : "N/A"
)
)
.join("");
const sentimentText =
entry.sentiment && typeof entry.sentiment === "object"
? entry.sentiment.sentiment
: entry.sentiment || "N/A";
let borderColor = "gray";
if (sentimentText === "negative") borderColor = "red";
else if (sentimentText === "positive") borderColor = "green";
template = template
.replace("{{time}}", time)
.replace("{{transcription}}", entry.transcription)
.replace(/--borderColor--/g, borderColor)
.replace("{{sentimentText}}", sentimentText)
.replace("{{emotions}}", emotionsWithEmojis);
return template;
};
const getEmojiForEmotion = (emotion) => {
const emotionToEmoji = {
admiration: "๐Ÿ˜Š",
amusement: "๐Ÿ˜†",
anger: "๐Ÿ˜ ",
annoyance: "๐Ÿ˜’",
approval: "๐Ÿ‘",
caring: "โค๏ธ",
confusion: "๐Ÿ˜•",
curiosity: "๐Ÿค”",
desire: "๐Ÿ˜",
disappointment: "๐Ÿ˜ž",
disapproval: "๐Ÿ‘Ž",
disgust: "๐Ÿคข",
embarrassment: "๐Ÿ˜ณ",
excitement: "๐ŸŽ‰",
fear: "๐Ÿ˜จ",
gratitude: "๐Ÿ™",
grief: "๐Ÿ˜ข",
joy: "๐Ÿ˜€",
love: "๐Ÿฅฐ",
nervousness: "๐Ÿ˜ฌ",
optimism: "๐Ÿ˜Œ",
pride: "๐Ÿฆ",
realization: "๐Ÿ’ก",
relief: "๐Ÿ˜Œ",
remorse: "๐Ÿ˜”",
sadness: "๐Ÿ˜ข",
surprise: "๐Ÿ˜ฎ",
neutral: "๐Ÿซฅ",
};
return emotionToEmoji[emotion] || "โ“";
};