Skip to content

refactor(record): pass isNewSet as prop from parent components to SetOptions chain #215

@andrewck24

Description

@andrewck24

📝 Description

目前 isNewSet 的計算邏輯分散在 Options 元件內部(第 40 行),但根據元件架構,這個邏輯應該在使用 SetOptions 實例的父層元件(如 SetsListRecord/Interval)中計算,然後透過 props 層層傳遞到最終的 Options 元件。

🔍 Current Implementation

在 options.tsx 第 40 行:

const isNewSet = setIndex === record?.sets.length;

父層元件使用方式:

  • SetsList 元件:<SetOptions recordId={recordId} setIndex={setIndex} />
  • Record/Interval 元件:<SetOptions recordId={recordId} setIndex={setIndex + 1} />

🎯 Expected Implementation

  1. SetsListRecord/Interval 父元件中計算 isNewSet
  2. 透過 props 層層傳遞:父元件 → SetOptionsSetOptionsPanelOptions
  3. 移除 Options 元件內部的計算邏輯

📂 Files to Modify

  • list.tsx (SetsList 元件)
  • index.tsx (Record/Interval 元件)
  • index.tsx (SetOptions 元件)
  • index.tsx (SetOptionsPanel 元件)
  • options.tsx (Options 元件)

✅ Acceptance Criteria

  • SetsList 元件計算 isNewSet 並傳遞給 SetOptions
  • Record/Interval 元件計算 isNewSet 並傳遞給 SetOptions
  • SetOptions 元件接收並傳遞 isNewSet prop
  • SetOptionsPanel 元件接收並傳遞 isNewSet prop
  • Options 元件接收 isNewSet 作為 prop,移除內部計算邏輯
  • 所有相關的 TypeScript 介面都已更新
  • 功能保持不變,現有行為不受影響

🛠 Implementation Steps

  1. 修改 SetsList 元件

    // 在 handleOptionsOpen 中計算
    const handleOptionsOpen = (setIndex: number) => {
      const isNewSet = setIndex === record.sets.length;
      setSetIndex(setIndex);
      setOptionsOpen(true);
    };
    
    // 傳遞 isNewSet prop
    <SetOptions recordId={recordId} setIndex={setIndex} isNewSet={isNewSet} />
  2. 修改 Record/Interval 元件

    // 在 Interval 元件中
    const isNewSet = true; // setIndex + 1 總是新一局
    <SetOptions recordId={recordId} setIndex={setIndex + 1} isNewSet={isNewSet} />
  3. 更新 SetOptions 元件介面

    export const SetOptions = ({
      recordId,
      setIndex,
      isNewSet,
    }: {
      recordId: string;
      setIndex: number;
      isNewSet: boolean;
    }) => {
  4. 更新 SetOptionsPanel 元件

    <SetOptionsPanel recordId={recordId} isNewSet={isNewSet} />
  5. 更新 Options 元件

    export const Options = ({ 
      recordId, 
      isNewSet 
    }: { 
      recordId: string; 
      isNewSet: boolean; 
    }) => {
      // 移除內部計算
      // const isNewSet = setIndex === record?.sets.length;

🏷 Labels

refactoring component-architecture typescript props-drilling medium-priority

💡 Benefits

  • 將業務邏輯提升到適當的層級
  • 提高元件的可預測性和可測試性
  • 減少元件對外部資料的依賴
  • 改善程式碼的可維護性
  • 使 props 流向更加清晰

⚠️ Notes

這個重構涉及多層元件的 props 傳遞,需要確保每一層都正確接收和傳遞 isNewSet 值,避免破壞現有功能。

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions