This commit is contained in:
Tom Moor committed 2024-07-24 21:49:07 -04:00
1 parent 36682574c6
commit 75e457feee
15 files changed
+293 -236

No files matched your search

@@ -88,6 +88,11 @@ export const DataAttributeForm = observer(function DataAttributeForm_({
<div key={index}>
<Input
value={option}
onChange={(event) => {
const newOptions = [...(values.options?.options ?? [])];
newOptions[index] = event.target.value;
setValue("options", { options: newOptions });
}}
type="text"
autoComplete="off"
autoFocus
+19 -1
View File
@@ -62,10 +62,12 @@ export default class Document extends ParanoidModel {
@observable
lastViewedAt: string | undefined;
@Field
@observable
dataAttributes: {
dataAttributeId: string;
value: string;
value: string | number;
updatedAt: string;
}[];
store: DocumentsStore;
@@ -490,6 +492,22 @@ export default class Document extends ParanoidModel {
});
};
@action
setDataAttribute = (dataAttributeId: string, value: string | number) => {
this.dataAttributes = (this.dataAttributes ?? [])
.filter((attr) => attr.dataAttributeId !== dataAttributeId)
.concat({ dataAttributeId, value, updatedAt: new Date().toISOString() });
return this;
};
@action
deleteDataAttribute = (dataAttributeId: string) => {
this.dataAttributes = this.dataAttributes?.filter(
(attr) => attr.dataAttributeId !== dataAttributeId
);
return this;
};
@action
updateLastViewed = (view: View) => {
this.lastViewedAt = view.lastViewedAt;
+20 -2
View File
@@ -13,6 +13,7 @@ import useCurrentTeam from "~/hooks/useCurrentTeam";
import useCurrentUser from "~/hooks/useCurrentUser";
import usePolicy from "~/hooks/usePolicy";
import useStores from "~/hooks/useStores";
import { Feature, FeatureFlags } from "~/utils/FeatureFlags";
import Logger from "~/utils/Logger";
import {
NotFoundError,
@@ -53,8 +54,16 @@ type Props = RouteComponentProps<Params, StaticContext, LocationState> & {
};
function DataLoader({ match, children }: Props) {
const { ui, views, shares, comments, documents, revisions, subscriptions } =
useStores();
const {
ui,
views,
shares,
comments,
documents,
revisions,
subscriptions,
dataAttributes,
} = useStores();
const team = useCurrentTeam();
const user = useCurrentUser();
const [error, setError] = React.useState<Error | null>(null);
@@ -82,6 +91,15 @@ function DataLoader({ match, children }: Props) {
const can = usePolicy(document);
const location = useLocation<LocationState>();
React.useEffect(() => {
if (
!dataAttributes.isLoaded &&
FeatureFlags.isEnabled(Feature.dataAttributes)
) {
void dataAttributes.fetchAll();
}
}, [dataAttributes]);
React.useEffect(() => {
async function fetchDocument() {
try {
+127 -39
View File
@@ -1,18 +1,23 @@
import { LocationDescriptor } from "history";
import { observer, useObserver } from "mobx-react";
import { CommentIcon } from "outline-icons";
import { CommentIcon, PlusIcon } from "outline-icons";
import * as React from "react";
import { useTranslation } from "react-i18next";
import { Link, useRouteMatch } from "react-router-dom";
import styled from "styled-components";
import Flex from "@shared/components/Flex";
import { TeamPreference } from "@shared/types";
import Document from "~/models/Document";
import Revision from "~/models/Revision";
import DocumentMeta from "~/components/DocumentMeta";
import Fade from "~/components/Fade";
import Input from "~/components/Input";
import InputSelect from "~/components/InputSelect";
import Text from "~/components/Text";
import useCurrentTeam from "~/hooks/useCurrentTeam";
import usePolicy from "~/hooks/usePolicy";
import useStores from "~/hooks/useStores";
import { Feature, FeatureFlags } from "~/utils/FeatureFlags";
import { documentPath, documentInsightsPath } from "~/utils/routeHelpers";
type Props = {
@@ -24,7 +29,7 @@ type Props = {
};
function TitleDocumentMeta({ to, document, revision, ...rest }: Props) {
const { views, comments, ui } = useStores();
const { views, comments, dataAttributes, ui } = useStores();
const { t } = useTranslation();
const match = useRouteMatch();
const team = useCurrentTeam();
@@ -33,53 +38,136 @@ function TitleDocumentMeta({ to, document, revision, ...rest }: Props) {
const onlyYou = totalViewers === 1 && documentViews[0].userId;
const viewsLoadedOnMount = React.useRef(totalViewers > 0);
const can = usePolicy(document);
const [draftAttribute, setDraftAttribute] = React.useState<{
dataAttributeId: string;
value: string;
} | null>(null);
const Wrapper = viewsLoadedOnMount.current ? React.Fragment : Fade;
const insightsPath = documentInsightsPath(document);
const commentsCount = comments.unresolvedCommentsInDocumentCount(document.id);
const hasDataAttributes =
FeatureFlags.isEnabled(Feature.dataAttributes) &&
dataAttributes.orderedData.length > 0;
const addProperty = (
<InlineLink
to={documentPath(document)}
onClick={() => setDraftAttribute((state) => (state ? null : {}))}
>
<PlusIcon size={18} /> Property
</InlineLink>
);
return (
<Meta document={document} revision={revision} to={to} replace {...rest}>
{team.getPreference(TeamPreference.Commenting) && can.comment && (
<>
&nbsp;•&nbsp;
<CommentLink
to={documentPath(document)}
onClick={() => ui.toggleComments(document.id)}
>
<CommentIcon size={18} />
{commentsCount
? t("{{ count }} comment", { count: commentsCount })
: t("Comment")}
</CommentLink>
</>
)}
{totalViewers &&
can.listViews &&
!document.isDraft &&
!document.isTemplate ? (
<Wrapper>
&nbsp;•&nbsp;
<Link
to={
match.url === insightsPath ? documentPath(document) : insightsPath
}
>
{t("Viewed by")}{" "}
{onlyYou
? t("only you")
: `${totalViewers} ${
totalViewers === 1 ? t("person") : t("people")
}`}
</Link>
</Wrapper>
) : null}
</Meta>
<>
<Meta document={document} revision={revision} to={to} replace {...rest}>
{team.getPreference(TeamPreference.Commenting) && can.comment && (
<>
&nbsp;•&nbsp;
<InlineLink
to={documentPath(document)}
onClick={() => ui.toggleComments(document.id)}
>
<CommentIcon size={18} />
{commentsCount
? t("{{ count }} comment", { count: commentsCount })
: t("Comment")}
</InlineLink>
</>
)}
{totalViewers &&
can.listViews &&
!document.isDraft &&
!document.isTemplate ? (
<Wrapper>
&nbsp;•&nbsp;
<Link
to={
match.url === insightsPath
? documentPath(document)
: insightsPath
}
>
{t("Viewed by")}{" "}
{onlyYou
? t("only you")
: `${totalViewers} ${
totalViewers === 1 ? t("person") : t("people")
}`}
</Link>
</Wrapper>
) : null}
{hasDataAttributes && document.dataAttributes.length === 0 ? (
<>&nbsp;•&nbsp;{addProperty}</>
) : null}
</Meta>
<Properties>
{document.dataAttributes?.map((dataAttribute) => {
const definition = dataAttributes.get(dataAttribute.dataAttributeId);
return (
<div key={dataAttribute.dataAttributeId}>
<Text type="tertiary" weight="bold">
{definition?.name}:
</Text>{" "}
<Text type="tertiary">{dataAttribute.value}</Text>
</div>
);
})}
{document.dataAttributes.length > 1 && addProperty}
{draftAttribute && (
<Flex gap={8} auto>
<InputSelect
ariaLabel="Type"
options={dataAttributes.orderedData.map((attribute) => ({
label: attribute.name,
value: attribute.id,
}))}
value={
draftAttribute?.dataAttributeId ??
dataAttributes.orderedData[0].id
}
onChange={(dataAttributeId) =>
setDraftAttribute({ ...draftAttribute, dataAttributeId })
}
/>
<Input
placeholder="Value"
value={draftAttribute?.value ?? ""}
onBlur={async () => {
if (draftAttribute.value) {
document.setDataAttribute(
draftAttribute.dataAttributeId,
draftAttribute.value
);
await document.save();
}
}}
onChange={(event) =>
setDraftAttribute({
...draftAttribute,
value: event.target.value,
})
}
/>
</Flex>
)}
</Properties>
</>
);
}
const CommentLink = styled(Link)`
const Properties = styled.div`
margin-top: -1.9em;
margin-bottom: 2em;
font-size: 14px;
position: relative;
`;
const InlineLink = styled(Link)`
display: inline-flex;
align-items: center;
`;
+1 -26
View File
@@ -20,12 +20,11 @@ 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, dataAttributes, documents } = useStores();
const { views, documents } = useStores();
const { t } = useTranslation();
const match = useRouteMatch<{ documentSlug: string }>();
const history = useHistory();
@@ -45,9 +44,6 @@ function Insights() {
useKeyDown("Escape", onCloseInsights);
// Temporarily here, insights will be removed / renamed.
const hasDataAttributes = FeatureFlags.isEnabled(Feature.dataAttributes);
return (
<Sidebar title={t("Insights")} onClose={onCloseInsights}>
{document ? (
@@ -58,27 +54,6 @@ function Insights() {
justify="space-between"
>
<div>
{hasDataAttributes && (
<Content column>
<Heading>{t("Data attributes")}</Heading>
{dataAttributes.orderedData.map((dataAttribute) => (
<Text
as="p"
type="secondary"
size="small"
key={dataAttribute.id}
>
{dataAttribute.name}:{" "}
{
document.dataAttributes?.find(
(da) => da.dataAttributeId === dataAttribute.id
)?.value
}
</Text>
))}
</Content>
)}
<Content column>
{document.sourceMetadata && (
<>
+1 -1
View File
@@ -50,7 +50,7 @@ function DataAttributes() {
</Trans>
</Text>
<PaginatedList
fetch={dataAttributes.fetchPage}
fetch={dataAttributes.fetchAll}
items={dataAttributes.orderedData}
renderItem={(dataAttribute: DataAttribute) => (
<span>{JSON.stringify(dataAttribute)}</span>
+21 -27
View File
@@ -1,5 +1,6 @@
import uniqBy from "lodash/uniqBy";
import { Transaction } from "sequelize";
import { Event, Document, User, DocumentDataAttribute } from "@server/models";
import { Event, Document, User } from "@server/models";
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
type Props = {
@@ -7,8 +8,11 @@ type Props = {
user: User;
/** The existing document */
document: Document;
/** Data attributes to apply to the document */
attributes?: Pick<DocumentDataAttribute, "value" | "dataAttributeId">[];
/** TODO: Data attributes to apply to the document */
dataAttributes?: {
dataAttributeId: string;
value: string;
}[];
/** The new title */
title?: string;
/** The document icon */
@@ -49,7 +53,7 @@ type Props = {
export default async function documentUpdater({
user,
document,
attributes,
dataAttributes,
title,
icon,
color,
@@ -68,8 +72,19 @@ export default async function documentUpdater({
const previousTitle = document.title;
const cId = collectionId || document.collectionId;
if (attributes !== undefined) {
await updateDataAttributes(user, document, attributes, { transaction });
if (dataAttributes !== undefined) {
// TODO: Validate schema
document.dataAttributes = uniqBy(
[
...dataAttributes.map(({ dataAttributeId, value }) => ({
dataAttributeId,
value,
updatedAt: new Date(),
})),
...(document.dataAttributes ?? []),
],
"dataAttributeId"
);
}
if (title !== undefined) {
@@ -157,24 +172,3 @@ export default async function documentUpdater({
transaction,
});
}
async function updateDataAttributes(
user: User,
document: Document,
attributes: Pick<DocumentDataAttribute, "value" | "dataAttributeId">[],
{ transaction }: { transaction: Transaction }
) {
await DocumentDataAttribute.destroy({
where: { documentId: document.id },
transaction,
});
await DocumentDataAttribute.bulkCreate(
attributes.map((attribute) => ({
...attribute,
userId: user.id,
documentId: document.id,
})),
{ transaction }
);
}
@@ -74,70 +74,18 @@ module.exports = {
{ transaction }
);
await queryInterface.createTable(
"document_data_attributes",
{
id: {
type: Sequelize.UUID,
allowNull: false,
primaryKey: true,
},
userId: {
type: Sequelize.UUID,
allowNull: false,
onDelete: "cascade",
references: {
model: "users",
}
},
documentId: {
type: Sequelize.UUID,
allowNull: false,
onDelete: "cascade",
references: {
model: "documents",
}
},
dataAttributeId: {
type: Sequelize.UUID,
allowNull: false,
onDelete: "cascade",
references: {
model: "data_attributes",
}
},
value: {
type: Sequelize.STRING,
allowNull: false,
},
createdAt: {
type: Sequelize.DATE,
allowNull: false,
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false,
},
}, {
transaction
});
await queryInterface.addIndex(
"document_data_attributes",
["documentId"],
{ transaction }
);
await queryInterface.addIndex(
"document_data_attributes",
["dataAttributeId"],
{ transaction }
);
await queryInterface.addColumn("documents", "dataAttributes", {
type: Sequelize.JSONB,
allowNull: true,
});
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.dropTable("document_data_attributes", { transaction });
await queryInterface.dropTable("data_attributes", { transaction });
await queryInterface.removeColumn("documents", "dataAttributes", {
transaction,
});
});
}
};
+32 -3
View File
@@ -1,4 +1,8 @@
import { InferAttributes, InferCreationAttributes } from "sequelize";
import {
InferAttributes,
InferCreationAttributes,
type SaveOptions,
} from "sequelize";
import {
Column,
Table,
@@ -10,6 +14,7 @@ import {
Default,
BeforeCreate,
} from "sequelize-typescript";
import { z } from "zod";
import {
DataAttributeDataType,
type DataAttributeOptions,
@@ -52,7 +57,7 @@ class DataAttribute extends ParanoidModel<
@Column(DataType.ENUM(...Object.values(DataAttributeDataType)))
dataType: DataAttributeDataType;
/** Options for `list` data type. */
/** Additional options for some datatypes. */
@AllowNull
@Column(DataType.JSONB)
options: DataAttributeOptions;
@@ -68,11 +73,15 @@ class DataAttribute extends ParanoidModel<
// hooks
@BeforeCreate
static async checkMaximumAttributes(model: DataAttribute) {
static async checkMaximumAttributes(
model: DataAttribute,
{ transaction }: SaveOptions<DataAttribute>
) {
const count = await this.count({
where: {
teamId: model.teamId,
},
transaction,
});
if (count >= DataAttributeValidation.max) {
@@ -80,6 +89,26 @@ class DataAttribute extends ParanoidModel<
}
}
/**
* The Zod type for validating this data attribute.
*/
get zodType() {
switch (this.dataType) {
case DataAttributeDataType.String:
return z.string();
case DataAttributeDataType.Number:
return z.number();
case DataAttributeDataType.Boolean:
return z.boolean();
case DataAttributeDataType.List:
return z
.string()
.refine((value) => this.options.values.includes(value));
default:
throw new Error(`Unknown data type: ${this.dataType}`);
}
}
// associations
@BelongsTo(() => User, "createdById")
+52 -8
View File
@@ -41,6 +41,7 @@ import {
BelongsToMany,
} from "sequelize-typescript";
import isUUID from "validator/lib/isUUID";
import { ZodError } from "zod";
import type {
NavigationNode,
ProsemirrorData,
@@ -54,7 +55,7 @@ import { ValidationError } from "@server/errors";
import { generateUrlId } from "@server/utils/url";
import Backlink from "./Backlink";
import Collection from "./Collection";
import DocumentDataAttribute from "./DocumentDataAttribute";
import DataAttribute from "./DataAttribute";
import FileOperation from "./FileOperation";
import Revision from "./Revision";
import Star from "./Star";
@@ -88,10 +89,6 @@ type AdditionalFindOptions = {
as: "updatedBy",
paranoid: false,
},
{
model: DocumentDataAttribute,
as: "dataAttributes",
},
],
where: {
publishedAt: {
@@ -284,6 +281,10 @@ class Document extends ParanoidModel<
@Column
color: string | null;
// TODO
@Column(DataType.JSONB)
dataAttributes: Record<string, any>[];
/**
* The content of the document as Markdown.
*
@@ -370,6 +371,52 @@ class Document extends ParanoidModel<
// hooks
@BeforeSave
static async validateDataAttributes(
model: Document,
{ transaction }: SaveOptions<Document>
) {
if (model.changed("dataAttributes")) {
const dataAttributeIds = model.dataAttributes.map(
(d) => d.dataAttributeId
);
const definitions = await DataAttribute.findAll({
where: {
id: dataAttributeIds,
},
transaction,
});
for (const attr of model.dataAttributes) {
const definition = definitions.find(
(d) => d.id === attr.dataAttributeId
);
if (!definition) {
throw ValidationError(
`Data attribute ${attr.dataAttributeId} not found`
);
}
try {
definition.zodType.parse(attr.value);
} catch (err) {
if (err instanceof ZodError) {
const { path, message } = err.issues[0];
const errMessage =
path.length > 0
? `${path[path.length - 1]}: ${message}`
: message;
throw ValidationError(
`Data attribute ${attr.dataAttributeId} has invalid value: ${errMessage}`
);
}
throw err;
}
}
}
}
@BeforeSave
static async updateCollectionStructure(
model: Document,
@@ -559,9 +606,6 @@ class Document extends ParanoidModel<
@Column(DataType.UUID)
collectionId?: string | null;
@HasMany(() => DocumentDataAttribute)
dataAttributes: DocumentDataAttribute[];
@HasMany(() => UserMembership)
memberships: UserMembership[];
-51
View File
@@ -1,51 +0,0 @@
import { InferAttributes, InferCreationAttributes } from "sequelize";
import {
BelongsTo,
ForeignKey,
Column,
Table,
DataType,
} from "sequelize-typescript";
import DataAttribute from "./DataAttribute";
import Document from "./Document";
import User from "./User";
import IdModel from "./base/IdModel";
import Fix from "./decorators/Fix";
@Table({
tableName: "document_data_attributes",
modelName: "document_data_attribute",
})
@Fix
class DocumentDataAttribute extends IdModel<
InferAttributes<DocumentDataAttribute>,
Partial<InferCreationAttributes<DocumentDataAttribute>>
> {
@Column
value: string;
// associations
@BelongsTo(() => DataAttribute, "dataAttributeId")
dataAttribute: DataAttribute;
@ForeignKey(() => DataAttribute)
@Column(DataType.UUID)
dataAttributeId: string;
@BelongsTo(() => Document, "documentId")
document: Document;
@ForeignKey(() => Document)
@Column(DataType.UUID)
documentId: string;
@BelongsTo(() => User, "userId")
user: User;
@ForeignKey(() => User)
@Column(DataType.UUID)
userId: string;
}
export default DocumentDataAttribute;
+1 -3
View File
@@ -3,7 +3,6 @@ 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 "./documentDataAttribute";
import presentUser from "./user";
type Options = {
@@ -77,8 +76,7 @@ async function presentDocument(
if (!options.isPublic) {
const source = await document.$get("import");
data.dataAttributes =
document.dataAttributes?.map(presentDocumentDataAttribute) ?? [];
data.dataAttributes = document.dataAttributes;
data.isCollectionDeleted = await document.isCollectionDeleted();
data.collectionId = document.collectionId;
data.parentDocumentId = document.parentDocumentId;
@@ -1,14 +0,0 @@
import type DocumentDataAttribute from "@server/models/DocumentDataAttribute";
export default function presentDocumentDataAttribute(
dataAttribute: DocumentDataAttribute
) {
return {
id: dataAttribute.id,
value: dataAttribute.value,
dataAttributeId: dataAttribute.dataAttributeId,
documentId: dataAttribute.documentId,
createdAt: dataAttribute.createdAt,
updatedAt: dataAttribute.updatedAt,
};
}
+5
View File
@@ -249,6 +249,11 @@ export const DocumentsUpdateSchema = BaseSchema.extend({
/** Whether the editing session is complete */
done: z.boolean().optional(),
// TODO
dataAttributes: z
.array(z.object({ dataAttributeId: z.string(), value: z.string() }))
.optional(),
}),
}).refine((req) => !(req.body.append && !req.body.text), {
message: "text is required while appending",
+2 -2
View File
@@ -1,10 +1,10 @@
export enum DataAttributeDataType {
String = "string",
Integer = "integer",
Number = "number",
Boolean = "boolean",
List = "list",
}
export type DataAttributeOptions = {
options: string[];
values: string[];
};