Skip to content

Commit

Permalink
feat: add switch component
Browse files Browse the repository at this point in the history
Allow users to toggle shortcuts
  • Loading branch information
wholesome-ghoul committed Jun 27, 2022
1 parent 619ce77 commit 36ecdf3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"react-scripts": "5.0.0",
"sass": "^1.49.9",
"styled-components": "^5.3.3",
"tubeyou-components": "^0.0.14",
"tubeyou-components": "^0.6.1",
"typescript": "^4.4.2",
"web-vitals": "^2.1.0"
},
Expand Down
34 changes: 25 additions & 9 deletions src/home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import {
A,
Sidebar,
Modal,
Switch,
} from "tubeyou-components";
import { ThemeContext } from "tubeyou-components/dist/context";
import { themes } from "tubeyou-components/dist/constants";
import {
useDarkMode,
useResizeObserver,
useEventListener,
useLocalStorage,
} from "tubeyou-components/dist/hooks";
import { TyIcon, EmailIcon } from "tubeyou-components/dist/icons";
import { Grid, IconSize } from "tubeyou-components/dist/utils";
Expand Down Expand Up @@ -62,6 +64,10 @@ const Home = () => {
const [youtubeID, setYoutubeID] = useState<string | null>(null);
const [darkModeEnabled, setDarkModeEnabled] = useDarkMode(themes.dark);
const [shortcutsModal, setShortcutsModal] = useState(false);
const [shortcutsEnabled, setShortcutsEnabled] = useLocalStorage(
"tubeyou-shortcuts",
true
);
const [mainGrid, setMainGrid] = useState<Grid>({});
const [gridPosition, setGridPosition] =
useState<HomeGridPositions>(homeGridItems);
Expand All @@ -76,16 +82,20 @@ const Home = () => {
});

const holdStartListener = useCallback(() => {
holdTime = Date.now();
}, []);
if (shortcutsEnabled) {
holdTime = Date.now();
}
}, [shortcutsEnabled]);
const holdEndListener = useCallback(async () => {
holdTime = Date.now() - holdTime;
if (holdTime >= MIN_MOUSE_HOLD_TIME) {
const clipboard = await navigator.clipboard.readText();
setYtLink(clipboard);
if (shortcutsEnabled) {
holdTime = Date.now() - holdTime;
if (holdTime >= MIN_MOUSE_HOLD_TIME) {
const clipboard = await navigator.clipboard.readText();
setYtLink(clipboard);
}
holdTime = 0;
}
holdTime = 0;
}, []);
}, [shortcutsEnabled]);

useEventListener("touchstart", holdStartListener, document.body);
useEventListener("touchend", holdEndListener, document.body);
Expand All @@ -95,7 +105,7 @@ const Home = () => {

useEventListener(
"keydown",
(e) => e.keyCode === 27 && setShortcutsModal(false),
(e) => shortcutsEnabled && e.keyCode === 27 && setShortcutsModal(false),
document.body
);

Expand Down Expand Up @@ -140,6 +150,12 @@ const Home = () => {
{MIN_MOUSE_HOLD_TIME} milliseconds to paste the last shit you copied
</li>
<li>Press `ESC` or click the logo again to close this modal</li>
<Switch
checked={shortcutsEnabled}
onChange={() => setShortcutsEnabled((prev: boolean) => !prev)}
>
Shortcuts are {shortcutsEnabled ? "enabled" : "disabled"}
</Switch>
</Container>
</Modal>
<Container grid={mainGrid} className={styles.home}>
Expand Down

0 comments on commit 36ecdf3

Please sign in to comment.