From 82aba727a5b74701f765afa9bacffa880b879bee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Penido?= Date: Wed, 25 Sep 2024 16:34:35 -0300 Subject: [PATCH] fixup! feat: improve collection sidebar --- .../collections/CollectionDetails.tsx | 4 +- .../collections/CollectionInfo.tsx | 6 +- .../collections/CollectionInfoHeader.tsx | 2 + src/library-authoring/common/context.tsx | 28 ++++++-- .../components/CollectionCard.tsx | 48 ++++++++++--- src/library-authoring/components/messages.ts | 4 +- .../library-sidebar/LibrarySidebar.tsx | 67 ++++++++++++------- 7 files changed, 113 insertions(+), 46 deletions(-) diff --git a/src/library-authoring/collections/CollectionDetails.tsx b/src/library-authoring/collections/CollectionDetails.tsx index a413f2bf19..07076c334d 100644 --- a/src/library-authoring/collections/CollectionDetails.tsx +++ b/src/library-authoring/collections/CollectionDetails.tsx @@ -115,7 +115,9 @@ const CollectionDetails = ({ library, collection }: CollectionDetailsProps) => { }; return ( - +

{intl.formatMessage(messages.detailsTabDescriptionTitle)} diff --git a/src/library-authoring/collections/CollectionInfo.tsx b/src/library-authoring/collections/CollectionInfo.tsx index b082616540..82fc7f7221 100644 --- a/src/library-authoring/collections/CollectionInfo.tsx +++ b/src/library-authoring/collections/CollectionInfo.tsx @@ -27,7 +27,11 @@ const CollectionInfo = ({ library, collection }: CollectionInfoProps) => { Manage tab placeholder - + ); diff --git a/src/library-authoring/collections/CollectionInfoHeader.tsx b/src/library-authoring/collections/CollectionInfoHeader.tsx index ede05d2b64..01a78f99ae 100644 --- a/src/library-authoring/collections/CollectionInfoHeader.tsx +++ b/src/library-authoring/collections/CollectionInfoHeader.tsx @@ -39,6 +39,8 @@ const CollectionInfoHeader = ({ library, collection } : CollectionInfoHeaderProp }).finally(() => { setIsActive(false); }); + } else { + setIsActive(false); } }, [collection, showToast, intl], diff --git a/src/library-authoring/common/context.tsx b/src/library-authoring/common/context.tsx index cd82a2d84a..cc26471e48 100644 --- a/src/library-authoring/common/context.tsx +++ b/src/library-authoring/common/context.tsx @@ -1,5 +1,6 @@ import { useToggle } from '@openedx/paragon'; import React from 'react'; +import type { CollectionHit } from '../../search-manager'; export enum SidebarBodyComponentId { AddContent = 'add-content', @@ -18,7 +19,8 @@ export interface LibraryContextData { isCreateCollectionModalOpen: boolean; openCreateCollectionModal: () => void; closeCreateCollectionModal: () => void; - openCollectionInfoSidebar: () => void; + openCollectionInfoSidebar: (collectionHit: CollectionHit) => void + currentCollectionHit?: CollectionHit; } export const LibraryContext = React.createContext({ @@ -30,7 +32,8 @@ export const LibraryContext = React.createContext({ isCreateCollectionModalOpen: false, openCreateCollectionModal: () => {}, closeCreateCollectionModal: () => {}, - openCollectionInfoSidebar: () => {}, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + openCollectionInfoSidebar: (_collectionHit: CollectionHit) => {}, } as LibraryContextData); /** @@ -39,29 +42,38 @@ export const LibraryContext = React.createContext({ export const LibraryProvider = (props: { children?: React.ReactNode }) => { const [sidebarBodyComponent, setSidebarBodyComponent] = React.useState(null); const [currentComponentUsageKey, setCurrentComponentUsageKey] = React.useState(); + const [currentCollectionHit, setcurrentCollectionHit] = React.useState(); const [isCreateCollectionModalOpen, openCreateCollectionModal, closeCreateCollectionModal] = useToggle(false); - const closeLibrarySidebar = React.useCallback(() => { + const resetSidebar = React.useCallback(() => { + setCurrentComponentUsageKey(undefined); + setcurrentCollectionHit(undefined); setSidebarBodyComponent(null); + }, []); + + const closeLibrarySidebar = React.useCallback(() => { + resetSidebar(); setCurrentComponentUsageKey(undefined); }, []); const openAddContentSidebar = React.useCallback(() => { - setCurrentComponentUsageKey(undefined); + resetSidebar(); setSidebarBodyComponent(SidebarBodyComponentId.AddContent); }, []); const openInfoSidebar = React.useCallback(() => { - setCurrentComponentUsageKey(undefined); + resetSidebar(); setSidebarBodyComponent(SidebarBodyComponentId.Info); }, []); const openComponentInfoSidebar = React.useCallback( (usageKey: string) => { + resetSidebar(); setCurrentComponentUsageKey(usageKey); setSidebarBodyComponent(SidebarBodyComponentId.ComponentInfo); }, [], ); - const openCollectionInfoSidebar = React.useCallback(() => { - setCurrentComponentUsageKey(undefined); + const openCollectionInfoSidebar = React.useCallback((collectionHit: CollectionHit) => { + resetSidebar(); + setcurrentCollectionHit(collectionHit); setSidebarBodyComponent(SidebarBodyComponentId.CollectionInfo); }, []); @@ -76,6 +88,7 @@ export const LibraryProvider = (props: { children?: React.ReactNode }) => { openCreateCollectionModal, closeCreateCollectionModal, openCollectionInfoSidebar, + currentCollectionHit, }), [ sidebarBodyComponent, closeLibrarySidebar, @@ -87,6 +100,7 @@ export const LibraryProvider = (props: { children?: React.ReactNode }) => { openCreateCollectionModal, closeCreateCollectionModal, openCollectionInfoSidebar, + currentCollectionHit, ]); return ( diff --git a/src/library-authoring/components/CollectionCard.tsx b/src/library-authoring/components/CollectionCard.tsx index 2f56a0ecc4..6b0bb90d2b 100644 --- a/src/library-authoring/components/CollectionCard.tsx +++ b/src/library-authoring/components/CollectionCard.tsx @@ -1,15 +1,44 @@ -import { useIntl } from '@edx/frontend-platform/i18n'; +import { useIntl, FormattedMessage } from '@edx/frontend-platform/i18n'; import { ActionRow, + Dropdown, Icon, IconButton, } from '@openedx/paragon'; import { MoreVert } from '@openedx/paragon/icons'; -import { useNavigate } from 'react-router-dom'; +import { useContext } from 'react'; +import { Link } from 'react-router-dom'; import { type CollectionHit } from '../../search-manager'; -import messages from './messages'; +import { LibraryContext } from '../common/context'; import BaseComponentCard from './BaseComponentCard'; +import messages from './messages'; + +export const CollectionMenu = ({ collectionHit }: { collectionHit: CollectionHit }) => { + const intl = useIntl(); + + return ( + e.stopPropagation()}> + + + + + + + + ); +}; type CollectionCardProps = { collectionHit: CollectionHit, @@ -17,7 +46,9 @@ type CollectionCardProps = { const CollectionCard = ({ collectionHit }: CollectionCardProps) => { const intl = useIntl(); - const navigate = useNavigate(); + const { + openCollectionInfoSidebar, + } = useContext(LibraryContext); const { type, @@ -39,16 +70,11 @@ const CollectionCard = ({ collectionHit }: CollectionCardProps) => { tags={tags} actions={( - + )} blockTypeDisplayName={blockTypeDisplayName} - openInfoSidebar={() => navigate(`/library/${collectionHit.contextKey}/collection/${collectionHit.blockId}`)} + openInfoSidebar={() => openCollectionInfoSidebar(collectionHit)} /> ); }; diff --git a/src/library-authoring/components/messages.ts b/src/library-authoring/components/messages.ts index e801f7ec0b..a3bf410db4 100644 --- a/src/library-authoring/components/messages.ts +++ b/src/library-authoring/components/messages.ts @@ -22,9 +22,9 @@ const messages = defineMessages({ description: 'Collection type text with children count', }, menuEdit: { - id: 'course-authoring.library-authoring.component.menu.edit', + id: 'course-authoring.library-authoring.component-collection.menu.edit', defaultMessage: 'Edit', - description: 'Menu item for edit a component.', + description: 'Menu item for edit a component/collection.', }, menuCopyToClipboard: { id: 'course-authoring.library-authoring.component.menu.copy', diff --git a/src/library-authoring/library-sidebar/LibrarySidebar.tsx b/src/library-authoring/library-sidebar/LibrarySidebar.tsx index 640010960e..0d521e6b6f 100644 --- a/src/library-authoring/library-sidebar/LibrarySidebar.tsx +++ b/src/library-authoring/library-sidebar/LibrarySidebar.tsx @@ -6,18 +6,21 @@ import { } from '@openedx/paragon'; import { Close } from '@openedx/paragon/icons'; import { useIntl } from '@edx/frontend-platform/i18n'; -import messages from '../messages'; +import { SearchParams } from 'meilisearch'; + +import { + SearchContextProvider, +} from '../../search-manager'; import { AddContentContainer, AddContentHeader } from '../add-content'; +import { CollectionInfo, CollectionInfoHeader } from '../collections'; +import { ContentLibrary } from '../data/api'; import { LibraryContext, SidebarBodyComponentId } from '../common/context'; -import { LibraryInfo, LibraryInfoHeader } from '../library-info'; import { ComponentInfo, ComponentInfoHeader } from '../component-info'; -import { ContentLibrary } from '../data/api'; -import { CollectionInfo, CollectionInfoHeader } from '../collections'; -import { type CollectionHit } from '../../search-manager/data/api'; +import { LibraryInfo, LibraryInfoHeader } from '../library-info'; +import messages from '../messages'; type LibrarySidebarProps = { library: ContentLibrary, - collection?: CollectionHit, }; /** @@ -29,12 +32,13 @@ type LibrarySidebarProps = { * You can add more components in `bodyComponentMap`. * Use the returned actions to open and close this sidebar. */ -const LibrarySidebar = ({ library, collection }: LibrarySidebarProps) => { +const LibrarySidebar = ({ library }: LibrarySidebarProps) => { const intl = useIntl(); const { sidebarBodyComponent, closeLibrarySidebar, currentComponentUsageKey, + currentCollectionHit, } = useContext(LibraryContext); const bodyComponentMap = { @@ -43,7 +47,9 @@ const LibrarySidebar = ({ library, collection }: LibrarySidebarProps) => { [SidebarBodyComponentId.ComponentInfo]: ( currentComponentUsageKey && ), - [SidebarBodyComponentId.CollectionInfo]: collection && , + [SidebarBodyComponentId.CollectionInfo]: ( + currentCollectionHit && + ), unknown: null, }; @@ -54,7 +60,7 @@ const LibrarySidebar = ({ library, collection }: LibrarySidebarProps) => { currentComponentUsageKey && ), [SidebarBodyComponentId.CollectionInfo]: ( - collection && + currentCollectionHit && ), unknown: null, }; @@ -62,23 +68,36 @@ const LibrarySidebar = ({ library, collection }: LibrarySidebarProps) => { const buildBody = () : React.ReactNode => bodyComponentMap[sidebarBodyComponent || 'unknown']; const buildHeader = (): React.ReactNode => headerComponentMap[sidebarBodyComponent || 'unknown']; + const collectionQuery: SearchParams | undefined = currentCollectionHit ? { + filter: ['type = "collection"', `context_key = "${library.id}"`, `block_id = "${currentCollectionHit.blockId}"`], + limit: 1, + } : undefined; + return ( - - - {buildHeader()} - + + + + {buildHeader()} + + +
+ {buildBody()} +
-
- {buildBody()} -
-
+ ); };