Files
shoptime/com.twin.app.shoptime/src/hooks/useDebugKey.js
2024-02-13 19:16:40 +09:00

40 lines
1.2 KiB
JavaScript

import { useRef, useCallback, useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import * as Config from "../utils/Config";
import { on, off } from "@enact/core/dispatcher";
import { pushPanel } from "../actions/panelActions";
const useDebugKey = ({isLandingPage=false}) => {
const panels = useSelector((state) => state.panels.panels);
const debugKey = useRef([]);
const dispatch = useDispatch();
const handleKeydown = useCallback(
(ev) => {
if (isLandingPage && panels && panels.length > 0) {
return;
}
if (ev && ev.key >= 0 && ev.key <= 9) {
if (debugKey.current.length >= Config.DEBUG_KEY.length) {
debugKey.current.shift();
}
debugKey.current.push(String(ev.key));
if (debugKey.current.join("") === Config.DEBUG_KEY) {
debugKey.current = [];
dispatch(pushPanel({ name: Config.panel_names.DEBUG_PANEL, panelInfo: {} }));
}
}
},
[panels, dispatch, isLandingPage]
);
useEffect(() => {
on("keydown", handleKeydown);
return () => {
off("keydown", handleKeydown);
};
}, [handleKeydown]);
};
export default useDebugKey;