This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sanityClient from 'part:@sanity/base/client' | |
const remoteURL = 'https://example.com' | |
const localURL = 'http://localhost:8000' | |
const previewURL = | |
window.location.hostname === 'localhost' ? localURL : remoteURL | |
export default function resolveProductionUrl (document) { | |
if (!document.slug) return null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import userStore from 'part:@sanity/base/user' | |
import { useEffect, useState } from 'react' | |
// Retrieves the current logged-in user details | |
export function useCurrentUser() { | |
const [user, setUser] = useState() | |
useEffect(() => { | |
userStore.currentUser.subscribe(e => setUser(e.user)) | |
}, []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react' | |
import resolveConfig from 'tailwindcss/resolveConfig' | |
import throttle from 'lodash.throttle' | |
import tailwindConfig from '../tailwind.config.js' | |
const findKeyByValue = (object, value) => | |
Object.keys(object).find((key) => object[key] === value) | |
const getDeviceConfig = (width) => { | |
const fullConfig = resolveConfig(tailwindConfig) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import slugify from 'slugify' | |
/** | |
* Slug field which will append a 'prefix' so that your slug is the complete path to the file | |
* And so that retrieving the slug from a reference to a document is a ready-made link | |
* | |
* Example with no prefix: | |
* `office` becomes `/office` | |
* | |
* Example with `office` prefix: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {SlugSourceFn, SlugifierFn, defineField, defineType} from 'sanity' | |
import get from 'lodash/get' | |
// Fetch the category's slug – if present – and append the post's title | |
const handleSlugSource: SlugSourceFn = async (doc, context) => { | |
const categoryRef = get(doc, `category._ref`) | |
if (categoryRef) { | |
const client = context.getClient({apiVersion: `2023-07-01`}) | |
const categorySlug = await client.fetch(`*[_id == $id][0].slug.current`, {id: categoryRef}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./pages/api/preview.ts | |
import { groq } from "next-sanity"; | |
import { previewClient } from "../../lib/sanity.client"; | |
import { NextApiRequest, NextApiResponse } from "next"; | |
export const STUDIO_URL_DEV = "http://localhost:3333"; | |
export const STUDIO_URL_PROD = "https://replace-with-yours.sanity.studio"; | |
export const WEBSITE_URL_DEV = "http://localhost:3000"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {useState} from 'react' | |
import {randomKey} from '@sanity/util/content' | |
import sanityClient from 'part:@sanity/base/client' | |
const apiVersion = `2021-05-19` | |
const client = sanityClient.withConfig({apiVersion}) | |
export default function AnnotateAction({draft, published}) { | |
const [isAnnotating, setIsAnnotating] = useState(false) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import S from '@sanity/desk-tool/structure-builder' | |
import userStore from 'part:@sanity/base/user' | |
// Get the logged in user | |
const getCurrentUser = () => { | |
userStore.me.subscribe((user) => { | |
// Instead of a local variable, we use this window object to re-use it through the Studio | |
if (user) { | |
window._sanityUser = user ?? undefined | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable @next/next/no-img-element */ | |
import Head from 'next/head' | |
import PropTypes from 'prop-types' | |
import React, {useMemo} from 'react' | |
import {getImageDimensions} from '@sanity/asset-utils' | |
// eslint-disable-next-line import/no-cycle | |
import {urlFor} from '../lib/sanity' | |
function getImageDetails(image, width, height) { |
OlderNewer