Skip to content

Instantly share code, notes, and snippets.

@yuvalkarmi
yuvalkarmi / blockNextNavigation.js
Last active April 30, 2024 13:42
How to block a user from navigating away in NextJS when clicking on Link component
/*
In standard web apps, when users navigate away to a different page or website or attempt to refresh a page,
we can use the window event listener `beforeunload` to help users avoid losing the work.
However, when a user clicks on a native NextJS <Link> component, the navigation is not intercepted
because behind the scenes, no page changes are actually happening.
To intercept user navigation in a NextJS app, we can subscribe to the
router routeChangeError event and throw an error.
Note: we must throw an Error string - not a `new Error()` - which is why in the code below, we have
// eslint-disable-next-line no-throw-literal
import SingletonRouter, { Router } from 'next/router';
import { useEffect } from 'react';
const defaultConfirmationDialog = async (msg?: string) => window.confirm(msg);
/**
* Inspiration from: https://stackoverflow.com/a/70759912/2592233
*/
export const useLeavePageConfirmation = (
shouldPreventLeaving: boolean,
@coder054
coder054 / The Y combinator
Created April 5, 2023 03:23
The Y combinator
const Y = (fn) => ((g) => g(g))((g) => fn((x) => g(g)(x)))
const factorialGenerator = (f) => (n) => n === 0 ? 1 : n * f(n - 1)
const factorial = Y(factorialGenerator)
factorial(5) // 120
const sumFromZeroToNGenerator = (f) => (n) => n <= 1 ? n : n + f(n - 1)
const sumFromZeroToN = Y(sumFromZeroToNGenerator)
sumFromZeroToN(5) // 15
@leafac
leafac / init.lua
Created August 25, 2021 14:54
Hammerspoon Snippets
hs.alert("Hammerspoon configuration loaded")
hs.hotkey.bind({"⌥", "⌃"}, "return", function() hs.reload() end)
hs.hotkey.bind({"⌥", "⌃"}, ",",
function() hs.execute([[code ~/.hammerspoon]], true) end)
hs.hotkey.bind({"⌥", "⌃"}, "space", function() hs.toggleConsole() end)
hs.hotkey.bind({"⌥", "⌃"}, "escape", function()
hs.osascript.applescript("beep")
hs.sound.getByName("Submarine"):play()
end)
@bradtraversy
bradtraversy / typescript-crash.ts
Last active February 23, 2025 18:41
Basic intro to TypeScript (From YouTube Crash Course)
// Basic Types
let id: number = 5
let company: string = 'Traversy Media'
let isPublished: boolean = true
let x: any = 'Hello'
let ids: number[] = [1, 2, 3, 4, 5]
let arr: any[] = [1, true, 'Hello']
// Tuple
@markstos
markstos / Corne-3x6-v2,--markstos-layout-_All-Layers_.kbd.json
Last active March 31, 2025 11:28
Corne 3x6 v2, markstos layout (All Layers)
[
{
"name": "Corne 3x6 v2, markstos layout (All Layers)",
"author": "https://github.com/markstos",
"plate": false,
"keyboard-layout-editor": "http://www.keyboard-layout-editor.com/#/gists/adfa7c3cd8f0c6d3a7b9155c6eab1748#file-corne-crkbd_c0psrul3-default-json",
"qmk-config": "https://config.qmk.fm/#/crkbd/rev1/LAYOUT"
},
[
{

nvm Cheatsheet

Check if nvm is installed

command -v nvm

Get currently active version

nvm current -- or -- node -v

@Apsu
Apsu / vimrc
Created April 25, 2021 15:40
Colemak DHm vim remap
noremap f e
noremap p r
noremap b t
noremap j y
noremap l u
noremap u i
noremap y o
noremap ' p
noremap r s
noremap s d
@dungsaga
dungsaga / spleeter.md
Last active February 9, 2025 02:38
Voice removal AKA karaoke creator
@D7x7w9pHnT-cmd
D7x7w9pHnT-cmd / .envrc
Last active November 3, 2024 20:29
Purescript Environment and IDE in Nix
# Use with direnv. I use it in zsh and it seems to work pretty well for me.
# https://nixos.wiki/wiki/Development_environment_with_nix-shell
# https://direnv.net/
use_nix