The modal held the state of seven independent features next to the staged settings form. Each feature now has its own file with a hook for its state and the rows that show it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
159 lines
7.2 KiB
TypeScript
159 lines
7.2 KiB
TypeScript
import { Check, Copy } from 'lucide-react'
|
|
import React, { useEffect, useRef, useState } from 'react'
|
|
import { I18nKey, t } from '../../i18n'
|
|
import { createVMSettingRequests, type VMSettingState } from '../../lib/vmSettingRequests'
|
|
import { Locale, VMDetail } from '../../types'
|
|
import { IconButton } from '../../ui/controls'
|
|
import { PopupSelect, SetRow } from '../../ui/settings'
|
|
import type { ToastInput } from '../../ui/toast'
|
|
import { api } from '../../wails'
|
|
|
|
type VMTimezoneStatus = Awaited<ReturnType<typeof api.GetVMTimezone>>
|
|
|
|
const GUEST_TOOLS_INSTALL_CMD = 'sh /Volumes/REIMSTOOLS/Install-Reims-Tools.command'
|
|
const GUEST_TIMEZONE_APPLY_CMD = 'sh /Volumes/REIMSTOOLS/Apply-Timezone.command'
|
|
|
|
const GUEST_TIMEZONE_PRESETS: ReadonlyArray<{ value: string; labelKey: I18nKey; detail?: string }> = [
|
|
{ value: 'Asia/Shanghai', labelKey: 'guestTimezoneShanghai', detail: 'Asia/Shanghai' },
|
|
{ value: 'Asia/Hong_Kong', labelKey: 'guestTimezoneHongKong', detail: 'Asia/Hong_Kong' },
|
|
{ value: 'Asia/Taipei', labelKey: 'guestTimezoneTaipei', detail: 'Asia/Taipei' },
|
|
{ value: 'Asia/Tokyo', labelKey: 'guestTimezoneTokyo', detail: 'Asia/Tokyo' },
|
|
{ value: 'Asia/Seoul', labelKey: 'guestTimezoneSeoul', detail: 'Asia/Seoul' },
|
|
{ value: 'Asia/Singapore', labelKey: 'guestTimezoneSingapore', detail: 'Asia/Singapore' },
|
|
{ value: 'Europe/London', labelKey: 'guestTimezoneLondon', detail: 'Europe/London' },
|
|
{ value: 'Europe/Berlin', labelKey: 'guestTimezoneBerlin', detail: 'Europe/Berlin' },
|
|
{ value: 'America/Los_Angeles', labelKey: 'guestTimezoneLosAngeles', detail: 'America/Los_Angeles' },
|
|
{ value: 'America/New_York', labelKey: 'guestTimezoneNewYork', detail: 'America/New_York' },
|
|
{ value: 'Australia/Sydney', labelKey: 'guestTimezoneSydney', detail: 'Australia/Sydney' },
|
|
{ value: 'UTC', labelKey: 'guestTimezoneUTC' },
|
|
]
|
|
|
|
/** The time zone the VM follows, refreshed while shown. */
|
|
export function useGuestTimezone(driverID: string, vm: string, running: boolean, shown: boolean) {
|
|
const vmSettingsKey = `${driverID}:${vm}`
|
|
const activeVMKeyRef = useRef(vmSettingsKey)
|
|
activeVMKeyRef.current = vmSettingsKey
|
|
const [{ value: tzStatus, error: tzErr, busy: tzBusy }, setTzState] = useState<VMSettingState<VMTimezoneStatus>>({ value: null, error: '', busy: false })
|
|
const [tzRequests] = useState(() => createVMSettingRequests<VMTimezoneStatus>(() => activeVMKeyRef.current,
|
|
(patch) => setTzState((state) => ({ ...state, ...patch }))))
|
|
|
|
useEffect(() => {
|
|
setTzState({ value: null, error: '', busy: tzRequests.isWriting(vmSettingsKey) })
|
|
}, [vmSettingsKey, tzRequests])
|
|
|
|
useEffect(() => {
|
|
if (!shown) return
|
|
const { refresh, stop } = tzRequests.watch(vmSettingsKey, () => api.GetVMTimezone(driverID, vm))
|
|
void refresh()
|
|
const timer = window.setInterval(refresh, 3000)
|
|
return () => { stop(); window.clearInterval(timer) }
|
|
}, [shown, driverID, vm, running, vmSettingsKey, tzRequests])
|
|
|
|
return {
|
|
tzStatus,
|
|
tzErr,
|
|
tzBusy,
|
|
/** Whether a change is being saved right now, ahead of the next render. */
|
|
isWriting: () => tzRequests.isWriting(vmSettingsKey),
|
|
write: (setting: string) => tzRequests.write(vmSettingsKey, () => api.SetVMTimezone(driverID, vm, setting)),
|
|
}
|
|
}
|
|
|
|
interface Props {
|
|
locale: Locale
|
|
detail: VMDetail
|
|
hostTimezone?: string
|
|
state: ReturnType<typeof useGuestTimezone>
|
|
disabled: boolean
|
|
isBlocked: () => boolean
|
|
onToast: (toast: ToastInput) => void
|
|
onCopy?: (text: string) => void
|
|
}
|
|
|
|
export const GuestTimezoneRow: React.FC<Props> = ({ locale, detail, hostTimezone, state, disabled, isBlocked, onToast, onCopy }) => {
|
|
const tr = (k: I18nKey, v?: Record<string, string | number>) => t(locale, k, v)
|
|
const { tzStatus, tzErr, tzBusy, write } = state
|
|
|
|
const updateTimezone = async (nextSetting: string) => {
|
|
if (isBlocked() || !tzStatus) return
|
|
const currentSetting = tzStatus.mode === 'off' ? 'off' : tzStatus.mode === 'custom' ? tzStatus.timezone : 'host'
|
|
if (nextSetting === currentSetting) return
|
|
if (await write(nextSetting)) {
|
|
onToast({ kind: 'ok', title: tr('guestTimezoneSaved') })
|
|
}
|
|
}
|
|
|
|
const detectedHostTZ = tzStatus?.host_timezone || hostTimezone || ''
|
|
const tzSelectedValue = tzStatus?.mode === 'custom' ? tzStatus.timezone : tzStatus?.mode
|
|
const tzOptions = [
|
|
{ value: 'host', label: tr('guestTimezoneHost'), detail: detectedHostTZ || undefined },
|
|
...GUEST_TIMEZONE_PRESETS.map((p) => ({
|
|
value: p.value,
|
|
label: tr(p.labelKey),
|
|
detail: p.detail,
|
|
})),
|
|
...(tzStatus?.mode === 'custom' && tzStatus.timezone && !GUEST_TIMEZONE_PRESETS.some((p) => p.value === tzStatus.timezone)
|
|
? [{ value: tzStatus.timezone, label: tzStatus.timezone }]
|
|
: []),
|
|
{ value: 'off', label: tr('guestTimezoneOff') },
|
|
]
|
|
let tzDesc: React.ReactNode = tr('guestTimezoneDesc')
|
|
let tzCommand = ''
|
|
if (tzStatus) {
|
|
const tzEffective = tzStatus.effective_timezone
|
|
if (tzStatus.mode === 'off') {
|
|
tzDesc = tr('guestTimezoneOffDesc')
|
|
} else if (detail.guest_tools_disabled) {
|
|
tzDesc = <span className="warn-text">{tr('gtStatusOffTip')}</span>
|
|
} else if (!tzEffective) {
|
|
tzDesc = <span className="warn-text">{tr('guestTimezoneHostUnknown')}</span>
|
|
} else if (tzStatus.running && tzStatus.guest_connected) {
|
|
const tzFailure = tzStatus.timezone_error
|
|
? <span className="warn-text">{tr('guestTimezoneApplyFailed', { tz: tzEffective })}</span>
|
|
: null
|
|
if (tzStatus.guest_agent_version < tzStatus.required_agent_version) {
|
|
tzDesc = <span className="warn-text">{tr(detail.guest_tools_attached ? 'guestTimezoneUpgradeHint' : 'guestTimezoneUpgradeAttachHint', { version: tzStatus.required_agent_version })}</span>
|
|
if (detail.guest_tools_attached) tzCommand = GUEST_TOOLS_INSTALL_CMD
|
|
} else if (!tzStatus.helper_ready) {
|
|
const hint = tr(detail.guest_tools_attached ? 'guestTimezoneApplyHint' : 'guestTimezoneApplyAttachHint')
|
|
tzDesc = tzFailure ? <div className="stack-1">{tzFailure}<span>{hint}</span></div> : hint
|
|
if (detail.guest_tools_attached) tzCommand = GUEST_TIMEZONE_APPLY_CMD
|
|
} else if (tzFailure) {
|
|
tzDesc = tzFailure
|
|
} else if (tzStatus.guest_applied === tzEffective) {
|
|
tzDesc = <span className="ok-text"><Check size={12} aria-hidden="true" />{tr('guestTimezoneSynced', { tz: tzEffective })}</span>
|
|
} else {
|
|
tzDesc = tr('guestTimezoneSyncing', { tz: tzEffective })
|
|
}
|
|
} else if (detail.guest_tools_attached) {
|
|
tzCommand = GUEST_TOOLS_INSTALL_CMD
|
|
}
|
|
}
|
|
|
|
return (
|
|
<SetRow
|
|
title={tr('guestTimezoneTitle')}
|
|
desc={tzDesc}
|
|
disabled={!tzStatus || tzBusy || disabled}
|
|
error={tzErr || undefined}
|
|
control={
|
|
<PopupSelect
|
|
label={tr('guestTimezoneTitle')}
|
|
value={tzSelectedValue}
|
|
placeholder={tr('guestTimezoneLoading')}
|
|
onChange={updateTimezone}
|
|
options={tzOptions}
|
|
/>
|
|
}
|
|
block={tzCommand ? (
|
|
<div className="cmd-line">
|
|
<code>{tzCommand}</code>
|
|
{onCopy && (
|
|
<IconButton small label={tr('copy')} icon={<Copy size={14} />} onClick={() => onCopy(tzCommand)} />
|
|
)}
|
|
</div>
|
|
) : undefined}
|
|
/>
|
|
)
|
|
}
|