Skip to content

Instantly share code, notes, and snippets.

@runeb
runeb / Dockerfile
Created July 18, 2023 21:39
pg_embedding docker compose
FROM postgres:15.3
RUN apt update && apt upgrade -y
RUN apt install -y git build-essential postgresql-server-dev-15
# Copy in files from ph_embedding dir
RUN git clone https://github.com/neondatabase/pg_embedding.git
RUN cd pg_embedding && make && make install
import * as React from 'react'
import {StringInputProps, useClient, useFormValue} from 'sanity'
export const ReadOnlyIfPublished = (props: StringInputProps) => {
const client = useClient({ apiVersion: '2021-03-25', })
const docId = useFormValue(['_id']) as string
const createdAt = useFormValue(['_createdAt'])
const [readOnly, setReadOnly] = React.useState(createdAt !== undefined)
@runeb
runeb / MyReactComponent.tsx
Created April 24, 2023 22:04
Example useHasDocumentPermissionInDataset hook for evaluating grants and permissions in configurable dataset
import React from 'react'
import {useHasDocumentPermissionInDataset} from './useHasDocumentPermissionInDataset'
export function MyReactComponent() {
const targetDataset = 'playground'
const hasGrant = useHasDocumentPermissionInDataset({
dataset: targetDataset,
permission: 'update',
documentStub: {_type: 'provide-me', _id: 'provide-me'},
})
@runeb
runeb / patch.ts
Last active February 25, 2023 02:37
Patch Sanity documents from one dataset to another
// Example of how to patch documents in a dataset with data from another dataset
// using the Sanity client and transactions. More info: https://www.sanity.io/docs/transactions
import {createClient} from '@sanity/client'
// The 'from' dataset
const source = createClient({
projectId: 'rwmuledy',
dataset: 'production',
useCdn: false,
token: 'skseugwsW...',
@runeb
runeb / netlifyFunctions.ts
Last active March 2, 2023 17:29
Indexing algolia from a sanity webhook with Netlify functions and typescript
import algoliasearch from "algoliasearch";
import { createClient, SanityDocumentStub } from "@sanity/client";
import { Handler } from "@netlify/functions";
import indexer from "sanity-algolia"
const algolia = algoliasearch(
"application-id",
process.env.ALGOLIA_ADMIN_API_KEY
);
@runeb
runeb / deleteUndefined.tsx
Created January 31, 2023 01:11
Example of a custom tool to discover possibly orphaned documents, create a mutation to delete them and optionally perform the mutation
import * as React from 'react'
import {definePlugin, SanityClient, useClient, useSchema} from 'sanity'
import type {Tool} from 'sanity'
import {Card, Stack, Grid, Checkbox, Flex, Box, Text, Heading, Button} from '@sanity/ui'
import {useEffect} from 'react'
type ToolProps = {
tool: Tool
}
{
"name": "article",
"type": "document",
"title": "Article",
"fields": [
{
"type": "string",
"name": "title"
},
{
@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)
@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>;
};
[
"aahed",
"aalii",
"aargh",
"aarti",
"abaca",
"abaci",
"abacs",
"abaft",
"abaka",