Skip to content

Instantly share code, notes, and snippets.

@yuvalkarmi
yuvalkarmi / blockNextNavigation.js
Last active October 31, 2023 08:43
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
@bradtraversy
bradtraversy / typescript-crash.ts
Last active April 8, 2024 15:07
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

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 March 17, 2024 20:00
Voice removal AKA karaoke creator
@semilin
semilin / isrt-linux
Last active April 6, 2024 19:06
NotGate's ISRT Layout for Linux XKB
// Standard ISRT for ANSI and ISO keyboards
default partial
xkb_symbols "basic" {
// the default variant has no AltGr layer and does
// not use CAPS as BackSpace
include "us(basic)"
@paulmwatson
paulmwatson / cypress_test_404_spec.js
Created January 14, 2021 08:33
Testing a 404 page with Cypress
cy.visit('/404')
//=> Test fails
cy.visit('/404', {failOnStatusCode: false})
//=> Test passes but does not test the HTTP code was 404
cy.request({url: '/404', failOnStatusCode: false}).its('status').should('equal', 404)
cy.visit('/404', {failOnStatusCode: false})
//=> Test passes, tests that the HTTP code was 404, and tests page was visited
type TemplateType = {
template: string;
id: string;
};
function withTemplate(templateObj: TemplateType) {
return function (constructor: any) {
let el = document.getElementById(templateObj.id);
let p = new constructor();
if (el) {