Skip to content

Instantly share code, notes, and snippets.

View SimeonGriggs's full-sized avatar

Simeon Griggs SimeonGriggs

View GitHub Profile
@SimeonGriggs
SimeonGriggs / filteredObject.js
Last active October 5, 2021 11:04
Call Objects using a function that will filter its child fields down to specific keys
const filteredObject = (name, filters = []) => ({
name,
type: 'object',
fields: [
{name: 'size', type: 'string', hidden: () => filters.includes('size')},
{name: 'tone', type: 'string', hidden: () => filters.includes('tone')}
]
})
export default {
@SimeonGriggs
SimeonGriggs / example.js
Last active December 8, 2021 10:30
A "listening" version of usePreviewSubscription() that re-queries the client. Not yet recommended for Production use.
const { data, loading } = useListeningQuery(
query,
{
params: params ?? {},
initialData: page,
enabled: preview,
delay: 250,
}
);
@SimeonGriggs
SimeonGriggs / i18n-document-level-unique-slugs.js
Created November 22, 2021 14:22
An `isUnique` function for unique slugs across a schema type but the same across a base document
{
name: 'slug',
title: 'Slug',
type: 'slug',
options: {
source: 'title',
isUnique: async (slug, options) => {
const {document: sanityDocument} = options
// Filter out additional parts of the _id to get the original
@SimeonGriggs
SimeonGriggs / Tabs.js
Last active November 26, 2021 14:22
inputComponent drop-in replacement for `sanity-plugin-tabs`
/* eslint-disable no-negated-condition */
/* eslint-disable react/prop-types */
/* eslint-disable react/display-name */
/* eslint-disable max-nested-callbacks */
/**
* inputComponent drop-in replacement for `sanity-plugin-tabs`
* Provided as-is with no explicit or implied future support
* Addresses some UI issues with that plugin, but may introduce others
*
@SimeonGriggs
SimeonGriggs / ReferenceCustomSearch.js
Created December 10, 2021 11:03
Reference Field Custom Input with Restrained Search
/* eslint-disable react/prop-types */
/**
* 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 reference field
* It's also a bit wonky in terms of the UI, but it's a start
* ...and it does solve this one specific use case
*/
@SimeonGriggs
SimeonGriggs / DecoratedArray.js
Created December 10, 2021 14:19
Sanity.io Custom Input that will still resolve the default UI for field editing
/**
* Custom Input that will resolve Sanity's default UI for field editing
* As well as display some contextual information from the document
* This is example is for an `array` field of `car` type fields
* For which the default UI is shown below an image from the first array item
*
* The implementation is *not* bulletproof and only has basic handling
* of the potential pitfalls of recursive rendering of the
* <FormBuilderInput /> component
*/
@SimeonGriggs
SimeonGriggs / DateTimeDisplay.jsx
Created January 25, 2022 12:07
DateTimeDisplay Tool
import React, {useState} from 'react'
import {Box, Text, Container, Card, Stack, Flex} from '@sanity/ui'
import subDays from 'date-fns/subDays'
import {format} from 'date-fns'
import DateTimeInput from 'part:@sanity/form-builder'
export default function DateTimeDisplay() {
const [fromDate, setFromDate] = useState(subDays(new Date(), 1))
const [toDate, setToDate] = useState(new Date())
@SimeonGriggs
SimeonGriggs / SuperSecretTool.jsx
Created February 3, 2022 10:45
A tool that renders differently based on User Role Name
import React from 'react'
import {Box, Card, Container, Text} from '@sanity/ui'
import {useCurrentUser} from './useCurrentUser'
export default function SuperSecretTool() {
const {roles} = useCurrentUser()
const isAdmin = roles?.length ? roles.find((role) => role?.name === 'administrator') : false
if (!isAdmin) {
@SimeonGriggs
SimeonGriggs / wordle.js
Last active February 11, 2022 09:54
Sanity Schema for Wordle
import React from 'react'
import {withDocument} from 'part:@sanity/form-builder'
import {Stack, Card, Flex, Text} from '@sanity/ui'
import {format} from 'date-fns'
const WORD_LENGTH = 5
function Guess({char, answer, index}) {
const answerSplit = answer.split('')
let tone = `default`
@SimeonGriggs
SimeonGriggs / TradingHours.jsx
Created February 16, 2022 10:13
A mock prototype for a TradingHours custom input designed with Sanity UI
import React from 'react'
import {Stack, Card, Flex, Switch, Label, Box, Text, Autocomplete, Button} from '@sanity/ui'
import {AddIcon, TrashIcon} from '@sanity/icons'
import {daysOfTheWeek, hoursOfTheDay} from './data'
export default function TradingHours() {
// Update how trading times are searched for
const handleAutocompleteFilter = (query, option) => {
// Treat a search for 1.00 as 1:00