Skip to content

Commit 1e579de

Browse files
committed
updated favouriting feature to handle duplicate names
1 parent 15b7052 commit 1e579de

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

packages/client/src/components/containers/BottomInformationContainer.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ function BottomInformationContainer() {
2828
[currentAppState, favourites],
2929
);
3030

31+
const formatPathLabel = (path: string) =>
32+
path
33+
.split(".")
34+
.map(
35+
(segment) =>
36+
segment
37+
.replace(/([a-z])([A-Z])/g, "$1 $2") // lowerUpper → lower Upper
38+
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2") // ABCDef → ABC Def
39+
.replace(/([a-zA-Z])(\d)/g, "$1 $2"), // Unit3 → Unit 3
40+
)
41+
.join(" "); // use " " or " →" or " / " if preferred
42+
3143
return (
3244
<div className="align-middle">
3345
<div className="flex h-full flex-row flex-wrap justify-evenly gap-4 pt-1 text-center text-base md:gap-2 2xl:text-xl">
@@ -37,7 +49,7 @@ function BottomInformationContainer() {
3749
className="text-xs hover:cursor-pointer 2xl:text-sm"
3850
onClick={() => handleRemoveFavourite(favourite)}
3951
>
40-
{favourite}
52+
{formatPathLabel(favourite)}
4153
</div>
4254
<div className="text-helios">
4355
{/*Search for the value associated with the favourite name string in the lookupTable */

packages/client/src/components/transformers/PISTransformer.tsx

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,33 +88,31 @@ function FieldDataFormatter(props: FieldDataFormatterProps): JSX.Element {
8888

8989
type FieldPrinterProps = {
9090
field: I_PISField;
91+
fieldPath: string;
9192
};
9293

9394
function FieldPrinter(props: FieldPrinterProps): JSX.Element {
9495
const { setCurrentAppState } = useAppState();
95-
const { field } = props;
96+
const { field, fieldPath } = props;
9697

9798
const handleAddToFavourites = useCallback(() => {
9899
const storedFavourites = localStorage.getItem("favourites");
99100
const parsedFavourites: string[] = storedFavourites
100101
? (JSON.parse(storedFavourites) as string[])
101102
: [];
102103

103-
if (
104-
!parsedFavourites.some((fav) => fav === field.name) &&
105-
typeof field.name === "string"
106-
) {
107-
if (parsedFavourites.length === 8 && typeof field.name === "string") {
104+
if (!parsedFavourites.includes(fieldPath)) {
105+
if (parsedFavourites.length === 8) {
108106
parsedFavourites.shift();
109107
}
110-
parsedFavourites.push(field.name);
108+
parsedFavourites.push(fieldPath);
111109
setCurrentAppState((prev) => ({
112110
...prev,
113111
favourites: parsedFavourites,
114112
}));
115113
localStorage.setItem("favourites", JSON.stringify(parsedFavourites));
116114
}
117-
}, [field.name, setCurrentAppState]);
115+
}, [fieldPath, setCurrentAppState]);
118116

119117
if (
120118
field.fstring !== undefined &&
@@ -151,6 +149,7 @@ function FieldPrinter(props: FieldPrinterProps): JSX.Element {
151149
type FieldsPrinterProps = {
152150
fields: I_PISField[];
153151
depth?: number;
152+
basePath: string; // NEW
154153
};
155154

156155
function FieldsPrinter(props: FieldsPrinterProps): JSX.Element {
@@ -170,30 +169,39 @@ function FieldsPrinter(props: FieldsPrinterProps): JSX.Element {
170169
className={`block overflow-x-hidden md:grid md:grid-cols-3 md:gap-x-2 lg:block lg:overflow-x-hidden ${getMaxHeightClass()}`}
171170
>
172171
{fields.map((field, index) => (
173-
<FieldPrinter field={field} key={index} />
172+
<FieldPrinter
173+
field={field}
174+
fieldPath={`${basePath}.${field.name}`}
175+
key={index}
176+
/>
174177
))}
175178
</div>
176179
);
177180
}
181+
178182
type PIStransformerProps = {
179183
root: I_PIS;
180184
depth?: number;
185+
path?: string[];
181186
};
182187

183188
function PISTransformer(props: PIStransformerProps): JSX.Element {
184-
const { depth = 0, root } = props;
189+
const { depth = 0, path = [], root } = props;
190+
185191
const formatKey = (key: string): string => {
186192
return key
187-
.replace(/([a-z])([A-Z])/g, "$1 $2") // Add space between lowercase and uppercase
188-
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2") // Add space between consecutive uppercase followed by lowercase
189-
.replace(/([a-zA-Z])(\d)/g, "$1 $2"); // Add space between letters and numbers
193+
.replace(/([a-z])([A-Z])/g, "$1 $2")
194+
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
195+
.replace(/([a-zA-Z])(\d)/g, "$1 $2");
190196
};
191197

192198
return (
193199
root && (
194200
<div className="flex size-full flex-col gap-x-2 lg:h-[375px] lg:flex-wrap xl:h-[350px]">
195201
{Object.keys(root).map((key, index) => {
196202
const value = root[key];
203+
const newPath = [...path, key]; // Track path
204+
197205
return (
198206
<div className={`flex flex-col`} id={key} key={index}>
199207
<div className="flex w-full items-center justify-evenly border-b-2 border-helios">
@@ -205,10 +213,19 @@ function PISTransformer(props: PIStransformerProps): JSX.Element {
205213
{formatKey(key)}
206214
</p>
207215
</div>
216+
208217
{Array.isArray(value) ? (
209-
<FieldsPrinter depth={depth + 1} fields={value} />
218+
<FieldsPrinter
219+
basePath={newPath.join(".")}
220+
depth={depth + 1}
221+
fields={value}
222+
/>
210223
) : (
211-
<PISTransformer depth={depth + 1} root={value as I_PIS} />
224+
<PISTransformer
225+
depth={depth + 1}
226+
path={newPath}
227+
root={value as I_PIS}
228+
/>
212229
)}
213230
</div>
214231
);

packages/client/src/hooks/favouriteLookupTable.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ export const useFavouriteLookupTable = (
88
return useMemo(() => {
99
const table: Record<string, () => string | number | undefined> = {};
1010

11-
const extractFields = (node: unknown) => {
11+
const extractFields = (node: unknown, path: string[] = []) => {
1212
if (!node || typeof node !== "object") return;
1313

14-
// If the current level is an array of I_PISField
14+
// If node is an array of I_PISField
1515
if (
1616
Array.isArray(node) &&
1717
node.length &&
18+
typeof node[0] === "object" &&
1819
"name" in node[0] &&
1920
"data" in node[0]
2021
) {
2122
for (const field of node as I_PISField[]) {
2223
if (!field?.name) continue;
23-
table[field.name] = () => {
24+
const fullPath = [...path, field.name].join(".");
25+
table[fullPath] = () => {
2426
const value = field.data?.[0]?.value;
2527
if (typeof value === "boolean") return value ? "T" : "F";
2628
return value;
@@ -29,9 +31,9 @@ export const useFavouriteLookupTable = (
2931
return;
3032
}
3133

32-
// Traverse object properties recursively if not an array of I_PISField (meaning it's a nested object)
34+
// Traverse nested properties
3335
for (const key of Object.keys(node)) {
34-
extractFields((node as Record<string, unknown>)[key]);
36+
extractFields((node as Record<string, unknown>)[key], [...path, key]);
3537
}
3638
};
3739

0 commit comments

Comments
 (0)