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

refactor(sanity): move bundle metadata store to resource cache #7077

Merged
merged 1 commit into from
Jul 8, 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

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
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>
juice49 marked this conversation as resolved.
Show resolved Hide resolved
)
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
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
Loading