choo121600 commented on code in PR #68200: URL: https://github.com/apache/airflow/pull/68200#discussion_r3377561511
########## airflow-core/src/airflow/ui/src/hooks/useShortcut.ts: ########## @@ -0,0 +1,73 @@ +/*! + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { type DependencyList, useEffect, useId, useMemo } from "react"; +import { type HotkeyCallback, type Keys, type Options, useHotkeys } from "react-hotkeys-hook"; + +import { type ShortcutCategory, useShortcutRegistry } from "src/context/keyboardShortcuts"; + +type UseShortcutParams = { + readonly callback: HotkeyCallback; + readonly category: ShortcutCategory; + readonly dependencies?: DependencyList; + readonly description: string; + readonly keys: Keys; + readonly options?: Options; +}; + +/** + * Thin wrapper around `react-hotkeys-hook`'s `useHotkeys` that also publishes the + * shortcut to the keyboard-shortcut registry so it shows up in the help modal. + * + * Behaviour of the underlying hotkey is unchanged: `keys`, `callback`, `options` + * and `dependencies` are forwarded verbatim. The shortcut is registered only + * while it is enabled, matching when the hotkey is actually bound. + */ +export const useShortcut = ({ + callback, + category, + dependencies, + description, + keys, + options, +}: UseShortcutParams) => { + const id = useId(); + const { register, unregister } = useShortcutRegistry(); + + const ref = useHotkeys(keys, callback, options, dependencies); + + const keyList = useMemo<Array<string>>(() => (typeof keys === "string" ? [keys] : [...keys]), [keys]); Review Comment: I overlooked this during the review. Thanks for the reminder! You're right, useMemo isn't needed here, so I'll remove it 🙂 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
