Skip to content

Commit

Permalink
feat(vision): link to edit documents on _id, _ref attributes (#6124)
Browse files Browse the repository at this point in the history
Co-authored-by: Espen Hovlandsdal <[email protected]>
  • Loading branch information
sgulseth and rexxars authored Dec 4, 2024
1 parent a20fb8f commit 2379f2b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
21 changes: 19 additions & 2 deletions packages/@sanity/vision/src/components/ResultView.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import JSONInspector from '@rexxars/react-json-inspector'
import {LinkIcon} from '@sanity/icons'
import {Code} from '@sanity/ui'
import LRU from 'quick-lru'
import {useDataset} from 'sanity'
import {IntentLink} from 'sanity/router'

import {ResultViewWrapper} from './ResultView.styled'

const lru = new LRU({maxSize: 50000})

export function ResultView(props: {data: unknown}) {
const {data} = props
export function ResultView(props: {data: unknown; datasetName: string}): JSX.Element {
const {data, datasetName} = props
const workspaceDataset = useDataset()

if (isRecord(data) || Array.isArray(data)) {
return (
Expand All @@ -17,6 +21,7 @@ export function ResultView(props: {data: unknown}) {
search={false}
isExpanded={isExpanded}
onClick={toggleExpanded}
interactiveLabel={workspaceDataset === datasetName ? DocumentEditLabel : undefined}
/>
</ResultViewWrapper>
)
Expand All @@ -25,6 +30,18 @@ export function ResultView(props: {data: unknown}) {
return <Code language="json">{JSON.stringify(data)}</Code>
}

function DocumentEditLabel(props: {value: string; isKey: boolean; keypath: string}) {
if (props.isKey || (!props.keypath.endsWith('_id') && !props.keypath.endsWith('_ref'))) {
return null
}

return (
<IntentLink intent="edit" params={{id: props.value}}>
<LinkIcon />
</IntentLink>
)
}

function isExpanded(keyPath: string, value: unknown): boolean {
const depthLimit = 4
const cached = lru.get(keyPath) as boolean | undefined
Expand Down
4 changes: 2 additions & 2 deletions packages/@sanity/vision/src/components/VisionGui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -954,9 +954,9 @@ export class VisionGui extends PureComponent<VisionGuiProps, VisionGuiState> {
</Box>
)}
{error && <QueryErrorDialog error={error} />}
{hasResult && <ResultView data={queryResult} />}
{hasResult && <ResultView data={queryResult} datasetName={dataset} />}
{listenInProgress && listenMutations.length > 0 && (
<ResultView data={listenMutations} />
<ResultView data={listenMutations} datasetName={dataset} />
)}
</Box>
</Result>
Expand Down

0 comments on commit 2379f2b

Please sign in to comment.