Skip to content

Commit

Permalink
reading and setting dataTypeId from value metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
CiaranMn committed Dec 4, 2024
1 parent 41b1a6a commit 929c94d
Show file tree
Hide file tree
Showing 24 changed files with 421 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
type EntityRootType,
extractEntityUuidFromEntityId,
} from "@local/hash-subgraph";
import { getRoots } from "@local/hash-subgraph/stdlib/subgraph/roots";
import { getRoots } from "@local/hash-subgraph/stdlib";
import { Box } from "@mui/material";
import { useMemo } from "react";

Expand All @@ -33,13 +33,11 @@ import {
} from "./links-section/shared/table-styling";

export const ClaimsSection = () => {
const { entitySubgraph, onEntityClick } = useEntityEditor();
const { entity, onEntityClick, isLocalDraftOnly } = useEntityEditor();

const entityUuid = useMemo(() => {
return extractEntityUuidFromEntityId(
getRoots(entitySubgraph)[0]!.metadata.recordId.entityId,
);
}, [entitySubgraph]);
return extractEntityUuidFromEntityId(entity.metadata.recordId.entityId);
}, [entity]);

const { data: claimsData } = useQuery<
GetEntitySubgraphQuery,
Expand Down Expand Up @@ -75,7 +73,7 @@ export const ClaimsSection = () => {
includeDrafts: true,
},
},
skip: !entityUuid,
skip: !entityUuid || isLocalDraftOnly,
fetchPolicy: "cache-and-network",
});

Expand Down Expand Up @@ -109,6 +107,10 @@ export const ClaimsSection = () => {
16,
);

if (isLocalDraftOnly) {
return null;
}

return (
<SectionWrapper
title="Claims"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { Entity } from "@local/hash-graph-sdk/entity";
import type { EntityProperties } from "@local/hash-graph-types/entity";
import { extractEntityUuidFromEntityId } from "@local/hash-subgraph";
import { getRoots } from "@local/hash-subgraph/stdlib";
import type { PropsWithChildren } from "react";
import {
Expand All @@ -13,6 +16,8 @@ import type { EntityEditorProps } from "../entity-editor";
export type TableExpandStatus = Record<string, boolean>;

interface Props extends EntityEditorProps {
entity: Entity<EntityProperties>;
isLocalDraftOnly: boolean;
propertyExpandStatus: TableExpandStatus;
togglePropertyExpand: (id: string) => void;
}
Expand Down Expand Up @@ -50,9 +55,11 @@ export const EntityEditorContextProvider = ({
});
}, []);

