blob: 6904e3eb28f01c1b821a1012dec0e6c9b84332e3 [file] [log] [blame]
Adrià Vilanova Martínez19f6a652023-11-15 19:38:50 +01001import {createPlainTooltip} from '../../../../common/tooltip.js';
2import {kItemMetadataState, kItemMetadataStateI18n} from '../consts.js';
3
4export default class StatesExtraInfoService {
5 static getPendingStateChip(endPendingStateTimestampMicros) {
6 const endPendingStateTimestamp =
7 Math.floor(endPendingStateTimestampMicros / 1e3);
8 const now = Date.now();
9 if (!endPendingStateTimestampMicros || endPendingStateTimestamp < now)
10 return [null, null];
11
12 const span = document.createElement('span');
13 span.textContent =
14 chrome.i18n.getMessage('inject_extrainfo_message_pendingstate');
15
16 const date = new Date(endPendingStateTimestamp).toLocaleString();
17 const pendingTooltip = createPlainTooltip(
18 span,
19 chrome.i18n.getMessage(
20 'inject_extrainfo_message_pendingstate_tooltip', [date]),
21 false);
22 return [span, pendingTooltip];
23 }
24
25 static getLiveReviewStatusChip(liveReviewStatus) {
26 const verdict = liveReviewStatus?.['1'];
27 if (!verdict) return [null, null];
28
29 const [label, labelClass] = this.getLiveReviewStatusLabel(verdict);
30 if (!label || !labelClass) return [null, null];
31
32 const reviewedBy = liveReviewStatus?.['2'];
33 const timestamp = liveReviewStatus?.['3'];
34 const date = (new Date(Math.floor(timestamp / 1e3))).toLocaleString();
35
36 let a = document.createElement('a');
37 a.href = 'https://support.google.com/s/community/user/' + reviewedBy;
38 a.classList.add(labelClass);
39 a.textContent = chrome.i18n.getMessage(
40 'inject_extrainfo_message_livereviewverdict',
41 [chrome.i18n.getMessage(
42 'inject_extrainfo_message_livereviewverdict_' + label)]);
43 let liveReviewTooltip = createPlainTooltip(a, date, false);
44 return [a, liveReviewTooltip];
45 }
46
47 static getLiveReviewStatusLabel(verdict) {
48 let label, labelClass;
49 switch (verdict) {
50 case 1: // LIVE_REVIEW_RELEVANT
51 label = 'relevant';
52 labelClass = 'TWPT-extrainfo-good';
53 break;
54
55 case 2: // LIVE_REVIEW_OFF_TOPIC
56 label = 'offtopic';
57 labelClass = 'TWPT-extrainfo-bad';
58 break;
59
60 case 3: // LIVE_REVIEW_ABUSE
61 label = 'abuse';
62 labelClass = 'TWPT-extrainfo-bad';
63 break;
64
65 default:
66 return [null, null];
67 }
68 return [label, labelClass];
69 }
70
71 static getMetadataChips(itemMetadata) {
72 return [
73 this.getStateChip(itemMetadata),
74 this.getShadowBlockChip(itemMetadata),
75 ].filter(chip => chip !== null);
76 }
77
78 static getStateChip(itemMetadata) {
79 const state = itemMetadata?.['1'];
80 if (!state || state == 1) return null;
81
82 const stateI18nKey =
83 'inject_extrainfo_message_state_' + kItemMetadataStateI18n[state];
84 const stateLocalized = chrome.i18n.getMessage(stateI18nKey) ?? state;
85
86 const span = document.createElement('span');
87 span.textContent = chrome.i18n.getMessage(
88 'inject_extrainfo_message_state', [stateLocalized]);
89 span.title = kItemMetadataState[state] ?? state;
90 return span;
91 }
92
93 static getShadowBlockChip(itemMetadata) {
94 const shadowBlockInfo = itemMetadata?.['10'];
95 const blockedTimestampMicros = shadowBlockInfo?.['2'];
96 if (!blockedTimestampMicros) return null;
97
98 const isBlocked = shadowBlockInfo?.['1'];
99 let span = document.createElement('span');
100 span.textContent = chrome.i18n.getMessage(
101 'inject_extrainfo_message_shadowblock' +
102 (isBlocked ? 'active' : 'notactive'));
103 if (isBlocked) span.classList.add('TWPT-extrainfo-bad');
104 return span;
105 }
106}