Skip to content

Commit

Permalink
refactor(sanity): move bundle metadata store to resource cache
Browse files Browse the repository at this point in the history
Co-authored-by: Pedro Bonamin <pedrobonamin@gmail.com>
  • Loading branch information
juice49 and pedrobonamin committed Jul 8, 2024
1 parent b5b7776 commit cbf7da1
Show file tree
Hide file tree
Showing 20 changed files with 345 additions and 258 deletions.

This file was deleted.

1 change: 0 additions & 1 deletion packages/sanity/src/_singletons/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './core/bundles/BundlesContext'
export * from './core/changeIndicators/ConnectorContext'
export * from './core/components/previewCard/PreviewCardContext'
export * from './core/components/scroll/scrollContext'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'sanity'

import {Button} from '../../../../ui-components'
import {useBundles} from '../../../store/bundles/BundlesProvider'
import {useBundles} from '../../../store/bundles'
import {type BundleDocument} from '../../../store/bundles/types'
import {getAllVersionsOfDocument, versionDocumentExists} from '../../util/dummyGetters'

Expand Down
2 changes: 1 addition & 1 deletion packages/sanity/src/core/bundles/hooks/usePerspective.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {useRouter} from 'sanity/router'

import {useBundles} from '../../store/bundles/BundlesProvider'
import {useBundles} from '../../store/bundles'
import {type BundleDocument} from '../../store/bundles/types'
import {LATEST} from '../util/const'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {type LayoutProps} from '../../config'
import {BundlesProvider} from '../../store/bundles/BundlesProvider'
import {AddonDatasetProvider} from '../../studio'

export function ReleasesStudioLayout(props: LayoutProps) {
Expand All @@ -10,9 +9,5 @@ export function ReleasesStudioLayout(props: LayoutProps) {
return props.renderDefault(props)
}

return (
<AddonDatasetProvider>
<BundlesProvider>{props.renderDefault(props)}</BundlesProvider>
</AddonDatasetProvider>
)
return <AddonDatasetProvider>{props.renderDefault(props)}</AddonDatasetProvider>
}
2 changes: 1 addition & 1 deletion packages/sanity/src/core/releases/tool/BundleDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {LoadingBlock} from 'sanity'
import {useRouter} from 'sanity/router'

import {Button as StudioButton} from '../../../ui-components'
import {useBundles} from '../../store/bundles/BundlesProvider'
import {useBundles} from '../../store/bundles'
import {BundleMenuButton} from '../components/BundleMenuButton/BundleMenuButton'
import {type ReleasesRouterState} from '../types/router'

Expand Down
2 changes: 1 addition & 1 deletion packages/sanity/src/core/releases/tool/BundlesOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {type MouseEventHandler, useCallback, useEffect, useMemo, useState} from
import {Button as StudioButton} from '../../../ui-components'
import {CreateBundleDialog} from '../../bundles/components/dialog/CreateBundleDialog'
import {LoadingBlock} from '../../components/loadingBlock/LoadingBlock'
import {useBundles} from '../../store/bundles/BundlesProvider'
import {useBundles} from '../../store/bundles'
import {type BundleDocument} from '../../store/bundles/types'
import {BundlesTable} from '../components/BundlesTable/BundlesTable'
import {containsBundles} from '../types/bundle'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {type ReactNode} from 'react'

import {queryByDataUi} from '../../../../../test/setup/customQueries'
import {createTestProvider} from '../../../../../test/testUtils/TestProvider'
import {useBundlesStore} from '../../../store/bundles'
import {BundlesProvider} from '../../../store/bundles/BundlesProvider'
import {useBundles} from '../../../store/bundles'
import {type BundleDocument} from '../../../store/bundles/types'
import {releasesUsEnglishLocaleBundle} from '../../i18n'
import BundlesOverview from '../BundlesOverview'
Expand All @@ -15,8 +14,8 @@ jest.mock('../../../store/bundles/useBundleOperations', () => ({
useBundleOperations: jest.fn().mockReturnValue({deleteBundle: jest.fn()}),
}))