useMemo(() => {
const entity = useMemo(() => {
const roots = getRoots(entitySubgraph);

const foundEntity = roots[0];

if (roots.length > 1) {
/**
* This is an early warning system in case we accidentally start passing a subgraph with multiple roots to the entity editor.
Expand All @@ -69,6 +76,12 @@ export const EntityEditorContextProvider = ({
.join(", ")}`,
);
}

if (!foundEntity) {
throw new Error("No root entity found in entity editor subgraph");
}

return foundEntity;
}, [entitySubgraph]);

const state = useMemo(
Expand All @@ -81,10 +94,14 @@ export const EntityEditorContextProvider = ({
disableTypeClick,
draftLinksToArchive,
draftLinksToCreate,
entity,
entityLabel,
entitySubgraph,
handleTypesChange,
isDirty,
isLocalDraftOnly:
extractEntityUuidFromEntityId(entity.metadata.recordId.entityId) ===
"draft",
onEntityClick,
onEntityUpdated,
propertyExpandStatus,
Expand All @@ -104,6 +121,7 @@ export const EntityEditorContextProvider = ({
disableTypeClick,
draftLinksToArchive,
draftLinksToCreate,
entity,
entityLabel,
entitySubgraph,
handleTypesChange,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { generateEntityLabel } from "@local/hash-isomorphic-utils/generate-entit
import { simplifyProperties } from "@local/hash-isomorphic-utils/simplify-properties";
import type { FileProperties } from "@local/hash-isomorphic-utils/system-types/shared";
import { extractOwnedByIdFromEntityId } from "@local/hash-subgraph";
import { getRoots } from "@local/hash-subgraph/stdlib";
import {
Box,
CircularProgress,
Expand Down Expand Up @@ -62,12 +61,10 @@ const ReplaceFile = ({
isImage: boolean;
close: () => void;
}) => {
const { entitySubgraph, onEntityUpdated } = useEntityEditor();
const { entity, onEntityUpdated } = useEntityEditor();
const { refetch: refetchUser } = useAuthInfo();
const [fileBeingUploaded, setFileBeingUploaded] = useState<File | null>(null);

const entity = getRoots(entitySubgraph)[0]!;

const { uploadFile, uploads } = useFileUploads();
const uploadsProgress = useFileUploadsProgress();

Expand Down Expand Up @@ -170,11 +167,9 @@ export const FilePreviewSection = () => {
const [showSearch, setShowSearch] = useState(false);
const [showThumbnails, setShowThumbnails] = useState(true);

const { isDirty, readonly, closedMultiEntityType, entitySubgraph } =
const { isDirty, readonly, closedMultiEntityType, entity } =
useEntityEditor();

const entity = getRoots(entitySubgraph)[0]!;

const { isImage, fileUrl } = getFileProperties(entity.properties);

if (!fileUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { extractBaseUrl, type VersionedUrl } from "@blockprotocol/type-system";
import { extractVersion } from "@blockprotocol/type-system/slim";
import { typedEntries } from "@local/advanced-types/typed-entries";
import type { EntityTypeIdDiff } from "@local/hash-graph-client";
import type { EntityId, PropertyPath } from "@local/hash-graph-types/entity";
import {
EntityId,
isValueMetadata,
PropertyPath,
} from "@local/hash-graph-types/entity";
import type {
BaseUrl,
EntityTypeWithMetadata,
Expand Down Expand Up @@ -153,13 +157,19 @@ export const getHistoryEvents = (diffs: EntityDiff[], subgraph: Subgraph) => {

if (diffData.diff.properties) {
for (const propertyDiff of diffData.diff.properties) {
const propertyProvenance = changedEntityEdition.propertyMetadata(
const propertyMetadata = changedEntityEdition.propertyMetadata(
propertyDiff.path as PropertyPath,
)?.provenance;
);

if (!propertyMetadata || !isValueMetadata(propertyMetadata)) {
/**
* @todo H-2775 – handle property objects and changes to array contents
*/
continue;
}

const propertyProvenance = propertyMetadata.metadata.provenance;

/**
* @todo H-2775 – handle property objects and changes to array contents
*/
const propertyBaseUrl = propertyDiff.path[0] as BaseUrl;
try {
const propertyTypeWithMetadata = getPropertyTypeForEntity(
Expand Down Expand Up @@ -207,12 +217,16 @@ export const getHistoryEvents = (diffs: EntityDiff[], subgraph: Subgraph) => {
for (const [index, [key, value]] of typedEntries(
firstEntityEdition.properties,
).entries()) {
/**
* @todo H-2775 – handle property objects and changes to array contents
*/
const propertyProvenance = firstEntityEdition.propertyMetadata([
key,
])?.provenance;
const propertyMetadata = firstEntityEdition.propertyMetadata([key]);

if (!propertyMetadata || !isValueMetadata(propertyMetadata)) {
/**
* @todo H-2775 – handle property objects and changes to array contents
*/
continue;
}

const propertyProvenance = propertyMetadata.metadata.provenance;

try {
const propertyTypeWithMetadata = getPropertyTypeForEntity(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { LinkEntity } from "@local/hash-graph-sdk/entity";
import { getRoots } from "@local/hash-subgraph/stdlib";
import type { FunctionComponent } from "react";
import { useMemo } from "react";

Expand All @@ -11,19 +10,14 @@ export const LinkSection: FunctionComponent = () => {
const {
closedMultiEntityTypesMap,
closedMultiEntityTypesDefinitions,
entity,
entitySubgraph,
onEntityClick,
} = useEntityEditor();

const linkEntity = useMemo(() => {
const [rootEntity] = getRoots(entitySubgraph);

if (!rootEntity) {
throw new Error("No root entity found in entity editor subgraph.");
}

return new LinkEntity(rootEntity);
}, [entitySubgraph]);
return new LinkEntity(entity);
}, [entity]);

return (
<SectionWrapper title="Link">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { systemEntityTypes } from "@local/hash-isomorphic-utils/ontology-type-id
import {
getIncomingLinkAndSourceEntities,
getOutgoingLinksForEntity,
getRoots,
} from "@local/hash-subgraph/stdlib";
import { Stack } from "@mui/material";

Expand All @@ -13,9 +12,7 @@ import { IncomingLinksSection } from "./links-section/incoming-links-section";
import { OutgoingLinksSection } from "./links-section/outgoing-links-section";

export const LinksSection = ({ isLinkEntity }: { isLinkEntity: boolean }) => {
const { draftLinksToArchive, entitySubgraph } = useEntityEditor();

const entity = getRoots(entitySubgraph)[0]!;
const { draftLinksToArchive, entity, entitySubgraph } = useEntityEditor();

const outgoingLinks = getOutgoingLinksForEntity(
entitySubgraph,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { faMagnifyingGlass } from "@fortawesome/free-solid-svg-icons";
import { Chip, FontAwesomeIcon, IconButton } from "@hashintel/design-system";
import type { Entity } from "@local/hash-graph-sdk/entity";
import type { EntityProperties } from "@local/hash-graph-types/entity";
import {
getOutgoingLinkAndTargetEntities,
getRoots,
} from "@local/hash-subgraph/stdlib";
import { getOutgoingLinkAndTargetEntities } from "@local/hash-subgraph/stdlib";
import { Paper, Stack } from "@mui/material";
import { useState } from "react";

Expand Down Expand Up @@ -33,7 +30,7 @@ export const OutgoingLinksSection = ({
}: OutgoingLinksSectionPropsProps) => {
const [showSearch, setShowSearch] = useState(false);

const { entitySubgraph, readonly } = useEntityEditor();
const { entitySubgraph, entity, readonly } = useEntityEditor();

const rows = useRows();
const createGetCellContent = useCreateGetCellContent();
Expand All @@ -47,8 +44,6 @@ export const OutgoingLinksSection = ({
return null;
}

const entity = getRoots(entitySubgraph)[0]!;

const outgoingLinksAndTargets = readonly
? getOutgoingLinkAndTargetEntities(
entitySubgraph,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {
Timestamp,
} from "@local/hash-graph-types/temporal-versioning";
import { extractDraftIdFromEntityId } from "@local/hash-subgraph";
import { getRoots } from "@local/hash-subgraph/stdlib";
import { Box } from "@mui/material";
import produce from "immer";
import { useMemo, useState } from "react";
Expand Down Expand Up @@ -82,7 +81,7 @@ export const createDraftLinkEntity = ({
export const LinkedEntityListEditor: ProvideEditorComponent<LinkedWithCell> = (
props,
) => {
const { entitySubgraph, setDraftLinksToCreate, readonly } = useEntityEditor();
const { entity, setDraftLinksToCreate, readonly } = useEntityEditor();
const markLinkEntityToArchive = useMarkLinkEntityToArchive();

const { value: cell, onFinishedEditing, onChange } = props;
Expand All @@ -96,8 +95,6 @@ export const LinkedEntityListEditor: ProvideEditorComponent<LinkedWithCell> = (

const [addingLink, setAddingLink] = useState(!linkAndTargetEntities.length);

const entity = useMemo(() => getRoots(entitySubgraph)[0]!, [entitySubgraph]);

const onSelect = (selectedEntity: Entity, entityLabel: string) => {
const alreadyLinked = linkAndTargetEntities.find(
({ rightEntity }) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { Entity } from "@local/hash-graph-sdk/entity";
import { getClosedMultiEntityTypeFromMap } from "@local/hash-graph-sdk/entity";
import type { EntityId } from "@local/hash-graph-types/entity";
import { generateEntityLabel } from "@local/hash-isomorphic-utils/generate-entity-label";
import { getRoots } from "@local/hash-subgraph/stdlib";
import type { PaperProps } from "@mui/material";
import { Stack, Typography } from "@mui/material";
import {
Expand Down Expand Up @@ -69,10 +68,9 @@ export const LinkedEntitySelector = ({
entityIdsToFilterOut,
linkEntityTypeId,
}: LinkedEntitySelectorProps) => {
const { entitySubgraph, readonly } = useEntityEditor();
const { entity, readonly } = useEntityEditor();

const entityId = getRoots(entitySubgraph)[0]?.metadata.recordId
.entityId as EntityId;
const entityId = entity.metadata.recordId.entityId;

const [showUploadFileMenu, setShowUploadFileMenu] = useState(false);

Expand Down Expand Up @@ -127,13 +125,13 @@ export const LinkedEntitySelector = ({
},
makePublic: false,
onComplete: (upload) => {
const entity = upload.createdEntities.fileEntity;
const fileEntity = upload.createdEntities.fileEntity;

const label =
entity.properties[
fileEntity.properties[
"https://blockprotocol.org/@blockprotocol/types/property-type/display-name/"
] ??
entity.properties[
fileEntity.properties[
"https://blockprotocol.org/@blockprotocol/types/property-type/file-name/"
] ??
"File";
Expand Down Expand Up @@ -183,15 +181,15 @@ export const LinkedEntitySelector = ({
expectedEntityTypes={expectedEntityTypes}
includeDrafts={includeDrafts}
multiple={false}
onSelect={(entity, closedMultiEntityTypeMap) => {
onSelect={(selectedEntity, closedMultiEntityTypeMap) => {
const closedType = getClosedMultiEntityTypeFromMap(
closedMultiEntityTypeMap,
entity.metadata.entityTypeIds,
selectedEntity.metadata.entityTypeIds,
);

const label = generateEntityLabel(closedType, entity);
const label = generateEntityLabel(closedType, selectedEntity);

onSelect(entity, label);
onSelect(selectedEntity, label);
}}
className={GRID_CLICK_IGNORE_CLASS}
open
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { ProvideEditorComponent } from "@glideapps/glide-data-grid";
import type { Entity } from "@local/hash-graph-sdk/entity";
import { extractDraftIdFromEntityId } from "@local/hash-subgraph";
import { getRoots } from "@local/hash-subgraph/stdlib";
import { useMemo } from "react";

import { useMarkLinkEntityToArchive } from "../../../../../shared/use-mark-link-entity-to-archive";
import { useEntityEditor } from "../../../../entity-editor-context";
Expand All @@ -16,7 +14,7 @@ import { LinkedEntitySelector } from "./linked-entity-selector";
export const LinkedWithCellEditor: ProvideEditorComponent<LinkedWithCell> = (
props,
) => {
const { entitySubgraph, setDraftLinksToCreate } = useEntityEditor();
const { entity, setDraftLinksToCreate } = useEntityEditor();
const markLinkEntityToArchive = useMarkLinkEntityToArchive();

const { value: cell, onFinishedEditing } = props;
Expand All @@ -28,8 +26,6 @@ export const LinkedWithCellEditor: ProvideEditorComponent<LinkedWithCell> = (
maxItems,
} = cell.data.linkRow;

const entity = useMemo(() => getRoots(entitySubgraph)[0]!, [entitySubgraph]);

const onSelectForSingleLink = (
selectedEntity: Entity,
selectedEntityLabel: string,
Expand Down
Loading

0 comments on commit 929c94d

Please sign in to comment.