import React from 'react'; import { Pressable, ScrollView, StyleSheet, Text, View, } from 'react-native'; import type { DebugFeatureRenderProps } from '../../types'; import { getLocale } from '../../i18n'; import { Colors } from '../../ui/theme/colors'; import { FontSize, FontWeight, Radius, Spacing, } from '../../ui/theme/layout'; import type { QuickAccountItem, QuickAccountsFeature, QuickAccountsSnapshot, } from './types'; export function isQuickAccountSwitchDisabled(state: { busy: boolean; suspended: boolean; switchConfigured?: boolean; }): boolean { return state.busy || state.suspended || state.switchConfigured === false; } export const QuickAccountsTab: React.FC< DebugFeatureRenderProps > = React.memo(({ feature }) => { const quickAccountsFeature = feature as QuickAccountsFeature; const state = quickAccountsFeature.getViewState(); const { copy } = state; const zh = getLocale() === 'zh-CN'; return ( {copy.title} {state.contextLabel ? ( {state.contextLabel} ) : null} {copy.description} {state.isAuthenticated === undefined ? ( {zh ? '认证状态未知' : 'Authentication status unknown'} ) : null} {state.accounts.length > 0 && !state.switchConfigured ? ( {zh ? '尚未提供切换逻辑' : 'Account switching has not been configured.'} ) : null} {state.busy && state.suspended ? ( {zh ? '已请求取消,正在等待登录或补偿结束。' : 'Cancellation requested. Waiting for login or rollback to finish.'} ) : null} {state.isAuthenticated === false ? ( {copy.unauthenticatedTitle} {copy.unauthenticatedDescription} ) : null} {state.currentAccountDetails.length > 0 ? ( {copy.currentLabel} {state.currentAccountDetails.map((detail) => ( {detail.title} {detail.value} ))} ) : null} {state.errorMessage ? ( {state.errorMessage} ) : state.lastResult === 'success' ? ( {copy.successMessage} ) : null} {state.accounts.length === 0 ? ( {copy.emptyTitle} {copy.emptyDescription} ) : ( {state.accounts.map((account, index) => { const isCurrent = account.id === state.currentAccountId; const isLastUsed = account.id === state.lastUsedAccountId; const disabled = isQuickAccountSwitchDisabled(state); return ( { quickAccountsFeature .switchAccount(account.id) .catch(() => undefined); }} style={({ pressed }) => [ styles.accountRow, index < state.accounts.length - 1 && styles.accountDivider, pressed && !disabled && styles.accountPressed, disabled && styles.accountDisabled, ]} > {account.title} {isCurrent ? ( {copy.currentLabel} ) : isLastUsed ? ( {copy.lastUsedLabel} ) : null} {account.subtitle ? ( {account.subtitle} ) : null} {account.note ? {account.note} : null} {state.busy ? copy.switchingLabel : copy.switchLabel} ); })} )} ); }); const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: Colors.background, }, content: { padding: Spacing.LG, gap: Spacing.MD, }, intro: { gap: Spacing.XS, }, titleRow: { flexDirection: 'row', alignItems: 'center', gap: Spacing.SM, }, title: { color: Colors.text, fontSize: FontSize.XL, fontWeight: FontWeight.bold, flex: 1, }, description: { color: Colors.textSecondary, fontSize: FontSize.MD, lineHeight: 19, }, contextPill: { paddingHorizontal: Spacing.SM, paddingVertical: Spacing.XS, borderRadius: Radius.Pill, backgroundColor: Colors.primaryGhost, }, contextText: { color: Colors.primaryLight, fontSize: FontSize.XS, fontWeight: FontWeight.semibold, }, notice: { padding: Spacing.MD, gap: Spacing.XS, borderRadius: Radius.MD, backgroundColor: Colors.surfaceInset, borderWidth: StyleSheet.hairlineWidth, borderColor: Colors.border, }, noticeTitle: { color: Colors.text, fontSize: FontSize.MD, fontWeight: FontWeight.semibold, }, noticeText: { color: Colors.textMuted, fontSize: FontSize.SM, }, detailsCard: { padding: Spacing.MD, gap: Spacing.SM, borderRadius: Radius.MD, backgroundColor: Colors.surface, borderWidth: StyleSheet.hairlineWidth, borderColor: Colors.border, }, sectionLabel: { color: Colors.textMuted, fontSize: FontSize.XS, fontWeight: FontWeight.semibold, textTransform: 'uppercase', }, detailRow: { flexDirection: 'row', alignItems: 'center', gap: Spacing.MD, }, detailLabel: { color: Colors.textMuted, fontSize: FontSize.SM, width: 76, }, detailValue: { color: Colors.text, fontSize: FontSize.SM, flex: 1, }, resultCard: { padding: Spacing.MD, borderRadius: Radius.MD, }, errorCard: { backgroundColor: Colors.errorDim, }, successCard: { backgroundColor: Colors.successDim, }, errorText: { color: Colors.error, fontSize: FontSize.SM, }, successText: { color: Colors.success, fontSize: FontSize.SM, }, emptyCard: { alignItems: 'center', padding: Spacing.XXL, gap: Spacing.SM, borderRadius: Radius.MD, backgroundColor: Colors.surface, }, emptyTitle: { color: Colors.text, fontSize: FontSize.LG, fontWeight: FontWeight.semibold, }, emptyText: { color: Colors.textMuted, fontSize: FontSize.SM, textAlign: 'center', }, accountsCard: { borderRadius: Radius.MD, backgroundColor: Colors.surface, borderWidth: StyleSheet.hairlineWidth, borderColor: Colors.border, overflow: 'hidden', }, accountRow: { flexDirection: 'row', alignItems: 'center', gap: Spacing.MD, minHeight: 72, padding: Spacing.MD, }, accountDivider: { borderBottomWidth: StyleSheet.hairlineWidth, borderBottomColor: Colors.borderLight, }, accountPressed: { backgroundColor: Colors.surfaceHover, }, accountDisabled: { opacity: 0.62, }, accountBody: { flex: 1, gap: Spacing.XS, }, accountTitleRow: { flexDirection: 'row', alignItems: 'center', gap: Spacing.SM, }, accountLabel: { color: Colors.text, fontSize: FontSize.MD, fontWeight: FontWeight.semibold, flexShrink: 1, }, subtitle: { color: Colors.textSecondary, fontSize: FontSize.SM, }, note: { color: Colors.textMuted, fontSize: FontSize.XS, }, actionText: { color: Colors.primaryLight, fontSize: FontSize.SM, fontWeight: FontWeight.semibold, }, currentPill: { paddingHorizontal: Spacing.SM, paddingVertical: Spacing.XXS, borderRadius: Radius.Pill, backgroundColor: Colors.successDim, }, currentText: { color: Colors.success, fontSize: FontSize.XXS, fontWeight: FontWeight.semibold, }, recentPill: { paddingHorizontal: Spacing.SM, paddingVertical: Spacing.XXS, borderRadius: Radius.Pill, backgroundColor: Colors.primaryGhost, }, recentText: { color: Colors.primaryLight, fontSize: FontSize.XXS, fontWeight: FontWeight.semibold, }, });