Adrià Vilanova Martínez | 19f6a65 | 2023-11-15 19:38:50 +0100 | [diff] [blame] | 1 | import {createPlainTooltip} from '../../../../common/tooltip.js'; |
| 2 | import {kItemMetadataState, kItemMetadataStateI18n} from '../consts.js'; |
| 3 | |
| 4 | export 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 | |
Adrià Vilanova Martínez | 05c926b | 2024-02-28 23:52:21 +0100 | [diff] [blame^] | 82 | let stateLocalized; |
| 83 | if (kItemMetadataStateI18n[state]) { |
| 84 | const stateI18nKey = |
| 85 | 'inject_extrainfo_message_state_' + kItemMetadataStateI18n[state]; |
| 86 | stateLocalized = chrome.i18n.getMessage(stateI18nKey) ?? state; |
| 87 | } else { |
| 88 | stateLocalized = kItemMetadataState[state] ?? state; |
| 89 | } |
Adrià Vilanova Martínez | 19f6a65 | 2023-11-15 19:38:50 +0100 | [diff] [blame] | 90 | |
| 91 | const span = document.createElement('span'); |
| 92 | span.textContent = chrome.i18n.getMessage( |
| 93 | 'inject_extrainfo_message_state', [stateLocalized]); |
| 94 | span.title = kItemMetadataState[state] ?? state; |
| 95 | return span; |
| 96 | } |
| 97 | |
| 98 | static getShadowBlockChip(itemMetadata) { |
| 99 | const shadowBlockInfo = itemMetadata?.['10']; |
| 100 | const blockedTimestampMicros = shadowBlockInfo?.['2']; |
| 101 | if (!blockedTimestampMicros) return null; |
| 102 | |
| 103 | const isBlocked = shadowBlockInfo?.['1']; |
| 104 | let span = document.createElement('span'); |
| 105 | span.textContent = chrome.i18n.getMessage( |
| 106 | 'inject_extrainfo_message_shadowblock' + |
| 107 | (isBlocked ? 'active' : 'notactive')); |
| 108 | if (isBlocked) span.classList.add('TWPT-extrainfo-bad'); |
| 109 | return span; |
| 110 | } |
| 111 | } |