Skip to content

Instantly share code, notes, and snippets.

View coreyward's full-sized avatar

Corey Ward coreyward

View GitHub Profile
@coreyward
coreyward / injectFnComments.js
Created January 20, 2023 21:38
Inject JSDoc comments into generated TypeScript Type definition files
/**
* This script injects JSDoc comments from the JS source files into the type
* definition files. This is necessary because the type definition files
* generated by TypeScript do not include JSDoc comments.
*
* @see https://github.com/microsoft/TypeScript/issues/14619
*
* The strategy is a bit hacky, but straightforward:
*
* 1. Recursively walk the output folder looking for .d.ts files
@coreyward
coreyward / usePersistantState.js
Created July 26, 2020 19:32
Async Storage backed `useState` for React Native
import { useState, useEffect, useCallback } from "react";
import { useAsyncStorage } from "@react-native-community/async-storage";
/**
* @example
* const [isReady, setReady, refresh] = usePersistantState(199, "ready")
*/
export default (id, property, defaultValue = null) => {
const [value, setValue] = useState(defaultValue);
const { getItem, setItem } = useAsyncStorage(`${id}#${property}`);
@coreyward
coreyward / lazy-component-map-react.jsx
Created February 20, 2023 21:45
Example of using React 18 Lazy to trigger code splitting when using an import map to render a Sanity-based list of components
import React, { Suspense } from "react";
const componentMap = {
sectionOne: React.lazy(() => import("./SectionOne")),
sectionTwo: React.lazy(() => import("./SectionTwo")),
};
const Section = ({ _type, ...sectionProps }) => {
const SectionComponent = componentMap[_type];
@coreyward
coreyward / upload-image-to-sanity.js
Created June 30, 2022 15:24
Quick reference code for uploading an image to the Sanity Studio from the browser
const getImageData = async url => {
const response = await global.fetch(url)
const contentType = response.headers.get("Content-Type")
return response.blob().then(imageData => ({ contentType, imageData }))
}
const uploadImage = ({ imageData, contentType }) =>
client.assets.upload("image", imageData, { contentType })
// usage example
@coreyward
coreyward / replaceRefs.js
Created June 28, 2022 14:23
Replace all references - Sanity
// import and configure the client
const oldRef = "abc123"
const newRef = "def456"
client
.fetch(
`*[references($oldRef)][0...250] {
_id,
_rev,
@coreyward
coreyward / _Inline SVGs Gatsby Sanity README.md
Last active June 28, 2022 14:22
Inline SVGs from Sanity in Gatsby

To render the SVG, it's useful to have a wrapper component that allows passing in additional props:

import React from "react"
import { compiler } from "markdown-to-jsx"

const InlineSvg = ({ content, ...props }) => 
  compiler(content, {
    createElement: (type, elProps, children) =>
 React.createElement(type, { ...elProps, ...props }, children),
@coreyward
coreyward / README.md
Last active November 7, 2021 09:39
I wrote this hook to persist a simple user settings (think dark mode) to local storage in a cross-tab compatible way.

useStorage React Hook

Similar to useState but with some lightweight behind-the-scenes writing to localStorage; also subscribes to changes in localStorage to allow for cross-tab changes to sync automatically.

Example Usage

The first argument is the name of the local storage property/key you want to control with this hook. The second argument, options, really just has one available directive: bool. Setting bool to true has the effect of evaluating the data

@coreyward
coreyward / gatsby-node.js
Created January 29, 2021 15:54
Generating a Netlify Redirects file from redirect-directives created in Sanity.io
exports.onPostBuild = ({ graphql }) =>
graphql(`
{
redirects: allSanityRedirect {
nodes {
matchPath
target
statusCode
}
}
@coreyward
coreyward / bundleChecker.js
Created March 16, 2021 21:06 — forked from rexxars/bundleChecker.js
Sanity studio bundle update checker
import { useEffect } from "react"
import config from "config:sanity"
const BUNDLE_CHECK_INTERVAL = 60 * 1000
const CHANGES_AVAILABLE_MESSAGE =
"There are changes to the Studio. For the best results the page will be refreshed to update to the latest version of the Studio."
async function getCurrentHash() {
const basePath = (config.project && config.project.basePath) || "/"
const html = await window.fetch(basePath).then((res) => res.text())