diff --git a/app/menus/DataAttributeMenu.tsx b/app/menus/DataAttributeMenu.tsx index 07e87e22d2..e9b2cb8c7a 100644 --- a/app/menus/DataAttributeMenu.tsx +++ b/app/menus/DataAttributeMenu.tsx @@ -1,7 +1,9 @@ +import copy from "copy-to-clipboard"; import { observer } from "mobx-react"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { useMenuState } from "reakit/Menu"; +import { toast } from "sonner"; import DataAttribute from "~/models/DataAttribute"; import ContextMenu from "~/components/ContextMenu"; import MenuItem from "~/components/ContextMenu/MenuItem"; @@ -34,6 +36,11 @@ function DataAttributeMenu({ dataAttribute }: Props) { }); }, [t, dialogs, dataAttribute]); + const handleCopy = React.useCallback(() => { + copy(dataAttribute.id); + toast.success("Copied to clipboard"); + }, [dataAttribute]); + const handleDelete = React.useCallback(() => { void dataAttribute.delete(); }, [dataAttribute]); @@ -45,6 +52,9 @@ function DataAttributeMenu({ dataAttribute }: Props) { {t("Edit")}… + + {t("Copy ID")} + {t("Delete")}… diff --git a/app/models/DataAttribute.ts b/app/models/DataAttribute.ts index a5b43d1b89..12f3d578a6 100644 --- a/app/models/DataAttribute.ts +++ b/app/models/DataAttribute.ts @@ -47,6 +47,17 @@ class DataAttribute extends ParanoidModel { /** The user ID that created this data attribute. */ createdById: string; + + /** The default value to use when creating a new property. */ + get defaultValue() { + switch (this.dataType) { + case DataAttributeDataType.Boolean: + return false; + case DataAttributeDataType.List: + return this.options?.options?.[0]?.value; + } + return undefined; + } } export default DataAttribute; diff --git a/app/scenes/Document/components/Properties.tsx b/app/scenes/Document/components/Properties.tsx index 3c91cf4c20..08cfef968e 100644 --- a/app/scenes/Document/components/Properties.tsx +++ b/app/scenes/Document/components/Properties.tsx @@ -32,26 +32,30 @@ export type PropertiesRef = { export const Properties = observer( React.forwardRef(function Properties_({ document }: Props, ref) { + const { dataAttributes } = useStores(); const [draftAttribute, setDraftAttribute] = React.useState(null); - const handleAddProperty = (dataAttributeId: string) => + const handleAddProperty = (dataAttributeId: string) => { + const definition = dataAttributes.get(dataAttributeId); + setDraftAttribute((state) => state ? null : { - value: "", + value: definition?.defaultValue ?? "", dataAttributeId, updatedAt: new Date().toISOString(), } ); + }; React.useImperativeHandle(ref, () => ({ addProperty: handleAddProperty, })); const handleSave = async () => { - if (draftAttribute?.value) { + if (draftAttribute) { document.setDataAttribute( draftAttribute.dataAttributeId, draftAttribute.value