Skip to content

Commit

Permalink
refactor: flatten _singletons and increase usage of `sanity/_create…
Browse files Browse the repository at this point in the history
…Context` (#7311)

* chore: add eslint rule to prevent react.createContext

* refactor: move and flatten contexts

* refactor: switch to sanity/_createContext
  • Loading branch information
ricokahler authored Aug 5, 2024
1 parent e82e815 commit c512fa7
Show file tree
Hide file tree
Showing 113 changed files with 671 additions and 515 deletions.
5 changes: 5 additions & 0 deletions packages/sanity/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ module.exports = {
name: 'sanity/router',
message: 'Use relative type imports instead',
},
{
name: 'react',
importNames: ['createContext'],
message: "Use `createContext` from 'sanity/_createContext' instead",
},
],
},
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {createContext} from 'sanity/_createContext'

import type {ActiveWorkspaceMatcherContextValue} from '../../../../core/studio/activeWorkspaceMatcher/ActiveWorkspaceMatcherContext'
import type {ActiveWorkspaceMatcherContextValue} from '../../core/studio/activeWorkspaceMatcher/ActiveWorkspaceMatcherContext'

/** @internal */
export const ActiveWorkspaceMatcherContext =
Expand Down
12 changes: 12 additions & 0 deletions packages/sanity/src/_singletons/context/AddonDatasetContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {createContext} from 'sanity/_createContext'

import type {AddonDatasetContextValue} from '../../core/studio/addonDataset/types'

/**
* @beta
* @hidden
*/
export const AddonDatasetContext = createContext<AddonDatasetContextValue | null>(
'sanity/_singletons/context/addon-dataset',
null,
)
11 changes: 11 additions & 0 deletions packages/sanity/src/_singletons/context/CalendarContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {createContext} from 'sanity/_createContext'

import type {CalendarContextValue} from '../../core/studio/components/navbar/search/components/filters/filter/inputs/date/datePicker/calendar/contexts/CalendarContext'

