mirror of
https://github.com/outline/outline.git
synced 2026-12-06 19:22:29 +03:00
feat: Multi-select in the shares settings table (#13360)
Revoking a share link now operates on every active share, so it can be performed from the bulk selection toolbar as well as the row menu. The copy link and go to source actions read their share from the action context too, and hide when a multi-selection is active. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
76b357f8f1
commit
2cc0d60ed8
6 files changed
+168
-87
No files matched your search
@@ -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: <CopyIcon />,
|
||||
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: <CopyIcon />,
|
||||
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: <ArrowIcon />,
|
||||
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<string, boolean>;
|
||||
}) =>
|
||||
createAction({
|
||||
name: ({ t }) => t("Revoke link"),
|
||||
analyticsName: "Revoke share",
|
||||
section: ShareSection,
|
||||
icon: <TrashIcon />,
|
||||
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: <ArrowIcon />,
|
||||
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: <TrashIcon />,
|
||||
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;
|
||||
};
|
||||
@@ -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);
|
||||
}
|
||||
+11
-8
@@ -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 (
|
||||
<DropdownMenu
|
||||
action={rootAction}
|
||||
align="end"
|
||||
ariaLabel={t("Share options")}
|
||||
>
|
||||
<OverflowMenuButton />
|
||||
</DropdownMenu>
|
||||
<ActionContextProvider value={{ activeModels: [share] }}>
|
||||
<DropdownMenu
|
||||
action={rootAction}
|
||||
align="end"
|
||||
ariaLabel={t("Share options")}
|
||||
>
|
||||
<OverflowMenuButton />
|
||||
</DropdownMenu>
|
||||
</ActionContextProvider>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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 (
|
||||
<ModelSelectionActionToolbar
|
||||
models={selectedShares}
|
||||
actions={toolbarActions}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default observer(ShareSelectionToolbar);
|
||||
@@ -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 (
|
||||
<ContextMenu action={action} ariaLabel={menuLabel}>
|
||||
{children}
|
||||
</ContextMenu>
|
||||
<ActionContextProvider value={{ activeModels: [share] }}>
|
||||
<ContextMenu action={action} ariaLabel={menuLabel}>
|
||||
{children}
|
||||
</ContextMenu>
|
||||
</ActionContextProvider>
|
||||
);
|
||||
});
|
||||
|
||||
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) => (
|
||||
<ShareRowContextMenu share={share} menuLabel={t("Share options")}>
|
||||
@@ -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={<ShareSelectionToolbar />}
|
||||
{...rest}
|
||||
/>
|
||||
);
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in new issue
Block a user