|
| 1 | +import { css, html, LitElement } from 'lit'; |
| 2 | +import { property, state } from 'lit/decorators.js'; |
| 3 | +import type { UAParser } from 'ua-parser-js'; |
| 4 | +import baseStyles from './styles/base'; |
| 5 | + |
| 6 | +const OS_ICON_MAP = { |
| 7 | + Windows: 'i-logos:microsoft-windows-icon', |
| 8 | + macOS: 'i-logos:apple', |
| 9 | + Linux: 'i-logos:linux-tux', |
| 10 | + Android: 'i-logos:android-icon', |
| 11 | + iOS: 'i-logos:apple', |
| 12 | + 'Chrome OS': 'i-logos:chrome', |
| 13 | + Arch: 'i-logos:archlinux', |
| 14 | + Manjaro: 'i-logos:manjaro', |
| 15 | + Ubuntu: 'i-logos:ubuntu', |
| 16 | + Fedora: 'i-logos:fedora', |
| 17 | +}; |
| 18 | + |
| 19 | +const BROWSER_ICON_MAP = { |
| 20 | + Chrome: 'i-logos:chrome', |
| 21 | + 'Mobile Chrome': 'i-logos:chrome', |
| 22 | + 'Chrome WebView': 'i-logos:chrome', |
| 23 | + Firefox: 'i-logos:firefox', |
| 24 | + 'Mobile Firefox': 'i-logos:firefox', |
| 25 | + Safari: 'i-logos:safari', |
| 26 | + 'Mobile Safari': 'i-logos:safari', |
| 27 | + Edge: 'i-logos:microsoft-edge', |
| 28 | + 'Edge WebView': 'i-logos:microsoft-edge', |
| 29 | + 'Edge WebView2': 'i-logos:microsoft-edge', |
| 30 | + Opera: 'i-logos:opera', |
| 31 | +}; |
| 32 | + |
| 33 | +export class CommenterUABar extends LitElement { |
| 34 | + @property({ type: String }) |
| 35 | + ua: string = ''; |
| 36 | + |
| 37 | + @state() |
| 38 | + parser: UAParser | undefined; |
| 39 | + |
| 40 | + getOSIcon(os?: string) { |
| 41 | + return OS_ICON_MAP[os as keyof typeof OS_ICON_MAP]; |
| 42 | + } |
| 43 | + |
| 44 | + getBrowserIcon(browser?: string) { |
| 45 | + return BROWSER_ICON_MAP[browser as keyof typeof BROWSER_ICON_MAP]; |
| 46 | + } |
| 47 | + |
| 48 | + override connectedCallback(): void { |
| 49 | + super.connectedCallback(); |
| 50 | + this.init(); |
| 51 | + } |
| 52 | + |
| 53 | + async init() { |
| 54 | + const { UAParser } = await import('ua-parser-js'); |
| 55 | + this.parser = new UAParser(this.ua); |
| 56 | + } |
| 57 | + |
| 58 | + protected override render() { |
| 59 | + if (!this.parser) { |
| 60 | + return html``; |
| 61 | + } |
| 62 | + |
| 63 | + const osIcon = this.getOSIcon(this.parser.getOS().name); |
| 64 | + const browserIcon = this.getBrowserIcon(this.parser.getBrowser().name); |
| 65 | + |
| 66 | + return html`<div class="inline-flex gap-2 items-center"> |
| 67 | + <div class="inline-flex items-center gap-1 bg-muted-3 rounded-md px-1.5 py-1"> |
| 68 | + ${osIcon ? html`<i class="${osIcon} opacity-90 size-3"></i>` : ''} |
| 69 | + <span class="text-xs text-text-2">${this.parser.getOS().name}</span> |
| 70 | + </div> |
| 71 | + <div class="inline-flex items-center gap-1 bg-muted-3 rounded-md px-1.5 py-1"> |
| 72 | + ${browserIcon ? html`<i class="${browserIcon} opacity-90 size-3"></i>` : ''} |
| 73 | + <span class="text-xs text-text-2">${this.parser.getBrowser().name}</span> |
| 74 | + </div> |
| 75 | + </div>`; |
| 76 | + } |
| 77 | + |
| 78 | + static override styles = [ |
| 79 | + ...baseStyles, |
| 80 | + css` |
| 81 | + @unocss-placeholder; |
| 82 | + `, |
| 83 | + ]; |
| 84 | +} |
| 85 | + |
| 86 | +customElements.get('commenter-ua-bar') || |
| 87 | + customElements.define('commenter-ua-bar', CommenterUABar); |
| 88 | + |
| 89 | +declare global { |
| 90 | + interface HTMLElementTagNameMap { |
| 91 | + 'commenter-ua-bar': CommenterUABar; |
| 92 | + } |
| 93 | +} |
0 commit comments