jest.mock('../../../store/bundles/useBundlesStore', () => ({
useBundlesStore: jest.fn(),
jest.mock('../../../store/bundles', () => ({
useBundles: jest.fn(),
}))

jest.mock('sanity/router', () => ({
Expand All @@ -29,23 +28,18 @@ const createWrapper = async () => {
resources: [releasesUsEnglishLocaleBundle],
})
return function Wrapper({children}: {children: ReactNode}) {
return (
<TestProvider>
<BundlesProvider>{children}</BundlesProvider>
</TestProvider>
)
return <TestProvider>{children}</TestProvider>
}
}

const mockUseBundleStore = useBundlesStore as jest.Mock<typeof useBundlesStore>
const mockUseBundleStore = useBundles as jest.Mock<typeof useBundles>

describe('BundlesOverview', () => {
describe('when loading bundles', () => {
beforeEach(async () => {
mockUseBundleStore.mockReturnValue({
data: null,
loading: true,
error: null,
dispatch: jest.fn(),
})

Expand Down Expand Up @@ -78,7 +72,6 @@ describe('BundlesOverview', () => {
mockUseBundleStore.mockReturnValue({
data: [],
loading: false,
error: null,
dispatch: jest.fn(),
})
const wrapper = await createWrapper()
Expand Down Expand Up @@ -116,7 +109,6 @@ describe('BundlesOverview', () => {
mockUseBundleStore.mockReturnValue({
data: bundles,
loading: false,
error: null,
dispatch: jest.fn(),
})
const wrapper = await createWrapper()
Expand Down
27 changes: 26 additions & 1 deletion packages/sanity/src/core/store/_legacy/datastores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import {of} from 'rxjs'

import {useClient, useSchema, useTemplates} from '../../hooks'
import {createDocumentPreviewStore, type DocumentPreviewStore} from '../../preview'
import {useSource, useWorkspace} from '../../studio'
import {useAddonDataset, useSource, useWorkspace} from '../../studio'
import {DEFAULT_STUDIO_CLIENT_OPTIONS} from '../../studioClient'
import {createBundlesStore} from '../bundles/createBundlesStore'
import {type BundlesStore} from '../bundles/types'
import {createKeyValueStore, type KeyValueStore} from '../key-value'
import {useCurrentUser} from '../user'
import {
Expand Down Expand Up @@ -273,3 +275,26 @@ export function useKeyValueStore(): KeyValueStore {
return keyValueStore
}, [client, resourceCache, workspace])
}

/** @internal */
export function useBundlesStore(): BundlesStore {
const resourceCache = useResourceCache()
const workspace = useWorkspace()
const {client} = useAddonDataset()

return useMemo(() => {
const bundlesStore =
resourceCache.get<BundlesStore>({
dependencies: [workspace, client],
namespace: 'BundlesStore',
}) || createBundlesStore({client})

resourceCache.set({
dependencies: [workspace, client],
namespace: 'BundlesStore',
value: bundlesStore,
})

return bundlesStore
}, [client, resourceCache, workspace])
}
60 changes: 0 additions & 60 deletions packages/sanity/src/core/store/bundles/BundlesProvider.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import {Button} from '../../../../ui-components'
import {BundleForm} from '../../../bundles/components/dialog/BundleForm'
import {LoadingBlock} from '../../../components/loadingBlock/LoadingBlock'
import {AddonDatasetProvider} from '../../../studio/addonDataset/AddonDatasetProvider'
import {BundlesProvider, useBundles} from '../BundlesProvider'
import {type BundleDocument} from '../types'
import {useBundleOperations} from '../useBundleOperations'
import {useBundles} from '../useBundles'

const WithAddonDatasetProvider = <P extends object>(Component: ComponentType<P>): React.FC<P> => {
const WrappedComponent: React.FC<P> = (props) => (
<AddonDatasetProvider>
<BundlesProvider>
<Component {...props} />
</BundlesProvider>
<Component {...props} />
</AddonDatasetProvider>
)
WrappedComponent.displayName = `WithAddonDatasetProvider(${Component.displayName || Component.name || 'Component'})`
Expand Down
Loading

0 comments on commit cbf7da1

Please sign in to comment.