diff --git a/app/components/DataAttribute/DataAttributeForm.tsx b/app/components/DataAttribute/DataAttributeForm.tsx index a273293292..736e8cd2ab 100644 --- a/app/components/DataAttribute/DataAttributeForm.tsx +++ b/app/components/DataAttribute/DataAttributeForm.tsx @@ -9,7 +9,6 @@ import { type DataAttributeOptions, } from "@shared/models/types"; import { DataAttributeValidation } from "@shared/validations"; -import DataAttribute from "~/models/DataAttribute"; import Button from "~/components/Button"; import Flex from "~/components/Flex"; import Input from "~/components/Input"; @@ -70,7 +69,7 @@ export const DataAttributeForm = observer(function DataAttributeForm_({ field.onChange(value); if (value === DataAttributeDataType.List) { - setValue("options", { options: [""] }); + setValue("options", { values: [""] }); } }} ariaLabel={t("Format")} @@ -84,14 +83,14 @@ export const DataAttributeForm = observer(function DataAttributeForm_({ /> {values.dataType === DataAttributeDataType.List && ( - {values.options?.options?.map((option, index) => ( + {values.options?.values?.map((option, index) => (
{ - const newOptions = [...(values.options?.options ?? [])]; - newOptions[index] = event.target.value; - setValue("options", { options: newOptions }); + const newValues = [...(values.options?.values ?? [])]; + newValues[index] = event.target.value; + setValue("options", { values: newValues }); }} type="text" autoComplete="off" @@ -106,12 +105,12 @@ export const DataAttributeForm = observer(function DataAttributeForm_({ render={({ field }) => ( = + (values.options?.values?.length ?? 0) >= DataAttributeValidation.maxOptions } onClick={() => { field.onChange({ - options: [...(field.value?.options ?? []), ""], + values: [...(field.value?.values ?? []), ""], }); }} > diff --git a/app/hooks/useSettingsConfig.ts b/app/hooks/useSettingsConfig.ts index a84a4ad5f4..e3a8cc6279 100644 --- a/app/hooks/useSettingsConfig.ts +++ b/app/hooks/useSettingsConfig.ts @@ -20,8 +20,8 @@ import React, { ComponentProps } from "react"; import { useTranslation } from "react-i18next"; import { integrationSettingsPath } from "@shared/utils/routeHelpers"; import ZapierIcon from "~/components/Icons/ZapierIcon"; -import { Hook, PluginManager } from "~/utils/PluginManager"; import { Feature, FeatureFlags } from "~/utils/FeatureFlags"; +import { Hook, PluginManager } from "~/utils/PluginManager"; import isCloudHosted from "~/utils/isCloudHosted"; import lazy from "~/utils/lazyWithRetry"; import { settingsPath } from "~/utils/routeHelpers"; @@ -122,16 +122,6 @@ const useSettingsConfig = () => { group: t("Workspace"), icon: BeakerIcon, }, - { - name: t("Attributes"), - path: settingsPath("attributes"), - component: DataAttributes, - enabled: - can.createDataAttribute && - FeatureFlags.isEnabled(Feature.dataAttributes), - group: t("Workspace"), - icon: DatabaseIcon, - }, { name: t("Members"), path: settingsPath("members"), @@ -156,6 +146,16 @@ const useSettingsConfig = () => { group: t("Workspace"), icon: ShapesIcon, }, + { + name: t("Data Attributes"), + path: settingsPath("attributes"), + component: DataAttributes, + enabled: + can.createDataAttribute && + FeatureFlags.isEnabled(Feature.dataAttributes), + group: t("Workspace"), + icon: DatabaseIcon, + }, { name: t("Shared Links"), path: settingsPath("shares"), diff --git a/app/models/Document.ts b/app/models/Document.ts index b245fb61b1..8df5878392 100644 --- a/app/models/Document.ts +++ b/app/models/Document.ts @@ -6,6 +6,7 @@ import { action, autorun, computed, observable, set } from "mobx"; import { Node, Schema } from "prosemirror-model"; import ExtensionManager from "@shared/editor/lib/ExtensionManager"; import { richExtensions, withComments } from "@shared/editor/nodes"; +import { DataAttributeDataType } from "@shared/models/types"; import type { JSONObject, NavigationNode, @@ -498,7 +499,32 @@ export default class Document extends ParanoidModel { }; @action - setDataAttribute = (dataAttributeId: string, value: string | number) => { + setDataAttribute = ( + dataAttributeId: string, + input: string | number | boolean + ) => { + const definition = this.store.rootStore.dataAttributes.get(dataAttributeId); + if (!definition) { + throw new Error(`Data attribute ${dataAttributeId} not found`); + } + + let value: string | number | boolean = input; + switch (definition.dataType) { + case DataAttributeDataType.Number: + value = Number(input); + break; + case DataAttributeDataType.Boolean: + value = input === true || input === "true"; + break; + case DataAttributeDataType.List: + if (!definition.options?.values.includes(input as string)) { + throw new Error( + `Invalid value for data attribute ${dataAttributeId}` + ); + } + break; + } + this.dataAttributes = (this.dataAttributes ?? []) .filter((attr) => attr.dataAttributeId !== dataAttributeId) .concat({ dataAttributeId, value, updatedAt: new Date().toISOString() }); diff --git a/app/scenes/Document/components/DocumentMeta.tsx b/app/scenes/Document/components/DocumentMeta.tsx index ee0f906cbb..715dfa1504 100644 --- a/app/scenes/Document/components/DocumentMeta.tsx +++ b/app/scenes/Document/components/DocumentMeta.tsx @@ -9,6 +9,7 @@ import Flex from "@shared/components/Flex"; import { TeamPreference } from "@shared/types"; import Document from "~/models/Document"; import Revision from "~/models/Revision"; +import Button from "~/components/Button"; import DocumentMeta from "~/components/DocumentMeta"; import Fade from "~/components/Fade"; import Input from "~/components/Input"; @@ -55,7 +56,15 @@ function TitleDocumentMeta({ to, document, revision, ...rest }: Props) { const addProperty = ( setDraftAttribute((state) => (state ? null : {}))} + onClick={() => + setDraftAttribute((state) => + state + ? null + : { + dataAttributeId: dataAttributes.orderedData[0].id, + } + ) + } > Property @@ -100,7 +109,8 @@ function TitleDocumentMeta({ to, document, revision, ...rest }: Props) { ) : null} - {hasDataAttributes && document.dataAttributes.length === 0 ? ( + {hasDataAttributes && + (!document.dataAttributes || document.dataAttributes.length === 0) ? ( <> • {addProperty} ) : null} @@ -112,11 +122,20 @@ function TitleDocumentMeta({ to, document, revision, ...rest }: Props) { {definition?.name}: {" "} - {dataAttribute.value} + {String(dataAttribute.value)} +
); })} - {document.dataAttributes.length > 1 && addProperty} + {document.dataAttributes?.length > 0 && addProperty} {draftAttribute && ( + dataAttributeId && setDraftAttribute({ ...draftAttribute, dataAttributeId }) } /> diff --git a/app/scenes/Settings/DataAttributes.tsx b/app/scenes/Settings/DataAttributes.tsx index c3a9a61a7e..6b5d51b98d 100644 --- a/app/scenes/Settings/DataAttributes.tsx +++ b/app/scenes/Settings/DataAttributes.tsx @@ -1,11 +1,12 @@ import { observer } from "mobx-react"; -import { DatabaseIcon } from "outline-icons"; +import { DatabaseIcon, MoreIcon } from "outline-icons"; import * as React from "react"; import { useTranslation, Trans } from "react-i18next"; import DataAttribute from "~/models/DataAttribute"; import { Action } from "~/components/Actions"; import Button from "~/components/Button"; import Heading from "~/components/Heading"; +import Item from "~/components/List/Item"; import PaginatedList from "~/components/PaginatedList"; import Scene from "~/components/Scene"; import Text from "~/components/Text"; @@ -14,6 +15,7 @@ import useActionContext from "~/hooks/useActionContext"; import useCurrentTeam from "~/hooks/useCurrentTeam"; import usePolicy from "~/hooks/usePolicy"; import useStores from "~/hooks/useStores"; +import { DataAttributesHelper } from "~/utils/DataAttributesHelper"; function DataAttributes() { const team = useCurrentTeam(); @@ -24,7 +26,7 @@ function DataAttributes() { return ( } actions={ <> @@ -41,19 +43,25 @@ function DataAttributes() { } > - {t("Attributes")} + {t("Data Attributes")} Attributes allow you to define data to be stored with your documents. - They can be used to store metadata, tags, or any other structured - data. + They can be used to store custom properties, metadata, or any other + structured information that is common across documents. ( - {JSON.stringify(dataAttribute)} + } + /> )} /> diff --git a/app/utils/DataAttributesHelper.tsx b/app/utils/DataAttributesHelper.tsx new file mode 100644 index 0000000000..ebddd153cb --- /dev/null +++ b/app/utils/DataAttributesHelper.tsx @@ -0,0 +1,26 @@ +import { + CaseSensitiveIcon, + DoneIcon, + HashtagIcon, + TableOfContentsIcon, +} from "outline-icons"; +import * as React from "react"; +import { DataAttributeDataType } from "@shared/models/types"; +import DataAttribute from "~/models/DataAttribute"; + +export class DataAttributesHelper { + public static getIcon(dataAttribute: DataAttribute) { + switch (dataAttribute.dataType) { + case DataAttributeDataType.Boolean: + return ; + case DataAttributeDataType.Number: + return ; + case DataAttributeDataType.String: + return ; + case DataAttributeDataType.List: + return ; + default: + return null; + } + } +} diff --git a/server/commands/documentUpdater.ts b/server/commands/documentUpdater.ts index c198ec0162..1b1a279b2f 100644 --- a/server/commands/documentUpdater.ts +++ b/server/commands/documentUpdater.ts @@ -75,14 +75,11 @@ export default async function documentUpdater({ if (dataAttributes !== undefined) { // TODO: Validate schema document.dataAttributes = uniqBy( - [ - ...dataAttributes.map(({ dataAttributeId, value }) => ({ - dataAttributeId, - value, - updatedAt: new Date(), - })), - ...(document.dataAttributes ?? []), - ], + dataAttributes.map(({ dataAttributeId, value, updatedAt }) => ({ + dataAttributeId, + value, + updatedAt: updatedAt ?? new Date(), + })), "dataAttributeId" ); } diff --git a/server/routes/api/dataAttributes/schema.ts b/server/routes/api/dataAttributes/schema.ts index dbe31a5e28..eb764c12b8 100644 --- a/server/routes/api/dataAttributes/schema.ts +++ b/server/routes/api/dataAttributes/schema.ts @@ -48,7 +48,7 @@ export const DataAttributesCreateSchema = BaseSchema.extend({ /** Options for the data attribute (if list type) */ options: z .object({ - options: z.array(z.string()), + values: z.array(z.string()), }) .optional(), diff --git a/server/routes/api/documents/schema.ts b/server/routes/api/documents/schema.ts index 760c23b856..90c93f92a6 100644 --- a/server/routes/api/documents/schema.ts +++ b/server/routes/api/documents/schema.ts @@ -255,9 +255,14 @@ export const DocumentsUpdateSchema = BaseSchema.extend({ /** Whether the editing session is complete */ done: z.boolean().optional(), - // TODO + /** Data attributes to be updated, attributes not included will be removed */ dataAttributes: z - .array(z.object({ dataAttributeId: z.string(), value: z.string() })) + .array( + z.object({ + dataAttributeId: z.string(), + value: z.string().or(z.boolean()).or(z.number()), + }) + ) .optional(), }), }).refine((req) => !(req.body.append && !req.body.text), {