More assignment refactor

This commit is contained in:
loucass003 committed 2026-09-07 19:55:38 +02:00
1 parent 6cffadf792
commit f9df514b95
4 files changed
+42 -211

No files matched your search

@@ -1,5 +1,11 @@
import classNames from 'classnames';
import { HTMLAttributes, ReactNode, useCallback, useMemo } from 'react';
import {
Fragment,
HTMLAttributes,
ReactNode,
useCallback,
useMemo,
} from 'react';
import { useAtomValue } from 'jotai';
import { BodyPart } from 'solarxr-protocol';
import {
@@ -227,14 +233,16 @@ function BodyAssignmentView({
>
{groups.map((group, index) => (
<div key={index} className="flex flex-col gap-2">
{group.filter(hasBodyPart).map((role) =>
card({
role,
direction,
td: trackerByPart[role],
roleError: rolesWithErrors[role]?.label,
})
)}
{group.filter(hasBodyPart).map((role) => (
<Fragment key={role}>
{card({
role,
direction,
td: trackerByPart[role],
roleError: rolesWithErrors[role]?.label,
})}
</Fragment>
))}
</div>
))}
</div>
@@ -1,202 +1,27 @@
import { HTMLAttributes, ReactNode, useCallback, useMemo } from 'react';
import { useAtomValue } from 'jotai';
import { BodyPart } from 'solarxr-protocol';
import { ExtremityFigure } from '@/components/commons/ExtremityFigure';
import { ExtremityDescriptor } from '@/utils/extremities';
import {
DigitFlow,
ExtremityLayoutProvider,
ExtremityLayoutValue,
ExtremitySlot,
} from './extremities/ExtremityLayout';
import { ExtremityDescriptor, ExtremitySide } from '@/utils/extremities';
import { BodyPartError } from '@/hooks/tracker-picker';
import {
assignedRolesAtom,
FlatDeviceTracker,
trackerByBodyPartAtom,
} from '@/store/app-store';
BodyPartAssignment,
ExtremityAssignmentViewProps,
} from './BodyPartAssignment';
export type ExtremityRow = {
role: BodyPart;
td: FlatDeviceTracker | undefined;
roleError: string | undefined;
labelId?: string;
number?: number;
/** Whether this row draws a line to its dot */
connector: boolean;
};
export type ExtremityGroupRenderer = (args: {
id: string;
labelId?: string;
direction: 'left' | 'right';
rows: ExtremityRow[];
edge?: 'side' | 'cap';
flow?: DigitFlow;
}) => ReactNode;
export type {
ExtremityGroupRenderer,
ExtremityRow,
} from './BodyPartAssignment';
export function ExtremityAssignment({
descriptor,
side,
dotSize,
fillHeight,
compact = false,
highlightedRoles = [],
rolesWithErrors = {},
onRoleSelected,
renderGroup,
dotClass,
dotProps,
activeParts,
}: {
...props
}: Omit<ExtremityAssignmentViewProps, 'view'> & {
descriptor: ExtremityDescriptor;
side: ExtremitySide;
dotSize?: number;
fillHeight?: boolean;
compact?: boolean;
highlightedRoles?: BodyPart[];
rolesWithErrors?: Partial<Record<BodyPart, BodyPartError>>;
onRoleSelected: (role: BodyPart) => void;
renderGroup: ExtremityGroupRenderer;
dotClass?: (part: BodyPart) => string | undefined;
dotProps?: (part: BodyPart) => HTMLAttributes<HTMLDivElement>;
activeParts?: BodyPart[];
}) {
const assignedRoles = useAtomValue(assignedRolesAtom);
const trackerByPart = useAtomValue(trackerByBodyPartAtom);
const { digits, root } = descriptor.sides[side];
const sideNames = useMemo(() => {
const names = new Set(
[...Object.values(digits).flat(), root].map((part) => BodyPart[part])
);
return {
left: side === 'left' ? names : new Set<string>(),
right: side === 'right' ? names : new Set<string>(),
};
}, [side, digits, root]);
/** Parts of a digit share one line, so their dots go by number */
const partNumbers = useMemo(() => {
const numbers = new Map<BodyPart, number>();
descriptor.digits.forEach((digit) => {
if (digits[digit].length < 2) return;
digits[digit].forEach((part, i) => numbers.set(part, i + 1));
});
return numbers;
}, [descriptor, digits]);
const dotContent = useCallback(
(part: BodyPart) => {
const number = partNumbers.get(part);
return (
number && (
<span className="text-[10px] font-bold leading-none text-background-90">
{number}
</span>
)
);
},
[partNumbers]
);
const cell = useCallback(
(role: BodyPart) => ({
role,
td: trackerByPart[role],
roleError: rolesWithErrors[role]?.label,
}),
[trackerByPart, rolesWithErrors]
);
const mirrored = side === 'left';
const digit = useCallback(
(name: string, { direction, edge }: ExtremitySlot, flow: DigitFlow) => {
if (name === 'root')
return renderGroup({
id: name,
direction,
edge,
rows: [{ ...cell(root), connector: true }],
});
const parts = digits[name];
const main = descriptor.mainPart(parts);
const rows = parts.map((part, i) => ({
...cell(part),
labelId: descriptor.partLabelId(part),
number: parts.length > 1 ? i + 1 : undefined,
connector: part === main,
}));
return renderGroup({
id: name,
// rows of a single part carry their own name already
labelId: parts.length > 1 ? descriptor.digitLabelId(name) : undefined,
direction,
edge,
flow,
// spread across the figure, so they follow the mirrored dots
rows: flow === 'columns' && mirrored ? [...rows].reverse() : rows,
});
},
[descriptor, digits, root, cell, renderGroup, mirrored]
);
const figure = useCallback(
(height?: number) => (
<ExtremityFigure
spec={descriptor.figure}
parts={descriptor.sides[side]}
side={side}
height={height}
className={fillHeight ? 'absolute inset-0 h-full w-full' : 'w-full'}
/>
),
[descriptor, side, fillHeight]
);
const layout = useMemo<ExtremityLayoutValue>(
() => ({
compact,
mirrored,
digit,
figure,
interactions: {
dotsSize: dotSize,
dotClass,
dotContent,
dotProps,
activeParts,
sideNames,
assignedRoles,
highlightedRoles,
onSelectRole: onRoleSelected,
},
}),
[
compact,
mirrored,
digit,
figure,
dotSize,
dotClass,
dotContent,
dotProps,
activeParts,
sideNames,
assignedRoles,
highlightedRoles,
onRoleSelected,
]
);
const { Layout } = descriptor;
return (
<ExtremityLayoutProvider value={layout}>
<Layout />
</ExtremityLayoutProvider>
<BodyPartAssignment
{...props}
view={{
kind: 'extremity',
descriptor,
}}
/>
);
}
@@ -7,14 +7,11 @@ import {
useMemo,
} from 'react';
import { BodyPart } from 'solarxr-protocol';
import { MirrorLegend } from '@/components/onboarding/BodyAssignment';
import {
BodyAssignment,
MirrorLegend,
} from '@/components/onboarding/BodyAssignment';
import {
ExtremityAssignment,
BodyPartAssignment,
ExtremityGroupRenderer,
} from '@/components/onboarding/ExtremityAssignment';
} from '@/components/onboarding/BodyPartAssignment';
import {
AssignedTrackerLabel,
ExtremityGroupCard,
@@ -131,8 +128,8 @@ export function PickerPanel({
)}
>
{extremity ? (
<ExtremityAssignment
descriptor={extremity}
<BodyPartAssignment
view={{ kind: 'extremity', descriptor: extremity }}
side={side}
dotSize={dotSize[dots]}
compact={isTight}
@@ -146,7 +143,8 @@ export function PickerPanel({
activeParts={activeParts}
/>
) : (
<BodyAssignment
<BodyPartAssignment
view={{ kind: 'body' }}
dotSize={dotSize[dots]}
fillHeight
highlightedRoles={firstError?.affectedRoles || []}
@@ -11,7 +11,7 @@ import {
velocityGlowStyle,
} from '@/hooks/tracker';
import { FlatDeviceTracker } from '@/store/app-store';
import { ExtremityRow } from '@/components/onboarding/ExtremityAssignment';
import type { ExtremityRow } from '@/components/onboarding/BodyPartAssignment';
import { DigitFlow } from '@/components/onboarding/extremities/ExtremityLayout';
export type PartCardProps = {