diff --git a/app/components/Collection/CollectionForm.tsx b/app/components/Collection/CollectionForm.tsx index 8e308d0927..8a56476b1f 100644 --- a/app/components/Collection/CollectionForm.tsx +++ b/app/components/Collection/CollectionForm.tsx @@ -5,7 +5,7 @@ import { Trans, useTranslation } from "react-i18next"; import styled from "styled-components"; import Icon from "@shared/components/Icon"; import { randomElement } from "@shared/random"; -import { CollectionPermission } from "@shared/types"; +import { CollectionDisplay, CollectionPermission } from "@shared/types"; import { IconLibrary } from "@shared/utils/IconLibrary"; import { colorPalette } from "@shared/utils/collections"; import { CollectionValidation } from "@shared/validations"; @@ -27,6 +27,7 @@ export interface FormData { icon: string; color: string | null; sharing: boolean; + display: CollectionDisplay | undefined; permission: CollectionPermission | undefined; } @@ -62,6 +63,7 @@ export const CollectionForm = observer(function CollectionForm_({ defaultValues: { name: collection?.name ?? "", icon: collection?.icon, + display: collection?.display, sharing: collection?.sharing ?? true, permission: collection?.permission, color: iconColor, diff --git a/app/components/DocumentPostItem.tsx b/app/components/DocumentPostItem.tsx new file mode 100644 index 0000000000..d089d24d12 --- /dev/null +++ b/app/components/DocumentPostItem.tsx @@ -0,0 +1,219 @@ +import { + useFocusEffect, + useRovingTabIndex, +} from "@getoutline/react-roving-tabindex"; +import { observer } from "mobx-react"; +import * as React from "react"; +import { useTranslation } from "react-i18next"; +import { Link } from "react-router-dom"; +import styled from "styled-components"; +import breakpoint from "styled-components-breakpoint"; +import EventBoundary from "@shared/components/EventBoundary"; +import Icon from "@shared/components/Icon"; +import { richExtensions, withComments } from "@shared/editor/nodes"; +import { s, hover } from "@shared/styles"; +import Document from "~/models/Document"; +import Badge from "~/components/Badge"; +import Flex from "~/components/Flex"; +import Highlight from "~/components/Highlight"; +import NudeButton from "~/components/NudeButton"; +import StarButton from "~/components/Star"; +import Tooltip from "~/components/Tooltip"; +import useBoolean from "~/hooks/useBoolean"; +import useCurrentUser from "~/hooks/useCurrentUser"; +import { useLocationSidebarContext } from "~/hooks/useLocationSidebarContext"; +import DocumentMenu from "~/menus/DocumentMenu"; +import { documentPath } from "~/utils/routeHelpers"; +import { Avatar, AvatarSize } from "./Avatar"; +import Editor from "./Editor"; +import { determineSidebarContext } from "./Sidebar/components/SidebarContext"; +import Text from "./Text"; +import Time from "./Time"; + +const extensions = withComments(richExtensions); + +type Props = { + document: Document; + highlight?: string | undefined; + context?: string | undefined; + showParentDocuments?: boolean; + showCollection?: boolean; + showPublished?: boolean; + showPin?: boolean; + showDraft?: boolean; +}; + +function DocumentPostItem( + props: Props, + ref: React.RefObject +) { + const { t } = useTranslation(); + const user = useCurrentUser(); + const locationSidebarContext = useLocationSidebarContext(); + const [menuOpen, handleMenuOpen, handleMenuClose] = useBoolean(); + + let itemRef: React.Ref = + React.useRef(null); + if (ref) { + itemRef = ref; + } + + const { focused, ...rovingTabIndex } = useRovingTabIndex(itemRef, false); + useFocusEffect(focused, itemRef); + + const { + document, + showParentDocuments, + showCollection, + showPublished, + showPin, + showDraft = true, + highlight, + context, + ...rest + } = props; + const canStar = !document.isArchived && !document.isTemplate; + + const sidebarContext = determineSidebarContext({ + document, + user, + currentContext: locationSidebarContext, + }); + + return ( + + + + {document.icon && ( + <> + +   + + )} + + {document.isBadgedNew && document.createdBy?.id !== user.id && ( + <Badge yellow>{t("New")}</Badge> + )} + {document.isDraft && showDraft && ( + <Tooltip content={t("Only visible to you")} placement="top"> + <Badge>{t("Draft")}</Badge> + </Tooltip> + )} + {canStar && ( + <StarPositioner> + <StarButton document={document} /> + </StarPositioner> + )} + <Actions> + <DocumentMenu + document={document} + showPin={showPin} + onOpen={handleMenuOpen} + onClose={handleMenuClose} + modal={false} + /> + </Actions> + </Heading> + + <Flex justify="space-between" style={{ marginBottom: 8 }}> + <Flex gap={6} align="center"> + <Avatar model={document.createdBy} size={AvatarSize.Medium} /> + <Text type="secondary" size="small"> + {t("By {{ author }}", { author: document.createdBy?.name })}{" "} + <Time dateTime={document.updatedAt} addSuffix /> + </Text> + </Flex> + </Flex> + + <Editor defaultValue={document.data} extensions={extensions} readOnly /> + </Content> + </Post> + ); +} + +const Content = styled.div` + flex-grow: 1; + flex-shrink: 1; + min-width: 0; +`; + +const Actions = styled(EventBoundary)` + display: none; + align-items: center; + margin: 8px; + flex-shrink: 0; + flex-grow: 0; + color: ${s("textSecondary")}; + + ${NudeButton} { + &: ${hover}, &[aria-expanded= "true"] { + background: ${s("sidebarControlHoverBackground")}; + } + } + + ${breakpoint("tablet")` + display: flex; + `}; +`; + +const Post = styled.div<{ + $isStarred?: boolean; + $menuOpen?: boolean; +}>` + position: relative; + margin-top: 10px; + margin-bottom: 2em; + padding: 6px 0; +`; + +const Heading = styled(Link)<{ rtl?: boolean }>` + display: flex; + justify-content: ${(props) => (props.rtl ? "flex-end" : "flex-start")}; + align-items: center; + margin-top: 8px; + margin-bottom: -4px; + white-space: nowrap; + color: ${s("text")}; + font-size: 20px; + font-family: ${s("fontFamily")}; + font-weight: 500; + width: 100%; +`; + +const StarPositioner = styled(Flex)` + margin-left: 4px; + align-items: center; +`; + +const Title = styled(Highlight)` + max-width: 90%; + overflow: hidden; + text-overflow: ellipsis; + + &: ${hover} { + text-decoration: underline; + } +`; + +export default observer(React.forwardRef(DocumentPostItem)); diff --git a/app/components/PaginatedDocumentList.tsx b/app/components/PaginatedDocumentList.tsx index 3e40739820..5dcb4bd880 100644 --- a/app/components/PaginatedDocumentList.tsx +++ b/app/components/PaginatedDocumentList.tsx @@ -1,9 +1,11 @@ import * as React from "react"; import { useTranslation } from "react-i18next"; +import { CollectionDisplay } from "@shared/types"; import Document from "~/models/Document"; import DocumentListItem from "~/components/DocumentListItem"; import Error from "~/components/List/Error"; import PaginatedList from "~/components/PaginatedList"; +import DocumentPostItem from "./DocumentPostItem"; type Props = { documents: Document[]; @@ -11,6 +13,7 @@ type Props = { options?: Record<string, any>; heading?: React.ReactNode; empty?: React.ReactNode; + display?: CollectionDisplay; showParentDocuments?: boolean; showCollection?: boolean; showPublished?: boolean; @@ -24,6 +27,7 @@ const PaginatedDocumentList = React.memo<Props>(function PaginatedDocumentList({ documents, fetch, options, + display, showParentDocuments, showCollection, showPublished, @@ -42,18 +46,30 @@ const PaginatedDocumentList = React.memo<Props>(function PaginatedDocumentList({ fetch={fetch} options={options} renderError={(props) => <Error {...props} />} - renderItem={(item: Document, _index) => ( - <DocumentListItem - key={item.id} - document={item} - showPin={!!options?.collectionId} - showParentDocuments={showParentDocuments} - showCollection={showCollection} - showPublished={showPublished} - showTemplate={showTemplate} - showDraft={showDraft} - /> - )} + renderItem={(item: Document, _index) => + display === CollectionDisplay.List ? ( + <DocumentListItem + key={item.id} + document={item} + showPin={!!options?.collectionId} + showParentDocuments={showParentDocuments} + showCollection={showCollection} + showPublished={showPublished} + showTemplate={showTemplate} + showDraft={showDraft} + /> + ) : ( + <DocumentPostItem + key={item.id} + document={item} + showPin={!!options?.collectionId} + showParentDocuments={showParentDocuments} + showCollection={showCollection} + showPublished={showPublished} + showDraft={showDraft} + /> + ) + } {...rest} /> ); diff --git a/app/models/Collection.ts b/app/models/Collection.ts index 5dfec65c86..fe4de9438a 100644 --- a/app/models/Collection.ts +++ b/app/models/Collection.ts @@ -1,6 +1,7 @@ import invariant from "invariant"; import { action, computed, observable, runInAction } from "mobx"; import { + CollectionDisplay, CollectionPermission, FileOperationFormat, type NavigationNode, @@ -42,6 +43,11 @@ export default class Collection extends ParanoidModel { @observable color?: string | null; + /** The display mode of the collection index. */ + @Field + @observable + display: CollectionDisplay; + /** The default permission for workspace users. */ @Field @observable diff --git a/app/scenes/Collection/index.tsx b/app/scenes/Collection/index.tsx index 996d3df9d0..20884bc553 100644 --- a/app/scenes/Collection/index.tsx +++ b/app/scenes/Collection/index.tsx @@ -353,6 +353,7 @@ const CollectionScene = observer(function _CollectionScene() { exact > <PaginatedDocumentList + display={collection.display} documents={documents.rootInCollection(collection.id)} fetch={documents.fetchPage} options={{ diff --git a/server/migrations/20250204021142-add-collection-display.js b/server/migrations/20250204021142-add-collection-display.js new file mode 100644 index 0000000000..be7404157e --- /dev/null +++ b/server/migrations/20250204021142-add-collection-display.js @@ -0,0 +1,19 @@ +"use strict"; + +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.sequelize.transaction(async transaction => { + await queryInterface.addColumn("collections", "display", { + type: Sequelize.STRING, + defaultValue: "list", + }, { transaction }); + }); + }, + + async down(queryInterface) { + await queryInterface.sequelize.transaction(async transaction => { + await queryInterface.removeColumn("collections", "display", { transaction }); + }); + }, +}; diff --git a/server/models/Collection.ts b/server/models/Collection.ts index bd57ed52f2..087b24bd53 100644 --- a/server/models/Collection.ts +++ b/server/models/Collection.ts @@ -35,7 +35,11 @@ import { } from "sequelize-typescript"; import isUUID from "validator/lib/isUUID"; import type { CollectionSort, ProsemirrorData } from "@shared/types"; -import { CollectionPermission, NavigationNode } from "@shared/types"; +import { + CollectionDisplay, + CollectionPermission, + NavigationNode, +} from "@shared/types"; import { UrlHelper } from "@shared/utils/UrlHelper"; import { sortNavigationNodes } from "@shared/utils/collections"; import slugify from "@shared/utils/slugify"; @@ -210,6 +214,10 @@ class Collection extends ParanoidModel< @Column icon: string | null; + /** The display mode of the collection index. */ + @Column(DataType.STRING) + display: CollectionDisplay; + /** The color of the icon. */ @IsHexColor @Column diff --git a/server/presenters/collection.ts b/server/presenters/collection.ts index cc379dedb3..0e5e7e18c7 100644 --- a/server/presenters/collection.ts +++ b/server/presenters/collection.ts @@ -20,6 +20,7 @@ export default async function presentCollection( icon: collection.icon, index: collection.index, color: collection.color, + display: collection.display, permission: collection.permission, sharing: collection.sharing, createdAt: collection.createdAt, diff --git a/server/routes/api/collections/collections.ts b/server/routes/api/collections/collections.ts index 8994945216..df3a1088df 100644 --- a/server/routes/api/collections/collections.ts +++ b/server/routes/api/collections/collections.ts @@ -55,7 +55,7 @@ router.post( transaction(), async (ctx: APIContext<T.CollectionsCreateReq>) => { const { transaction } = ctx.state; - const { name, color, description, data, permission, sharing, icon, sort } = + const { name, color, description, data, display, permission, sharing, icon, sort } = ctx.input.body; let { index } = ctx.input.body; @@ -80,6 +80,7 @@ router.post( color, teamId: user.teamId, createdById: user.id, + display, permission, sharing, sort, @@ -570,6 +571,7 @@ router.post( name, description, data, + display, icon, permission, color, @@ -623,6 +625,10 @@ router.post( collection.description = DocumentHelper.toMarkdown(collection); } + if (display !== undefined) { + collection.display = display; + } + if (icon !== undefined) { collection.icon = icon; } diff --git a/server/routes/api/collections/schema.ts b/server/routes/api/collections/schema.ts index 0d807dc1ca..e7b48d206b 100644 --- a/server/routes/api/collections/schema.ts +++ b/server/routes/api/collections/schema.ts @@ -1,6 +1,7 @@ import isUndefined from "lodash/isUndefined"; import { z } from "zod"; import { + CollectionDisplay, CollectionPermission, CollectionStatusFilter, FileOperationFormat, @@ -24,6 +25,7 @@ export const CollectionsCreateSchema = BaseSchema.extend({ .nullish(), description: z.string().nullish(), data: ProsemirrorSchema({ allowEmpty: true }).nullish(), + display: z.nativeEnum(CollectionDisplay).nullish(), permission: z .nativeEnum(CollectionPermission) .nullish() @@ -159,6 +161,7 @@ export const CollectionsUpdateSchema = BaseSchema.extend({ data: ProsemirrorSchema({ allowEmpty: true }).nullish(), icon: zodIconType().nullish(), permission: z.nativeEnum(CollectionPermission).nullish(), + display: z.nativeEnum(CollectionDisplay).nullish(), color: z .string() .regex(ValidateColor.regex, { message: ValidateColor.message }) diff --git a/shared/types.ts b/shared/types.ts index 2133fdfaaa..66870bcc26 100644 --- a/shared/types.ts +++ b/shared/types.ts @@ -1,3 +1,8 @@ +export enum CollectionDisplay { + List = "list", + Post = "post", +} + export enum UserRole { Admin = "admin", Member = "member",