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(COREL): refactor version context & renaming #7071

Merged
merged 19 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
8291ec2
feat(sanity): update types, add icon and hue picker to bundles, updat…
RitaDias Jul 2, 2024
510cecb
feat(sanity): add date picker to bundleform
RitaDias Jul 3, 2024
9431645
refactor(sanity): update publishAt fields in menus
RitaDias Jul 3, 2024
5f6688e
chore(sanity): update LATEST type to Partial
RitaDias Jul 3, 2024
0912b1f
refactor(sanity): make single bundle menu + clean up
RitaDias Jul 3, 2024
28f3e24
chore(sanity): remove warnings comments
RitaDias Jul 4, 2024
e317489
refactor(sanity): move version provider to its own file + organise + …
RitaDias Jul 4, 2024
a22ae8c
refactor(sanity): remove provider + context, move logic to hook
RitaDias Jul 4, 2024
9ffad8a
chore(sanity): remove comments + change name from setCurrentVersion t…
RitaDias Jul 4, 2024
2ad93d7
chore(sanity): rename useVersion to useBundle
RitaDias Jul 4, 2024
a7b4760
chore(sanity): update comments
RitaDias Jul 4, 2024
f4d6c4b
chore(sanity): Rename currentVersion
RitaDias Jul 4, 2024
2214147
refactor(sanity): rename currentBudnle and setGlobalBundle
RitaDias Jul 4, 2024
aebb678
chore(sanity): rename versions to bundles in core directory
RitaDias Jul 4, 2024
7141050
chore(sanity): rename to GlobalPerspectiveMenu and move to navbar dir…
RitaDias Jul 4, 2024
bda07b9
chore(sanity): rename to DocumentPerspectiveMenu and move to navbar d…
RitaDias Jul 4, 2024
62ec3e4
chore(sanity): remove isDraft from usePerspective, add new util
RitaDias Jul 5, 2024
8916d19
refactor(sanity): remove isDraft
RitaDias Jul 5, 2024
2c9eb11
refactor(sanity): use current global in the bundle badge
RitaDias Jul 5, 2024
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
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ const config = {

// Ignore i18n in releases files. TODO: Remove this before moving releases to GA.
{
files: ['**/*/core/releases/**/*', '**/*/core/versions/**/*'],
files: ['**/*/core/releases/**/*', '**/*/core/bundles/**/*'],
rules: {
'i18next/no-literal-string': 'off',
'@sanity/i18n/no-attribute-string-literals': 'off',
Expand Down
62 changes: 0 additions & 62 deletions packages/sanity/src/_singletons/core/form/VersionContext.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import {CheckmarkIcon} from '@sanity/icons'
import {Box, Flex, Menu, MenuButton, MenuDivider, MenuItem, Spinner, Text} from '@sanity/ui'
import {type ReactElement, useCallback, useContext} from 'react'
import {type ReactElement, useCallback} from 'react'
import {RelativeTime} from 'sanity'
import {styled} from 'styled-components'

import {
VersionContext,
type VersionContextValue,
} from '../../../_singletons/core/form/VersionContext'
import {type BundleDocument} from '../../store/bundles/types'
import {usePerspective} from '../hooks/usePerspective'
import {LATEST} from '../util/const'
import {isDraftOrPublished} from '../util/dummyGetters'
import {BundleBadge} from './BundleBadge'
Expand All @@ -33,16 +30,15 @@ export function BundleMenu(props: BundleListProps): JSX.Element {
const {bundles, loading, actions, button} = props
const hasBundles = bundles && bundles.filter((b) => !isDraftOrPublished(b.name)).length > 0

// TODO MAKE SURE THIS IS HOW WE WANT TO DO THIS
const {currentVersion, setCurrentVersion, isDraft} =
useContext<VersionContextValue>(VersionContext)
const {currentGlobalBundle, setPerspective} = usePerspective()

// FIXME REPLACE WHEN WE HAVE REAL DATA
const handleBundleChange = useCallback(
(bundle: Partial<BundleDocument>) => () => {
setCurrentVersion(bundle)
if (bundle.name) {
setPerspective(bundle.name)
}
},
[setCurrentVersion],
[setPerspective],
)

return (
Expand All @@ -59,7 +55,9 @@ export function BundleMenu(props: BundleListProps): JSX.Element {
) : (
<>
<MenuItem
iconRight={isDraft ? <CheckmarkIcon /> : undefined}
iconRight={
currentGlobalBundle.name === LATEST.name ? <CheckmarkIcon /> : undefined
}
onClick={handleBundleChange(LATEST)}
pressed={false}
text={LATEST.title}
Expand Down Expand Up @@ -100,7 +98,7 @@ export function BundleMenu(props: BundleListProps): JSX.Element {
<Box padding={2}>
<Text size={1}>
<CheckmarkIcon
style={{opacity: currentVersion.name === b.name ? 1 : 0}}
style={{opacity: currentGlobalBundle.name === b.name ? 1 : 0}}
/>
</Text>
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ export function BundleForm(props: {
</Text>
</Card>
)}

{/* TODO ADD CHECK FOR EXISTING NAMES AND AVOID DUPLICATES */}
<Text size={1} weight="medium">
{/* localize text */}
Title
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import {ArrowRightIcon} from '@sanity/icons'
import {Box, Button, Dialog, Flex} from '@sanity/ui'
import {type FormEvent, useCallback, useContext, useState} from 'react'
import {type FormEvent, useCallback, useState} from 'react'

import {
VersionContext,
type VersionContextValue,
} from '../../../../_singletons/core/form/VersionContext'
import {type BundleDocument} from '../../../store/bundles/types'
import {useBundleOperations} from '../../../store/bundles/useBundleOperations'
import {usePerspective} from '../../hooks/usePerspective'
import {isDraftOrPublished} from '../../util/dummyGetters'
import {BundleForm} from './BundleForm'

Expand All @@ -30,24 +27,26 @@ export function CreateBundleDialog(props: CreateBundleDialogProps): JSX.Element
const [isCreating, setIsCreating] = useState(false)

// TODO MAKE SURE THIS IS HOW WE WANT TO DO THIS
const {setCurrentVersion} = useContext<VersionContextValue>(VersionContext)
const {setPerspective} = usePerspective()

const handleOnSubmit = useCallback(
async (event: FormEvent<HTMLFormElement>) => {
try {
event.preventDefault()
setIsCreating(true)
await createBundle(value)
setValue(value)
} catch (err) {
console.error(err)
} finally {
setIsCreating(false)
setCurrentVersion(value)
onCreate()
if (value.name) {
try {
event.preventDefault()
setIsCreating(true)
await createBundle(value)
setValue(value)
} catch (err) {
console.error(err)
} finally {
setIsCreating(false)
setPerspective(value.name)
onCreate()
}
}
},
[createBundle, value, setCurrentVersion, onCreate],
[createBundle, value, setPerspective, onCreate],
)

const handleOnChange = useCallback((changedValue: Partial<BundleDocument>) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import {type BundleDocument} from '../../../store/bundles/types'
import {getAllVersionsOfDocument, versionDocumentExists} from '../../util/dummyGetters'

interface BundleActionsProps {
currentVersion: BundleDocument
currentGlobalBundle: Partial<BundleDocument>
documentId: string
documentType: string
}

export function BundleActions(props: BundleActionsProps): JSX.Element {
const {currentVersion, documentId, documentType} = props
const {name, title} = currentVersion
const {currentGlobalBundle, documentId, documentType} = props
const {name, title} = currentGlobalBundle
const {data: bundles, loading} = useBundles()
const documentStore = useDocumentStore()

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

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

/**
* @internal
*/
export interface VersionContextValue {
/* Return the current global bundle */
currentGlobalBundle: Partial<BundleDocument>
/* Change the perspective in the studio based on the perspective name */
setPerspective: (name: string) => void
}

export function usePerspective(): VersionContextValue {
const router = useRouter()
const {data: bundles} = useBundlesStore()

const setPerspective = (name: string | undefined) => {
if (name === 'drafts') {
router.navigateStickyParam('perspective', '')
} else {
router.navigateStickyParam('perspective', `bundle.${name}`)
}
}
const selectedBundle =
router.stickyParams?.perspective && bundles
? bundles.find((bundle: Partial<BundleDocument>) => {
return (
`bundle.${bundle.name}`.toLocaleLowerCase() ===
router.stickyParams.perspective?.toLocaleLowerCase()
)
})
: LATEST

const currentGlobalBundle = selectedBundle || LATEST

return {
setPerspective,
currentGlobalBundle,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {Box, Card, Flex, Stack, Text} from '@sanity/ui'
import {RelativeTime} from 'sanity'
import {useRouter} from 'sanity/router'

import {BundleBadge} from '../../../bundles/components/BundleBadge'
import {type BundleDocument} from '../../../store/bundles/types'
import {BundleBadge} from '../../../versions/components/BundleBadge'
import {BundleMenuButton} from '../BundleMenuButton/BundleMenuButton'

type Props = {
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 @@ -4,10 +4,10 @@ import {isBefore} from 'date-fns'
import {type MouseEventHandler, useCallback, useMemo, useState} from 'react'

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 {type BundleDocument} from '../../store/bundles/types'
import {CreateBundleDialog} from '../../versions/components/dialog/CreateBundleDialog'
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 @@ -2,9 +2,9 @@ import {Card, Flex, Stack, Text} from '@sanity/ui'
import {type ComponentType, type FormEvent, useCallback, useState} from 'react'

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 {BundleForm} from '../../../versions/components/dialog/BundleForm'
import {BundlesProvider, useBundles} from '../BundlesProvider'
import {type BundleDocument} from '../types'
import {useBundleOperations} from '../useBundleOperations'
Expand Down
14 changes: 4 additions & 10 deletions packages/sanity/src/core/studio/StudioProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import javascript from 'refractor/lang/javascript.js'
import json from 'refractor/lang/json.js'
import jsx from 'refractor/lang/jsx.js'
import typescript from 'refractor/lang/typescript.js'
import {AddonDatasetProvider} from 'sanity'

import {VersionProvider} from '../../_singletons/core/form/VersionContext'
import {LoadingBlock} from '../components/loadingBlock'
import {ErrorLogger} from '../error/ErrorLogger'
import {errorReporter} from '../error/errorReporter'
Expand Down Expand Up @@ -71,14 +69,10 @@ export function StudioProvider({
<WorkspaceLoader LoadingComponent={LoadingBlock} ConfigErrorsComponent={ConfigErrorsScreen}>
<StudioTelemetryProvider config={config}>
<LocaleProvider>
<AddonDatasetProvider>
<VersionProvider>
<PackageVersionStatusProvider>
<EnableErrorReporting />
<ResourceCacheProvider>{children}</ResourceCacheProvider>
</PackageVersionStatusProvider>
</VersionProvider>
</AddonDatasetProvider>
<PackageVersionStatusProvider>
<EnableErrorReporting />
<ResourceCacheProvider>{children}</ResourceCacheProvider>
</PackageVersionStatusProvider>
</LocaleProvider>
</StudioTelemetryProvider>
</WorkspaceLoader>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {Button, TooltipDelayGroupProvider} from '../../../../ui-components'
import {type NavbarProps} from '../../../config/studio/types'
import {isDev} from '../../../environment'
import {useTranslation} from '../../../i18n'
import {GlobalBundleMenu} from '../../../versions/components/GlobalBundleMenu'
import {useToolMenuComponent} from '../../studio-components-hooks'
import {useWorkspace} from '../../workspace'
import {ConfigIssuesButton} from './configIssues/ConfigIssuesButton'
Expand All @@ -28,6 +27,7 @@ import {FreeTrialProvider} from './free-trial/FreeTrialProvider'
import {HomeButton} from './home/HomeButton'
import {NavDrawer} from './navDrawer'
import {NewDocumentButton, useNewDocumentOptions} from './new-document'
import {GlobalPerspectiveMenu} from './perspective/GlobalPerspectiveMenu'
import {PresenceMenu} from './presence'
import {ResourcesButton} from './resources/ResourcesButton'
import {SearchButton, SearchDialog} from './search'
Expand Down Expand Up @@ -215,7 +215,7 @@ export function StudioNavbar(props: Omit<NavbarProps, 'renderDefault'>) {
</Flex>
{/* Versions button */}
<Flex gap={2}>
<GlobalBundleMenu />
<GlobalPerspectiveMenu />
{/* New document button */}
<NewDocumentButton
{...newDocumentOptions}
Expand Down
Loading
Loading