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

fix: update assistTasksStatusId to support version documents #54

Merged
merged 13 commits into from
Jan 9, 2025
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
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
@@ -1,10 +1,9 @@
import {ArrowRightIcon, CloseIcon, PlayIcon, RetryIcon} from '@sanity/icons'

Check warning on line 1 in plugin/src/assistInspector/AssistInspector.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

Run autofix to sort these imports!
import {Box, Button, Card, Flex, Spinner, Stack, Text} from '@sanity/ui'
import {useCallback, useMemo, useRef} from 'react'
import {
type DocumentInspectorProps,
PresenceOverlay,
useEditState,
VirtualizerScrollInstanceProvider,
} from 'sanity'
import {
Expand All @@ -18,11 +17,7 @@
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 @@
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 @@
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 @@
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 @@
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 @@
)}

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