forked from ivLis-Studio/ivLyrics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSongInfoTicker.js
More file actions
348 lines (311 loc) · 17 KB
/
Copy pathSongInfoTicker.js
File metadata and controls
348 lines (311 loc) · 17 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* Song Info TMI Component
* - Fullscreen TMI view when album art is clicked
*/
const SongInfoTMI = (() => {
const react = Spicetify.React;
const { useState, useEffect, useRef, useCallback, useMemo } = react;
// Cache for TMI data (메모리 캐시 - 빠른 조회용, IndexedDB도 함께 사용)
const tmiCache = new Map();
// Simple markdown bold parser
const renderMarkdown = (text) => {
if (!text) return null;
const parts = text.split(/(\*\*.*?\*\*)/);
return parts.map((part, i) => {
if (part.startsWith('**') && part.endsWith('**')) {
return react.createElement("strong", { key: i }, part.slice(2, -2));
}
return part;
});
};
// Fetch song info from backend (via LyricsService)
async function fetchSongInfo(trackId, regenerate = false) {
const configuredTargetLang = CONFIG.visual["translate:target-language"];
const lang = configuredTargetLang && configuredTargetLang !== "auto"
? configuredTargetLang
: (window.I18n?.getCurrentLanguage?.() || CONFIG.visual["language"] || Spicetify.Locale?.getLocale()?.split('-')[0] || 'en');
const cacheKey = `${trackId}:${lang || 'auto'}`;
// Check memory cache first (skip if regenerating)
if (!regenerate && tmiCache.has(cacheKey)) {
return tmiCache.get(cacheKey);
}
try {
// 현재 트랙 정보 가져오기
const trackData = Spicetify.Player.data?.item;
const title = trackData?.name || '';
const artist = trackData?.artists?.map(a => a.name).join(', ') || '';
// LyricsService.getTMI 사용 (Addon_AI + Local Cache 통합 처리)
// regenerate=true일 경우 캐시 무시 여부는 getTMI 내부 구현에 따라 다를 수 있으나,
// 현재 getTMI는 캐시 우선이므로, regenerate 시에는 캐시를 먼저 확인하지 않는 로직이 필요할 수 있음.
// 하지만 LyricsService.getTMI는 현재 ignoreCache 파라미터가 없으므로,
// regenerate가 필요한 경우 직접 AIAddonManager를 호출하거나 LyricsCache를 클리어해야 함.
// 일단 기존 로직과 최대한 비슷하게 구현하되, 불필요한 중복 제거.
// 만약 regenerate가 true라면 캐시를 무시해야 하므로, LyricsCache에서 해당 항목을 지워야 할 수도 있음.
// 여기서는 단순화를 위해 LyricsService.getTMI에 위임하되,
// regenerate 기능이 중요하면 LyricsService.getTMI에 ignoreCache 파라미터를 추가하는 것이 좋음.
// 일단은 기존 동작 유지를 위해 직접 호출하는 대신 service를 이용.
const lyricsService = window.LyricsService;
if (!lyricsService?.getTMI) {
return { error: true, message: 'LyricsService.getTMI is not available.' };
}
const result = await lyricsService.getTMI({
trackId,
title,
artist,
lang,
ignoreCache: regenerate
});
if (result) {
tmiCache.set(cacheKey, result);
return result;
}
} catch (e) {
console.warn('[SongInfoTMI] fetchSongInfo failed:', e);
return { error: true, message: e.message || 'TMI generation failed' };
}
// 실패 시
return { error: true, message: '데이터를 가져올 수 없습니다.' };
}
// Source link renderer helper - supports new {title, uri} format
const renderSourceLinks = (sources, isVerified) => {
if (!sources || sources.length === 0) return null;
return react.createElement("div", { className: "tmi-sources" },
sources.map((source, i) => {
// Support both old URL string format and new {title, uri} format
const url = typeof source === 'string' ? source : source?.uri;
const title = typeof source === 'string' ? null : source?.title;
if (!url) return null;
// Extract display text: use title if available, otherwise extract hostname
let displayText;
try {
displayText = title || new URL(url).hostname.replace('www.', '').replace('vertexaisearch.cloud.google.com', 'Google Search');
} catch {
displayText = title || url;
}
return react.createElement("a", {
key: i,
href: url,
className: "tmi-source-link",
onClick: (e) => {
e.preventDefault();
window.open(url, '_blank');
},
title: url
},
react.createElement("span", { className: "tmi-source-icon" }, "🔗"),
react.createElement("span", { className: "tmi-source-text" }, displayText)
);
}).filter(Boolean)
);
};
// Reliability badge component based on confidence level
const ReliabilityBadge = ({ reliability }) => {
if (!reliability) return null;
const { confidence, has_verified_sources, verified_source_count, related_source_count, total_source_count, unique_domains } = reliability;
// Determine badge style and text based on confidence
const badgeConfig = {
very_high: { icon: "✓✓", className: "very-high", textKey: "tmi.confidenceVeryHigh" },
high: { icon: "✓", className: "high", textKey: "tmi.confidenceHigh" },
medium: { icon: "◐", className: "medium", textKey: "tmi.confidenceMedium" },
low: { icon: "○", className: "low", textKey: "tmi.confidenceLow" },
none: { icon: "?", className: "none", textKey: "tmi.confidenceNone" }
};
const config = badgeConfig[confidence] || badgeConfig.none;
// Show verified + related count out of total
const verifiedRelatedCount = (verified_source_count || 0) + (related_source_count || 0);
const sourceInfo = total_source_count > 0
? ` (${verifiedRelatedCount}/${total_source_count})`
: '';
return react.createElement("span", {
className: `tmi-reliability-badge ${config.className}`,
title: I18n.t(config.textKey) + sourceInfo
},
react.createElement("span", { className: "tmi-badge-icon" }, config.icon),
react.createElement("span", { className: "tmi-badge-text" },
I18n.t(config.textKey)
)
);
};
// Full TMI View Component (replaces left panel content)
const TMIFullView = react.memo(({ info, onClose, trackName, artistName, coverUrl, onRegenerate, tmiScale: propTmiScale }) => {
// prop으로 받은 tmiScale 사용, 없으면 CONFIG에서 가져옴
const tmiScale = propTmiScale ?? (CONFIG?.visual?.["fullscreen-tmi-font-size"] || 100) / 100;
// Handle error state
if (info?.error) {
const isQuotaError = info.message?.includes('429') || info.message?.includes('quota') || info.message?.includes('RESOURCE_EXHAUSTED');
return react.createElement("div", {
className: "tmi-fullview tmi-fullview-error",
style: { "--tmi-scale": tmiScale }
},
react.createElement("div", { className: "tmi-fullview-header" },
coverUrl && react.createElement("img", {
src: coverUrl,
className: "tmi-fullview-cover"
}),
react.createElement("div", { className: "tmi-fullview-info" },
react.createElement("span", { className: "tmi-fullview-label" }, I18n.t("tmi.title")),
react.createElement("h2", { className: "tmi-fullview-track" }, trackName),
react.createElement("p", { className: "tmi-fullview-artist" }, artistName)
)
),
react.createElement("div", { className: "tmi-fullview-content tmi-error-content" },
react.createElement("div", { className: "tmi-error-icon" }, "⚠️"),
react.createElement("p", { className: "tmi-error-message" },
isQuotaError ? I18n.t("tmi.errorQuota") : I18n.t("tmi.errorFetch")
),
isQuotaError && react.createElement("p", { className: "tmi-error-hint" }, I18n.t("tmi.errorQuotaHint"))
),
react.createElement("div", { className: "tmi-fullview-footer" },
onRegenerate && react.createElement("button", {
className: "tmi-btn-regenerate",
onClick: onRegenerate,
title: I18n.t("tmi.regenerate")
},
react.createElement("span", { style: { fontSize: "18px", lineHeight: 1 } }, "↻")
),
react.createElement("button", {
className: "tmi-btn-close",
onClick: onClose
},
react.createElement("span", null, "✕"),
react.createElement("span", null, I18n.t("tmi.close"))
)
)
);
}
const track = info?.track || {};
const triviaList = track.trivia || [];
const description = track.description || '';
// New API structure: sources are in track.sources.verified, track.sources.related, and track.sources.other
const sources = track.sources || {};
const verifiedSources = sources.verified || [];
const relatedSources = sources.related || [];
const otherSources = sources.other || [];
const allSources = [...verifiedSources, ...relatedSources, ...otherSources];
// Reliability info from new API structure
const reliability = track.reliability || {};
return react.createElement("div", {
className: "tmi-fullview",
style: { "--tmi-scale": tmiScale }
},
// Header
react.createElement("div", { className: "tmi-fullview-header" },
coverUrl && react.createElement("img", {
src: coverUrl,
className: "tmi-fullview-cover"
}),
react.createElement("div", { className: "tmi-fullview-info" },
react.createElement("div", { className: "tmi-header-top" },
react.createElement("span", { className: "tmi-fullview-label" }, I18n.t("tmi.title"))
),
react.createElement("h2", { className: "tmi-fullview-track" }, trackName),
react.createElement("p", { className: "tmi-fullview-artist" }, artistName)
)
),
// Content - scrollable area
react.createElement("div", { className: "tmi-fullview-content" },
// Description with reliability badge
description && react.createElement("div", {
className: `tmi-fullview-description ${reliability.confidence || ''}`
},
react.createElement("p", null, renderMarkdown(description))
),
// All Trivia items
triviaList.length > 0 && react.createElement("div", { className: "tmi-fullview-trivia-list" },
react.createElement("div", { className: "tmi-fullview-trivia-label" },
I18n.t("tmi.didYouKnow")
),
triviaList.map((item, i) => react.createElement("div", {
key: i,
className: "tmi-fullview-trivia-item"
},
react.createElement("div", { className: "tmi-trivia-content" },
react.createElement("span", { className: "tmi-trivia-bullet" }, "✦"),
react.createElement("div", { className: "tmi-trivia-body" },
react.createElement("span", { className: "tmi-trivia-text" }, renderMarkdown(item))
)
)
))
),
// Sources section at the bottom
allSources.length > 0 && react.createElement("div", { className: "tmi-sources-section" },
react.createElement("div", { className: "tmi-sources-header" },
react.createElement("span", { className: "tmi-sources-label" }, I18n.t("tmi.sources")),
react.createElement(ReliabilityBadge, { reliability })
),
// Verified sources
verifiedSources.length > 0 && react.createElement("div", { className: "tmi-sources-group verified" },
react.createElement("span", { className: "tmi-sources-group-label" },
I18n.t("tmi.verifiedSources") + ` (${verifiedSources.length})`
),
renderSourceLinks(verifiedSources, true)
),
// Related sources
relatedSources.length > 0 && react.createElement("div", { className: "tmi-sources-group related" },
react.createElement("span", { className: "tmi-sources-group-label" },
I18n.t("tmi.relatedSources") + ` (${relatedSources.length})`
),
renderSourceLinks(relatedSources, false)
),
// Other sources
otherSources.length > 0 && react.createElement("div", { className: "tmi-sources-group other" },
react.createElement("span", { className: "tmi-sources-group-label" },
I18n.t("tmi.otherSources") + ` (${otherSources.length})`
),
renderSourceLinks(otherSources, false)
)
),
// No data fallback
!description && triviaList.length === 0 && react.createElement("div", { className: "tmi-fullview-empty" },
react.createElement("p", null, I18n.t("tmi.noData"))
)
),
// Footer with buttons
react.createElement("div", { className: "tmi-fullview-footer" },
onRegenerate && react.createElement("button", {
className: "tmi-btn-regenerate",
onClick: onRegenerate,
title: I18n.t("tmi.regenerate")
},
react.createElement("span", { style: { fontSize: "18px", lineHeight: 1 } }, "↻")
),
react.createElement("button", {
className: "tmi-btn-close",
onClick: onClose
},
react.createElement("span", null, "✕"),
react.createElement("span", null, I18n.t("tmi.close"))
)
)
);
});
// Loading View
const TMILoadingView = react.memo(({ onClose, tmiScale: propTmiScale }) => {
// prop으로 받은 tmiScale 사용, 없으면 CONFIG에서 가져옴
const tmiScale = propTmiScale ?? (CONFIG?.visual?.["fullscreen-tmi-font-size"] || 100) / 100;
return react.createElement("div", {
className: "tmi-fullview tmi-fullview-loading",
style: { "--tmi-scale": tmiScale }
},
react.createElement("div", { className: "tmi-fullview-header" },
react.createElement("span", { className: "tmi-fullview-label" }, I18n.t("tmi.title"))
),
react.createElement("div", { className: "tmi-fullview-content tmi-loading-content" },
react.createElement("div", { className: "tmi-loading-spinner" }),
react.createElement("p", null, I18n.t("tmi.loading"))
),
react.createElement("div", { className: "tmi-fullview-footer" },
react.createElement("button", {
className: "tmi-fullview-close-btn",
onClick: onClose
},
react.createElement("span", null, "✕"),
react.createElement("span", null, I18n.t("tmi.cancel"))
)
)
);
});
return { TMIFullView, TMILoadingView, fetchSongInfo, tmiCache };
})();
// Register globally
window.SongInfoTMI = SongInfoTMI;