Skip to content

Instantly share code, notes, and snippets.

View VladSez's full-sized avatar

Vlad Sazonau VladSez

View GitHub Profile
import { Dispatch, SetStateAction, useCallback, useState } from "react";
/**
* Returns a stateful value, its previous value, and a function to update it.
*/
export function useStateWithPrev<S>(
initialState: S | (() => S),
initialPrevState: S | (() => S)
): [prevState: S, state: S, setState: Dispatch<SetStateAction<S>>];
// convenience overload when second argument is omitted
/**
@typeofweb
typeofweb / SuperLink.tsx
Last active June 21, 2024 15:06
Link with on-hover prefetching in Next.js
@huozhi
huozhi / checkout-react-compiler-with-nextjs.sh
Created May 16, 2024 23:47
checkout-react-compiler-with-nextjs.sh
pnpm add next@canary react@beta react-dom@beta
pnpm add -D babel-plugin-react-compiler
echo "module.exports = {
experimental: {
reactCompiler: true
},
}" > next.config.js
import path from 'path';
import { fileURLToPath } from 'url';
import comments from '@eslint-community/eslint-plugin-eslint-comments/configs';
import { FlatCompat } from '@eslint/eslintrc';
import js from '@eslint/js';
import nextPlugin from '@next/eslint-plugin-next';
import eslintConfigPrettier from 'eslint-config-prettier';
import hooksPlugin from 'eslint-plugin-react-hooks';
import reactRecommended from 'eslint-plugin-react/configs/recommended.js';
import Benchmark from "benchmark";
const datetimeValidationSuite = new Benchmark.Suite("datetime");
const DATA = "2020-01-01";
const MONTHS_31 = new Set([1, 3, 5, 7, 8, 10, 12]);
const MONTHS_30 = new Set([4, 6, 9, 11]);
const simpleDatetimeRegex = /^(\d{4})-(\d{2})-(\d{2})$/;
const datetimeRegexNoLeapYearValidation =
@lithdew
lithdew / Dockerfile
Created April 4, 2024 20:31
Dockerfile for deploying a Next.js standalone bundle on Fly.io with Bun.
# syntax = docker/dockerfile:1
# Adjust BUN_VERSION as desired
ARG BUN_VERSION=1.1.1
FROM oven/bun:${BUN_VERSION}-slim as base
LABEL fly_launch_runtime="Next.js"
# Next.js app lives here
WORKDIR /app
@fabiospampinato
fabiospampinato / repro.js
Created April 3, 2024 15:16
Setting Array.prototype.length doesn't explicitly tell you which properties got deleted
const proxy = new Proxy ( [0, 1, 2], {
get: ( ...args ) => {
console.log ( '[get]', ...args );
return Reflect.get ( ...args );
},
apply: ( ...args ) => {
console.log ( '[apply]', ...args );
return Reflect.apply ( ...args );
},
@Temzasse
Temzasse / useParsedSearchParams.tsx
Last active April 6, 2024 10:12
A utility hook to parse and type URL search params based on a configuration object. This hook is useful when you want to access URL search params in a typesafe way and with proper casting.
import { useMemo } from "react";
import { useSearchParams } from "react-router-dom";
type ParseConfig = Record<
string,
| { type: "string"; defaultValue?: string }
| { type: "number"; defaultValue?: number }
| { parse: (value: URLSearchParams) => unknown }
>;
@thesamesam
thesamesam / xz-backdoor.md
Last active June 27, 2024 15:18
xz-utils backdoor situation (CVE-2024-3094)

FAQ on the xz-utils backdoor (CVE-2024-3094)

This is a living document. Everything in this document is made in good faith of being accurate, but like I just said; we don't yet know everything about what's going on.

Background

On March 29th, 2024, a backdoor was discovered in xz-utils, a suite of software that

@OrionReed
OrionReed / dom3d.js
Last active June 29, 2024 11:18
3D DOM viewer, copy-paste this into your console to visualise the DOM topographically.
// 3D Dom viewer, copy-paste this into your console to visualise the DOM as a stack of solid blocks.
// You can also minify and save it as a bookmarklet (https://www.freecodecamp.org/news/what-are-bookmarklets/)
(() => {
const SHOW_SIDES = false; // color sides of DOM nodes?
const COLOR_SURFACE = true; // color tops of DOM nodes?
const COLOR_RANDOM = false; // randomise color?
const COLOR_HUE = 190; // hue in HSL (https://hslpicker.com)
const MAX_ROTATION = 180; // set to 360 to rotate all the way round
const THICKNESS = 20; // thickness of layers
const DISTANCE = 10000; // ¯\\_(ツ)_/¯