import { Exercise, getLanguageByShortName, NameAutocompleter, useLanguageQuery } from "@/components/Exercises"; import { BaseConfig } from "@/components/Routines/models/BaseConfig"; import { Slot } from "@/components/Routines/models/Slot"; import { SlotEntry } from "@/components/Routines/models/SlotEntry"; import { useDeleteSlotEntryQuery, useEditSlotEntryQuery } from "@/components/Routines/queries"; import { ConfigDetailsRiRField, SlotBaseConfigValueField } from "@/components/Routines/widgets/forms/BaseConfigForm"; import { SlotEntryRepetitionUnitField, SlotEntryTypeField, SlotEntryWeightUnitField } from "@/components/Routines/widgets/forms/SlotEntryForm"; import DeleteIcon from "@mui/icons-material/DeleteOutlined"; import EditIcon from "@mui/icons-material/Edit"; import EditOffIcon from '@mui/icons-material/EditOff'; import { Alert, AlertTitle, IconButton, Typography } from "@mui/material"; import Grid from '@mui/material/Grid'; import React, { useState } from "react"; import { useTranslation } from "react-i18next"; /* * Converts a number to an alphabetic string, useful for counting */ function toAlphabetic(num: number) { let result = ""; while (num > 0) { num--; // Adjust for 0-based index result = String.fromCharCode(97 + (num % 26)) + result; num = Math.floor(num / 26); } return result; } type ConfigType = "weight" | "max-weight" | "reps" | "max-reps" | "max-sets" | "sets" | "rest" | "max-rest" | "rir"; const getConfigComponent = (type: ConfigType, configs: BaseConfig[], routineId: number, slotEntryId: number) => { return configs.length > 0 ? : ; }; export const SlotDetails = (props: { slot: Slot, routineId: number, simpleMode: boolean, isGrouped?: boolean }) => { const { t } = useTranslation(); return (<> {props.slot.entries.length === 0 && ( {t('routines.setHasNoExercises')} {t('nothingHereYetAction')} )} {props.slot.entries.map((slotEntry: SlotEntry, index) => ( ))} ); }; export const SlotEntryDetails = (props: { slotEntry: SlotEntry, routineId: number, simpleMode: boolean, index: number, total: number, isGrouped?: boolean, }) => { const { t, i18n } = useTranslation(); const [editExercise, setEditExercise] = useState(false); const toggleEditExercise = () => setEditExercise(!editExercise); const languageQuery = useLanguageQuery(); const editSlotEntryQuery = useEditSlotEntryQuery(props.routineId); const deleteSlotEntryQuery = useDeleteSlotEntryQuery(props.routineId); const isPending = editSlotEntryQuery.isPending || deleteSlotEntryQuery.isPending; const handleExerciseChange = (exercise: Exercise | null) => { if (exercise === null) { return; } editSlotEntryQuery.mutate(SlotEntry.clone(props.slotEntry, { exerciseId: exercise.id! })); setEditExercise(false); }; let language = undefined; if (languageQuery.isSuccess) { language = getLanguageByShortName( i18n.language, languageQuery.data! ); } const getForm = () => (props.simpleMode ? {getConfigComponent('sets', props.slotEntry.nrOfSetsConfigs, props.routineId, props.slotEntry.id!)} {getConfigComponent('weight', props.slotEntry.weightConfigs, props.routineId, props.slotEntry.id!)} {getConfigComponent('reps', props.slotEntry.repetitionsConfigs, props.routineId, props.slotEntry.id!)} // Show all config details in advanced mode, also in a grid : {getConfigComponent('sets', props.slotEntry.nrOfSetsConfigs, props.routineId, props.slotEntry.id!)} {getConfigComponent('max-sets', props.slotEntry.maxNrOfSetsConfigs, props.routineId, props.slotEntry.id!)} 0 ? props.slotEntry.rirConfigs[0] : undefined} slotEntryId={props.slotEntry.id!} /> {getConfigComponent('rest', props.slotEntry.restTimeConfigs, props.routineId, props.slotEntry.id!)} {getConfigComponent('max-rest', props.slotEntry.maxRestTimeConfigs, props.routineId, props.slotEntry.id!)} {getConfigComponent('weight', props.slotEntry.weightConfigs, props.routineId, props.slotEntry.id!)} {getConfigComponent('max-weight', props.slotEntry.maxWeightConfigs, props.routineId, props.slotEntry.id!)} {getConfigComponent('reps', props.slotEntry.repetitionsConfigs, props.routineId, props.slotEntry.id!)} {getConfigComponent('max-reps', props.slotEntry.maxRepetitionsConfigs, props.routineId, props.slotEntry.id!)} ); const counter = props.total > 1 ? toAlphabetic(props.index + 1) + ') ' : ''; return ( ( {!props.isGrouped && <> {editExercise ? : } deleteSlotEntryQuery.mutate(props.slotEntry.id!)} disabled={isPending} > {counter} {props.slotEntry.exercise ? props.slotEntry.exercise.getTranslation(language).name : t('routines.exerciseNotAvailable')} {editExercise && } } {props.slotEntry.hasProgressionRules ? {t('routines.exerciseHasProgression')} : getForm() } ) ); };