Skip to content

Commit

Permalink
feat: adds support for versions ids in Studio releases (#54)
Browse files Browse the repository at this point in the history
* fix: update assistTasksStatusId to support version documents

* fix: plugin ImageContext sync id

* fix: add forced version for AssistInspector

* fix: use selectedReleaseId in assistDocument context and ImageContext

* fix: always use assistableDocumentId from useAssistDocumentContextValue

* fix: uses context dependent id for new documents

* fix(plugin): introduce AssitDocumentLayout

* fix(plugin): restore final dot to idPrefix

* fix: correctly uses assistableDocumentId for presence and image caption gen

* chore(deps): use latest studio for testing

* fix: removed source from package.json as it is covered by exports

* chore(deps): regen package-lock.json

* chore(deps): migrate husky

---------

Co-authored-by: Snorre Eskeland Brekke <snorre.e.brekke@gmail.com>
  • Loading branch information
pedrobonamin and snorrees authored Jan 9, 2025
1 parent 96169a8 commit 6b1e8d4
Show file tree
Hide file tree
Showing 21 changed files with 17,377 additions and 17,747 deletions.
3 changes: 0 additions & 3 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
34,845 changes: 17,214 additions & 17,631 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dev": "cd studio && npm run dev",
"format": "prettier --write --cache --ignore-unknown .",
"lint": "npm run lint --workspaces",
"prepare": "husky install",
"prepare": "husky",
"prepublishOnly": "npm run prepublishOnly --workspaces --if-present",
"release": "npm run release --workspaces --if-present",
"test": "npm run test --workspaces --if-present"
Expand All @@ -22,7 +22,7 @@
"@commitlint/cli": "^19.2.1",
"@commitlint/config-conventional": "^19.1.0",
"eslint": "^8.57.0",
"husky": "^8.0.3",
"husky": "^9.1.7",
"lint-staged": "^15.2.10",
"prettier": "^3.2.5",
"prettier-plugin-packagejson": "^2.4.14",
Expand Down
5 changes: 2 additions & 3 deletions plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
},
"main": "./dist/index.js",
"module": "./dist/index.esm.js",
"source": "./src/index.ts",
"types": "./dist/index.d.ts",
"files": [
"dist",
Expand Down Expand Up @@ -82,10 +81,10 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rimraf": "^5.0.5",
"sanity": "^3.37.2",
"sanity": "^3.69.0",
"semantic-release": "^23.0.8",
"styled-components": "^6.1.8",
"typescript": "5.4.2",
"typescript": "^5.7.2",
"vitest": "^1.4.0"
},
"peerDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion plugin/src/assistDocument/AssistDocumentContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ export type AssistDocumentContextValue = (
| {assistDocument: undefined; loading: true}
) & {
documentIsNew: boolean
/**
* This is the _actual_ id of the current document (ie the document loaded in the pane); it contains draft. versions. prefix ect depending on context
*/
assistableDocumentId: string
documentIsAssistable: boolean
documentId: string
documentSchemaType: ObjectSchemaType
openInspector: (inspectorName: string, paneParams?: Record<string, string>) => void
closeInspector: (inspectorName?: string) => void
Expand Down
7 changes: 3 additions & 4 deletions plugin/src/assistDocument/AssistDocumentContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import {PropsWithChildren} from 'react'
import {ObjectSchemaType} from 'sanity'

import {AssistDocumentContext} from './AssistDocumentContext'
import {useAssistDocumentContextValue} from './hooks/useAssistDocumentContextValue'

export interface AIDocumentInputProps {
documentId: string
schemaType: ObjectSchemaType
documentType: string
}

export function AssistDocumentContextProvider(props: PropsWithChildren<AIDocumentInputProps>) {
const {documentId, schemaType} = props
const value = useAssistDocumentContextValue(documentId, schemaType)
const {documentId, documentType} = props
const value = useAssistDocumentContextValue(documentId, documentType)
return (
<AssistDocumentContext.Provider value={value}>{props.children}</AssistDocumentContext.Provider>
)
Expand Down
8 changes: 1 addition & 7 deletions plugin/src/assistDocument/AssistDocumentInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {usePathKey} from '../helpers/misc'
import {isType} from '../helpers/typeUtils'
import {FirstAssistedPathProvider} from '../onboarding/FirstAssistedPathProvider'
import {assistDocumentTypeName} from '../types'
import {AssistDocumentContextProvider} from './AssistDocumentContextProvider'
import {useInstructionToaster} from './hooks/useInstructionToaster'

export function AssistDocumentInputWrapper(props: InputProps) {
Expand Down Expand Up @@ -44,12 +43,7 @@ function AssistDocumentInput({documentId, ...props}: ObjectInputProps & {documen

return (
<FirstAssistedPathProvider members={props.members}>
<AssistDocumentContextProvider schemaType={schemaType} documentId={documentId}>
{props.renderDefault({
...props,
schemaType,
})}
</AssistDocumentContextProvider>
{props.renderDefault({...props, schemaType})}
</FirstAssistedPathProvider>
)
}
Expand Down
12 changes: 12 additions & 0 deletions plugin/src/assistDocument/AssistDocumentLayout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {DocumentLayoutProps} from 'sanity'

import {AssistDocumentContextProvider} from './AssistDocumentContextProvider'

export function AssistDocumentLayout(props: DocumentLayoutProps) {
const {documentId, documentType} = props
return (
<AssistDocumentContextProvider documentType={documentType} documentId={documentId}>
{props.renderDefault(props)}
</AssistDocumentContextProvider>
)
}
6 changes: 0 additions & 6 deletions plugin/src/assistDocument/RequestRunInstructionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {useCallback, useEffect, useState} from 'react'
import {ObjectSchemaType, PatchEvent, SanityDocument, unset} from 'sanity'

import {useRunInstruction} from '../assistLayout/RunInstructionProvider'
import {publicId} from '../helpers/ids'

export interface DraftDelayedTaskArgs<T> {
documentOnChange: (event: PatchEvent) => void
Expand All @@ -19,11 +18,6 @@ export function isDocAssistable(
return !!(documentSchemaType.liveEdit ? published : draft)
}

export function getAssistableDocId(documentSchemaType: ObjectSchemaType, documentId: string) {
const baseId = publicId(documentId)
return documentSchemaType.liveEdit ? baseId : `drafts.${baseId}`
}

export function useRequestRunInstruction(args: {
documentOnChange: (event: PatchEvent) => void
// indicates if the document is a draft or liveEditable currently
Expand Down
6 changes: 2 additions & 4 deletions plugin/src/assistDocument/components/FieldRefPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ import {useSelectedField} from '../../assistInspector/helpers'
import {SelectedFieldContext} from './SelectedFieldContext'

export function FieldRefPreview(props: PreviewProps & {path?: string}) {
const {actions} = props
const documentSchema = useContext(SelectedFieldContext)?.documentSchema
const path = (useContext(InlineBlockValueContext) as {path?: string})?.path ?? props.path
const selectedField = useSelectedField(documentSchema, path)
return (
<Flex gap={2} align="center" style={{width: '100%'}}>
<Flex flex={1} gap={2} align="center" paddingY={3} paddingX={1}>
{/*<Box>
<Text>{selectedField?.icon ? createElement(selectedField?.icon) : <CodeIcon />}</Text>
</Box>*/}
<Box>
<Text size={1} textOverflow="ellipsis">
{selectedField?.title ?? 'Select field'}
</Text>
</Box>
</Flex>
<>{props.actions}</>
{actions as any}
</Flex>
)
}
57 changes: 40 additions & 17 deletions plugin/src/assistDocument/hooks/useAssistDocumentContextValue.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {useMemo} from 'react'
import {getPublishedId, type ObjectSchemaType, useEditState} from 'sanity'
import {getDraftId, getVersionId, type ObjectSchemaType, useSchema} from 'sanity'
import {useDocumentPane} from 'sanity/structure'

import {useAiPaneRouter} from '../../assistInspector/helpers'
Expand All @@ -8,31 +8,55 @@ import type {AssistDocumentContextValue} from '../AssistDocumentContext'
import {isDocAssistable} from '../RequestRunInstructionProvider'
import {useStudioAssistDocument} from './useStudioAssistDocument'

export function useAssistDocumentContextValue(
documentId: string,
documentSchemaType: ObjectSchemaType,
) {
const {published, draft} = useEditState(
getPublishedId(documentId),
documentSchemaType.name,
'low',
)
const assistableDocumentId = draft?._id || published?._id || documentId
const documentIsNew = Boolean(!draft?._id && !published?._id)
const documentIsAssistable = isDocAssistable(documentSchemaType, published, draft)
export function useAssistDocumentContextValue(documentId: string, documentType: string) {
const schema = useSchema()

const documentSchemaType = useMemo(() => {
const schemaType = schema.get(documentType) as ObjectSchemaType | undefined
if (!schemaType) {
throw new Error(`Schema type "${documentType}" not found`)
}
return schemaType
}, [documentType, schema])

const {
openInspector,
closeInspector,
inspector,
onChange: documentOnChange,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore this is a valid option available in `corel` - Remove after corel is merged to next
selectedReleaseId,
editState,
} = useDocumentPane()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore this is a valid option available in `corel` - Remove after corel is merged to next
const {draft, published, version} = editState || {}

let assistableDocumentId = version?._id || draft?._id || published?._id
if (!assistableDocumentId) {
assistableDocumentId = selectedReleaseId
? getVersionId(documentId, selectedReleaseId)
: documentSchemaType.liveEdit
? documentId
: getDraftId(documentId)
}

const documentIsNew = selectedReleaseId ? !version?._id : !draft?._id && !published?._id
const documentIsAssistable = selectedReleaseId
? !!version
: isDocAssistable(documentSchemaType, published, draft)

const {params} = useAiPaneRouter()
const selectedPath = params[fieldPathParam]
const {openInspector, closeInspector, inspector, onChange: documentOnChange} = useDocumentPane()

const assistDocument = useStudioAssistDocument({
documentId,
documentId: assistableDocumentId,
schemaType: documentSchemaType,
})

const value: AssistDocumentContextValue = useMemo(() => {
const base = {
documentId,
assistableDocumentId,
documentSchemaType,
documentIsNew,
Expand All @@ -54,7 +78,6 @@ export function useAssistDocumentContextValue(
}, [
assistDocument,
documentIsAssistable,
documentId,
assistableDocumentId,
documentSchemaType,
documentIsNew,
Expand Down
27 changes: 15 additions & 12 deletions plugin/src/assistInspector/AssistInspector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {useCallback, useMemo, useRef} from 'react'
import {
type DocumentInspectorProps,
PresenceOverlay,
useEditState,
VirtualizerScrollInstanceProvider,
} from 'sanity'
import {
Expand All @@ -18,11 +17,7 @@ import {styled} from 'styled-components'
import {DocumentForm} from '../_lib/form'
import {AssistTypeContext} from '../assistDocument/components/AssistTypeContext'
import {useStudioAssistDocument} from '../assistDocument/hooks/useStudioAssistDocument'
import {
getAssistableDocId,
isDocAssistable,
useRequestRunInstruction,
} from '../assistDocument/RequestRunInstructionProvider'
import {useRequestRunInstruction} from '../assistDocument/RequestRunInstructionProvider'
import {useAiAssistanceConfig} from '../assistLayout/AiAssistanceConfigContext'
import {giveFeedbackUrl, pluginTitle, releaseAnnouncementUrl, salesUrl} from '../constants'
import {getConditionalMembers} from '../helpers/conditionalMembers'
Expand All @@ -39,6 +34,7 @@ import {
useTypePath,
} from './helpers'
import {InstructionTaskHistoryButton} from './InstructionTaskHistoryButton'
import {useAssistDocumentContext} from '../assistDocument/AssistDocumentContext'

const CardWithShadowBelow = styled(Card)`
position: relative;
Expand Down Expand Up @@ -209,15 +205,15 @@ export function AssistInspector(props: DocumentInspectorProps) {
onChange: documentOnChange,
formState,
} = documentPane
const {published, draft} = useEditState(documentId, documentType, 'low')

const {assistableDocumentId, documentIsAssistable} = useAssistDocumentContext()

const formStateRef = useRef(formState)
formStateRef.current = formState

const assistableDocId = getAssistableDocId(schemaType, documentId)
const {instructionLoading, requestRunInstruction} = useRequestRunInstruction({
documentOnChange,
isDocAssistable: isDocAssistable(schemaType, published, draft),
isDocAssistable: documentIsAssistable,
})

const typePath = useTypePath(docValue, pathKey ?? '')
Expand Down Expand Up @@ -268,14 +264,14 @@ export function AssistInspector(props: DocumentInspectorProps) {
pathKey &&
typePath &&
requestRunInstruction({
documentId: assistableDocId,
documentId: assistableDocumentId,
path: pathKey,
typePath,
assistDocumentId: assistDocumentId(documentType),
instruction,
conditionalMembers: formStateRef.current ? getConditionalMembers(formStateRef.current) : [],
}),
[pathKey, instruction, typePath, documentType, assistableDocId, requestRunInstruction],
[pathKey, instruction, typePath, documentType, assistableDocumentId, requestRunInstruction],
)

const Region = useCallback((_props: any) => {
Expand Down Expand Up @@ -331,6 +327,13 @@ export function AssistInspector(props: DocumentInspectorProps) {
index={documentPane.index}
itemId="ai"
pane={paneNode}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore this is a valid option available in `corel` - Remove after corel is merged to next
forcedVersion={{
isReleaseLocked: false,
selectedPerspectiveName: 'published',
selectedReleaseId: undefined,
}}
>
<DocumentForm />
</DocumentPaneProvider>
Expand Down Expand Up @@ -373,7 +376,7 @@ export function AssistInspector(props: DocumentInspectorProps) {
)}

<InstructionTaskHistoryButton
documentId={assistableDocId}
documentId={assistableDocumentId}
tasks={tasks}
instructions={instructions}
showTitles={!instructionKey}
Expand Down
Loading

0 comments on commit 6b1e8d4

Please sign in to comment.