Skip to content

Commit faec3ab

Browse files
authored
Extract get schema icon logic into a utils function (#6451)
1 parent 9b51d87 commit faec3ab

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

frontend/app/src/entities/nodes/object/ui/node-description.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { getNodeLabel } from "@/entities/nodes/object/utils/get-node-label";
22
import { NodeCore } from "@/entities/nodes/types";
33
import { getObjectDetailsUrl } from "@/entities/nodes/utils";
44
import { useSchema } from "@/entities/schema/ui/hooks/useSchema";
5+
import { getSchemaIcon } from "@/entities/schema/utils/get-schema-icon";
56
import { classNames } from "@/shared/utils/common";
67
import { Icon } from "@iconify-icon/react";
78
import { InputHTMLAttributes } from "react";
@@ -18,7 +19,7 @@ export function NodeDescription({ node, className, ...props }: ObjectInlineDispl
1819
return (
1920
<div className={classNames("flex flex-col text-sm", className)} {...props}>
2021
<div className="flex items-center gap-1">
21-
<Icon icon={schema?.icon ?? "mdi:cube-outline"} className="text-gray-400 text-xs" />
22+
<Icon icon={getSchemaIcon(schema)} className="text-gray-400 text-xs" />
2223
{schemaLabel}
2324
</div>
2425

frontend/app/src/entities/nodes/object/ui/object-table/cells/table-column-header-icon.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { AttributeKind, AttributeSchema, RelationshipSchema } from "@/entities/schema/types";
22
import { useSchema } from "@/entities/schema/ui/hooks/useSchema";
3+
import { getSchemaIcon } from "@/entities/schema/utils/get-schema-icon";
34
import { Icon } from "@iconify-icon/react";
45

56
const ATTRIBUTE_ICONS: Record<AttributeKind, string> = {
@@ -52,5 +53,5 @@ export function RelationshipTableColumnHeaderIcon({
5253
}: { relationshipSchema: RelationshipSchema }) {
5354
const { schema } = useSchema(relationshipSchema.peer);
5455

55-
return <Icon icon={schema?.icon ?? "mdi:cube-outline"} className="text-stone-400" />;
56+
return <Icon icon={getSchemaIcon(schema)} className="text-stone-400" />;
5657
}

frontend/app/src/entities/nodes/object/ui/object-table/cells/table-relationship-cell.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { getObjectDetailsUrl } from "@/entities/nodes/utils";
99
import { RelationshipSchema } from "@/entities/schema/types";
1010
import { useSchema } from "@/entities/schema/ui/hooks/useSchema";
11+
import { getSchemaIcon } from "@/entities/schema/utils/get-schema-icon";
1112
import { LinkButton } from "@/shared/components/buttons/button-primitive";
1213
import { Icon } from "@iconify-icon/react";
1314

@@ -49,7 +50,7 @@ export function RelationshipNodeDisplay({ node }: { node: NodeCore }) {
4950
to={getObjectDetailsUrl(node.__typename, node.id)}
5051
className="rounded-full truncate hover:underline hover:border-custom-blue-700 pr-2.5"
5152
>
52-
<Icon icon={schema.icon ?? "mdi:cube-outline"} className="mr-1 text-custom-blue-800" />
53+
<Icon icon={getSchemaIcon(schema)} className="mr-1 text-custom-blue-800" />
5354
{getNodeLabel(node)}
5455
</LinkButton>
5556
);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { getSchemaIcon } from "@/entities/schema/utils/get-schema-icon";
2+
import { describe, expect, it } from "vitest";
3+
import { generateNodeSchema } from "../../../../tests/fake/schema";
4+
5+
describe("getSchemaIcon", () => {
6+
it("should return default icon when schema is null", () => {
7+
// GIVEN
8+
const schema = null;
9+
10+
// WHEN
11+
const result = getSchemaIcon(schema);
12+
13+
// THEN
14+
expect(result).toBe("mdi:cube-outline");
15+
});
16+
17+
it("should return default icon when schema is undefined", () => {
18+
// GIVEN
19+
const schema = undefined;
20+
21+
// WHEN
22+
const result = getSchemaIcon(schema);
23+
24+
// THEN
25+
expect(result).toBe("mdi:cube-outline");
26+
});
27+
28+
it("should return default icon when schema has no icon property", () => {
29+
// GIVEN
30+
const schema = generateNodeSchema({ icon: undefined });
31+
32+
// WHEN
33+
const result = getSchemaIcon(schema);
34+
35+
// THEN
36+
expect(result).toBe("mdi:cube-outline");
37+
});
38+
39+
it("should return schema icon when it exists", () => {
40+
// GIVEN
41+
const schema = generateNodeSchema({ icon: "mdi:icon" });
42+
43+
// WHEN
44+
const result = getSchemaIcon(schema);
45+
46+
// THEN
47+
expect(result).toBe("mdi:icon");
48+
});
49+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { ModelSchema } from "@/entities/schema/types";
2+
3+
export function getSchemaIcon(schema: ModelSchema | null | undefined): string {
4+
if (!schema?.icon) return "mdi:cube-outline";
5+
return schema.icon;
6+
}

0 commit comments

Comments
 (0)