diff --git a/app/actions/definitions/shares.tsx b/app/actions/definitions/shares.tsx index 338d213920..de89f4d00c 100644 --- a/app/actions/definitions/shares.tsx +++ b/app/actions/definitions/shares.tsx @@ -1,60 +1,92 @@ import copy from "copy-to-clipboard"; -import { errToString } from "@shared/utils/error"; -import type Share from "~/models/Share"; +import Share from "~/models/Share"; import { createAction, createInternalLinkAction } from ".."; +import { + everyActiveModel, + performBatchOnActiveModels, +} from "~/actions/definitions/common"; import { ArrowIcon, CopyIcon, TrashIcon } from "outline-icons"; import { ShareSection } from "../sections"; +import type { ActionContext } from "~/types"; import env from "~/env"; import { toast } from "sonner"; -export const copyShareUrlActionFactory = ({ share }: { share: Share }) => - createAction({ - name: ({ t }) => t("Copy link"), - analyticsName: "Copy share link", - section: ShareSection, - icon: , - perform: ({ t }) => { - copy(share.url, { - debug: env.ENVIRONMENT !== "production", - format: "text/plain", - }); - toast.success(t("Share link copied")); - }, - }); +export const copyShareUrl = createAction({ + name: ({ t }) => t("Copy link"), + analyticsName: "Copy share link", + section: ShareSection, + icon: , + visible: (context) => !!singleActiveShare(context), + perform: (context) => { + const share = singleActiveShare(context); + if (!share) { + return; + } -export const goToShareSourceActionFactory = ({ share }: { share: Share }) => - createInternalLinkAction({ - name: ({ t }) => - share.collectionId ? t("Go to collection") : t("Go to document"), - analyticsName: "Go to share source", - section: ShareSection, - icon: , - to: { - pathname: share.sourcePathWithFallback, - state: { sidebarContext: "collections" }, // optimistic preference of "collections" - }, - }); + copy(share.url, { + debug: env.ENVIRONMENT !== "production", + format: "text/plain", + }); + toast.success(context.t("Share link copied")); + }, +}); -export const revokeShareActionFactory = ({ - share, - can, -}: { - share: Share; - can: Record; -}) => - createAction({ - name: ({ t }) => t("Revoke link"), - analyticsName: "Revoke share", - section: ShareSection, - icon: , - dangerous: true, - visible: !!can.revoke, - perform: async ({ t, stores }) => { - try { - await stores.shares.revoke(share); - toast.message(t("Share link revoked")); - } catch (err) { - toast.error(errToString(err)); - } - }, - }); +export const goToShareSource = createInternalLinkAction({ + name: (context) => + singleActiveShare(context)?.collectionId + ? context.t("Go to collection") + : context.t("Go to document"), + analyticsName: "Go to share source", + section: ShareSection, + icon: , + visible: (context) => !!singleActiveShare(context), + to: (context) => { + const share = singleActiveShare(context); + return share + ? { + pathname: share.sourcePathWithFallback, + state: { sidebarContext: "collections" }, // optimistic preference of "collections" + } + : ""; + }, +}); + +export const revokeShare = createAction({ + name: ({ t }) => t("Revoke link"), + analyticsName: "Revoke share", + section: ShareSection, + icon: , + dangerous: true, + visible: (context) => + everyActiveModel( + context, + Share, + (share) => context.stores.policies.abilities(share.id).revoke + ), + perform: async (context) => { + const shares = context.getActiveModels(Share); + + const succeeded = await performBatchOnActiveModels( + context, + Share, + (share) => context.stores.shares.revoke(share), + (models, count, t) => + models.length === 1 + ? t("Share link revoked") + : t("{{ count }} share link revoked", { count }) + ); + + if (succeeded < shares.length) { + toast.error( + context.t("Could not revoke {{ count }} share link", { + count: shares.length - succeeded, + }) + ); + } + }, +}); + +const singleActiveShare = ({ getActiveModels }: ActionContext) => { + const shares = getActiveModels(Share); + return shares.length === 1 ? shares[0] : undefined; +}; diff --git a/app/hooks/useShareMenuActions.tsx b/app/hooks/useShareMenuActions.tsx index 7ec77d3974..531851c54a 100644 --- a/app/hooks/useShareMenuActions.tsx +++ b/app/hooks/useShareMenuActions.tsx @@ -1,35 +1,23 @@ -import * as React from "react"; -import type Share from "~/models/Share"; -import usePolicy from "~/hooks/usePolicy"; +import { useMemo } from "react"; import { ActionSeparator } from "~/actions"; import { - copyShareUrlActionFactory, - goToShareSourceActionFactory, - revokeShareActionFactory, + copyShareUrl, + goToShareSource, + revokeShare, } from "~/actions/definitions/shares"; import { useMenuAction } from "~/hooks/useMenuAction"; /** - * Hook that constructs the action menu for share management operations. + * Hook that constructs the action menu for share management operations. The + * share is read from the active models in the action context. * - * @param targetShare - the share to build actions for, or null to skip. - * @returns action with children for use in menus, or undefined if share is null. + * @returns action with children for use in menus. */ -export function useShareMenuActions(targetShare: Share | null) { - const can = usePolicy(targetShare ?? ({} as Share)); - - const actionList = React.useMemo( - () => - !targetShare - ? [] - : [ - copyShareUrlActionFactory({ share: targetShare }), - goToShareSourceActionFactory({ share: targetShare }), - ActionSeparator, - revokeShareActionFactory({ share: targetShare, can }), - ], - [targetShare, can] +export function useShareMenuActions() { + const actions = useMemo( + () => [copyShareUrl, goToShareSource, ActionSeparator, revokeShare], + [] ); - return useMenuAction(actionList); + return useMenuAction(actions); } diff --git a/app/menus/ShareMenu.tsx b/app/menus/ShareMenu.tsx index 72e470527d..80df978037 100644 --- a/app/menus/ShareMenu.tsx +++ b/app/menus/ShareMenu.tsx @@ -4,6 +4,7 @@ import { useTranslation } from "react-i18next"; import type Share from "~/models/Share"; import { DropdownMenu } from "~/components/Menu/DropdownMenu"; import { OverflowMenuButton } from "~/components/Menu/OverflowMenuButton"; +import { ActionContextProvider } from "~/hooks/useActionContext"; import { useShareMenuActions } from "~/hooks/useShareMenuActions"; type Props = { @@ -12,16 +13,18 @@ type Props = { function ShareMenu({ share }: Props) { const { t } = useTranslation(); - const rootAction = useShareMenuActions(share); + const rootAction = useShareMenuActions(); return ( - - - + + + + + ); } diff --git a/app/scenes/Settings/components/ShareSelectionToolbar.tsx b/app/scenes/Settings/components/ShareSelectionToolbar.tsx new file mode 100644 index 0000000000..922ba38e46 --- /dev/null +++ b/app/scenes/Settings/components/ShareSelectionToolbar.tsx @@ -0,0 +1,41 @@ +import { observer } from "mobx-react"; +import { revokeShare } from "~/actions/definitions/shares"; +import { ModelSelectionActionToolbar } from "~/components/ModelSelectionActionToolbar"; +import { useModelSelection } from "~/components/ModelSelectionContext"; +import useStores from "~/hooks/useStores"; +import type Share from "~/models/Share"; +import type { Action } from "~/types"; + +/** + * The share actions offered in the bulk selection toolbar. These are the same + * action definitions used by the share menu — they operate on the active + * models, which the toolbar feeds from the current selection. + */ +const toolbarActions: Action[] = [revokeShare]; + +/** + * Renders the selection toolbar with the bulk actions available for shares. + * + * @returns the toolbar element, or null when no list selection is in scope. + */ +function ShareSelectionToolbar() { + const selection = useModelSelection(); + const { shares } = useStores(); + + if (!selection) { + return null; + } + + const selectedShares = selection.selectedIds + .map((id) => shares.get(id)) + .filter((share): share is Share => !!share); + + return ( + + ); +} + +export default observer(ShareSelectionToolbar); diff --git a/app/scenes/Settings/components/SharesTable.tsx b/app/scenes/Settings/components/SharesTable.tsx index 863e0860c3..396a0ef16f 100644 --- a/app/scenes/Settings/components/SharesTable.tsx +++ b/app/scenes/Settings/components/SharesTable.tsx @@ -13,10 +13,13 @@ import { import { type Column as TableColumn } from "~/components/Table"; import { UserLabel } from "~/components/UserLabel"; import { ContextMenu } from "~/components/Menu/ContextMenu"; +import { ActionContextProvider } from "~/hooks/useActionContext"; import { useShareMenuActions } from "~/hooks/useShareMenuActions"; import Time from "~/components/Time"; import ShareMenu from "~/menus/ShareMenu"; import { useFormatNumber } from "~/hooks/useFormatNumber"; +import useStores from "~/hooks/useStores"; +import ShareSelectionToolbar from "./ShareSelectionToolbar"; const ROW_HEIGHT = 50; @@ -33,19 +36,27 @@ const ShareRowContextMenu = observer(function ShareRowContextMenu({ menuLabel: string; children: React.ReactNode; }) { - const action = useShareMenuActions(share); + const action = useShareMenuActions(); return ( - - {children} - + + + {children} + + ); }); export function SharesTable({ data, canManage, ...rest }: Props) { const { t } = useTranslation(); const formatNumber = useFormatNumber(); + const { policies } = useStores(); const hasDomain = data.some((share) => share.domain); + const isRowSelectable = useCallback( + (share: Share) => !!policies.abilities(share.id).revoke, + [policies] + ); + const applyContextMenu = useCallback( (share: Share, rowElement: React.ReactNode) => ( @@ -142,6 +153,8 @@ export function SharesTable({ data, canManage, ...rest }: Props) { rowHeight={ROW_HEIGHT} stickyOffset={HEADER_HEIGHT} decorateRow={canManage ? applyContextMenu : undefined} + isRowSelectable={canManage ? isRowSelectable : undefined} + selectionToolbar={} {...rest} /> ); diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 0df473e87f..d11f580055 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -213,6 +213,10 @@ "Go to document": "Go to document", "Revoke link": "Revoke link", "Share link revoked": "Share link revoked", + "{{ count }} share link revoked": "{{ count }} share link revoked", + "{{ count }} share link revoked_plural": "{{ count }} share links revoked", + "Could not revoke {{ count }} share link": "Could not revoke {{ count }} share link", + "Could not revoke {{ count }} share link_plural": "Could not revoke {{ count }} share links", "Switch workspace": "Switch workspace", "Select a workspace": "Select a workspace", "New workspace": "New workspace",