Skip to content

Commit

Permalink
fix(vision): add a warning about unsupported perspective when pasting (
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge authored Dec 3, 2024
1 parent e855a41 commit bd2b3cf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
15 changes: 15 additions & 0 deletions packages/@sanity/vision/src/SUPPORTED_PERSPECTIVES.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {type ClientPerspective} from '@sanity/client'

export type SupportedPerspective = 'raw' | 'previewDrafts' | 'published' | 'drafts'

export const SUPPORTED_PERSPECTIVES = [
'raw',
'previewDrafts',
'published',
'drafts',
] satisfies ClientPerspective[]
export const DEFAULT_PERSPECTIVE = SUPPORTED_PERSPECTIVES[0]

export function isSupportedPerspective(p: string): p is SupportedPerspective {
return SUPPORTED_PERSPECTIVES.includes(p as SupportedPerspective)
}
35 changes: 22 additions & 13 deletions packages/@sanity/vision/src/components/VisionGui.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
/* eslint-disable complexity */
import SplitPane from '@rexxars/react-split-pane'
import {
type ClientPerspective,
type ListenEvent,
type MutationEvent,
type SanityClient,
} from '@sanity/client'
import {type ListenEvent, type MutationEvent, type SanityClient} from '@sanity/client'
import {CopyIcon, ErrorOutlineIcon, PlayIcon, StopIcon} from '@sanity/icons'
import {
Box,
Expand All @@ -28,7 +23,12 @@ import {type TFunction, Translate} from 'sanity'

import {API_VERSIONS, DEFAULT_API_VERSION} from '../apiVersions'
import {VisionCodeMirror} from '../codemirror/VisionCodeMirror'
import {DEFAULT_PERSPECTIVE, isPerspective, PERSPECTIVES} from '../perspectives'
import {
DEFAULT_PERSPECTIVE,
isSupportedPerspective,
SUPPORTED_PERSPECTIVES,
type SupportedPerspective,
} from '../SUPPORTED_PERSPECTIVES'
import {type VisionProps} from '../types'
import {encodeQueryString} from '../util/encodeQueryString'
import {getCsvBlobUrl, getJsonBlobUrl} from '../util/getBlobUrl'
Expand Down Expand Up @@ -116,7 +116,7 @@ interface VisionGuiState {
dataset: string
apiVersion: string
customApiVersion: string | false
perspective: ClientPerspective
perspective: SupportedPerspective

// Selected options validation state
isValidApiVersion: boolean
Expand Down Expand Up @@ -187,7 +187,7 @@ export class VisionGui extends PureComponent<VisionGuiProps, VisionGuiState> {
apiVersion = DEFAULT_API_VERSION
}

if (!PERSPECTIVES.includes(perspective)) {
if (!SUPPORTED_PERSPECTIVES.includes(perspective)) {
perspective = DEFAULT_PERSPECTIVE
}

Expand Down Expand Up @@ -336,10 +336,19 @@ export class VisionGui extends PureComponent<VisionGuiProps, VisionGuiState> {
}
}

const perspective = PERSPECTIVES.includes(parts.options.perspective as ClientPerspective)
? (parts.options.perspective as ClientPerspective)
const perspective = isSupportedPerspective(parts.options.perspective)
? parts.options.perspective
: undefined

if (!isSupportedPerspective(parts.options.perspective)) {
this.props.toast.push({
closable: true,
id: 'vision-paste-unsupported-perspective',
status: 'warning',
title: 'Perspective in pasted url is currently not supported. Falling back to "raw"',
})
}

evt.preventDefault()
this.setState(
(prevState) => ({
Expand Down Expand Up @@ -447,7 +456,7 @@ export class VisionGui extends PureComponent<VisionGuiProps, VisionGuiState> {

handleChangePerspective(evt: ChangeEvent<HTMLSelectElement>) {
const perspective = evt.target.value
if (!isPerspective(perspective)) {
if (!isSupportedPerspective(perspective)) {
return
}

Expand Down Expand Up @@ -759,7 +768,7 @@ export class VisionGui extends PureComponent<VisionGuiProps, VisionGuiState> {
</Card>

<Select value={perspective} onChange={this.handleChangePerspective}>
{PERSPECTIVES.map((p) => (
{SUPPORTED_PERSPECTIVES.map((p) => (
<option key={p}>{p}</option>
))}
</Select>
Expand Down
8 changes: 0 additions & 8 deletions packages/@sanity/vision/src/perspectives.ts

This file was deleted.

0 comments on commit bd2b3cf

Please sign in to comment.