Skip to content

Instantly share code, notes, and snippets.

View SimeonGriggs's full-sized avatar

Simeon Griggs SimeonGriggs

View GitHub Profile
@SimeonGriggs
SimeonGriggs / createAndScheduleArticle.js
Created July 29, 2022 12:37
Create a single `article` document as a draft and Schedule it to be published in 2025
/**
* Create a single `article` document as a draft
* and Schedule it to be published in 2025
*
* Save this file to the root of your Studio and run with:
* sanity exec createAndScheduleArticle.js --with-user-token
*/
import sanityClient from 'part:@sanity/base/client'
import {uuid} from '@sanity/uuid'
@SimeonGriggs
SimeonGriggs / getUniqueFieldPaths.ts
Created July 20, 2022 18:12
Takes in a Sanity schema.get(`type`) to return a flat list of every possible field path
function getInnerFieldTypeNames(of = []) {
if (!of?.find((field) => field.fields)) {
return ``
}
const arrayObjectTypeNames = of.map((field) => field.name)
return arrayObjectTypeNames.join(`|`)
}
@SimeonGriggs
SimeonGriggs / TemplatePopulator.tsx
Created June 16, 2022 06:36
A component to write predefined into an Array field
// Add to your schema like...
// {name: `pageBuilderTemplatePopulator`, type: `string`, inputComponent: TemplatePopulator},
import {Button, Grid} from '@sanity/ui'
import React, {useCallback, useState} from 'react'
import sanityClient from 'part:@sanity/base/client'
const client = sanityClient.withConfig({apiVersion: `2021-05-19`})
const contentData = [
@SimeonGriggs
SimeonGriggs / CustomPreview.tsx
Last active June 16, 2022 05:37
An unofficial custom input for the Reference field with specific search paths + a custom document preview
import React from 'react'
import {Text, Spinner, Card} from '@sanity/ui'
import Preview from 'part:@sanity/base/preview'
import schema from 'part:@sanity/base/schema'
import useListeningQuery from './useListeningQuery'
export default function CustomPreview(props) {
const {id} = props
@SimeonGriggs
SimeonGriggs / ReferenceCustomSearch.jsx
Created June 7, 2022 11:21
Custom Reference Search Component
/**
* This is a Custom Input POC for a Reference field with defined search parameters on the target document
* The built-in reference field currently can filter results but only statically, not using the search query
*
* This custom input is NOT recommended as it lacks some of the amazing features of the built-in refernce field
* It's also a bit wonky in terms of the UI, but it's a start
* But it does solve this one specific use case
*/
import React from 'react'
@SimeonGriggs
SimeonGriggs / CopyToMarket.jsx
Last active April 27, 2022 11:52
Sanity.io Document Action to duplicate a document + update the `market` field to one selected.
/* eslint-disable react/prop-types */
/* eslint-disable react/jsx-no-bind */
import React, {useState, useCallback, useMemo} from 'react'
import {SearchIcon, ArrowRightIcon} from '@sanity/icons'
import {useToast, Text, Flex, Box, Button, Autocomplete, Label, Stack} from '@sanity/ui'
import sanityClient from 'part:@sanity/base/client'
export const MARKETS = [
{
name: `DE`,
@SimeonGriggs
SimeonGriggs / write-nextjs-redirect.js
Created April 25, 2022 10:38
A script to be run `prebuild` to dynamically generate a new vercel.json file with redirects written in Sanity
const sanityClient = require('@sanity/client')
const fs = require('fs')
const client = sanityClient({
apiVersion: '2021-04-01',
dataset: `XXX`,
projectId: `XXX`,
useCdn: true,
})
@SimeonGriggs
SimeonGriggs / market-separated-desk-structure.js
Last active March 30, 2022 12:38
Structure Builder for "markets" that share schema and populate an initial value template
import React from 'react'
import S from '@sanity/desk-tool/structure-builder'
import schema from 'part:@sanity/base/schema'
import userStore from 'part:@sanity/base/user'
// npm i pluralize
import pluralize from 'pluralize'
// BYO "Flag" component for an Icon
// import Flag from '../components/Flag/index'
@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 / ToggleLive.js
Last active March 3, 2022 15:51
Document action to toggle an `isLive` field with double-confirmation
import {useState, useMemo, useEffect} from 'react'
import {useDocumentOperation} from '@sanity/react-hooks'
import {FiZap, FiZapOff} from 'react-icons/fi'
export function ToggleLive({id, type, draft, published, onComplete}) {
const {patch, publish} = useDocumentOperation(id, type)
const doc = useMemo(() => draft || published, [draft, published])
const [isPublishing, setIsPublishing] = useState(false)
const [dialogOpen, setDialogOpen] = useState(false)