|
| 1 | +import {CustomElement, InjectAfter, InjectionMode} from '../injectors'; |
| 2 | +import {FloatElement} from '../custom'; |
| 3 | +import {css, html} from 'lit'; |
| 4 | +import {state} from 'lit/decorators.js'; |
| 5 | +import {FetchReversalStatusResponse} from '../../bridge/handlers/fetch_reversal_status'; |
| 6 | +import {gReversalFetcher} from '../../services/reversal_fetcher'; |
| 7 | +import {defined} from '../../utils/checkers'; |
| 8 | + |
| 9 | +@CustomElement() |
| 10 | +@InjectAfter( |
| 11 | + '.profile_in_game.persona + .profile_ban_status, .profile_in_game.persona:not(:has(+ .profile_ban_status))', |
| 12 | + InjectionMode.ONCE |
| 13 | +) |
| 14 | +export class ReversalStatus extends FloatElement { |
| 15 | + @state() |
| 16 | + reversalStatus: FetchReversalStatusResponse | undefined = undefined; |
| 17 | + |
| 18 | + static styles = [ |
| 19 | + ...FloatElement.styles, |
| 20 | + css` |
| 21 | + .container { |
| 22 | + display: flex; |
| 23 | + align-items: center; |
| 24 | + gap: 6px; |
| 25 | + color: #de6667; |
| 26 | + margin-bottom: 10px; |
| 27 | +
|
| 28 | + .warning { |
| 29 | + display: inline; |
| 30 | +
|
| 31 | + .info-link-container { |
| 32 | + color: #828282; |
| 33 | +
|
| 34 | + .info-link { |
| 35 | + text-decoration: none; |
| 36 | + color: #ebebeb; |
| 37 | +
|
| 38 | + &:hover { |
| 39 | + color: #66c0f4; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | +
|
| 44 | + .powered-by-container { |
| 45 | + font-size: 12px; |
| 46 | + color: #828282; |
| 47 | +
|
| 48 | + .powered-by-link { |
| 49 | + text-decoration: none; |
| 50 | + color: #ebebeb; |
| 51 | +
|
| 52 | + &:hover { |
| 53 | + color: #66c0f4; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + `, |
| 60 | + ]; |
| 61 | + |
| 62 | + get show(): boolean { |
| 63 | + return !!this.reversalStatus?.has_reversed; |
| 64 | + } |
| 65 | + |
| 66 | + get daysSinceLastReversal(): number | null { |
| 67 | + if (!this.reversalStatus?.last_reversal_timestamp) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + const now = Date.now(); |
| 72 | + const timeSince = now - this.reversalStatus.last_reversal_timestamp; |
| 73 | + return Math.floor(timeSince / (24 * 60 * 60 * 1000)); |
| 74 | + } |
| 75 | + |
| 76 | + getSteamId(): string | undefined { |
| 77 | + if (defined(typeof g_rgProfileData) && g_rgProfileData) { |
| 78 | + return g_rgProfileData.steamid; |
| 79 | + } |
| 80 | + |
| 81 | + const match = window.location.pathname.match(/^\/profiles\/(\d+)/); |
| 82 | + if (match) { |
| 83 | + return match[1]; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + async connectedCallback() { |
| 88 | + super.connectedCallback(); |
| 89 | + |
| 90 | + try { |
| 91 | + const steamId = this.getSteamId(); |
| 92 | + if (!steamId) { |
| 93 | + console.error('failed to get steam id'); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + this.reversalStatus = await gReversalFetcher.fetch({steam_id64: steamId}); |
| 98 | + } catch (e) { |
| 99 | + console.error('failed to fetch reversal status', e); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + protected render() { |
| 104 | + if (!this.show) { |
| 105 | + return html``; |
| 106 | + } |
| 107 | + |
| 108 | + const daysSince = this.daysSinceLastReversal ?? 0; |
| 109 | + const message = `${daysSince} day(s) since last trade reversal`; |
| 110 | + return html` |
| 111 | + <div class="container"> |
| 112 | + <div class="warning"> |
| 113 | + ${message} |
| 114 | + <span class="info-link-container"> |
| 115 | + | |
| 116 | + <a |
| 117 | + class="info-link" |
| 118 | + href="https://help.steampowered.com/en/faqs/view/365F-4BEE-2AE2-7BDD" |
| 119 | + target="_blank" |
| 120 | + rel="noreferrer" |
| 121 | + > |
| 122 | + Info |
| 123 | + </a> |
| 124 | + </span> |
| 125 | + <span class="powered-by-container" |
| 126 | + >(powered by <a class="powered-by-link" href="https://reverse.watch">reverse.watch</a>)</span |
| 127 | + > |
| 128 | + </div> |
| 129 | + </div> |
| 130 | + `; |
| 131 | + } |
| 132 | +} |
0 commit comments