Skip to content

Instantly share code, notes, and snippets.

View kentcdodds's full-sized avatar
馃
working hard to make the world better with software

Kent C. Dodds kentcdodds

馃
working hard to make the world better with software
View GitHub Profile
@kentcdodds
kentcdodds / index.js
Last active October 16, 2024 18:35
check local access to mocking workshop
#!/usr/bin/env node
import { promises as fs } from 'fs'
import { homedir } from 'os'
import path from 'path'
const homeDir = homedir()
const dataFilePath = path.join(homeDir, '.epicshop', 'data.json')
let data
@kentcdodds
kentcdodds / index.tsx
Created September 26, 2024 20:33
Hoist Title and Meta tags in React 19
import { Suspense, use, useDeferredValue, useState, useTransition } from 'react'
import * as ReactDOM from 'react-dom/client'
import { ErrorBoundary } from 'react-error-boundary'
import { useSpinDelay } from 'spin-delay'
import { getImageUrlForShip, getShip, imgSrc, searchShips } from './utils.tsx'
const shipFallbackSrc = '/img/fallback-ship.png'
function Shell() {
return (
@kentcdodds
kentcdodds / index.tsx
Created September 26, 2024 20:32
Deduplicate Title tags with React 19
import { Suspense, use, useDeferredValue, useState, useTransition } from 'react'
import * as ReactDOM from 'react-dom/client'
import { ErrorBoundary } from 'react-error-boundary'
import { useSpinDelay } from 'spin-delay'
import { getImageUrlForShip, getShip, imgSrc, searchShips } from './utils.tsx'
const shipFallbackSrc = '/img/fallback-ship.png'
function Shell() {
return (
@kentcdodds
kentcdodds / index.tsx
Created September 26, 2024 20:31
Script for Efficiently Render Script Tags in Components React 19
import {
Suspense,
use,
useDeferredValue,
useState,
useTransition,
useEffect,
useRef,
} from 'react'
import * as ReactDOM from 'react-dom/client'
@kentcdodds
kentcdodds / lifetime-contributions.js
Last active September 5, 2024 05:09
Calculate your total lifetime github contributions
// 鈿狅笍 warning: This script was written by ChatGPT, not entirely reviewed, and is completely unmodified
// This counts your lifetime contributions so you can decide
// whether to block Twitter: https://x.com/mjackson/status/1831554887706169674
const GITHUB_API_URL = 'https://api.github.com/graphql'
// scope needed: "user:read" for public contributions and "repo" for private
// https://github.com/settings/tokens/new
const GITHUB_TOKEN = process.env.GITHUB_TOKEN
@kentcdodds
kentcdodds / screenshot-node.js
Created August 14, 2024 22:05
Create a screenshot of a DOM element (while preserving a transparent background).
const { default: html2canvas } = await import(
'https://unpkg.com/[email protected]/dist/html2canvas.esm.js'
)
async function captureElement(element, { scale = 1, backgroundColor = null } = {}) {
const canvas = await html2canvas(element, {
backgroundColor,
scale,
})
// Name: EpicShop Update
// Description: Update the EpicShop workshop app in all the epic web workshop repos
// Author: Kent C. Dodds
// Twitter: @kentcdodds
import '@johnlindquist/kit'
import {globby} from 'globby'
import {execa} from 'execa'
const workshopDirs = [
// Menu: Twimage Download
// Description: Download twitter images and set their exif info based on the tweet metadata
// Shortcut: command option control t
// Author: Kent C. Dodds
// Twitter: @kentcdodds
import '@johnlindquist/kit'
import fs from 'fs'
import {URL} from 'url'
import {getTweet} from '../lib/twitter'
import { useSyncExternalStore } from 'react'
/**
* This will call the given callback function whenever the contents of the map
* change.
*/
class ObservableMap extends Map {
constructor(entries) {
super(entries)
this.listeners = new Set()
@kentcdodds
kentcdodds / current-implementation.tsx
Created March 21, 2024 17:10
Simpler Control Props Implementation?
import { useReducer, useRef } from 'react'
import { Switch } from '#shared/switch.tsx'
function callAll<Args extends Array<unknown>>(
...fns: Array<((...args: Args) => unknown) | undefined>
) {
return (...args: Args) => fns.forEach(fn => fn?.(...args))
}
export type ToggleState = { on: boolean }