From 36682574c6eab9d2bc2da81e3ceddceaeebe40de Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 13 Jun 2024 09:10:18 -0400 Subject: [PATCH] stash --- app/actions/definitions/dataAttributes.tsx | 3 +- .../DataAttribute/DataAttributeForm.tsx | 161 ++++++++++++++++++ .../DataAttribute/DataAttributeNew.tsx | 30 ++++ app/hooks/useSettingsConfig.ts | 5 +- app/models/Document.ts | 6 + app/scenes/DataAttributeNew.tsx | 0 app/scenes/Document/components/Insights.tsx | 27 ++- app/utils/FeatureFlags.ts | 3 + .../20240611010054-create-data-attributes.js | 10 +- server/models/DataAttribute.ts | 2 +- server/models/DocumentDataAttribute.ts | 4 +- server/presenters/dataAttribute.ts | 15 +- server/presenters/document.ts | 7 +- server/presenters/documentDataAttribute.ts | 15 +- server/routes/api/dataAttributes/schema.ts | 38 +++-- shared/validations.ts | 9 + 16 files changed, 294 insertions(+), 41 deletions(-) create mode 100644 app/components/DataAttribute/DataAttributeForm.tsx create mode 100644 app/components/DataAttribute/DataAttributeNew.tsx delete mode 100644 app/scenes/DataAttributeNew.tsx diff --git a/app/actions/definitions/dataAttributes.tsx b/app/actions/definitions/dataAttributes.tsx index 11ac550b2f..1756a3eadb 100644 --- a/app/actions/definitions/dataAttributes.tsx +++ b/app/actions/definitions/dataAttributes.tsx @@ -1,6 +1,7 @@ import { PlusIcon } from "outline-icons"; import * as React from "react"; import stores from "~/stores"; +import { DataAttributeNew } from "~/components/DataAttribute/DataAttributeNew"; import { createAction } from ".."; import { SettingsSection } from "../sections"; @@ -18,7 +19,7 @@ export const createDataAttribute = createAction({ stores.dialogs.openModal({ title: t("New attribute"), - // content: TODO + content: , }); }, }); diff --git a/app/components/DataAttribute/DataAttributeForm.tsx b/app/components/DataAttribute/DataAttributeForm.tsx new file mode 100644 index 0000000000..a42ffda63e --- /dev/null +++ b/app/components/DataAttribute/DataAttributeForm.tsx @@ -0,0 +1,161 @@ +import capitalize from "lodash/capitalize"; +import { observer } from "mobx-react"; +import { PlusIcon } from "outline-icons"; +import * as React from "react"; +import { Controller, useForm } from "react-hook-form"; +import { useTranslation } from "react-i18next"; +import { + DataAttributeDataType, + 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"; +import InputSelect from "../InputSelect"; +import NudeButton from "../NudeButton"; +import Switch from "../Switch"; + +export interface FormData { + name: string; + description?: string; + dataType: DataAttributeDataType; + options?: DataAttributeOptions; + pinned: boolean; +} + +export const DataAttributeForm = observer(function DataAttributeForm_({ + handleSubmit, + dataAttribute, +}: { + handleSubmit: (data: FormData) => void; + dataAttribute?: DataAttribute; +}) { + const { t } = useTranslation(); + const { + register, + handleSubmit: formHandleSubmit, + formState, + watch, + control, + setFocus, + setValue, + } = useForm({ + mode: "all", + defaultValues: { + name: dataAttribute?.name, + description: dataAttribute?.description ?? undefined, + dataType: dataAttribute?.dataType ?? DataAttributeDataType.String, + options: dataAttribute?.options ?? undefined, + pinned: dataAttribute?.pinned ?? false, + }, + }); + React.useEffect(() => { + setTimeout(() => setFocus("name", { shouldSelect: true }), 100); + }, [setFocus]); + + const values = watch(); + + return ( +
+ ( + { + field.onChange(value); + + if (value === DataAttributeDataType.List) { + setValue("options", { options: [""] }); + } + }} + ariaLabel={t("Format")} + label={t("Format")} + options={Object.values(DataAttributeDataType).map((dataType) => ({ + value: dataType, + label: t(capitalize(dataType)), + }))} + /> + )} + /> + {values.dataType === DataAttributeDataType.List && ( + + {values.options?.options?.map((option, index) => ( +
+ +
+ ))} + ( + = + DataAttributeValidation.maxOptions + } + onClick={() => { + field.onChange({ + options: [...(field.value?.options ?? []), ""], + }); + }} + > + + + )} + /> +
+ )} + + + + + + + + ); +}); diff --git a/app/components/DataAttribute/DataAttributeNew.tsx b/app/components/DataAttribute/DataAttributeNew.tsx new file mode 100644 index 0000000000..e274c9e7e2 --- /dev/null +++ b/app/components/DataAttribute/DataAttributeNew.tsx @@ -0,0 +1,30 @@ +import { observer } from "mobx-react"; +import * as React from "react"; +import { toast } from "sonner"; +import DataAttribute from "~/models/DataAttribute"; +import useStores from "~/hooks/useStores"; +import { DataAttributeForm, FormData } from "./DataAttributeForm"; + +type Props = { + onSubmit: () => void; +}; + +export const DataAttributeNew = observer(function DataAttributeNew_({ + onSubmit, +}: Props) { + const { dataAttributes } = useStores(); + const handleSubmit = React.useCallback( + async (data: FormData) => { + try { + const dataAttribute = new DataAttribute(data, dataAttributes); + await dataAttribute.save(); + onSubmit?.(); + } catch (error) { + toast.error(error.message); + } + }, + [dataAttributes, onSubmit] + ); + + return ; +}); diff --git a/app/hooks/useSettingsConfig.ts b/app/hooks/useSettingsConfig.ts index ef7cec5b82..8f9662592e 100644 --- a/app/hooks/useSettingsConfig.ts +++ b/app/hooks/useSettingsConfig.ts @@ -21,6 +21,7 @@ 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 isCloudHosted from "~/utils/isCloudHosted"; import lazy from "~/utils/lazyWithRetry"; import { settingsPath } from "~/utils/routeHelpers"; @@ -125,7 +126,9 @@ const useSettingsConfig = () => { name: t("Attributes"), path: settingsPath("attributes"), component: DataAttributes, - enabled: can.createDataAttribute, + enabled: + can.createDataAttribute && + FeatureFlags.isEnabled(Feature.dataAttributes), group: t("Workspace"), icon: DatabaseIcon, }, diff --git a/app/models/Document.ts b/app/models/Document.ts index 77cc48b11f..c8245bdbef 100644 --- a/app/models/Document.ts +++ b/app/models/Document.ts @@ -62,6 +62,12 @@ export default class Document extends ParanoidModel { @observable lastViewedAt: string | undefined; + @observable + dataAttributes: { + dataAttributeId: string; + value: string; + }[]; + store: DocumentsStore; @Field diff --git a/app/scenes/DataAttributeNew.tsx b/app/scenes/DataAttributeNew.tsx deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/app/scenes/Document/components/Insights.tsx b/app/scenes/Document/components/Insights.tsx index 156178030e..3ab5a1b8e3 100644 --- a/app/scenes/Document/components/Insights.tsx +++ b/app/scenes/Document/components/Insights.tsx @@ -20,11 +20,12 @@ import usePolicy from "~/hooks/usePolicy"; import useStores from "~/hooks/useStores"; import useTextSelection from "~/hooks/useTextSelection"; import InsightsMenu from "~/menus/InsightsMenu"; +import { Feature, FeatureFlags } from "~/utils/FeatureFlags"; import { documentPath } from "~/utils/routeHelpers"; import Sidebar from "./SidebarLayout"; function Insights() { - const { views, documents } = useStores(); + const { views, dataAttributes, documents } = useStores(); const { t } = useTranslation(); const match = useRouteMatch<{ documentSlug: string }>(); const history = useHistory(); @@ -44,6 +45,9 @@ function Insights() { useKeyDown("Escape", onCloseInsights); + // Temporarily here, insights will be removed / renamed. + const hasDataAttributes = FeatureFlags.isEnabled(Feature.dataAttributes); + return ( {document ? ( @@ -54,6 +58,27 @@ function Insights() { justify="space-between" >
+ {hasDataAttributes && ( + + {t("Data attributes")} + + {dataAttributes.orderedData.map((dataAttribute) => ( + + {dataAttribute.name}:{" "} + { + document.dataAttributes?.find( + (da) => da.dataAttributeId === dataAttribute.id + )?.value + } + + ))} + + )} {document.sourceMetadata && ( <> diff --git a/app/utils/FeatureFlags.ts b/app/utils/FeatureFlags.ts index 18ce3741ff..2e4ce0ccc5 100644 --- a/app/utils/FeatureFlags.ts +++ b/app/utils/FeatureFlags.ts @@ -4,11 +4,14 @@ import Storage from "@shared/utils/Storage"; export enum Feature { /** New collection permissions UI */ newCollectionSharing = "newCollectionSharing", + /** Document data attributes */ + dataAttributes = "dataAttributes", } /** Default values for feature flags */ const FeatureDefaults: Record = { [Feature.newCollectionSharing]: true, + [Feature.dataAttributes]: false, }; /** diff --git a/server/migrations/20240611010054-create-data-attributes.js b/server/migrations/20240611010054-create-data-attributes.js index 9f249c8461..d491e70fd6 100644 --- a/server/migrations/20240611010054-create-data-attributes.js +++ b/server/migrations/20240611010054-create-data-attributes.js @@ -109,7 +109,15 @@ module.exports = { value: { type: Sequelize.STRING, allowNull: false, - } + }, + createdAt: { + type: Sequelize.DATE, + allowNull: false, + }, + updatedAt: { + type: Sequelize.DATE, + allowNull: false, + }, }, { transaction }); diff --git a/server/models/DataAttribute.ts b/server/models/DataAttribute.ts index cd26d426cb..a994798066 100644 --- a/server/models/DataAttribute.ts +++ b/server/models/DataAttribute.ts @@ -69,7 +69,7 @@ class DataAttribute extends ParanoidModel< // hooks @BeforeCreate static async checkMaximumAttributes(model: DataAttribute) { - const count = await DataAttribute.count({ + const count = await this.count({ where: { teamId: model.teamId, }, diff --git a/server/models/DocumentDataAttribute.ts b/server/models/DocumentDataAttribute.ts index a2c74dd5bb..5f9e54e278 100644 --- a/server/models/DocumentDataAttribute.ts +++ b/server/models/DocumentDataAttribute.ts @@ -9,7 +9,7 @@ import { import DataAttribute from "./DataAttribute"; import Document from "./Document"; import User from "./User"; -import Model from "./base/Model"; +import IdModel from "./base/IdModel"; import Fix from "./decorators/Fix"; @Table({ @@ -17,7 +17,7 @@ import Fix from "./decorators/Fix"; modelName: "document_data_attribute", }) @Fix -class DocumentDataAttribute extends Model< +class DocumentDataAttribute extends IdModel< InferAttributes, Partial> > { diff --git a/server/presenters/dataAttribute.ts b/server/presenters/dataAttribute.ts index f1126151c3..f66d81643e 100644 --- a/server/presenters/dataAttribute.ts +++ b/server/presenters/dataAttribute.ts @@ -1,14 +1,15 @@ -import DocumentDataAttribute from "@server/models/DocumentDataAttribute"; +import DataAttribute from "@server/models/DataAttribute"; -export default function presentDocumentDataAttribute( - dataAttribute: DocumentDataAttribute -) { +export default function presentDataAttribute(dataAttribute: DataAttribute) { return { id: dataAttribute.id, - value: dataAttribute.value, - dataAttributeId: dataAttribute.dataAttributeId, - documentId: dataAttribute.documentId, + name: dataAttribute.name, + description: dataAttribute.description, + dataType: dataAttribute.dataType, + options: dataAttribute.options, + pinned: dataAttribute.pinned, createdAt: dataAttribute.createdAt, updatedAt: dataAttribute.updatedAt, + deletedAt: dataAttribute.deletedAt, }; } diff --git a/server/presenters/document.ts b/server/presenters/document.ts index 892a801abd..2f3ae325c6 100644 --- a/server/presenters/document.ts +++ b/server/presenters/document.ts @@ -3,7 +3,7 @@ import { Document } from "@server/models"; import { DocumentHelper } from "@server/models/helpers/DocumentHelper"; import { TextHelper } from "@server/models/helpers/TextHelper"; import { APIContext } from "@server/types"; -import presentDocumentDataAttribute from "./dataAttribute"; +import presentDocumentDataAttribute from "./documentDataAttribute"; import presentUser from "./user"; type Options = { @@ -77,9 +77,8 @@ async function presentDocument( if (!options.isPublic) { const source = await document.$get("import"); - data.dataAttributes = document.dataAttributes?.map( - presentDocumentDataAttribute(dataAttribute) - ) ?? []; + data.dataAttributes = + document.dataAttributes?.map(presentDocumentDataAttribute) ?? []; data.isCollectionDeleted = await document.isCollectionDeleted(); data.collectionId = document.collectionId; data.parentDocumentId = document.parentDocumentId; diff --git a/server/presenters/documentDataAttribute.ts b/server/presenters/documentDataAttribute.ts index f66d81643e..4924cc31f3 100644 --- a/server/presenters/documentDataAttribute.ts +++ b/server/presenters/documentDataAttribute.ts @@ -1,15 +1,14 @@ -import DataAttribute from "@server/models/DataAttribute"; +import type DocumentDataAttribute from "@server/models/DocumentDataAttribute"; -export default function presentDataAttribute(dataAttribute: DataAttribute) { +export default function presentDocumentDataAttribute( + dataAttribute: DocumentDataAttribute +) { return { id: dataAttribute.id, - name: dataAttribute.name, - description: dataAttribute.description, - dataType: dataAttribute.dataType, - options: dataAttribute.options, - pinned: dataAttribute.pinned, + value: dataAttribute.value, + dataAttributeId: dataAttribute.dataAttributeId, + documentId: dataAttribute.documentId, createdAt: dataAttribute.createdAt, updatedAt: dataAttribute.updatedAt, - deletedAt: dataAttribute.deletedAt, }; } diff --git a/server/routes/api/dataAttributes/schema.ts b/server/routes/api/dataAttributes/schema.ts index 0cc5c3f569..dbe31a5e28 100644 --- a/server/routes/api/dataAttributes/schema.ts +++ b/server/routes/api/dataAttributes/schema.ts @@ -1,5 +1,5 @@ import z from "zod"; -import { DataAttributeDataType } from "@server/models/DataAttribute"; +import { DataAttributeDataType } from "@shared/models/types"; import { BaseSchema } from "../schema"; const BaseIdSchema = z.object({ @@ -34,24 +34,32 @@ export const DataAttributesListSchema = BaseSchema.extend({ export type DataAttributesListReq = z.infer; export const DataAttributesCreateSchema = BaseSchema.extend({ - body: z.object({ - /** Name of the data attribute */ - name: z.string(), + body: z + .object({ + /** Name of the data attribute */ + name: z.string(), - /** Description of the data attribute */ - description: z.string().optional(), + /** Description of the data attribute */ + description: z.string().optional(), - /** Type of the data attribute */ - dataType: z.nativeEnum(DataAttributeDataType), + /** Type of the data attribute */ + dataType: z.nativeEnum(DataAttributeDataType), - /** Options for the data attribute (if list type) */ - options: z.object({ - options: z.array(z.string()), - }), + /** Options for the data attribute (if list type) */ + options: z + .object({ + options: z.array(z.string()), + }) + .optional(), - /** Whether the data attribute is pinned to the top of document */ - pinned: z.boolean().optional(), - }), + /** Whether the data attribute is pinned to the top of document */ + pinned: z.boolean().optional(), + }) + .refine( + (val) => + val.dataType !== DataAttributeDataType.List || + (val.dataType === DataAttributeDataType.List && val.options) + ), }); export type DataAttributesCreateReq = z.infer< diff --git a/shared/validations.ts b/shared/validations.ts index cc4974681b..bc26a4b1f5 100644 --- a/shared/validations.ts +++ b/shared/validations.ts @@ -34,8 +34,17 @@ export const DataAttributeValidation = { /** The maximum length of the name */ maxNameLength: 255, + /** The maximum length of the description */ + maxDescriptionLength: 1000, + /** The maximum number of data attributes */ max: 25, + + /** The maximum number of data attributes that can be pinned */ + maxPinned: 3, + + /** The maximum number of options for a single attribute */ + maxOptions: 10, }; export const CollectionValidation = {