Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vision): link to edit documents #6124

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading