refactor: apply CodeRabbit suggestions for segment skip feature

- Add missing segment types (recap, commercial, preview) to JobStatus
- Consolidate duplicate useMemo blocks with factory function
- Improve code maintainability and consistency
This commit is contained in:
Uruk
2026-01-14 14:04:27 +01:00
committed by Gauvain
parent 0b6639fc4e
commit bb491d4e86
2 changed files with 62 additions and 69 deletions

View File

@@ -11,6 +11,37 @@ import { PlatformDropdown } from "@/components/PlatformDropdown";
import DisabledSetting from "@/components/settings/DisabledSetting"; import DisabledSetting from "@/components/settings/DisabledSetting";
import { useSettings } from "@/utils/atoms/settings"; import { useSettings } from "@/utils/atoms/settings";
/**
* Factory function to create skip options for a specific segment type
* Reduces code duplication across all 5 segment types
*/
const useSkipOptions = (
settingKey:
| "skipIntro"
| "skipOutro"
| "skipRecap"
| "skipCommercial"
| "skipPreview",
settings: ReturnType<typeof useSettings>["settings"],
updateSettings: ReturnType<typeof useSettings>["updateSettings"],
t: TFunction<"translation", undefined>,
) => {
return useMemo(
() => [
{
options: SEGMENT_SKIP_OPTIONS(t).map((option) => ({
type: "radio" as const,
label: option.label,
value: option.value,
selected: option.value === settings[settingKey],
onPress: () => updateSettings({ [settingKey]: option.value }),
})),
},
],
[settings[settingKey], updateSettings, t, settingKey],
);
};
export default function SegmentSkipPage() { export default function SegmentSkipPage() {
const { settings, updateSettings, pluginSettings } = useSettings(); const { settings, updateSettings, pluginSettings } = useSettings();
const { t } = useTranslation(); const { t } = useTranslation();
@@ -22,79 +53,35 @@ export default function SegmentSkipPage() {
}); });
}, [navigation, t]); }, [navigation, t]);
const skipIntroOptions = useMemo( const skipIntroOptions = useSkipOptions(
() => [ "skipIntro",
{ settings,
options: SEGMENT_SKIP_OPTIONS(t).map((option) => ({ updateSettings,
type: "radio" as const, t,
label: option.label,
value: option.value,
selected: option.value === settings.skipIntro,
onPress: () => updateSettings({ skipIntro: option.value }),
})),
},
],
[settings.skipIntro, updateSettings, t],
); );
const skipOutroOptions = useSkipOptions(
const skipOutroOptions = useMemo( "skipOutro",
() => [ settings,
{ updateSettings,
options: SEGMENT_SKIP_OPTIONS(t).map((option) => ({ t,
type: "radio" as const,
label: option.label,
value: option.value,
selected: option.value === settings.skipOutro,
onPress: () => updateSettings({ skipOutro: option.value }),
})),
},
],
[settings.skipOutro, updateSettings, t],
); );
const skipRecapOptions = useSkipOptions(
const skipRecapOptions = useMemo( "skipRecap",
() => [ settings,
{ updateSettings,
options: SEGMENT_SKIP_OPTIONS(t).map((option) => ({ t,
type: "radio" as const,
label: option.label,
value: option.value,
selected: option.value === settings.skipRecap,
onPress: () => updateSettings({ skipRecap: option.value }),
})),
},
],
[settings.skipRecap, updateSettings, t],
); );
const skipCommercialOptions = useSkipOptions(
const skipCommercialOptions = useMemo( "skipCommercial",
() => [ settings,
{ updateSettings,
options: SEGMENT_SKIP_OPTIONS(t).map((option) => ({ t,
type: "radio" as const,
label: option.label,
value: option.value,
selected: option.value === settings.skipCommercial,
onPress: () => updateSettings({ skipCommercial: option.value }),
})),
},
],
[settings.skipCommercial, updateSettings, t],
); );
const skipPreviewOptions = useSkipOptions(
const skipPreviewOptions = useMemo( "skipPreview",
() => [ settings,
{ updateSettings,
options: SEGMENT_SKIP_OPTIONS(t).map((option) => ({ t,
type: "radio" as const,
label: option.label,
value: option.value,
selected: option.value === settings.skipPreview,
onPress: () => updateSettings({ skipPreview: option.value }),
})),
},
],
[settings.skipPreview, updateSettings, t],
); );
if (!settings) return null; if (!settings) return null;

View File

@@ -150,6 +150,12 @@ export type JobStatus = {
introSegments?: MediaTimeSegment[]; introSegments?: MediaTimeSegment[];
/** Pre-downloaded credit segments (optional) - downloaded before video starts */ /** Pre-downloaded credit segments (optional) - downloaded before video starts */
creditSegments?: MediaTimeSegment[]; creditSegments?: MediaTimeSegment[];
/** Pre-downloaded recap segments (optional) - downloaded before video starts */
recapSegments?: MediaTimeSegment[];
/** Pre-downloaded commercial segments (optional) - downloaded before video starts */
commercialSegments?: MediaTimeSegment[];
/** Pre-downloaded preview segments (optional) - downloaded before video starts */
previewSegments?: MediaTimeSegment[];
/** The audio stream index selected for this download */ /** The audio stream index selected for this download */
audioStreamIndex?: number; audioStreamIndex?: number;
/** The subtitle stream index selected for this download */ /** The subtitle stream index selected for this download */