Skip to content

Instantly share code, notes, and snippets.

Avatar

Nick Farina nfarina

View GitHub Profile
@nfarina
nfarina / useResettableState.ts
Created Aug 6, 2021
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.
View useResettableState.ts
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,
View backup.sh
#!/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` \
View fast-styled-components.d.ts
/**
* 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 Feb 28, 2022
UserScript adding useful features to Duolingo, including more keyboard shortcuts, and hiding the "hint text" for translation exercises to focus on listening skills.
View duolingohelper.js
// ==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==
View useLocalStorage.ts
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
@nfarina
nfarina / AutoThemeSwitcher.js
Created May 14, 2019
Auto Dark Theme for Storybook
View AutoThemeSwitcher.js
import addons from '@storybook/addons';
import { FORCE_RE_RENDER } from '@storybook/core-events';
import { themes } from '@storybook/theming';
// Automatically switch light/dark theme based on system pref.
addons.register("auto-theme-switcher", api => {
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)');
let lastTheme;
@nfarina
nfarina / mock-storage.js
Last active Mar 25, 2022
Mock Google Cloud Storage for JS
View mock-storage.js
//
// Quick & Dirty Google Cloud Storage emulator for tests. Requires
// `stream-buffers` from npm. Use it like this:
//
// `new MockStorage().bucket('my-bucket').file('my_file').createWriteStream()`
//
class MockStorage {
buckets: {[name: string]: MockBucket};
@nfarina
nfarina / Client.java
Created Oct 16, 2013
Code to reproduce an issue where Vert.x's `HttpClient.connectWebSocket()` hangs indefinitely and never calls the exception handler, if the connection is terminated during WebSocket connection negotiation.
View Client.java
HttpClient httpClient = vertx.createHttpClient();
httpClient.setHost("localhost");
httpClient.setPort(8888);
httpClient.setConnectTimeout(1000); // 1 second
httpClient.exceptionHandler(new Handler<Throwable>() {
@Override
public void handle(Throwable event) {
System.out.println("Failed!"); // never called
}
});
@nfarina
nfarina / UIView+FrameAdditions.h
Created Aug 21, 2012
UIView Frame helper getter/setter category methods
View UIView+FrameAdditions.h
#import <UIKit/UIKit.h>
@interface UIView (SMFrameAdditions)
@property (nonatomic, assign) CGPoint $origin;
@property (nonatomic, assign) CGSize $size;
@property (nonatomic, assign) CGFloat $x, $y, $width, $height; // normal rect properties
@property (nonatomic, assign) CGFloat $left, $top, $right, $bottom; // these will stretch the rect
@end
@nfarina
nfarina / NSDate+Drawing.h
Created Nov 17, 2011
Useful functions for drawing NSDate in the exact style of Apple's iOS Mail app.
View NSDate+Drawing.h
/*
The MIT License
Copyright (c) 2010 Nick Farina
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is