Skip to content

Instantly share code, notes, and snippets.

View SimeonGriggs's full-sized avatar

Simeon Griggs SimeonGriggs

View GitHub Profile
/**
* ---------------------------------------------------------------------------------
* This file has been generated by Sanity TypeGen.
* Command: `sanity typegen generate`
*
* Any modifications made directly to this file will be overwritten the next time
* the TypeScript definitions are generated. Please make changes to the Sanity
* schema definitions and/or GROQ queries if you need to update these types.
*
* For more information on how to use Sanity TypeGen, visit the official documentation:
@SimeonGriggs
SimeonGriggs / PageBuilder.tsx
Last active March 19, 2024 12:54
Rendering blocks from a "page builder" array in Sanity
import type { KeyedObject, TypedObject } from "sanity";
import PageBuilderContent from "./pageBuilderContent";
import PageBuilderColumns from "./pageBuilderColumns";
const Components = {
pageBuilderContent: PageBuilderContent,
pageBuilderColumns: PageBuilderColumns,
};
@SimeonGriggs
SimeonGriggs / ImageExtendedInput.tsx
Created September 11, 2023 17:16
An extended image asset schema with a custom input to watch the value and update the image's fields with values from the asset itself
import {useEffect, useState} from 'react'
import {ImageAsset, ObjectInputProps, Reference, set, unset, useClient} from 'sanity'
type ExtendedImageValue = {
asset: Reference
lqip?: string
blurHash?: string
}
export default function ImageExtendedInput(props: ObjectInputProps<ExtendedImageValue>) {
@SimeonGriggs
SimeonGriggs / Dereference.tsx
Created December 16, 2022 15:42
Sanity Studio v3 Document Action to "de-reference" a document by patching all documents that reference the current one. Incredibly dangerous.
import {useState, useEffect, useCallback} from 'react'
import {useToast, Card, Button, Stack, Text, Code} from '@sanity/ui'
import {extractWithPath} from '@sanity/mutator'
import {
Preview,
DocumentActionProps,
SanityDocument,
useClient,
useSchema,
pathToString,
@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 / useListeningQuery.ts
Last active November 30, 2022 05:59
A custom hook for making and listening to document changes
import React, {useEffect, useState, useRef} from 'react'
import documentStore from 'part:@sanity/base/datastore/document'
import {catchError, distinctUntilChanged} from 'rxjs/operators'
import isEqual from 'react-fast-compare'
type Params = Record<string, string | number | boolean | string[]>
interface ListenQueryOptions {
tag?: string
apiVersion?: string