Spaces:
Sleeping
Sleeping
File size: 3,041 Bytes
90b3952 9ee5a0d 90b3952 9ee5a0d 90b3952 9ee5a0d 7087ddd 90b3952 9ee5a0d 90b3952 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | 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] || "โ";
};
|