Skip to content

Instantly share code, notes, and snippets.

@mrcoles
mrcoles / console-group-decorator.d.ts
Last active March 22, 2021 01:49
A decorator for wrapping a function in a `console.group(msg)` -> `console.groupEnd()` call.
export function groupCollapsedFn<F extends Function>(
msg: any, func: F,
): F;
export function groupFn<F extends Function>(
msg: any, func: F, isCollapsed?: boolean
): F;
@mrcoles
mrcoles / aws-amplify-build-settings-cache-static-files.yml
Last active March 9, 2021 11:43
Sample file caching custom headers to add to an AWS Amplify build-settings.yml file
frontend:
customHeaders:
# cache static assets! (can I just use a glob for this, e.g., '**/*.{js,css,gif,ico,jpg,png,svg,mp4,webm}' ?)
# js
- pattern: '**/*.js'
headers: [ { key: 'Cache-Control', value: 'public,max-age=31536000,immutable' } ]
# css
- pattern: '**/*.css'
headers: [ { key: 'Cache-Control', value: 'public,max-age=31536000,immutable' } ]
# images
@mrcoles
mrcoles / AsyncStripeProvider.js
Last active December 8, 2020 14:28
A react provider abstraction for loading the stripe.js file asynchronously to use with Stripe Elements
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { StripeProvider } from 'react-stripe-elements';
export default class AsyncStripeProvider extends Component {
static propTypes = {
apiKey: PropTypes.string.isRequired
};
// constructor
@mrcoles
mrcoles / detect-autoplay.js
Created May 8, 2013 01:25
A script to detect browser support for the autoplay attribute on the HTML5 Audio element.
// Detect autoplay
// ---------------
// This script detects whether the current browser supports the
// autoplay feature for HTML5 Audio elements, and it sets the
// `AUTOPLAY` variable accordingly.
// Used in the Meteor app [PicDinner](http://picdinner.com)
@mrcoles
mrcoles / BlockUnload.tsx
Created August 24, 2020 18:29
A simple React component meant for form editing that will conditionally block page unloads and React Router navigation
import React from 'react';
import { useBeforeunload } from 'react-beforeunload';
import { Prompt } from 'react-router';
const DEFAULT_MESSAGE = 'You will lose unsaved changes if you leave this page.';
type BlockUnloadProps = {
when?: boolean;
message?: string;
};
@mrcoles
mrcoles / stripe-pagination-exploration.md
Last active June 5, 2020 17:06
A look into how Stripe pagination filters and orders results when using pagination

Stripe Pagination Exploration

stripe.Event.list()

  • results: all events
  • order: created desc (most recent to oldest)

stripe.Event.list(ending_before=evt_id)

  • result: events that are newer than evt_id (created at a later time than evt_id)
@mrcoles
mrcoles / state-hooks.ts
Last active May 26, 2020 15:19
Helpful React hooks (for now just one…)
import { useState, Dispatch, SetStateAction } from 'react';
export const useUpdateState = <S extends object>(
defaultState: S
): [S, (newState: Partial<S>) => void, Dispatch<SetStateAction<S>>] => {
const [state, setState] = useState(defaultState);
const updateState = (newState: Partial<S>) =>
setState((prevState) => ({ ...prevState, ...newState }));
@mrcoles
mrcoles / djstripe_sync_webhook.py
Created May 22, 2020 16:01
A Django command function for syncing Stripe webhook events with your local server that happened while it wasn’t running
import json
from traceback import format_exc
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from djstripe import settings as djstripe_settings
from djstripe.models import Event, WebhookEventTrigger
from djstripe.signals import webhook_processing_error
# from djstripe.utils import convert_tstamp # convert stripe timestamps to datetimes
@mrcoles
mrcoles / draw-on-sids-canvas.js
Created April 27, 2020 16:21
Code to automate drawing on the canvas element on Sid’s website (using simulated mouse events).
var target = document.querySelector('canvas');
var input = document.querySelector('input.jscolor');
var body = document.body;
var rect = target.getBoundingClientRect();
var irect = input.getBoundingClientRect();
var brect = document.body.getBoundingClientRect();
var simulateMouseEvent = function simulateMouseEvent(type, point, elt, eltRect) {
elt = elt || target;
@mrcoles
mrcoles / routeurl.ts
Last active March 5, 2020 17:32
Define your React Router URLs as objects that can be used both in your router and also to generate URL strings.
import { generatePath } from 'react-router';
// ## Route URL
export default class RouteUrl<P extends { [K in keyof P]?: string }> {
constructor(public routePath: string) {}
asPath(params: { [K in keyof P]: string | number | boolean }) {
return generatePath(this.routePath, params);
}