Skip to content

Commit

Permalink
On generic schema viewer, display "Hierarchical" value (#5184)
Browse files Browse the repository at this point in the history
Bonus: replace function isGeneric -> isGenericSchema
  • Loading branch information
bilalabbad authored Dec 10, 2024
1 parent 9730d08 commit d657460
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 25 deletions.
1 change: 1 addition & 0 deletions changelog/+schema-viewer.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In schema viewer, we now display `Hierarchical` value for generics.
5 changes: 3 additions & 2 deletions frontend/app/src/components/filters/filter-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { DynamicInput } from "@/components/form/dynamic-form";
import { getFormFieldsFromSchema } from "@/components/form/utils/getFormFieldsFromSchema";
import { Form, FormProps, FormRef, FormSubmit } from "@/components/ui/form";
import { Filter } from "@/hooks/useFilters";
import { isGenericSchema } from "@/screens/schema/utils";
import { IModelSchema } from "@/state/atoms/schema.atom";
import { classNames, isGeneric } from "@/utils/common";
import { classNames } from "@/utils/common";
import { forwardRef } from "react";

export interface FilterFormProps extends FormProps {
Expand All @@ -30,7 +31,7 @@ export const FilterForm = forwardRef<FormRef, FilterFormProps>(
className={classNames("bg-custom-white flex flex-col flex-1 overflow-auto p-4", className)}
{...props}
>
{isGeneric(schema) && schema.used_by?.length ? (
{isGenericSchema(schema) && schema.used_by?.length ? (
<FilterKindSelector genericSchema={schema} />
) : null}

Expand Down
4 changes: 2 additions & 2 deletions frontend/app/src/hooks/useObjectDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { PROFILE_KIND, TASK_OBJECT } from "@/config/constants";
import { getObjectDetailsPaginated } from "@/graphql/queries/objects/getObjectDetails";
import useQuery from "@/hooks/useQuery";
import { getPermission } from "@/screens/permission/utils";
import { isGenericSchema } from "@/screens/schema/utils";
import { IModelSchema, genericsState } from "@/state/atoms/schema.atom";
import { isGeneric } from "@/utils/common";
import { getSchemaObjectColumns, getTabs } from "@/utils/getSchemaObjectColumns";
import { gql } from "@apollo/client";
import { useAtomValue } from "jotai";
Expand All @@ -28,7 +28,7 @@ export const useObjectDetails = (schema: IModelSchema, objectId: string) => {
queryProfiles:
!profileGenericSchema?.used_by?.includes(schema?.kind!) &&
schema?.kind !== PROFILE_KIND &&
!isGeneric(schema) &&
!isGenericSchema(schema) &&
schema?.generate_profile,
hasPermissions: !isProfileSchema,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { Generate } from "@/screens/artifacts/generate";
import { GroupsManagerTriggerButton } from "@/screens/groups/groups-manager-trigger-button";
import ObjectItemEditComponent from "@/screens/object-item-edit/object-item-edit-paginated";
import RepositoryActionMenu from "@/screens/repository/repository-action-menu";
import { isGenericSchema } from "@/screens/schema/utils";
import { IModelSchema } from "@/state/atoms/schema.atom";
import { isGeneric } from "@/utils/common";
import { Icon } from "@iconify-icon/react";
import { useState } from "react";
import { useLocation, useNavigate, useParams } from "react-router-dom";
Expand Down Expand Up @@ -52,7 +52,7 @@ export function DetailsButtons({ schema, objectDetailsData, permission }: Detail
/>
)}

{!isGeneric(schema) && schema.inherit_from?.includes(GENERIC_REPOSITORY_KIND) && (
{!isGenericSchema(schema) && schema.inherit_from?.includes(GENERIC_REPOSITORY_KIND) && (
<RepositoryActionMenu repositoryId={objectDetailsData.id} />
)}

Expand Down
5 changes: 3 additions & 2 deletions frontend/app/src/screens/schema/schema-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import Accordion from "@/components/display/accordion";
import { Badge } from "@/components/ui/badge";
import { SearchInput } from "@/components/ui/search-input";
import { QSP } from "@/config/qsp";
import { isGenericSchema } from "@/screens/schema/utils";
import { IModelSchema, genericsState, profilesAtom, schemaState } from "@/state/atoms/schema.atom";
import { classNames, isGeneric } from "@/utils/common";
import { classNames } from "@/utils/common";
import { Icon } from "@iconify-icon/react";
import { useAtomValue } from "jotai";
import * as R from "ramda";
Expand Down Expand Up @@ -80,7 +81,7 @@ export const SchemaSelector = ({ className = "" }: SchemaSelectorProps) => {
{schema.label}
</div>
<Badge className="self-baseline">
{isGeneric(schema) ? "Generic" : "Node"}
{isGenericSchema(schema) ? "Generic" : "Node"}
</Badge>
</h2>

Expand Down
14 changes: 9 additions & 5 deletions frontend/app/src/screens/schema/schema-viewer.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Button } from "@/components/buttons/button-primitive";
import { Badge } from "@/components/ui/badge";
import { QSP } from "@/config/qsp";
import { isGenericSchema, isNodeSchema, isProfileSchema } from "@/screens/schema/utils";
import { IModelSchema, genericsState, profilesAtom, schemaState } from "@/state/atoms/schema.atom";
import { classNames, isGeneric } from "@/utils/common";
import { classNames } from "@/utils/common";
import { Tab } from "@headlessui/react";
import { Icon } from "@iconify-icon/react";
import { useAtomValue } from "jotai";
Expand Down Expand Up @@ -75,7 +76,7 @@ export const SchemaViewer = ({
<div className="flex justify-between items-start">
<div className="space-x-1">
<Badge variant="blue">{schema.namespace}</Badge>
<Badge>{isGeneric(schema) ? "Generic" : "Node"}</Badge>
<Badge>{isGenericSchema(schema) ? "Generic" : "Node"}</Badge>
<span className="text-xs">{schema.id}</span>
</div>

Expand Down Expand Up @@ -165,13 +166,14 @@ const Properties = ({ schema }: { schema: IModelSchema }) => {
<PropertyRow title="Description" value={schema.description} />
</div>

{"used_by" in schema && (
{isGenericSchema(schema) && (
<div>
<PropertyRow title="Used by" value={<ModelDisplay kinds={schema.used_by} />} />
<PropertyRow title="Hierarchical" value={schema.hierarchical} />
</div>
)}

{"inherit_from" in schema && (
{isNodeSchema(schema) && (
<div>
<PropertyRow title="Inherit from" value={<ModelDisplay kinds={schema.inherit_from} />} />
<PropertyRow
Expand Down Expand Up @@ -202,7 +204,9 @@ const Properties = ({ schema }: { schema: IModelSchema }) => {
<PropertyRow title="Default filter" value={schema.default_filter} />
<PropertyRow title="Order by" value={schema.order_by} />
<PropertyRow title="Uniqueness constraints" value={schema.uniqueness_constraints} />
<PropertyRow title="Generate profile" value={schema.generate_profile} />
{!isProfileSchema(schema) && (
<PropertyRow title="Generate profile" value={schema.generate_profile} />
)}
</div>

<div>
Expand Down
22 changes: 19 additions & 3 deletions frontend/app/src/screens/schema/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import { IModelSchema } from "@/state/atoms/schema.atom";
import { isGeneric } from "@/utils/common";
import {
IModelSchema,
IProfileSchema,
iGenericSchema,
iNodeSchema,
} from "@/state/atoms/schema.atom";

export const isOfKind = (kind: string, schema: IModelSchema) => {
if (schema.kind === kind) return true;
if (!isGeneric(schema) && schema.inherit_from?.includes(kind)) return true;
if (!isGenericSchema(schema) && schema.inherit_from?.includes(kind)) return true;
return false;
};

export const isGenericSchema = (schema: IModelSchema): schema is iGenericSchema => {
return "used_by" in schema;
};

export const isNodeSchema = (schema: IModelSchema): schema is iNodeSchema => {
return "inherit_from" in schema;
};

export const isProfileSchema = (schema: IModelSchema): schema is IProfileSchema => {
return schema.namespace === "Profile";
};
5 changes: 0 additions & 5 deletions frontend/app/src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { IModelSchema, iGenericSchema } from "@/state/atoms/schema.atom";
import { type ClassValue, clsx } from "clsx";
import * as R from "ramda";
import { twMerge } from "tailwind-merge";
Expand Down Expand Up @@ -84,7 +83,3 @@ export const getTextColor = (background?: string) => {
export function warnUnexpectedType(x: never) {
console.warn(`unexpected type ${x}`);
}

export function isGeneric(schema: IModelSchema): schema is iGenericSchema {
return "used_by" in schema;
}
5 changes: 3 additions & 2 deletions frontend/app/src/utils/getSchemaObjectColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {
relationshipsForListView,
relationshipsForTabs,
} from "@/config/constants";
import { isGenericSchema } from "@/screens/schema/utils";
import { store } from "@/state";
import { iGenericSchema, iNodeSchema, profilesAtom } from "@/state/atoms/schema.atom";
import * as R from "ramda";
import { isGeneric, sortByOrderWeight } from "./common";
import { sortByOrderWeight } from "./common";

type tgetObjectAttributes = {
schema: iNodeSchema | iGenericSchema | undefined;
Expand Down Expand Up @@ -131,7 +132,7 @@ export const getSchemaObjectColumns = ({

// columns.length > 0 needed because of relationship-details-paginated.tsx
// Relationship needs refactoring to handle this better
return isGeneric(schema) && columns.length > 0 ? [kindColumn, ...columns] : columns;
return isGenericSchema(schema) && columns.length > 0 ? [kindColumn, ...columns] : columns;
};

export const getObjectTabs = (tabs: any[], data: any) => {
Expand Down
4 changes: 2 additions & 2 deletions frontend/app/src/utils/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
IP_PREFIX_GENERIC,
} from "@/screens/ipam/constants";
import { RESOURCE_GENERIC_KIND } from "@/screens/resource-manager/constants";
import { isGenericSchema } from "@/screens/schema/utils";
import { store } from "@/state";
import { genericsState, profilesAtom, schemaState } from "@/state/atoms/schema.atom";
import { isGeneric } from "@/utils/common";
import { constructPath, overrideQueryParams } from "./fetch";

const regex = /^Related/; // starts with Related
Expand Down Expand Up @@ -41,7 +41,7 @@ export const getObjectDetailsUrl2 = (
const schema = [...nodes, ...generics, ...profiles].find(({ kind }) => kind === objectKind);
if (!schema) return "#";

if (!isGeneric(schema)) {
if (!isGenericSchema(schema)) {
const inheritFrom = schema.inherit_from;

if (inheritFrom?.includes(IP_PREFIX_GENERIC)) {
Expand Down

0 comments on commit d657460

Please sign in to comment.