-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.tsx
175 lines (152 loc) · 4.63 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import React, { memo, useCallback } from "react";
import { Path } from "react-native-svg";
import differenceWith from "ramda/src/differenceWith";
import { bodyFront } from "./assets/bodyFront";
import { bodyBack } from "./assets/bodyBack";
import { SvgMaleWrapper } from "./components/SvgMaleWrapper";
import { bodyFemaleFront } from "./assets/bodyFemaleFront";
import { bodyFemaleBack } from "./assets/bodyFemaleBack";
import { SvgFemaleWrapper } from "./components/SvgFemaleWrapper";
export type Slug =
| "abs"
| "adductors"
| "ankles"
| "biceps"
| "calves"
| "chest"
| "deltoids"
| "deltoids"
| "feet"
| "forearm"
| "gluteal"
| "hamstring"
| "hands"
| "hair"
| "head"
| "knees"
| "lower-back"
| "neck"
| "obliques"
| "quadriceps"
| "tibialis"
| "trapezius"
| "triceps"
| "upper-back";
export interface BodyPart {
color?: string;
slug?: Slug;
path?: {
common?: string[];
left?: string[];
right?: string[];
};
}
export interface ExtendedBodyPart extends BodyPart {
intensity?: number;
side?: "left" | "right";
}
export type BodyProps = {
colors?: ReadonlyArray<string>;
data: ReadonlyArray<ExtendedBodyPart>;
scale?: number;
side?: "front" | "back";
gender?: "male" | "female";
onBodyPartPress?: (b: ExtendedBodyPart, side?: "left" | "right") => void;
border?: string | "none";
};
const comparison = (a: ExtendedBodyPart, b: ExtendedBodyPart) =>
a.slug === b.slug;
const Body = ({
colors = ["#0984e3", "#74b9ff"],
data,
scale = 1,
side = "front",
gender = "male",
onBodyPartPress,
border = "#dfdfdf",
}: BodyProps) => {
const mergedBodyParts = useCallback(
(dataSource: ReadonlyArray<BodyPart>) => {
const innerData = data
.map((d) => {
let foundedBodyPart = dataSource.find((e) => e.slug === d.slug);
return foundedBodyPart;
})
.filter(Boolean);
const coloredBodyParts = innerData.map((d) => {
const bodyPart = data.find((e) => e.slug === d?.slug);
let colorIntensity = 1;
if (bodyPart?.intensity) colorIntensity = bodyPart.intensity;
return { ...d, color: colors[colorIntensity - 1] };
});
const formattedBodyParts = differenceWith(comparison, dataSource, data);
return [...formattedBodyParts, ...coloredBodyParts];
},
[data, colors]
);
const getColorToFill = (bodyPart: ExtendedBodyPart) => {
let color;
if (bodyPart.intensity) {
color = colors[bodyPart.intensity];
} else {
color = bodyPart.color;
}
return color;
};
const renderBodySvg = (bodyToRender: ReadonlyArray<BodyPart>) => {
const SvgWrapper = gender === "male" ? SvgMaleWrapper : SvgFemaleWrapper;
return (
<SvgWrapper side={side} scale={scale} border={border}>
{mergedBodyParts(bodyToRender).map((bodyPart: ExtendedBodyPart) => {
const commonPaths = (bodyPart.path?.common || []).map((path) => {
const dataCommonPath = data.find((d) => d.slug === bodyPart.slug)
?.path?.common;
return (
<Path
key={path}
onPress={() => onBodyPartPress?.(bodyPart)}
id={bodyPart.slug}
fill={
dataCommonPath ? getColorToFill(bodyPart) : bodyPart.color
}
d={path}
/>
);
});
const leftPaths = (bodyPart.path?.left || []).map((path) => {
const isOnlyRight =
data.find((d) => d.slug === bodyPart.slug)?.side === "right";
return (
<Path
key={path}
onPress={() => onBodyPartPress?.(bodyPart, "left")}
id={bodyPart.slug}
fill={isOnlyRight ? "#3f3f3f" : getColorToFill(bodyPart)}
d={path}
/>
);
});
const rightPaths = (bodyPart.path?.right || []).map((path) => {
const isOnlyLeft =
data.find((d) => d.slug === bodyPart.slug)?.side === "left";
return (
<Path
key={path}
onPress={() => onBodyPartPress?.(bodyPart, "right")}
id={bodyPart.slug}
fill={isOnlyLeft ? "#3f3f3f" : getColorToFill(bodyPart)}
d={path}
/>
);
});
return [...commonPaths, ...leftPaths, ...rightPaths];
})}
</SvgWrapper>
);
};
if (gender === "female") {
return renderBodySvg(side === "front" ? bodyFemaleFront : bodyFemaleBack);
}
return renderBodySvg(side === "front" ? bodyFront : bodyBack);
};
export default Body;