More Tracker assignment refactoring

This commit is contained in:
loucass003 committed 2026-09-07 17:40:47 +02:00
1 parent 8250845177
commit 3e83e2153d
8 files changed
+419 -161

No files matched your search

@@ -397,7 +397,9 @@ export function BodyInteractions({
key={id}
{...(part != null ? dotProps?.(part) : undefined)}
className={classNames('absolute z-10')}
onClick={() => part != null && onSelectRole(part)}
onClick={() =>
!hidden && part != null && onSelectRole(part)
}
style={{
width: hitSize,
height: hitSize,
@@ -1,46 +1,13 @@
import classNames from 'classnames';
import { useLocalization } from '@fluent/react';
import {
Fragment,
HTMLAttributes,
ReactNode,
useCallback,
useMemo,
} from 'react';
import { BodyPart } from 'solarxr-protocol';
import { useConfig } from '@/hooks/config';
import { BodyInteractions } from '@/components/commons/BodyInteractions';
import { CheckboxInternal } from '@/components/commons/Checkbox';
import { CompareIcon } from '@/components/commons/icon/CompareIcon';
import { TogglePill, TogglePillOption } from '@/components/commons/TogglePill';
import { TrackerPartCard } from '@/components/tracker/TrackerPartCard';
import { PartCardRenderer } from './parts/PartCard';
import {
BodyPartError,
COMMONS,
useSuggestedBodyParts,
} from '@/hooks/tracker-picker';
import { PersonFrontIcon, SIDES } from '@/components/commons/PersonFrontIcon';
import { useAtomValue } from 'jotai';
import { assignedRolesAtom, trackerByBodyPartAtom } from '@/store/app-store';
type BodySide = (typeof SIDES)[number];
const LEFT_GROUPS = (side: BodySide): BodyPart[][] => [
[BodyPart.HEAD, BodyPart.NECK],
[side.shoulder, side.upperArm],
[side.lowerArm, side.hand],
[BodyPart.HIP],
[side.upperLeg, side.lowerLeg, side.foot],
];
const RIGHT_GROUPS = (side: BodySide): BodyPart[][] => [
[BodyPart.UPPER_CHEST, BodyPart.CHEST],
[side.shoulder, side.upperArm],
[side.lowerArm, side.hand],
[BodyPart.WAIST],
[side.upperLeg, side.lowerLeg, side.foot],
];
BodyAssignmentViewProps,
BodyPartAssignment,
} from './BodyPartAssignment';
export function ShowAllPartsToggle({ compact }: { compact?: boolean }) {
const { l10n } = useLocalization();
@@ -96,113 +63,14 @@ export function MirrorLegend({ compact }: { compact?: boolean }) {
}
export function BodyAssignment({
mirror,
onRoleSelected,
rolesWithErrors = {},
highlightedRoles = [],
dotSize,
fillHeight,
renderCard,
dotClass,
dotContent,
dotProps,
activeParts,
}: {
mirror: boolean;
rolesWithErrors?: Partial<Record<BodyPart, BodyPartError>>;
highlightedRoles?: BodyPart[];
onRoleSelected: (role: BodyPart) => void;
dotSize?: number;
fillHeight?: boolean;
renderCard?: PartCardRenderer;
dotClass?: (part: BodyPart) => string | undefined;
dotContent?: (part: BodyPart) => ReactNode;
dotProps?: (part: BodyPart) => HTMLAttributes<HTMLDivElement>;
activeParts?: BodyPart[];
}) {
const suggestedBodyParts = useSuggestedBodyParts();
const assignedRoles = useAtomValue(assignedRolesAtom);
const trackerByPart = useAtomValue(trackerByBodyPartAtom);
const left = +!mirror;
const right = +mirror;
const sideNames = useMemo(
() => ({
left: new Set(Object.values(SIDES[0]).map((part) => BodyPart[part])),
right: new Set(Object.values(SIDES[1]).map((part) => BodyPart[part])),
}),
[]
);
const figure = useMemo(
() => (
<PersonFrontIcon
mirror={mirror}
className={fillHeight ? 'absolute inset-0 h-full w-full' : 'w-full'}
/>
),
[mirror, fillHeight]
);
const hasBodyPart = useCallback(
(part: BodyPart) =>
COMMONS.includes(part) ||
suggestedBodyParts.includes(part) ||
assignedRoles.includes(part),
[suggestedBodyParts, assignedRoles]
);
const card: PartCardRenderer =
renderCard ??
(({ role, direction, td, roleError }) => (
<TrackerPartCard
roleError={roleError}
td={td}
role={role}
onClick={() => onRoleSelected(role)}
direction={direction}
/>
));
const column = (groups: BodyPart[][], direction: 'left' | 'right') => (
<div
className={classNames(
'flex flex-col justify-between h-full',
direction === 'right' && 'text-right'
)}
>
{groups.map((group, index) => (
<div key={index} className="flex flex-col gap-2">
{group.filter(hasBodyPart).map((role) => (
<Fragment key={role}>
{card({
role,
direction,
td: trackerByPart[role],
roleError: rolesWithErrors[role]?.label,
})}
</Fragment>
))}
</div>
))}
</div>
);
...props
}: Omit<BodyAssignmentViewProps, 'view'>) {
return (
<BodyInteractions
dotsSize={dotSize}
dotClass={dotClass}
dotContent={dotContent}
dotProps={dotProps}
activeParts={activeParts}
sideNames={sideNames}
figure={figure}
assignedRoles={assignedRoles}
highlightedRoles={highlightedRoles}
onSelectRole={onRoleSelected}
leftControls={column(LEFT_GROUPS(SIDES[left]), 'right')}
rightControls={column(RIGHT_GROUPS(SIDES[right]), 'left')}
<BodyPartAssignment
{...props}
view={{
kind: 'body',
}}
/>
);
}
@@ -0,0 +1,392 @@
import classNames from 'classnames';
import { HTMLAttributes, ReactNode, useCallback, useMemo } from 'react';
import { useAtomValue } from 'jotai';
import { BodyPart } from 'solarxr-protocol';
import {
BodyInteractions,
BodyInteractionsProps,
BodySideNames,
} from '@/components/commons/BodyInteractions';
import { ExtremityFigure } from '@/components/commons/ExtremityFigure';
import { PersonFrontIcon, SIDES } from '@/components/commons/PersonFrontIcon';
import { TrackerPartCard } from '@/components/tracker/TrackerPartCard';
import {
BodyPartError,
COMMONS,
useSuggestedBodyParts,
} from '@/hooks/tracker-picker';
import {
assignedRolesAtom,
FlatDeviceTracker,
trackerByBodyPartAtom,
} from '@/store/app-store';
import { ExtremityDescriptor, ExtremitySide } from '@/utils/extremities';
import {
DigitFlow,
ExtremityLayoutProvider,
ExtremityLayoutValue,
ExtremitySlot,
} from './extremities/ExtremityLayout';
import { ExtremityGroupCard, PartCardRenderer } from './parts/PartCard';
type BodySide = (typeof SIDES)[number];
const LEFT_GROUPS = (side: BodySide): BodyPart[][] => [
[BodyPart.HEAD, BodyPart.NECK],
[side.shoulder, side.upperArm],
[side.lowerArm, side.hand],
[BodyPart.HIP],
[side.upperLeg, side.lowerLeg, side.foot],
];
const RIGHT_GROUPS = (side: BodySide): BodyPart[][] => [
[BodyPart.UPPER_CHEST, BodyPart.CHEST],
[side.shoulder, side.upperArm],
[side.lowerArm, side.hand],
[BodyPart.WAIST],
[side.upperLeg, side.lowerLeg, side.foot],
];
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;
type CommonAssignmentProps = {
dotSize?: number;
fillHeight?: boolean;
highlightedRoles?: BodyPart[];
rolesWithErrors?: Partial<Record<BodyPart, BodyPartError>>;
onRoleSelected: (role: BodyPart) => void;
dotClass?: (part: BodyPart) => string | undefined;
dotContent?: (part: BodyPart) => ReactNode;
dotProps?: (part: BodyPart) => HTMLAttributes<HTMLDivElement>;
activeParts?: BodyPart[];
};
export type BodyAssignmentViewProps = CommonAssignmentProps & {
view: { kind: 'body' };
mirror: boolean;
renderCard?: PartCardRenderer;
};
export type ExtremityAssignmentViewProps = CommonAssignmentProps & {
view: { kind: 'extremity'; descriptor: ExtremityDescriptor };
side: ExtremitySide;
compact?: boolean;
renderGroup?: ExtremityGroupRenderer;
};
export type BodyPartAssignmentProps =
| BodyAssignmentViewProps
| ExtremityAssignmentViewProps;
type AssignmentRenderState = {
assignedRoles: BodyPart[];
trackerByPart: Record<number, FlatDeviceTracker | undefined>;
suggestedBodyParts: BodyPart[];
interactions: Omit<
BodyInteractionsProps,
| 'figure'
| 'leftControls'
| 'rightControls'
| 'topControls'
| 'bottomControls'
| 'sideNames'
>;
};
const defaultCard =
(onRoleSelected: (role: BodyPart) => void): PartCardRenderer =>
({ role, direction, td, roleError }) => (
<TrackerPartCard
roleError={roleError}
td={td}
role={role}
onClick={() => onRoleSelected(role)}
direction={direction}
/>
);
const defaultGroup =
(onRoleSelected: (role: BodyPart) => void): ExtremityGroupRenderer =>
({ id, labelId, direction, rows, edge, flow }) => (
<ExtremityGroupCard
key={id}
edge={edge}
flow={flow}
labelId={labelId}
direction={direction}
rows={rows}
renderRow={(props) =>
defaultCard(onRoleSelected)({ ...props, compact: true })
}
/>
);
export function BodyPartAssignment(props: BodyPartAssignmentProps) {
const assignedRoles = useAtomValue(assignedRolesAtom);
const trackerByPart = useAtomValue(trackerByBodyPartAtom);
const suggestedBodyParts = useSuggestedBodyParts();
const {
dotSize,
highlightedRoles = [],
onRoleSelected,
dotClass,
dotContent,
dotProps,
activeParts,
} = props;
const state: AssignmentRenderState = {
assignedRoles,
trackerByPart,
suggestedBodyParts,
interactions: {
dotsSize: dotSize,
dotClass,
dotContent,
dotProps,
activeParts,
assignedRoles,
highlightedRoles,
onSelectRole: onRoleSelected,
},
};
return props.view.kind === 'body' ? (
<BodyAssignmentView {...(props as BodyAssignmentViewProps)} state={state} />
) : (
<ExtremityAssignmentView
{...(props as ExtremityAssignmentViewProps)}
state={state}
/>
);
}
function BodyAssignmentView({
state,
mirror,
fillHeight,
rolesWithErrors = {},
onRoleSelected,
renderCard,
}: BodyAssignmentViewProps & { state: AssignmentRenderState }) {
const { assignedRoles, trackerByPart, suggestedBodyParts, interactions } =
state;
const left = +!mirror;
const right = +mirror;
const card = renderCard ?? defaultCard(onRoleSelected);
const sideNames = useMemo<BodySideNames>(
() => ({
left: new Set(Object.values(SIDES[0]).map((part) => BodyPart[part])),
right: new Set(Object.values(SIDES[1]).map((part) => BodyPart[part])),
}),
[]
);
const figure = useMemo(
() => (
<PersonFrontIcon
mirror={mirror}
className={fillHeight ? 'absolute inset-0 h-full w-full' : 'w-full'}
/>
),
[mirror, fillHeight]
);
const hasBodyPart = useCallback(
(part: BodyPart) =>
COMMONS.includes(part) ||
suggestedBodyParts.includes(part) ||
assignedRoles.includes(part),
[suggestedBodyParts, assignedRoles]
);
const column = (groups: BodyPart[][], direction: 'left' | 'right') => (
<div
className={classNames(
'flex flex-col justify-between h-full',
direction === 'right' && 'text-right'
)}
>
{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,
})
)}
</div>
))}
</div>
);
return (
<BodyInteractions
{...interactions}
sideNames={sideNames}
figure={figure}
leftControls={column(LEFT_GROUPS(SIDES[left]), 'right')}
rightControls={column(RIGHT_GROUPS(SIDES[right]), 'left')}
/>
);
}
function ExtremityAssignmentView({
state,
view,
side,
fillHeight,
compact = false,
rolesWithErrors = {},
onRoleSelected,
renderGroup,
dotContent,
}: ExtremityAssignmentViewProps & { state: AssignmentRenderState }) {
const { trackerByPart, interactions } = state;
const { descriptor } = view;
const { digits, root } = descriptor.sides[side];
const mirrored = side === 'left';
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 extremityDotContent = 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 group = renderGroup ?? defaultGroup(onRoleSelected);
const digit = useCallback(
(name: string, { direction, edge }: ExtremitySlot, flow: DigitFlow) => {
if (name === 'root')
return group({
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 group({
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, group, 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: {
...interactions,
dotContent: dotContent ?? extremityDotContent,
sideNames,
},
}),
[
compact,
mirrored,
digit,
figure,
interactions,
dotContent,
extremityDotContent,
sideNames,
]
);
const { Layout } = descriptor;
return (
<ExtremityLayoutProvider value={layout}>
<Layout />
</ExtremityLayoutProvider>
);
}
@@ -216,7 +216,7 @@ export function BodyAssignmentPanel({
);
}
function PickerTabs({ compact }: { compact?: boolean }) {
export function PickerTabs({ compact }: { compact?: boolean }) {
const { tab, setTab } = usePicker();
return (
@@ -63,23 +63,19 @@ export function SingleTrackerBodyAssignmentMenu({
</Button>
</div>
<ShowAllPartsToggle />
<MirrorLegend />
<Button
variant="secondary"
onClick={() => onRoleSelected(BodyPart.NONE)}
>
{l10n.getString('body_assignment_menu-unassign_tracker')}
</Button>
</div>
<div className="flex flex-col gap-4 rounded-xl fill-background-50 py-2 xsAssign:flex-grow">
<div className="flex justify-center">
<MirrorLegend />
</div>
<BodyAssignment
mirror={config?.mirrorView ?? defaultConfig.mirrorView}
onRoleSelected={tryOpenChokerWarning}
/>
<div className="flex justify-center">
<Button
variant="secondary"
onClick={() => onRoleSelected(BodyPart.NONE)}
>
{l10n.getString('body_assignment_menu-unassign_tracker')}
</Button>
</div>
</div>
</div>
</div>
+2 -2
View File
@@ -15,7 +15,7 @@ import { playTapSetupSound } from '@/sounds/sounds';
import { useAtomValue } from 'jotai';
import { donglesAtom } from '@/store/app-store';
import { hoveredBodyPartAtom } from './tracker-drag';
import { usePickerShell } from './tracker-picker';
import { providePicker } from './tracker-picker';
export type AssignmentMode = 'drag' | 'tap';
@@ -44,7 +44,7 @@ export function useTrackerAssignment(mode: AssignmentMode) {
const { config } = useConfig();
const { sendRPCPacket, useRPCPacket } = useWebsocketAPI();
const sendAssign = useAssignTracker();
const shell = usePickerShell();
const shell = providePicker();
const { trackerByPart, flatTrackers } = shell;
const [pending, setPending] = useState<Pending>(null);
+2 -2
View File
@@ -8,7 +8,7 @@ import {
} from '@/maths/quaternion';
import { FlatDeviceTracker } from '@/store/app-store';
import { useAssignTracker } from './tracker-assignment';
import { usePickerShell } from './tracker-picker';
import { providePicker } from './tracker-picker';
export function useMountingOrientation(td: FlatDeviceTracker | undefined) {
const assignTracker = useAssignTracker();
@@ -37,7 +37,7 @@ export function useMountingOrientation(td: FlatDeviceTracker | undefined) {
}
export function useMountingSelection() {
const shell = usePickerShell();
const shell = providePicker();
const [target, setTarget] = useState<BodyPart>(BodyPart.NONE);
const td = shell.trackerByPart[target];
+2 -2
View File
@@ -156,7 +156,7 @@ export const PICKER_TABS: Record<PickerTab, PickerTabSpec> = {
export const PICKER_TAB_ORDER: PickerTab[] = ['body', 'fingers', 'toes'];
export function usePickerShell() {
export function providePicker() {
const { l10n } = useLocalization();
const [tab, setTab] = useState<PickerTab>('body');
@@ -243,7 +243,7 @@ export function usePickerShell() {
};
}
export type PickerShell = ReturnType<typeof usePickerShell>;
export type PickerShell = ReturnType<typeof providePicker>;
export type Picker = PickerShell & {
activePart: BodyPart;