/** * VaultScreen 重构使用示例 * * 这个文件展示了如何使用新创建的组件和 hooks 来简化 VaultScreen * * 使用方法: * 1. 将这些代码片段复制到 VaultScreen.tsx 中替换对应的部分 * 2. 确保导入了所有必要的组件 */ // ============================================ // 1. 导入新组件和 Hooks // ============================================ // 在文件顶部添加这些导入 import { VaultButton, LabeledInput, AssetCard } from '@/components/vault'; import { useAddFlow, useMnemonicFlow } from '@/hooks/vault'; // ============================================ // 2. 使用 Hooks 管理状态 // ============================================ export default function VaultScreen() { // 原来的代码: // const [addStep, setAddStep] = useState(1); // const [addMethod, setAddMethod] = useState<'text' | 'file' | 'scan'>('text'); // const [addVerified, setAddVerified] = useState(false); // const [rehearsalConfirmed, setRehearsalConfirmed] = useState(false); // const [selectedType, setSelectedType] = useState('custom'); // const [newLabel, setNewLabel] = useState(''); // const [treasureContent, setTreasureContent] = useState(''); // const [accountProvider, setAccountProvider] = useState<'bank' | 'steam' | 'facebook' | 'custom'>('bank'); // 新代码:使用 useAddFlow hook const addFlow = useAddFlow(); // 原来的代码: // const [mnemonicWords, setMnemonicWords] = useState([]); // const [mnemonicParts, setMnemonicParts] = useState([]); // const [mnemonicStep, setMnemonicStep] = useState<1 | 2 | 3 | 4 | 5>(1); // const [heirStep, setHeirStep] = useState<'decision' | 'asset' | 'heir' | 'summary'>('decision'); // const [replaceIndex, setReplaceIndex] = useState(null); // const [replaceQuery, setReplaceQuery] = useState(''); // const [progressIndex, setProgressIndex] = useState(0); // const [isCapturing, setIsCapturing] = useState(false); // 新代码:使用 useMnemonicFlow hook const mnemonicFlow = useMnemonicFlow(); // ... 其他状态保持不变 // ============================================ // 3. 更新 resetAddFlow 函数 // ============================================ const resetAddFlow = () => { // 原来的代码:需要手动重置每个状态 // setAddStep(1); // setAddMethod('text'); // setAddVerified(false); // setRehearsalConfirmed(false); // setSelectedType('custom'); // setNewLabel(''); // setAccountProvider('bank'); // 新代码:一行搞定 addFlow.reset(); }; // ============================================ // 4. 使用 AssetCard 组件渲染资产列表 // ============================================ // 原来的代码(在 return 语句中的资产列表部分,第 1089-1159 行): /* {assets.map((asset, index) => { const config = assetTypeConfig[asset.type]; if (!assetAnimations.current.has(asset.id)) { const anim = new Animated.Value(0); assetAnimations.current.set(asset.id, anim); Animated.spring(anim, { toValue: 1, useNativeDriver: true, tension: 65, friction: 10, delay: index * 80, }).start(); } const animValue = assetAnimations.current.get(asset.id) || new Animated.Value(1); return ( handleOpenDetail(asset)} > {renderAssetTypeIcon(config, 24, colors.vault.primary)} {config.label} {asset.label} Sealed {formatDate(asset.createdAt)} ); })} */ // 新代码:简洁清晰 const renderAssetList = () => ( {assets.map((asset, index) => ( ))} ); // ============================================ // 5. 使用 VaultButton 组件替换按钮 // ============================================ // 原来的代码(解锁按钮,第 1026-1041 行): /* {hasS0 ? 'Captain\'s Verification' : 'Enter Vault'} */ // 新代码: const renderUnlockButton = () => ( {hasS0 ? "Captain's Verification" : "Enter Vault"} ); // 原来的代码(添加按钮,第 1162-1180 行): /* { resetAddFlow(); clearAddError(); setShowAddModal(true); }} activeOpacity={0.9} > Add Treasure */ // 新代码: const renderAddButton = () => ( { resetAddFlow(); clearAddError(); setShowAddModal(true); }} style={styles.addButton} > Add Treasure ); // ============================================ // 6. 使用 LabeledInput 组件替换输入框 // ============================================ // 在 Add Modal 中(第 1238-1245 行): /* TREASURE TITLE */ // 新代码: const renderTitleInput = () => ( ); // 在 Add Modal 内容步骤中(第 1305-1315 行): /* CONTENT */ // 新代码: const renderContentInput = () => ( ); // ============================================ // 7. 在 Modal 中使用 VaultButton // ============================================ // 原来的模态框按钮代码(第 1428-1481 行): /* { if (addStep === 1) { setShowAddModal(false); setTreasureContent(''); clearAddError(); } else { setAddStep(addStep - 1); clearAddError(); } }} > {addStep === 1 ? 'Cancel' : 'Back'} {addStep < 3 ? ( setAddStep(addStep + 1)} > Continue ) : ( {isSealing ? 'Sealing...' : 'Seal Treasure'} )} */ // 新代码: const renderModalButtons = () => { const canSeal = addFlow.canProceed(); return ( { if (addFlow.state.step === 1) { setShowAddModal(false); addFlow.reset(); clearAddError(); } else { addFlow.setStep(addFlow.state.step - 1); clearAddError(); } }} fullWidth > {addFlow.state.step === 1 ? 'Cancel' : 'Back'} {addFlow.state.step < 3 ? ( addFlow.setStep(addFlow.state.step + 1)} fullWidth > Continue ) : ( {isSealing ? 'Sealing...' : 'Seal Treasure'} )} ); }; // ============================================ // 8. 使用 Hook 访问状态的示例 // ============================================ // 原来访问状态的方式: // if (addStep === 1) { ... } // if (mnemonicStep === 3) { ... } // setAddStep(2) // setMnemonicWords(words) // 新的访问方式: // if (addFlow.state.step === 1) { ... } // if (mnemonicFlow.state.step === 3) { ... } // addFlow.setStep(2) // mnemonicFlow.setWords(words) return ( // ... 使用上面定义的渲染函数 ); } // ============================================ // 9. 可以删除的代码 // ============================================ /* 重构后可以删除以下内容: 1. 大量的状态变量声明(第 111-167 行) 2. assetAnimations ref 和相关逻辑(第 171 行及使用处) 3. 资产卡片的动画代码(已移到 AssetCard 组件) 4. 所有重复的按钮样式定义 5. 所有重复的输入框样式定义 StyleSheet 中可以删除: - unlockButton, unlockButtonGradient, unlockButtonText - addButton, addButtonGradient, addButtonText - assetCard, assetIconContainer, assetInfo, assetType, assetLabel, assetMetaRow, assetMeta, encryptedBadge - 大部分 modal 相关的样式(已移到 modalStyles.ts) */ // ============================================ // 10. 性能优化建议 // ============================================ /* 1. 使用 React.memo 包装 AssetCard 避免不必要的重渲染 2. 使用 useCallback 包装事件处理函数 3. 考虑使用 FlatList 替代 ScrollView(如果资产列表很长) 4. 延迟加载模态框组件(React.lazy) 示例: const AssetList = React.memo(({ assets, onOpenDetail }) => ( assets.map((asset, index) => ( )) )); const handleOpenDetail = useCallback((asset: VaultAsset) => { setSelectedAsset(asset); setShowDetail(true); }, []); */