Skip to content

Instantly share code, notes, and snippets.

View stern-shawn's full-sized avatar
🏠
Working from home

Shawn Stern stern-shawn

🏠
Working from home
  • Portland, OR
  • 19:08 (UTC -07:00)
View GitHub Profile
@stern-shawn
stern-shawn / get-prop-data.js
Created October 20, 2021 20:10
twilio-kickstart-serverless-tools
exports.handler = (context, event, callback) => {
const propId = parseInt(event.propId.trim());
console.log('propId, ', propId);
if (!(propId > 0 && propId < 6)) return callback("That's not a valid option, try again!");
return callback(null, props[propId]);
};
const props = {
@stern-shawn
stern-shawn / isShiftable.ts
Created January 27, 2021 02:13
Enable typesafe arr.shift() inside of an if check for if(arr.length) { arr.shift() }
export const isShiftable = <T>(arr: T[]): arr is { shift(): T } & Array<T> => arr.length > 0;
@stern-shawn
stern-shawn / useDebounce.ts
Last active March 5, 2020 19:00
useDebounce
import { useState, useEffect } from 'react'
const useDebounce = <T>(value: T, delay: number): T => {
const [debouncedValue, setDebouncedValue] = useState<T>(value)
useEffect(() => {
const handler = setTimeout(() => setDebouncedValue(value), delay)
return () => clearTimeout(handler)
}, [value, delay])
@stern-shawn
stern-shawn / useDrop.js
Created March 26, 2019 03:06
React hook for on-drop file uploading
import { useEffect, useState } from 'react';
function useDrop(ref, onLoad = () => {}) {
const [uploading, setUploading] = useState(false);
const [isOver, setOver] = useState(false);
const stopDefault = e => {
e.preventDefault();
e.stopPropagation();
};
const dragOverHandler = e => {
@stern-shawn
stern-shawn / gainzBoxAutoSurvey.js
Created January 24, 2018 20:43
Automatically fill out and submit responses to Gainz Box surveys, ain't nobody got time for that
document.querySelectorAll('.torro_element_radio input[id$="0"]').forEach((el) => el.checked = true);
document.querySelectorAll('.torro_element_checkbox input[id$="0"]').forEach((el) => el.checked = true);
document.querySelector('textarea').value = 'durr';
document.querySelector('input[type="submit"]').click();
import React, { Fragment, Component } from 'react';
import Helmet from 'react-helmet';
import ReactMarkdown from 'react-markdown';
import styles from './styles.scss';
const importAll = (r) => r.keys().map(r);
const markdownFiles = importAll(require.context('./posts', false, /\.md$/))
.sort()
.reverse();
// Example from Webpack docs here: https://webpack.js.org/guides/dependency-management/#context-module-api
function importAll (r) {
r.keys().forEach(r);
}
importAll(require.context('../components/', true, /\.js$/));
// That's a little confusing... we're taking the require context (r), and then calling itself on each key? Oh right!
// Because r is actually this...
// ƒ webpackContext(req) {
console.log(markdownContext.keys());
/* Console
(4) ["./2017-01-1.md", "./2017-12-11.md", "./2018-01-08.md", "./2018-2-2.md"]
*/
const markdownContext = require.context('./posts', false, /\.md$/);
console.log(markdownContext);
/* console
ƒ webpackContext(req) {
return __webpack_require__(webpackContextResolve(req));
}
*/
@stern-shawn
stern-shawn / singlepost.jsx
Last active January 9, 2018 18:27
Importing Multiple Markdown files into a React Component with Webpack - single post React Component
import React, { Fragment, Component } from 'react';
import ReactMarkdown from 'react-markdown';
import source from './posts/2018-01-08.md';
class WhatsNew extends Component {
state = {
post: null,
}