Skip to content

Instantly share code, notes, and snippets.

import React, { useRef } from 'react'
import { map } from 'rxjs/operators'
import { createWeightedSearch } from 'part:@sanity/base/search/weighted'
import client from 'part:@sanity/base/client'
import ExternalReferenceInput from './ExternalReferenceInput'
const ExternalReference = props => {
const { type } = props
const { options } = type
const { dataset } = options
{
"Client": {
"scope": "javascript,typescript",
"prefix": "sanity client",
"body": [
"const sanityClient = require('@sanity/client')",
"const client = sanityClient({",
"projectId: '${1:your-project-id}',",
"dataset: '${2:production}',",
"token: 'sanity-auth-token', // or leave blank to be anonymous user",
// Exif orientation value to css transform mapping
type Rotation = string;
const rotations: Record<number, Rotation> = {
1: "rotate(0deg)",
2: "rotateY(180deg)",
3: "rotate(180deg)",
4: "rotate(180deg) rotateY(180deg)",
5: "rotate(270deg) rotateY(180deg)",
6: "rotate(90deg)",
7: "rotate(90deg) rotateY(180deg)",
// Find all nested instances of _type `type`
const findAssets = (object, type, results = []) => {
if (object instanceof Object) {
if (object["_type"] === type) {
results.push(object);
return;
}
for (const key of Object.keys(object)) {
const value = object[key];
@runeb
runeb / listen.js
Created April 9, 2021 19:27
Listening for Sanity dataset changes and causing Eleventy rebuild
require("dotenv").config();
const fs = require("fs");
const { groqStore, groq } = require("@sanity/groq-store");
const store = groqStore({
projectId: process.env.SANITY_PROJECT_ID,
dataset: process.env.SANITY_DATASET,
listen: true,
overlayDrafts: true,
token: process.env.SANITY_TOKEN || "",
import React, {useState} from "react"
import { PublishAction } from 'part:@sanity/base/document-actions';
const ConfirmDialog = ({onClick, onCancel}) => {
return (
<>
<button onClick={onClick}>Yes</button>
<button onClick={onCancel}>Cancel</button>
</>
)
@runeb
runeb / Custom.js
Last active April 22, 2021 00:32
Example of custom document actions on Sanity. See documentation for more https://www.sanity.io/docs/document-actions-api
import React from "react";
import sanityClient from "part:@sanity/base/client";
import userStore from "part:@sanity/base/user";
// A more involved custom action example, doing some queries etc.
export default function Custom(props) {
// const {draft, published, id, type} = props
// Common operation in custom actions are to inspect the document (draft ||
[
"aahed",
"aalii",
"aargh",
"aarti",
"abaca",
"abaci",
"abacs",
"abaft",
"abaka",
@runeb
runeb / useSanityMessaging.ts
Last active May 9, 2022 07:22
React hook for using a Sanity dataset as a general messaging system between users
import { useCallback, useEffect } from "react";
import type { SanityClient, SanityDocument } from "@sanity/client";
import {nanoid} from "nanoid"
type Params<T> = {
id: string;
client: SanityClient;
handler: (from: string, payload: T) => Promise<void>;
};
@runeb
runeb / wave.ts
Last active September 25, 2022 20:03
import { assert } from "console"
import { open } from "fs/promises"
const strEncoder = new TextDecoder("utf-8")
function readString(scanner: DataView, cursor: number, len: number) {
const bytes = []
for (let i = 0; i < len; i++) {
bytes[i] = scanner.getUint8(cursor++)
}
const a = new Uint8Array(bytes)