action button supports v2

This commit is contained in:
hmacr committed 2025-08-08 12:47:58 +05:30
1 parent df611184b4
commit b14fa70c38
4 files changed
+31 -31

No files matched your search

+11 -12
View File
@@ -23,7 +23,7 @@ import Analytics from "~/utils/Analytics";
import history from "~/utils/history";
import { Action as KbarAction } from "kbar";
function resolve<T>(value: any, context: ActionContext): T {
export function resolve<T>(value: any, context: ActionContext): T {
return typeof value === "function" ? value(context) : value;
}
@@ -375,14 +375,6 @@ export function actionV2ToKBar(
case "action":
case "internal_link":
case "external_link": {
const perform =
action.variant === "action"
? () => performActionV2(action, context)
: action.variant === "internal_link"
? () =>
history.push(resolve<LocationDescriptor>(action.to, context))
: () => window.open(action.url, action.target);
return [
{
id: action.id,
@@ -392,7 +384,7 @@ export function actionV2ToKBar(
shortcut: action.shortcut,
icon,
priority,
perform,
perform: () => performActionV2(action, context),
},
];
}
@@ -430,10 +422,17 @@ export function actionV2ToKBar(
}
export async function performActionV2(
action: ActionV2,
action: Exclude<ActionV2Variant, ActionV2WithChildren>,
context: ActionContext
) {
const result = action.perform(context);
const perform =
action.variant === "action"
? () => action.perform(context)
: action.variant === "internal_link"
? () => history.push(resolve<LocationDescriptor>(action.to, context))
: () => window.open(action.url, action.target);
const result = perform();
if (result instanceof Promise) {
return result.catch((err: Error) => {
+14 -6
View File
@@ -1,9 +1,14 @@
/* oxlint-disable react/prop-types */
import * as React from "react";
import Tooltip, { Props as TooltipProps } from "~/components/Tooltip";
import { performAction } from "~/actions";
import { performAction, performActionV2, resolve } from "~/actions";
import useIsMounted from "~/hooks/useIsMounted";
import { Action, ActionContext } from "~/types";
import {
Action,
ActionContext,
ActionV2Variant,
ActionV2WithChildren,
} from "~/types";
export type Props = React.HTMLAttributes<HTMLButtonElement> & {
/** Show the button in a disabled state */
@@ -11,7 +16,7 @@ export type Props = React.HTMLAttributes<HTMLButtonElement> & {
/** Hide the button entirely if action is not applicable */
hideOnActionDisabled?: boolean;
/** Action to use on button */
action?: Action;
action?: Action | Exclude<ActionV2Variant, ActionV2WithChildren>;
/** Context of action, must be provided with action */
context?: ActionContext;
/** If tooltip props are provided the button will be wrapped in a tooltip */
@@ -40,8 +45,8 @@ const ActionButton = React.forwardRef<HTMLButtonElement, Props>(
const actionContext = { ...context, isButton: true };
if (
action?.visible &&
!action.visible(actionContext) &&
action.visible &&
!resolve<boolean>(action.visible, actionContext) &&
hideOnActionDisabled
) {
return null;
@@ -63,7 +68,10 @@ const ActionButton = React.forwardRef<HTMLButtonElement, Props>(
? (ev) => {
ev.preventDefault();
ev.stopPropagation();
const response = performAction(action, actionContext);
const response =
"variant" in action
? performActionV2(action, actionContext)
: performAction(action, actionContext);
if (response?.finally) {
setExecuting(true);
void response.finally(
-6
View File
@@ -69,11 +69,6 @@ export const DropdownMenu = observer(function DropdownMenu({
[onOpen, onClose]
);
const handleCloseAutoFocus = React.useCallback((ev: Event) => {
ev.preventDefault();
ev.stopPropagation();
}, []);
const enablePointerEvents = React.useCallback(() => {
if (contentRef.current) {
contentRef.current.style.pointerEvents = "auto";
@@ -112,7 +107,6 @@ export const DropdownMenu = observer(function DropdownMenu({
aria-label={ariaLabel}
onAnimationStart={disablePointerEvents}
onAnimationEnd={enablePointerEvents}
onCloseAutoFocus={handleCloseAutoFocus}
>
{content}
</DropdownMenuContent>
+6 -7
View File
@@ -27,6 +27,9 @@
"Restore": "Restore",
"Collection restored": "Collection restored",
"Delete collection": "Delete collection",
"Export": "Export",
"Export collection": "Export collection",
"New document": "New document",
"New template": "New template",
"Delete comment": "Delete comment",
"Mark as resolved": "Mark as resolved",
@@ -42,7 +45,6 @@
"Debug logging disabled": "Debug logging disabled",
"Development": "Development",
"Open document": "Open document",
"New document": "New document",
"New draft": "New draft",
"New from template": "New from template",
"New nested document": "New nested document",
@@ -269,7 +271,6 @@
"A ZIP file containing the images, and documents in the Markdown format.": "A ZIP file containing the images, and documents in the Markdown format.",
"A ZIP file containing the images, and documents as HTML files.": "A ZIP file containing the images, and documents as HTML files.",
"Structured data that can be used to transfer data to another compatible {{ appName }} instance.": "Structured data that can be used to transfer data to another compatible {{ appName }} instance.",
"Export": "Export",
"Exporting the collection <em>{{collectionName}}</em> may take some time.": "Exporting the collection <em>{{collectionName}}</em> may take some time.",
"You will receive an email when it's complete.": "You will receive an email when it's complete.",
"Include attachments": "Include attachments",
@@ -552,13 +553,12 @@
"API key": "API key",
"Show path to document": "Show path to document",
"Path to document": "Path to document",
"Group member options": "Group member options",
"Export collection": "Export collection",
"Rename": "Rename",
"Sort in sidebar": "Sort in sidebar",
"A-Z sort": "A-Z sort",
"Z-A sort": "Z-A sort",
"Manual sort": "Manual sort",
"Rename": "Rename",
"Collection menu": "Collection menu",
"Comment options": "Comment options",
"Show document menu": "Show document menu",
"{{ documentName }} restored": "{{ documentName }} restored",
@@ -568,13 +568,13 @@
"Apply template": "Apply template",
"Enable embeds": "Enable embeds",
"File": "File",
"Group member options": "Group member options",
"Group members": "Group members",
"Edit group": "Edit group",
"Delete group": "Delete group",
"Group options": "Group options",
"Cancel": "Cancel",
"Import menu options": "Import menu options",
"Member options": "Member options",
"New document in <em>{{ collectionName }}</em>": "New document in <em>{{ collectionName }}</em>",
"New child document": "New child document",
"Save in workspace": "Save in workspace",
@@ -619,7 +619,6 @@
"Custom": "Custom",
"No expiration": "No expiration",
"The document archive is empty at the moment.": "The document archive is empty at the moment.",
"Collection menu": "Collection menu",
"Drop documents to import": "Drop documents to import",
"<em>{{ collectionName }}</em> doesn’t contain any\n documents yet.": "<em>{{ collectionName }}</em> doesn’t contain any\n documents yet.",
"{{ usersCount }} users and {{ groupsCount }} groups with access": "{{ usersCount }} user and {{ groupsCount }} groups with access",