Skip to content

Instantly share code, notes, and snippets.

View nfarina's full-sized avatar

Nick Farina nfarina

View GitHub Profile
@nfarina
nfarina / eslint-plugin-react-compiler.js
Created October 28, 2024 20:23
React Compiler ESLint Plugin with all errors surfaced
import * as BabelParser from "@babel/parser";
import BabelTraverse from "@babel/traverse";
import {
compileProgram,
parsePluginOptions,
} from "babel-plugin-react-compiler";
const traverse = BabelTraverse["default"];
const parse = BabelParser.parse;
@nfarina
nfarina / worker.js
Created August 8, 2024 17:44
Our solution for hosting and transforming images stored in Firebase Storage, using Cloudflare Image Transformations. It's fast (~11ms per request for us) and cheap ($0.50 per million images served through Cloudflare). Modify as needed.
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
// Our images are immutable, so we can cache them forever.
const ONE_YEAR = 31536000;
const CACHE_CONTROL = `public, max-age=${ONE_YEAR}, immutable`
// One way (the most efficient way) to get the raw images is to fetch them
// directly from the underlying Google Cloud Storage bucket. But that requires
@nfarina
nfarina / package.json
Created March 15, 2024 16:34
Reproduction of `tsc --watch` not working
{
"name": "tsc-test",
"version": "1.0.0",
"description": "tsc watch problem repro",
"license": "ISC",
"scripts": {
"start": "tsc test.ts --watch"
},
"dependencies": {
"typescript": "^5.4.2"
@nfarina
nfarina / stripeiterators.ts
Last active January 5, 2023 22:56
Helpful utility functions for iterating objects from the Stripe API using new async iterators
import Stripe from "stripe";
/**
* Utility functions for iterating objects from the Stripe API.
*/
/**
* Iterates over a Stripe API list, automatically handling pagination.
*
'Declare statements
DECLARE SUB playGame ()
DECLARE SUB setParms ()
DECLARE SUB clearTanks ()
DECLARE SUB checkGround ()
DECLARE SUB explodeTank ()
DECLARE SUB fireShotRight ()
DECLARE SUB fireShotLeft ()
DECLARE SUB getInfoRight ()
@nfarina
nfarina / useResettableState.ts
Created August 6, 2021 16:24
A version of React's `useState` that resets the value to initial whenever the given dependency array changes. Very helpful when you need to reset some internal state as the result of getting new props.
import { DependencyList, Dispatch, SetStateAction, useState } from "react";
/**
* This is like useState() but with the added feature of returning the initial
* value whenever the dependency list changes. This is super useful for allowing
* components to "reset" some internal state as a result of getting new props.
*/
export function useResettableState<S>(
initial: S | (() => S),
deps: DependencyList,
#!/bin/bash
#
# Backs up my entire website, in case Tumblr or CloudApp goes down someday.
# Last time I ran this, it took 18 minutes.
#
wget \
--mirror `# turns on recursion and timestamping, basically says we want to "mirror" the whole site` \
--convert-links `# after download, convert all links to point to localhost` \
/**
* A "fast" version of @types/styled-components that does not impact the
* performance of the TypeScript language service (which is directly related to
* the performance of VSCode).
*
* NOTE: This implements only a fraction of the features provided by the
* @types/styled-component package. Notably, it does not support typing the
* "props" parameter when interpolating within template strings. If someone
* knows how to type that without impacting performance, please let me know!
*/
@nfarina
nfarina / duolingohelper.js
Last active February 28, 2022 16:10
UserScript adding useful features to Duolingo, including more keyboard shortcuts, and hiding the "hint text" for translation exercises to focus on listening skills.
// ==UserScript==
// @name Duolingo Helper
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Adds useful features to Duolingo, including more keyboard shortcuts, and hidden "hint text" for translation exercises to concentrate on listening skills.
// @author Nick Farina
// @match https://*.duolingo.com/*
// @grant none
// ==/UserScript==
@nfarina
nfarina / useLocalStorage.ts
Last active December 24, 2022 15:04
Fix for Safari
import { useCallback, useEffect, useState } from "react";
// Adapted from:
// https://github.com/rehooks/local-storage/blob/master/src/use-localstorage.ts
type Setter<S> = (value: S) => any;
/**
* React hook to enable updates to state via localStorage.
* This updates when the {writeLocalStorage} function is used, when the returned function