|
| 1 | +<script lang="ts"> |
| 2 | + import { onMount } from 'svelte' |
| 3 | + import { emitTo } from '@tauri-apps/api/event' |
| 4 | + import { getCurrentWindow } from '@tauri-apps/api/window' |
| 5 | + import SettingsSection from '../components/SettingsSection.svelte' |
| 6 | + import { |
| 7 | + openExternalUrl, |
| 8 | + getLicenseInfo, |
| 9 | + getLicenseStatus, |
| 10 | + type LicenseInfo, |
| 11 | + type LicenseStatus, |
| 12 | + } from '$lib/tauri-commands' |
| 13 | + import Button from '$lib/ui/Button.svelte' |
| 14 | +
|
| 15 | + let licenseInfo = $state<LicenseInfo | null>(null) |
| 16 | + let licenseStatus = $state<LicenseStatus | null>(null) |
| 17 | + let isLoading = $state(true) |
| 18 | +
|
| 19 | + onMount(async () => { |
| 20 | + try { |
| 21 | + const [info, status] = await Promise.all([ |
| 22 | + getLicenseInfo().catch(() => null), |
| 23 | + getLicenseStatus().catch(() => null), |
| 24 | + ]) |
| 25 | + licenseInfo = info |
| 26 | + licenseStatus = status |
| 27 | + } finally { |
| 28 | + isLoading = false |
| 29 | + } |
| 30 | + }) |
| 31 | +
|
| 32 | + function getLicenseTypeLabel(): string { |
| 33 | + if (!licenseInfo) return 'Personal (free)' |
| 34 | + if (licenseInfo.licenseType === 'commercial_perpetual') return 'Commercial perpetual' |
| 35 | + if (licenseInfo.licenseType === 'commercial_subscription') return 'Commercial subscription' |
| 36 | + if (licenseInfo.licenseType === 'supporter') return 'Supporter' |
| 37 | + return 'Personal (free)' |
| 38 | + } |
| 39 | +
|
| 40 | + function formatDate(dateStr: string | null | undefined): string { |
| 41 | + if (!dateStr) return '' |
| 42 | + try { |
| 43 | + return new Date(dateStr).toLocaleDateString(undefined, { |
| 44 | + year: 'numeric', |
| 45 | + month: 'long', |
| 46 | + day: 'numeric', |
| 47 | + }) |
| 48 | + } catch { |
| 49 | + return dateStr |
| 50 | + } |
| 51 | + } |
| 52 | +
|
| 53 | + function getStatusText(): string | null { |
| 54 | + if (!licenseStatus) return null |
| 55 | + if (licenseStatus.type === 'expired') return `Expired on ${formatDate(licenseStatus.expiredAt)}` |
| 56 | + if (licenseStatus.type === 'commercial') { |
| 57 | + if (licenseStatus.licenseType === 'commercial_perpetual') { |
| 58 | + return licenseStatus.expiresAt ? `Updates until ${formatDate(licenseStatus.expiresAt)}` : 'Active' |
| 59 | + } |
| 60 | + return licenseStatus.expiresAt ? `Valid until ${formatDate(licenseStatus.expiresAt)}` : 'Active' |
| 61 | + } |
| 62 | + return null |
| 63 | + } |
| 64 | +
|
| 65 | + const hasLicense = $derived(licenseInfo !== null) |
| 66 | + const statusText = $derived(getStatusText()) |
| 67 | +
|
| 68 | + async function handleManageLicense() { |
| 69 | + await emitTo('main', 'execute-command', { commandId: 'app.licenseKey' }) |
| 70 | + await getCurrentWindow().close() |
| 71 | + } |
| 72 | +
|
| 73 | + async function handleBuyLicense() { |
| 74 | + await openExternalUrl('https://getcmdr.com/pricing') |
| 75 | + } |
| 76 | +</script> |
| 77 | + |
| 78 | +<SettingsSection title="License"> |
| 79 | + {#if isLoading} |
| 80 | + <p class="loading-text">Loading...</p> |
| 81 | + {:else} |
| 82 | + <div class="license-info"> |
| 83 | + <div class="info-row"> |
| 84 | + <span class="info-label">License type</span> |
| 85 | + <span class="info-value">{getLicenseTypeLabel()}</span> |
| 86 | + </div> |
| 87 | + {#if licenseInfo?.organizationName} |
| 88 | + <div class="info-row"> |
| 89 | + <span class="info-label">Organization</span> |
| 90 | + <span class="info-value">{licenseInfo.organizationName}</span> |
| 91 | + </div> |
| 92 | + {/if} |
| 93 | + {#if statusText} |
| 94 | + <div class="info-row"> |
| 95 | + <span class="info-label">Status</span> |
| 96 | + <span |
| 97 | + class="info-value" |
| 98 | + class:status-expired={licenseStatus?.type === 'expired'} |
| 99 | + class:status-active={licenseStatus?.type === 'commercial' || |
| 100 | + licenseStatus?.type === 'supporter'}>{statusText}</span |
| 101 | + > |
| 102 | + </div> |
| 103 | + {/if} |
| 104 | + {#if licenseInfo?.shortCode} |
| 105 | + <div class="info-row"> |
| 106 | + <span class="info-label">License key</span> |
| 107 | + <span class="info-value mono">{licenseInfo.shortCode}</span> |
| 108 | + </div> |
| 109 | + {/if} |
| 110 | + </div> |
| 111 | + |
| 112 | + <div class="actions"> |
| 113 | + {#if hasLicense} |
| 114 | + <Button variant="secondary" onclick={handleManageLicense}>Manage license key</Button> |
| 115 | + {:else} |
| 116 | + <Button variant="secondary" onclick={handleManageLicense}>Enter license key</Button> |
| 117 | + <Button variant="secondary" onclick={handleBuyLicense}>Get a license</Button> |
| 118 | + {/if} |
| 119 | + </div> |
| 120 | + {/if} |
| 121 | +</SettingsSection> |
| 122 | + |
| 123 | +<style> |
| 124 | + .loading-text { |
| 125 | + color: var(--color-text-tertiary); |
| 126 | + font-size: var(--font-size-sm); |
| 127 | + margin: 0; |
| 128 | + } |
| 129 | +
|
| 130 | + .license-info { |
| 131 | + background: var(--color-bg-tertiary); |
| 132 | + border-radius: var(--radius-lg); |
| 133 | + padding: var(--spacing-xs) var(--spacing-lg); |
| 134 | + margin-bottom: var(--spacing-lg); |
| 135 | + } |
| 136 | +
|
| 137 | + .info-row { |
| 138 | + display: flex; |
| 139 | + justify-content: space-between; |
| 140 | + align-items: center; |
| 141 | + gap: var(--spacing-xl); |
| 142 | + padding: var(--spacing-sm) 0; |
| 143 | + } |
| 144 | +
|
| 145 | + .info-row:not(:last-child) { |
| 146 | + border-bottom: 1px solid var(--color-border-subtle); |
| 147 | + } |
| 148 | +
|
| 149 | + .info-label { |
| 150 | + font-size: var(--font-size-sm); |
| 151 | + color: var(--color-text-secondary); |
| 152 | + flex-shrink: 0; |
| 153 | + } |
| 154 | +
|
| 155 | + .info-value { |
| 156 | + font-size: var(--font-size-sm); |
| 157 | + color: var(--color-text-primary); |
| 158 | + font-weight: 500; |
| 159 | + text-align: right; |
| 160 | + } |
| 161 | +
|
| 162 | + .info-value.mono { |
| 163 | + font-family: var(--font-mono); |
| 164 | + letter-spacing: 0.02em; |
| 165 | + } |
| 166 | +
|
| 167 | + .info-value.status-expired { |
| 168 | + color: var(--color-error); |
| 169 | + } |
| 170 | +
|
| 171 | + .info-value.status-active { |
| 172 | + color: var(--color-toast-success-stripe); |
| 173 | + } |
| 174 | +
|
| 175 | + .actions { |
| 176 | + display: flex; |
| 177 | + gap: var(--spacing-md); |
| 178 | + } |
| 179 | +</style> |
0 commit comments