From 971c54261392390ce86a0280f01d85a7334d01c8 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 6 Dec 2025 18:02:58 -0500 Subject: [PATCH] fix: "Edit" button for collection overview (#10816) * refactor * working * collectionPath refactor * fix: Flush debounced save * Move to Actions component * Keyboard shortcuts * PR feedback --- app/components/CollectionBreadcrumb.tsx | 2 +- .../Sharing/Collection/SharePopover.tsx | 2 +- app/models/Notification.ts | 2 +- app/routes/authenticated.tsx | 32 ++- app/scenes/Collection/components/Actions.tsx | 65 ++++- app/scenes/Collection/components/Header.tsx | 68 ++++++ .../Collection/components/Navigation.tsx | 78 ++++++ app/scenes/Collection/components/Overview.tsx | 15 +- app/scenes/Collection/index.tsx | 223 ++++++------------ app/scenes/DocumentDelete.tsx | 5 +- app/scenes/Shared/Collection.tsx | 4 +- app/utils/routeHelpers.ts | 21 +- shared/i18n/locales/en_US/translation.json | 8 +- 13 files changed, 340 insertions(+), 185 deletions(-) create mode 100644 app/scenes/Collection/components/Header.tsx create mode 100644 app/scenes/Collection/components/Navigation.tsx diff --git a/app/components/CollectionBreadcrumb.tsx b/app/components/CollectionBreadcrumb.tsx index f865f1ea57..cb81119ecb 100644 --- a/app/components/CollectionBreadcrumb.tsx +++ b/app/components/CollectionBreadcrumb.tsx @@ -28,7 +28,7 @@ export const CollectionBreadcrumb: React.FC = ({ collection }) => { name: collection.name, section: ActiveCollectionSection, icon: , - to: collectionPath(collection.path), + to: collectionPath(collection), }), ], [collection, t] diff --git a/app/components/Sharing/Collection/SharePopover.tsx b/app/components/Sharing/Collection/SharePopover.tsx index 10bbb01748..48a0973339 100644 --- a/app/components/Sharing/Collection/SharePopover.tsx +++ b/app/components/Sharing/Collection/SharePopover.tsx @@ -331,7 +331,7 @@ function SharePopover({ collection, visible, onRequestClose }: Props) { ) : ( ); diff --git a/app/models/Notification.ts b/app/models/Notification.ts index dc9a73bec4..df7d913aa9 100644 --- a/app/models/Notification.ts +++ b/app/models/Notification.ts @@ -177,7 +177,7 @@ class Notification extends Model { const collection = this.collectionId ? this.store.rootStore.collections.get(this.collectionId) : undefined; - return collection ? collectionPath(collection.path) : ""; + return collection ? collectionPath(collection) : ""; } case NotificationEventType.AddUserToDocument: case NotificationEventType.GroupMentionedInDocument: diff --git a/app/routes/authenticated.tsx b/app/routes/authenticated.tsx index 5dd90ea812..274008ba53 100644 --- a/app/routes/authenticated.tsx +++ b/app/routes/authenticated.tsx @@ -17,7 +17,8 @@ import { homePath, searchPath, settingsPath, - matchDocumentSlug as slug, + matchDocumentSlug as documentSlug, + matchCollectionSlug as collectionSlug, trashPath, } from "~/utils/routeHelpers"; @@ -80,22 +81,39 @@ function AuthenticatedRoutes() { to={settingsPath("templates")} /> - + + - + - - + + import("./ShareButton")); type Props = { + /** The collection for which to render actions */ collection: Collection; + /** Whether the collection is in editing mode */ + isEditing: boolean; + /** Contextual information for the sidebar */ + sidebarContext: SidebarContextType; }; -function Actions({ collection }: Props) { +function Actions({ collection, isEditing, sidebarContext }: Props) { const { t } = useTranslation(); const can = usePolicy(collection); + const user = useCurrentUser(); + + const goToEdit = useCallback(() => { + history.push({ + pathname: collectionEditPath(collection), + state: { sidebarContext }, + }); + }, [collection, sidebarContext]); + + const goBack = useCallback(() => { + history.push({ + pathname: collectionPath(collection, CollectionTab.Overview), + state: { sidebarContext }, + }); + }, [collection, sidebarContext]); return ( <> + {(!isEditing || !user?.separateEditMode) && ( + + + + )} + {!isEditing && user?.separateEditMode && ( + + + + + + + )} + {isEditing && user?.separateEditMode && ( + + + + + )} {can.createDocument && ( <> @@ -33,6 +91,7 @@ function Actions({ collection }: Props) { to={collection ? newDocumentPath(collection.id) : ""} disabled={!collection} icon={} + neutral={isEditing} > {t("New doc")} diff --git a/app/scenes/Collection/components/Header.tsx b/app/scenes/Collection/components/Header.tsx new file mode 100644 index 0000000000..44a2deb601 --- /dev/null +++ b/app/scenes/Collection/components/Header.tsx @@ -0,0 +1,68 @@ +import { IconTitleWrapper } from "@shared/components/Icon"; +import breakpoint from "styled-components-breakpoint"; +import first from "lodash/first"; +import { Suspense, useCallback } from "react"; +import styled from "styled-components"; +import Heading from "~/components/Heading"; +import CollectionIcon from "~/components/Icons/CollectionIcon"; +import type Collection from "~/models/Collection"; +import { colorPalette } from "@shared/utils/collections"; +import usePolicy from "~/hooks/usePolicy"; +import { observer } from "mobx-react"; +import lazyWithRetry from "~/utils/lazyWithRetry"; + +const IconPicker = lazyWithRetry(() => import("~/components/IconPicker")); + +type Props = { + /** The collection for which to render a header */ + collection: Collection; +}; + +export const Header = observer(function Header_({ collection }: Props) { + const can = usePolicy(collection); + const handleIconChange = useCallback( + (icon: string | null, color: string | null) => + collection?.save({ icon, color }), + [collection] + ); + + const fallbackIcon = collection ? ( + + ) : null; + + return ( + + + {can.update ? ( + + + {fallbackIcon} + + + ) : ( + fallbackIcon + )} + + {collection.name} + + ); +}); + +const StyledHeading = styled(Heading)` + display: flex; + align-items: center; + position: relative; + margin-left: 40px; + + ${breakpoint("tablet")` + margin-left: 0; + `} +`; diff --git a/app/scenes/Collection/components/Navigation.tsx b/app/scenes/Collection/components/Navigation.tsx new file mode 100644 index 0000000000..52d480408a --- /dev/null +++ b/app/scenes/Collection/components/Navigation.tsx @@ -0,0 +1,78 @@ +import { observer } from "mobx-react"; +import { useTranslation } from "react-i18next"; +import Collection from "~/models/Collection"; +import Tab from "~/components/Tab"; +import Tabs from "~/components/Tabs"; +import { collectionPath } from "~/utils/routeHelpers"; +import { type SidebarContextType } from "~/components/Sidebar/components/SidebarContext"; + +export enum CollectionTab { + Overview = "overview", + Recent = "recent", + Popular = "popular", + Updated = "updated", + Published = "published", + Old = "old", + Alphabetical = "alphabetical", +} + +type Props = { + /** The collection for which to render navigation tabs */ + collection: Collection; + /** Callback when the tab is changed */ + onChangeTab: (tab: CollectionTab) => void; + /** Whether to show the overview tab */ + showOverview?: boolean; + /** Contextual information for the sidebar */ + sidebarContext: SidebarContextType; +}; + +/** + * Navigation component for collection tabs, providing navigation between + * different views of collection documents. + */ +const Navigation = observer(function Navigation({ + collection, + onChangeTab, + showOverview, + sidebarContext, +}: Props) { + const { t } = useTranslation(); + + const tabProps = (path: CollectionTab) => ({ + exact: true, + onClick: () => onChangeTab(path), + to: { + pathname: collectionPath(collection, path), + state: { sidebarContext }, + }, + }); + + return ( + + {showOverview && ( + + {t("Overview")} + + )} + {t("Documents")} + {!collection.isArchived && ( + <> + {t("Popular")} + + {t("Recently updated")} + + + {t("Recently published")} + + + {t("Least recently updated")} + + {t("A–Z")} + + )} + + ); +}); + +export default Navigation; diff --git a/app/scenes/Collection/components/Overview.tsx b/app/scenes/Collection/components/Overview.tsx index 2067470ecb..b4dcc5565f 100644 --- a/app/scenes/Collection/components/Overview.tsx +++ b/app/scenes/Collection/components/Overview.tsx @@ -1,6 +1,6 @@ import debounce from "lodash/debounce"; import { observer } from "mobx-react"; -import { useMemo, useRef, useCallback, Suspense } from "react"; +import { useMemo, useRef, useCallback, useEffect, Suspense } from "react"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; import styled from "styled-components"; @@ -24,10 +24,10 @@ const extensions = withUIExtensions(richExtensions); type Props = { collection: Collection; - shareId?: string; + readOnly?: boolean; }; -function Overview({ collection, shareId }: Props) { +function Overview({ collection, readOnly }: Props) { const { documents, collections } = useStores(); const { t } = useTranslation(); const user = useCurrentUser({ rejectOnEmpty: false }); @@ -48,6 +48,13 @@ function Overview({ collection, shareId }: Props) { [collection, t] ); + useEffect( + () => () => { + handleSave.flush(); + }, + [handleSave] + ); + const childRef = useRef(null); const childOffsetHeight = childRef.current?.offsetHeight || 0; const editorStyle = useMemo( @@ -91,7 +98,7 @@ function Overview({ collection, shareId }: Props) { maxLength={CollectionValidation.maxDescriptionLength} onCreateLink={onCreateLink} canUpdate={can.update} - readOnly={!can.update || !!shareId} + readOnly={!can.update || readOnly} userId={user?.id} editorStyle={editorStyle} /> diff --git a/app/scenes/Collection/index.tsx b/app/scenes/Collection/index.tsx index 2a9fdc0521..53fc39add3 100644 --- a/app/scenes/Collection/index.tsx +++ b/app/scenes/Collection/index.tsx @@ -1,5 +1,5 @@ import { observer } from "mobx-react"; -import { useState, useCallback, useEffect, Suspense } from "react"; +import { useState, useEffect } from "react"; import { useTranslation } from "react-i18next"; import { useParams, @@ -11,13 +11,9 @@ import { Redirect, } from "react-router-dom"; import styled from "styled-components"; -import breakpoint from "styled-components-breakpoint"; -import { IconTitleWrapper } from "@shared/components/Icon"; import { s } from "@shared/styles"; import { StatusFilter } from "@shared/types"; -import { colorPalette } from "@shared/utils/collections"; import Collection from "~/models/Collection"; -import { Action } from "~/components/Actions"; import CenteredContent from "~/components/CenteredContent"; import { CollectionBreadcrumb } from "~/components/CollectionBreadcrumb"; import Heading from "~/components/Heading"; @@ -28,77 +24,62 @@ import PaginatedDocumentList from "~/components/PaginatedDocumentList"; import PinnedDocuments from "~/components/PinnedDocuments"; import PlaceholderText from "~/components/PlaceholderText"; import Scene from "~/components/Scene"; -import Tab from "~/components/Tab"; -import Tabs from "~/components/Tabs"; import { editCollection } from "~/actions/definitions/collections"; import useCommandBarActions from "~/hooks/useCommandBarActions"; import { useLastVisitedPath } from "~/hooks/useLastVisitedPath"; import { useLocationSidebarContext } from "~/hooks/useLocationSidebarContext"; -import usePersistedState from "~/hooks/usePersistedState"; import { usePinnedDocuments } from "~/hooks/usePinnedDocuments"; import usePolicy from "~/hooks/usePolicy"; import useStores from "~/hooks/useStores"; import { NotFoundError } from "~/utils/errors"; -import { collectionPath, updateCollectionPath } from "~/utils/routeHelpers"; +import { + collectionEditPath, + collectionPath, + matchCollectionEdit, + updateCollectionPath, +} from "~/utils/routeHelpers"; import Error404 from "../Errors/Error404"; import Actions from "./components/Actions"; import DropToImport from "./components/DropToImport"; import Empty from "./components/Empty"; import MembershipPreview from "./components/MembershipPreview"; +import Navigation, { CollectionTab } from "./components/Navigation"; import Notices from "./components/Notices"; import Overview from "./components/Overview"; -import first from "lodash/first"; -import lazyWithRetry from "~/utils/lazyWithRetry"; - -const IconPicker = lazyWithRetry(() => import("~/components/IconPicker")); - -const ShareButton = lazyWithRetry(() => import("./components/ShareButton")); - -enum CollectionPath { - Overview = "overview", - Recent = "recent", - Popular = "popular", - Updated = "updated", - Published = "published", - Old = "old", - Alphabetical = "alphabetical", -} +import { Header } from "./components/Header"; +import usePersistedState from "~/hooks/usePersistedState"; +import useCurrentUser from "~/hooks/useCurrentUser"; const CollectionScene = observer(function _CollectionScene() { - const params = useParams<{ id?: string }>(); + const params = useParams<{ collectionSlug?: string }>(); const history = useHistory(); const match = useRouteMatch(); const location = useLocation(); const { t } = useTranslation(); + const user = useCurrentUser(); const { documents, collections, shares, ui } = useStores(); const [error, setError] = useState(); const currentPath = location.pathname; const [, setLastVisitedPath] = useLastVisitedPath(); const sidebarContext = useLocationSidebarContext(); + const isEditRoute = match.path === matchCollectionEdit; - const id = params.id || ""; + const id = params.collectionSlug || ""; const urlId = id.split("-").pop() ?? ""; const collection: Collection | null | undefined = collections.get(id); const can = usePolicy(collection); const { pins, count } = usePinnedDocuments(urlId, collection?.id); - const [collectionTab, setCollectionTab] = usePersistedState( + + const [collectionTab, setCollectionTab] = usePersistedState( `collection-tab:${collection?.id}`, - collection?.hasDescription - ? CollectionPath.Overview - : CollectionPath.Recent, + collection?.hasDescription ? CollectionTab.Overview : CollectionTab.Recent, { listen: false, } ); - const handleIconChange = useCallback( - (icon: string | null, color: string | null) => - collection?.save({ icon, color }), - [collection] - ); - useEffect(() => { setLastVisitedPath(currentPath); }, [currentPath, setLastVisitedPath]); @@ -149,23 +130,13 @@ const CollectionScene = observer(function _CollectionScene() { if (!collection && error) { return ; } + if (!collection) { + return ; + } - const hasOverview = can.update || collection?.hasDescription; + const showOverview = can.update || collection?.hasDescription; - const fallbackIcon = collection ? ( - - ) : null; - - const tabProps = (path: CollectionPath) => ({ - exact: true, - onClick: () => setCollectionTab(path), - to: { - pathname: collectionPath(collection!.path, path), - state: { sidebarContext }, - }, - }); - - return collection ? ( + return ( - - {can.update && } - - + } > @@ -204,28 +176,7 @@ const CollectionScene = observer(function _CollectionScene() { > - - - {can.update ? ( - - - {fallbackIcon} - - - ) : ( - fallbackIcon - )} - - {collection.name} - +
- - - {hasOverview && ( - - {t("Overview")} - - )} - {t("Documents")} - {!collection.isArchived && ( - <> - - {t("Popular")} - - - {t("Recently updated")} - - - {t("Recently published")} - - - {t("Least recently updated")} - - - {t("A–Z")} - - - )} - + + - + - {hasOverview ? ( - + {showOverview ? ( + ) : ( - + ) : ( )} - + - ) : ( - - - - - - ); }); +const Loading = () => ( + + + + + + +); + const KeyedCollection = () => { const params = useParams<{ id?: string }>(); @@ -434,20 +358,9 @@ const KeyedCollection = () => { return ; }; -const Documents = styled.div` +const Content = styled.div` position: relative; background: ${s("background")}; `; -const CollectionHeading = styled(Heading)` - display: flex; - align-items: center; - position: relative; - margin-left: 40px; - - ${breakpoint("tablet")` - margin-left: 0; - `} -`; - export default KeyedCollection; diff --git a/app/scenes/DocumentDelete.tsx b/app/scenes/DocumentDelete.tsx index e0e899cb41..74ce2579d7 100644 --- a/app/scenes/DocumentDelete.tsx +++ b/app/scenes/DocumentDelete.tsx @@ -11,6 +11,7 @@ import useStores from "~/hooks/useStores"; import { collectionPath, documentPath, + homePath, settingsPath, } from "~/utils/routeHelpers"; @@ -67,7 +68,9 @@ function DocumentDelete({ document, onSubmit }: Props) { // Otherwise redirect to the collection (or) home. const path = document.template ? settingsPath("templates") - : collectionPath(collection?.path || "/"); + : collection + ? collectionPath(collection) + : homePath(); history.push(path); } diff --git a/app/scenes/Shared/Collection.tsx b/app/scenes/Shared/Collection.tsx index 7cf56fa54c..eefcc57bfa 100644 --- a/app/scenes/Shared/Collection.tsx +++ b/app/scenes/Shared/Collection.tsx @@ -40,7 +40,7 @@ function SharedCollection({ collection }: Props) { as={Link} icon={} to={{ - pathname: collectionPath(collection.path, "overview"), + pathname: collectionPath(collection, "overview"), }} neutral > @@ -83,7 +83,7 @@ function SharedCollection({ collection }: Props) { ) : null} - + ); diff --git a/app/utils/routeHelpers.ts b/app/utils/routeHelpers.ts index 964b060230..b364996415 100644 --- a/app/utils/routeHelpers.ts +++ b/app/utils/routeHelpers.ts @@ -1,7 +1,7 @@ import queryString from "query-string"; -import Collection from "~/models/Collection"; -import Comment from "~/models/Comment"; -import Document from "~/models/Document"; +import type Collection from "~/models/Collection"; +import type Comment from "~/models/Comment"; +import type Document from "~/models/Document"; import env from "~/env"; export function homePath(): string { @@ -37,11 +37,18 @@ export function commentPath(document: Document, comment: Comment): string { }`; } -export function collectionPath(url: string, section?: string): string { +export function collectionPath( + collection: Collection, + section?: string +): string { if (section) { - return `${url}/${section}`; + return `${collection.path}/${section}`; } - return url; + return collection.path; +} + +export function collectionEditPath(collection: Collection): string { + return collectionPath(collection, "overview/edit"); } export function updateCollectionPath( @@ -144,6 +151,8 @@ export function urlify(path: string): string { export const matchCollectionSlug = ":collectionSlug([0-9a-zA-Z-_~]*-[a-zA-z0-9]{10,15})"; +export const matchCollectionEdit = `/collection/${matchCollectionSlug}/overview/edit`; + export const matchDocumentSlug = ":documentSlug([0-9a-zA-Z-_~]*-[a-zA-z0-9]{10,15})"; diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 4274d12da4..883ef17d15 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -678,6 +678,7 @@ "90 days": "90 days", "No expiration": "No expiration", "The document archive is empty at the moment.": "The document archive is empty at the moment.", + "Done editing": "Done editing", "Drop documents to import": "Drop documents to import", "{{ collectionName }} doesn’t contain any\n documents yet.": "{{ collectionName }} doesn’t contain any\n documents yet.", "{{ usersCount }} users and {{ groupsCount }} groups with access": "{{ usersCount }} user and {{ groupsCount }} groups with access", @@ -688,15 +689,15 @@ "{{ usersCount }} users with access_plural": "{{ usersCount }} users with access", "{{ groupsCount }} groups with access": "{{ groupsCount }} group with access", "{{ groupsCount }} groups with access_plural": "{{ groupsCount }} groups with access", - "Archived by {{userName}}": "Archived by {{userName}}", - "Sorry, an error occurred saving the collection": "Sorry, an error occurred saving the collection", - "Add a description": "Add a description", "Overview": "Overview", "Popular": "Popular", "Recently updated": "Recently updated", "Recently published": "Recently published", "Least recently updated": "Least recently updated", "A–Z": "A–Z", + "Archived by {{userName}}": "Archived by {{userName}}", + "Sorry, an error occurred saving the collection": "Sorry, an error occurred saving the collection", + "Add a description": "Add a description", "Signing in": "Signing in", "You can safely close this window once the Outline desktop app has opened": "You can safely close this window once the Outline desktop app has opened", "Error creating comment": "Error creating comment", @@ -745,7 +746,6 @@ "Edit {{noun}}": "Edit {{noun}}", "Archived": "Archived", "Save draft": "Save draft", - "Done editing": "Done editing", "Restore version": "Restore version", "No history yet": "No history yet", "Source": "Source",