Skip to content

Instantly share code, notes, and snippets.

View SimeonGriggs's full-sized avatar

Simeon Griggs SimeonGriggs

View GitHub Profile
@SimeonGriggs
SimeonGriggs / resolveProductionUrl.js
Created October 25, 2020 10:26
Sanity Preview URL with sanityClient Fetch
import sanityClient from 'part:@sanity/base/client'
const remoteURL = 'https://example.com'
const localURL = 'http://localhost:8000'
const previewURL =
window.location.hostname === 'localhost' ? localURL : remoteURL
export default function resolveProductionUrl (document) {
if (!document.slug) return null
@SimeonGriggs
SimeonGriggs / useCurrentUser.js
Created November 18, 2020 14:37
Sanity useCurrentUser() Custom Hook
import userStore from 'part:@sanity/base/user'
import { useEffect, useState } from 'react'
// Retrieves the current logged-in user details
export function useCurrentUser() {
const [user, setUser] = useState()
useEffect(() => {
userStore.currentUser.subscribe(e => setUser(e.user))
}, [])
@SimeonGriggs
SimeonGriggs / useTailwindBreakpoint.js
Created January 4, 2021 20:50
React Hook which returns the current TailwindCSS breakpoint size based on the current window width.
import { useState, useEffect } from 'react'
import resolveConfig from 'tailwindcss/resolveConfig'
import throttle from 'lodash.throttle'
import tailwindConfig from '../tailwind.config.js'
const findKeyByValue = (object, value) =>
Object.keys(object).find((key) => object[key] === value)
const getDeviceConfig = (width) => {
const fullConfig = resolveConfig(tailwindConfig)
@SimeonGriggs
SimeonGriggs / slugWithType.js
Created March 22, 2021 18:03
Sanity.io: Extend the built-in `slug` field type with validation rules and slug prefixes
import slugify from 'slugify'
/**
* Slug field which will append a 'prefix' so that your slug is the complete path to the file
* And so that retrieving the slug from a reference to a document is a ready-made link
*
* Example with no prefix:
* `office` becomes `/office`
*
* Example with `office` prefix:
@SimeonGriggs
SimeonGriggs / parentChild.js
Last active March 4, 2022 15:43
A Structure Builder function for Parent/Child Document Relationships
// This is all explained in a complete guide at:
// https://www.sanity.io/guides/parent-child-taxonomy
import S from '@sanity/desk-tool/structure-builder'
import documentStore from 'part:@sanity/base/datastore/document'
import {map} from 'rxjs/operators'
import {FiTag} from 'react-icons/fi'
/**
* This is an example of a Structure Builder list item that:
@SimeonGriggs
SimeonGriggs / postType.ts
Last active July 14, 2023 14:42
Sanity.io: A slug field with a prefix based on a "category" reference's slug
import {SlugSourceFn, SlugifierFn, defineField, defineType} from 'sanity'
import get from 'lodash/get'
// Fetch the category's slug – if present – and append the post's title
const handleSlugSource: SlugSourceFn = async (doc, context) => {
const categoryRef = get(doc, `category._ref`)
if (categoryRef) {
const client = context.getClient({apiVersion: `2023-07-01`})
const categorySlug = await client.fetch(`*[_id == $id][0].slug.current`, {id: categoryRef})
@SimeonGriggs
SimeonGriggs / preview.ts
Last active December 23, 2023 23:28
Sanity + Next.js Preview API Route with Config for SEO Pane
// ./pages/api/preview.ts
import { groq } from "next-sanity";
import { previewClient } from "../../lib/sanity.client";
import { NextApiRequest, NextApiResponse } from "next";
export const STUDIO_URL_DEV = "http://localhost:3333";
export const STUDIO_URL_PROD = "https://replace-with-yours.sanity.studio";
export const WEBSITE_URL_DEV = "http://localhost:3000";
@SimeonGriggs
SimeonGriggs / AnnotateAction.js
Created July 15, 2021 14:25
Sanity.io Document Action to automate Annotations from a 'Vocabulary' schema
import {useState} from 'react'
import {randomKey} from '@sanity/util/content'
import sanityClient from 'part:@sanity/base/client'
const apiVersion = `2021-05-19`
const client = sanityClient.withConfig({apiVersion})
export default function AnnotateAction({draft, published}) {
const [isAnnotating, setIsAnnotating] = useState(false)
@SimeonGriggs
SimeonGriggs / desk-structure.js
Last active February 7, 2023 20:33
Sanity.io – Customize Desk Structure based on User Roles
import S from '@sanity/desk-tool/structure-builder'
import userStore from 'part:@sanity/base/user'
// Get the logged in user
const getCurrentUser = () => {
userStore.me.subscribe((user) => {
// Instead of a local variable, we use this window object to re-use it through the Studio
if (user) {
window._sanityUser = user ?? undefined
}
@SimeonGriggs
SimeonGriggs / SanityImage.js
Created August 11, 2021 14:57
A far-from-feature-complete Component for loading Sanity Images in Next.js
/* eslint-disable @next/next/no-img-element */
import Head from 'next/head'
import PropTypes from 'prop-types'
import React, {useMemo} from 'react'
import {getImageDimensions} from '@sanity/asset-utils'
// eslint-disable-next-line import/no-cycle
import {urlFor} from '../lib/sanity'
function getImageDetails(image, width, height) {