/**
* @internal
*/
export const CalendarContext = createContext<CalendarContextValue | undefined>(
'sanity/_singletons/context/calendar',
undefined,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {createContext} from 'sanity/_createContext'

import type {
ChangeIndicatorTrackerContextStoreType,
ChangeIndicatorTrackerGetSnapshotType,
} from '../../core/changeIndicators/ChangeIndicatorTrackerContexts'

/** @internal */
export const ChangeIndicatorTrackerContextStore =
createContext<ChangeIndicatorTrackerContextStoreType>(
'sanity/_singletons/context/change-indicator-tracker-store',
null,
)

/** @internal */
export const ChangeIndicatorTrackerContextGetSnapshot =
createContext<ChangeIndicatorTrackerGetSnapshotType>(
'sanity/_singletons/context/change-indicator-tracker-get-snapshot',
null,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {createContext} from 'react'
import {createContext} from 'sanity/_createContext'

import type {StudioThemeColorSchemeKey} from '../../../core/theme/types'
import type {StudioThemeColorSchemeKey} from '../../core/theme/types'

/**
* The setter for ColorSchemeValueContext, in a separate context to avoid unnecessary re-renders
Expand All @@ -9,4 +9,4 @@ import type {StudioThemeColorSchemeKey} from '../../../core/theme/types'
*/
export const ColorSchemeSetValueContext = createContext<
((nextScheme: StudioThemeColorSchemeKey) => void) | false | null
>(null)
>('sanity/_singletons/context/color-scheme-set-value', null)
12 changes: 12 additions & 0 deletions packages/sanity/src/_singletons/context/ColorSchemeValueContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {createContext} from 'sanity/_createContext'

import type {StudioThemeColorSchemeKey} from '../../core/theme/types'

/**
* Used to keep track of the internal value, which can be "system" in addition to "light" and "dark"
* @internal
*/
export const ColorSchemeValueContext = createContext<StudioThemeColorSchemeKey | null>(
'sanity/_singletons/context/color-scheme-value',
null,
)
11 changes: 11 additions & 0 deletions packages/sanity/src/_singletons/context/CommentInputContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {createContext} from 'sanity/_createContext'

import type {CommentInputContextValue} from '../../core/comments/components/pte/comment-input/CommentInputProvider'

/**
* @internal
*/
export const CommentInputContext = createContext<CommentInputContextValue | null>(
'sanity/_singletons/context/comment-input',
null,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {createContext} from 'sanity/_createContext'

import type {CommentsAuthoringPathContextValue} from '../../core/comments/context/authoring-path/types'

/**
* @beta
* @hidden
*/
export const CommentsAuthoringPathContext = createContext<CommentsAuthoringPathContextValue | null>(
'sanity/_singletons/context/comments-authoring-path',
null,
)
11 changes: 11 additions & 0 deletions packages/sanity/src/_singletons/context/CommentsContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {createContext} from 'sanity/_createContext'

import type {CommentsContextValue} from '../../core/comments/context/comments/types'

/**
* @internal
*/
export const CommentsContext = createContext<CommentsContextValue | null>(
'sanity/_singletons/context/comments',
null,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {createContext} from 'react'
import {createContext} from 'sanity/_createContext'

import type {CommentsEnabledContextValue} from '../../../../core/comments/context/enabled/types'
import type {CommentsEnabledContextValue} from '../../core/comments/context/enabled/types'

// NOTE: We initialize this context with a default value (`enabled: false`)
// rather than `null` to handle cases where the comments feature's availability
Expand All @@ -11,7 +11,10 @@ import type {CommentsEnabledContextValue} from '../../../../core/comments/contex
/**
* @internal
*/
export const CommentsEnabledContext = createContext<CommentsEnabledContextValue>({
enabled: false,
mode: null,
})
export const CommentsEnabledContext = createContext<CommentsEnabledContextValue>(
'sanity/_singletons/context/comments-enabled',
{
enabled: false,
mode: null,
},
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {createContext} from 'sanity/_createContext'

import type {CommentsIntentContextValue} from '../../../../core/comments/context/intent/types'
import type {CommentsIntentContextValue} from '../../core/comments/context/intent/types'

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {createContext} from 'sanity/_createContext'

import type {CommentsOnboardingContextValue} from '../../core/comments/context/onboarding/types'

/**
* @internal
*/
export const CommentsOnboardingContext = createContext<CommentsOnboardingContextValue | null>(
'sanity/_singletons/context/comments-onboarding',
null,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {createContext} from 'sanity/_createContext'

import type {CommentsSelectedPathContextValue} from '../../core/comments/context/selected-path/types'

/**
* @internal
*/
export const CommentsSelectedPathContext = createContext<CommentsSelectedPathContextValue | null>(
'sanity/_singletons/context/comments-selected-path',
null,
)
11 changes: 11 additions & 0 deletions packages/sanity/src/_singletons/context/CommentsUpsellContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {createContext} from 'sanity/_createContext'

import type {CommentsUpsellContextValue} from '../../core/comments/context/upsell/types'

/**
* @internal
*/
export const CommentsUpsellContext = createContext<CommentsUpsellContextValue | null>(
'sanity/_singletons/context/comments-upsell',
null,
)
13 changes: 13 additions & 0 deletions packages/sanity/src/_singletons/context/ConnectorContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {createContext} from 'sanity/_createContext'

import type {ConnectorContextValue} from '../../core/changeIndicators/ConnectorContext'

/** @internal */
export const ConnectorContext = createContext<ConnectorContextValue>(
'sanity/_singletons/context/connector',
{
isReviewChangesOpen: false,
onOpenReviewChanges: () => undefined,
onSetFocus: () => undefined,
} as ConnectorContextValue,
)
12 changes: 12 additions & 0 deletions packages/sanity/src/_singletons/context/CopyPasteContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {createContext} from 'sanity/_createContext'

import type {CopyPasteContextType} from '../../core/studio/copyPaste'

/**
* @beta
* @hidden
*/
export const CopyPasteContext = createContext<CopyPasteContextType | null>(
'sanity/_singletons/context/copy-paste',
null,
)
9 changes: 9 additions & 0 deletions packages/sanity/src/_singletons/context/DiffContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type {Path} from '@sanity/types'
import {createContext} from 'sanity/_createContext'

/** @internal */
export const DiffContext = createContext<{
path: Path
}>('sanity/_singletons/context/diff', {
path: [],
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {createContext} from 'sanity/_createContext'

import type {DocumentActionProps} from '../../core/config/document/actions'

/**
* @internal
*/
export const DocumentActionPropsContext = createContext<DocumentActionProps | undefined>(
'sanity/_singletons/context/document-action-props',
undefined,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {createContext} from 'sanity/_createContext'

import type {DocumentChangeContextInstance} from '../../core/field/diff/contexts/DocumentChangeContext'

/** @internal */
export const DocumentChangeContext = createContext<DocumentChangeContextInstance | null>(
'sanity/_singletons/context/document-change',
null,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {createContext} from 'react'
import {createContext} from 'sanity/_createContext'

import type {DocumentFieldAction} from '../../../../core/config/document/fieldActions/types'
import type {DocumentFieldAction} from '../../core/config/document/fieldActions/types'

/**
* @internal
Expand All @@ -13,5 +13,6 @@ export interface DocumentFieldActionsContextValue {
* @internal
*/
export const DocumentFieldActionsContext = createContext<DocumentFieldActionsContextValue | null>(
'sanity/_singletons/context/document-field-actions',
null,
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {createContext} from 'react'
import {createContext} from 'sanity/_createContext'

/**
* @internal
Expand All @@ -10,4 +10,7 @@ export interface DocumentIdContextValue {
/**
* @internal
*/
export const DocumentIdContext = createContext<DocumentIdContextValue | null>(null)
export const DocumentIdContext = createContext<DocumentIdContextValue | null>(
'sanity/_singletons/context/document-id',
null,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {createContext} from 'sanity/_createContext'

import type {DocumentPaneContextValue} from '../../../../structure/panes/document/DocumentPaneContext'
import type {DocumentPaneContextValue} from '../../structure/panes/document/DocumentPaneContext'

/** @internal */
export const DocumentPaneContext = createContext<DocumentPaneContextValue | null>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {createContext} from 'sanity/_createContext'

import type {DocumentSheetListContextValue} from '../../structure/panes/documentList/sheetList/DocumentSheetListProvider'

/** @internal */
export const DocumentSheetListContext = createContext<DocumentSheetListContextValue | undefined>(
'sanity/_singletons/context/document-sheet-list',
undefined,
)
24 changes: 24 additions & 0 deletions packages/sanity/src/_singletons/context/FieldActionsContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {createContext} from 'sanity/_createContext'

import type {DocumentFieldActionNode} from '../../core/config/document/fieldActions/types'

/** @internal */
export interface FieldActionsContextValue {
actions: DocumentFieldActionNode[]
focused?: boolean
hovered: boolean
onMouseEnter: () => void
onMouseLeave: () => void
}

/** @internal */
export const FieldActionsContext = createContext<FieldActionsContextValue>(
'sanity/_singletons/context/field-actions',
{
actions: [],
focused: false,
hovered: false,
onMouseEnter: () => undefined,
onMouseLeave: () => undefined,
},
)
11 changes: 11 additions & 0 deletions packages/sanity/src/_singletons/context/FormBuilderContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {createContext} from 'sanity/_createContext'

import type {FormBuilderContextValue} from '../../core/form/FormBuilderContext'

/**
* @internal
*/
export const FormBuilderContext = createContext<FormBuilderContextValue | null>(
'sanity/_singletons/context/form-builder',
null,
)
11 changes: 11 additions & 0 deletions packages/sanity/src/_singletons/context/FormCallbacksContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {createContext} from 'sanity/_createContext'

import type {FormCallbacksValue} from '../../core/form/studio/contexts/FormCallbacks'

/**
* @internal
*/
export const FormCallbacksContext = createContext<FormCallbacksValue | null>(
'sanity/_singletons/context/form-callbacks',
null,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {createContext} from 'sanity/_createContext'

import type {FormNodePresence} from '../../core/presence/types'

/** @internal */
export const FormFieldPresenceContext = createContext<FormNodePresence[]>(
'sanity/_singletons/context/form-field-presence',
[],
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {createContext} from 'sanity/_createContext'

import type {FormValueContextValue} from '../../../core/form/contexts/FormValue'
import type {FormValueContextValue} from '../../core/form/contexts/FormValue'

/**
* @internal
Expand Down
11 changes: 11 additions & 0 deletions packages/sanity/src/_singletons/context/FreeTrialContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {createContext} from 'sanity/_createContext'

import type {FreeTrialContextProps} from '../../core/studio/components/navbar/free-trial/FreeTrialContext'

/**
* @internal
*/
export const FreeTrialContext = createContext<FreeTrialContextProps | undefined>(
'sanity/_singletons/context/free-trial',
undefined,
)
Loading

0 comments on commit c512fa7

Please sign in to comment.