Files
deerflow-code/frontend-web/src/hooks/use-global-shortcuts.ts
T
吉鑫升andClaude Opus 4.7 89a1fa5042 私密/公开互斥兜底 + 智能体搜索按显示后名称匹配 + 多处小修
- 后端 update_agent / set_agent_private 增加兜底:is_private=true 时强制
  published=false,杜绝同时落在公开+私密两个广场的灰色态
- agent-card 编辑弹窗暴露 editIsPrivate 开关,与公开发布开关在前端互斥校验
- agent-gallery 智能体模糊搜索改为前端针对 desensitize() 后的名字比对,
  解决脱敏显示与原始名字不一致导致看似搜索失效的问题
- useUpdateAgent 在 onSuccess 立刻把新数据 patch 进所有 ["agents"] 列表
  缓存,卡片在后台 refetch 完成前先行 re-render,避免「切换私密/发布后
  好像没反应」的体感延迟
- useGlobalShortcuts 对 event.key 为 undefined(IME 合成 / 扩展注入)做
  容错,否则会抛 TypeError 把后续所有快捷键吃掉

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 19:30:00 +08:00

61 lines
1.9 KiB
TypeScript

"use client";
import { useEffect } from "react";
type ShortcutAction = () => void;
interface Shortcut {
key: string;
meta: boolean;
shift?: boolean;
action: ShortcutAction;
}
/**
* Register global keyboard shortcuts on window.
* Shortcuts are suppressed when focus is inside an input, textarea, or
* contentEditable element - except for Cmd+K which always fires.
*/
export function useGlobalShortcuts(shortcuts: Shortcut[]) {
useEffect(() => {
function handleKeyDown(event: KeyboardEvent) {
// ``event.key`` is ``undefined`` for synthetic / IME-composition /
// some extension-injected keydown events. Calling ``.toLowerCase()``
// on it throws and kills every shortcut after this one — bail out
// before that happens.
if (typeof event.key !== "string") return;
const meta = event.metaKey || event.ctrlKey;
const pressed = event.key.toLowerCase();
for (const shortcut of shortcuts) {
if (typeof shortcut.key !== "string") continue;
if (
pressed === shortcut.key.toLowerCase() &&
meta === shortcut.meta &&
(shortcut.shift ?? false) === event.shiftKey
) {
// Allow Cmd+K even in inputs (standard command palette behavior)
if (shortcut.key !== "k") {
const target = event.target as HTMLElement;
const tag = target.tagName;
if (
tag === "INPUT" ||
tag === "TEXTAREA" ||
target.isContentEditable
) {
continue;
}
}
event.preventDefault();
shortcut.action();
return;
}
}
}
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}, [shortcuts]);
}