mirror of
https://github.com/block/buzz.git
synced 2026-11-05 00:41:28 +00:00
fix(desktop): keep profile shortcut out of editors
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -12,6 +12,7 @@ import { AppTopChrome } from "@/app/AppTopChrome";
|
||||
import { useAppNavigation } from "@/app/navigation/useAppNavigation";
|
||||
import { useBackForwardControls } from "@/app/navigation/useBackForwardControls";
|
||||
import { useMarkAsReadShortcuts } from "@/app/useMarkAsReadShortcuts";
|
||||
import { useSettingsShortcuts } from "@/app/useSettingsShortcuts";
|
||||
import { useWebviewZoomShortcuts } from "@/app/useWebviewZoomShortcuts";
|
||||
import {
|
||||
channelsQueryKey,
|
||||
@@ -588,34 +589,12 @@ export function AppShell() {
|
||||
settingsOpen,
|
||||
]);
|
||||
|
||||
React.useLayoutEffect(() => {
|
||||
function handleKeyDown(event: KeyboardEvent) {
|
||||
const hasSettingsShortcutModifiers =
|
||||
hasPrimaryShortcutModifier(event) && !event.altKey && !event.shiftKey;
|
||||
const isSettingsShortcut =
|
||||
hasSettingsShortcutModifiers &&
|
||||
(event.key === "," || event.code === "Comma");
|
||||
const isProfileShortcut =
|
||||
hasSettingsShortcutModifiers && event.key.toLowerCase() === "u";
|
||||
|
||||
if (!isSettingsShortcut && !isProfileShortcut) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
if (settingsOpen && (!isProfileShortcut || settingsMode === "profile")) {
|
||||
handleCloseSettings();
|
||||
return;
|
||||
}
|
||||
|
||||
handleOpenSettings(isProfileShortcut ? "profile" : undefined);
|
||||
}
|
||||
|
||||
window.addEventListener("keydown", handleKeyDown);
|
||||
return () => {
|
||||
window.removeEventListener("keydown", handleKeyDown);
|
||||
};
|
||||
}, [handleCloseSettings, handleOpenSettings, settingsMode, settingsOpen]);
|
||||
useSettingsShortcuts({
|
||||
mode: settingsMode,
|
||||
onClose: handleCloseSettings,
|
||||
onOpenSettings: handleOpenSettings,
|
||||
open: settingsOpen,
|
||||
});
|
||||
|
||||
useMarkAsReadShortcuts({
|
||||
activeChannelId: activeChannel?.id ?? null,
|
||||
|
||||
66
desktop/src/app/useSettingsShortcuts.ts
Normal file
66
desktop/src/app/useSettingsShortcuts.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import * as React from "react";
|
||||
|
||||
import { hasPrimaryShortcutModifier } from "@/shared/lib/platform";
|
||||
|
||||
const EDITABLE_SHORTCUT_TARGET_SELECTOR =
|
||||
'input, textarea, select, [contenteditable="true"]';
|
||||
|
||||
type SettingsMode = "profile" | "preferences";
|
||||
|
||||
type UseSettingsShortcutsOptions = {
|
||||
mode: SettingsMode;
|
||||
onClose: () => void;
|
||||
onOpenSettings: (section?: "profile") => void;
|
||||
open: boolean;
|
||||
};
|
||||
|
||||
function isEditableShortcutTarget(event: KeyboardEvent) {
|
||||
const target = event.target;
|
||||
return (
|
||||
target instanceof Element &&
|
||||
target.closest(EDITABLE_SHORTCUT_TARGET_SELECTOR)
|
||||
);
|
||||
}
|
||||
|
||||
export function useSettingsShortcuts({
|
||||
mode,
|
||||
onClose,
|
||||
onOpenSettings,
|
||||
open,
|
||||
}: UseSettingsShortcutsOptions) {
|
||||
React.useLayoutEffect(() => {
|
||||
function handleKeyDown(event: KeyboardEvent) {
|
||||
const hasSettingsShortcutModifiers =
|
||||
hasPrimaryShortcutModifier(event) && !event.altKey && !event.shiftKey;
|
||||
const isSettingsShortcut =
|
||||
hasSettingsShortcutModifiers &&
|
||||
(event.key === "," || event.code === "Comma");
|
||||
const isProfileShortcut =
|
||||
hasSettingsShortcutModifiers &&
|
||||
event.key.toLowerCase() === "u" &&
|
||||
!isEditableShortcutTarget(event);
|
||||
|
||||
if (!isSettingsShortcut && !isProfileShortcut) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
if (open && (!isProfileShortcut || mode === "profile")) {
|
||||
onClose();
|
||||
return;
|
||||
}
|
||||
|
||||
if (isProfileShortcut) {
|
||||
onOpenSettings("profile");
|
||||
return;
|
||||
}
|
||||
|
||||
onOpenSettings();
|
||||
}
|
||||
|
||||
window.addEventListener("keydown", handleKeyDown);
|
||||
return () => {
|
||||
window.removeEventListener("keydown", handleKeyDown);
|
||||
};
|
||||
}, [mode, onClose, onOpenSettings, open]);
|
||||
}
|
||||
Reference in New Issue
Block a user