Spaces:
Sleeping
Sleeping
Download client/src/services/formatService.js from lemachinarbo/lucas-dev: direct link, hf CLI and curl.
- Browser
- Download file 3.04 kB
-
https://huggingface.co/spaces/lemachinarbo/lucas-dev/resolve/main/client/src/services/formatService.js
- Command line
-
hf download hf://spaces/lemachinarbo/lucas-dev/client/src/services/formatService.js
-
curl -L -o formatService.js https://huggingface.co/spaces/lemachinarbo/lucas-dev/resolve/main/client/src/services/formatService.js
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] || "โ"; | |
| }; | |