Skip to content

Commit

Permalink
update entity editor for new data type structure
Browse files Browse the repository at this point in the history
  • Loading branch information
CiaranMn authored and TimDiekmann committed Sep 27, 2024
1 parent 7d5c010 commit d159b3d
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ const guessDataTypeFromValue = (
) => {
const editorType = guessEditorTypeFromValue(value, expectedTypes);

const expectedType = expectedTypes.find((type) => type.type === editorType);
const expectedType = expectedTypes.find((type) =>
"type" in type
? type.type === editorType
: /**
* @todo H-3374 support anyOf in expected types. also don't need to guess the value any more, use dataTypeId from property metadata
*/
type.anyOf.some((subType) => subType.type === editorType),
);
if (!expectedType) {
throw new Error(
`Could not find guessed editor type ${editorType} among expected types ${expectedTypes
Expand Down Expand Up @@ -55,7 +62,14 @@ export const renderValueCell: CustomRenderer<ValueCell> = {
const left = rect.x + getCellHorizontalPadding();

const editorType = guessEditorTypeFromValue(value, expectedTypes);
const relevantType = expectedTypes.find((type) => type.type === editorType);
const relevantType = expectedTypes.find((type) =>
"type" in type
? type.type === editorType
: /**
* @todo H-3374 support anyOf in expected types. also don't need to guess the value any more, use dataTypeId from property metadata
*/
type.anyOf.some((subType) => subType.type === editorType),
);

const editorSpec = getEditorSpecs(editorType, relevantType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ export const SortableRow = ({

const editorType =
overriddenEditorType ?? guessEditorTypeFromValue(value, expectedTypes);
const expectedType = expectedTypes.find((type) => type.type === editorType);

const expectedType = expectedTypes.find((type) =>
"type" in type
? type.type === editorType
: /**
* @todo H-3374 support multiple expected data types
*/
type.anyOf.some((subType) => subType.type === editorType),
);

const editorSpec = getEditorSpecs(editorType, expectedType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,15 @@ export const SingleValueEditor: ValueCellEditorComponent = (props) => {
);
}

const expectedType = expectedTypes.find((type) => type.type === editorType);
const expectedType = expectedTypes.find((type) =>
"type" in type
? type.type === editorType
: /**
* @todo H-3374 support anyOf in expected types. also don't need to guess the value any more, use dataTypeId from property metadata
*/
type.anyOf.some((subType) => subType.type === editorType),
);

if (!expectedType) {
throw new Error(
`Could not find guessed editor type ${editorType} among expected types ${expectedTypes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,54 @@
import type { DataType } from "@blockprotocol/type-system/slim";
import type { DataTypeWithMetadata } from "@local/hash-graph-types/ontology";
import isPlainObject from "lodash/isPlainObject";

import type { EditorType } from "./types";

const isEmptyArray = (value: unknown) => Array.isArray(value) && !value.length;

const isValidTypeForSchemas = (
type: "string" | "boolean" | "number" | "object" | "null",
expectedTypes: DataType[],
) =>
expectedTypes.some((dataType) =>
"type" in dataType
? dataType.type === type
: dataType.anyOf.some((subType) => subType.type === type),
);

/**
* @todo H-3374 we don't need to guess the type anymore, because the exact dataTypeId will be in the entity's metadata
*/
export const guessEditorTypeFromValue = (
value: unknown,
expectedTypes: DataTypeWithMetadata["schema"][],
): EditorType => {
if (
typeof value === "string" &&
expectedTypes.some((dataType) => dataType.type === "string")
isValidTypeForSchemas("string", expectedTypes)
) {
return "string";
}

if (
typeof value === "boolean" &&
expectedTypes.some((dataType) => dataType.type === "boolean")
isValidTypeForSchemas("boolean", expectedTypes)
) {
return "boolean";
}

if (
typeof value === "number" &&
expectedTypes.some((dataType) => dataType.type === "number")
isValidTypeForSchemas("number", expectedTypes)
) {
return "number";
}

if (
isPlainObject(value) &&
expectedTypes.some((dataType) => dataType.type === "object")
) {
if (isPlainObject(value) && isValidTypeForSchemas("object", expectedTypes)) {
return "object";
}

if (
value === null &&
expectedTypes.some((dataType) => dataType.type === "null")
) {
if (value === null && isValidTypeForSchemas("null", expectedTypes)) {
return "null";
}

Expand All @@ -61,11 +69,25 @@ export const guessEditorTypeFromExpectedType = (
return "emptyList";
}

if (dataType.type === "array") {
let type: "string" | "number" | "boolean" | "object" | "null" | "array";

if ("anyOf" in dataType) {
/**
* @todo H-3374 support multiple expected data types
*/
type = dataType.anyOf[0].type;
} else {
type = dataType.type;
}

if (type === "array") {
/**
* @todo H-3374 support array and tuple data types
*/
throw new Error("Array data types are not yet handled.");
}

return dataType.type;
return type;
};

export const isBlankStringOrNullish = (value: unknown) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,13 @@ export const useCreateGetCellContent = (
}

if (shouldShowChangeTypeCell) {
const currentType = row.expectedTypes.find(
(opt) => opt.type === guessedType,
const currentType = row.expectedTypes.find((opt) =>
"type" in opt
? opt.type === guessedType
: /**
* @todo H-3374 support anyOf in expected types. also don't need to guess the value any more, use dataTypeId from property metadata
*/
opt.anyOf.some((subType) => subType.type === guessedType),
);
if (!currentType) {
throw new Error(
Expand Down
10 changes: 7 additions & 3 deletions libs/@local/hash-isomorphic-utils/src/data-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ export const formatDataValue = (
value: JsonValue,
schema?: DataType | SimpleValueSchema,
): FormattedValuePart[] => {
const { type } = schema ?? {
type: getJsonSchemaTypeFromValue(value),
};
/**
* @todo H-3374 callers should always provide a schema, because the dataTypeId will be in the entity's metadata
*/
const type =
schema && "type" in schema
? schema.type
: getJsonSchemaTypeFromValue(value);

if (type === "null") {
return createFormattedParts({ inner: "Null", schema });
Expand Down

0 comments on commit d159b3d

Please sign in to comment.