Skip to content

Commit

Permalink
feat: testing multiple experimental perf boosting strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Jul 12, 2024
1 parent 6fad0c5 commit ae03801
Show file tree
Hide file tree
Showing 93 changed files with 14,158 additions and 1,730 deletions.
421 changes: 421 additions & 0 deletions dev/test-studio/components/debugStyledComponents.tsx

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {TranslateIcon} from '@sanity/icons'
import {Box, Button, Card, Checkbox, Flex, Popover, Stack, Text, useClickOutside} from '@sanity/ui'
import {type FormEvent, useCallback, useState} from 'react'
import {type FormEvent, useCallback, useRef, useState} from 'react'
import {type ObjectSchemaType} from 'sanity'

import {type LanguageFilterPluginOptions} from './types'
Expand All @@ -24,8 +24,8 @@ export function LanguageFilterMenuButton(props: LanguageFilterMenuButtonProps) {
const [open, setOpen] = useState(false)
const {selectableLanguages, selectedLanguages, selectAll, selectNone, toggleLanguage} =
usePaneLanguages({options})
const [button, setButton] = useState<HTMLElement | null>(null)
const [popover, setPopover] = useState<HTMLElement | null>(null)
const buttonRef = useRef<HTMLDivElement | null>(null)
const popoverRef = useRef<HTMLDivElement | null>(null)

const handleToggleAll = useCallback(
(event: FormEvent<HTMLInputElement>) => {
Expand All @@ -42,9 +42,10 @@ export function LanguageFilterMenuButton(props: LanguageFilterMenuButtonProps) {

const handleClick = useCallback(() => setOpen((o) => !o), [])

const handleClickOutside = useCallback(() => setOpen(false), [])

useClickOutside(handleClickOutside, [button, popover])
useClickOutside(
() => setOpen(false),
() => [buttonRef.current, popoverRef.current],
)

const allSelected = selectedLanguages.length === selectableLanguages.length

Expand Down Expand Up @@ -96,12 +97,12 @@ export function LanguageFilterMenuButton(props: LanguageFilterMenuButtonProps) {
)

return (
<Popover constrainSize content={content} open={open} portal ref={setPopover}>
<Popover constrainSize content={content} open={open} portal ref={popoverRef}>
<Button
icon={TranslateIcon}
mode="bleed"
onClick={handleClick}
ref={setButton}
ref={buttonRef}
selected={open}
/>
</Popover>
Expand Down
2 changes: 2 additions & 0 deletions dev/test-studio/sanity.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {imageHotspotArrayPlugin} from 'sanity-plugin-hotspot-array'
import {muxInput} from 'sanity-plugin-mux-input'

import {imageAssetSource} from './assetSources'
import {debugStyledComponents} from './components/debugStyledComponents'
import {
Annotation,
Block,
Expand Down Expand Up @@ -102,6 +103,7 @@ const sharedSettings = definePlugin({
badges: (prev, context) => (context.schemaType === 'author' ? [CustomBadge, ...prev] : prev),
},
plugins: [
debugStyledComponents(),
structureTool({
icon: BookIcon,
structure,
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"check:deps": "pnpm --recursive --parallel exec depcheck",
"check:format": "prettier . --check",
"check:lint": "turbo run lint --continue -- --quiet",
"check:react-compiler": "eslint --no-inline-config --no-eslintrc --ext .cjs,.mjs,.js,.jsx,.ts,.tsx --parser @typescript-eslint/parser --plugin react-compiler --rule 'react-compiler/react-compiler: [warn]' --ignore-path .eslintignore.react-compiler --max-warnings 31 .",
"check:react-compiler": "eslint --no-inline-config --no-eslintrc --ext .cjs,.mjs,.js,.jsx,.ts,.tsx --parser @typescript-eslint/parser --plugin react-compiler --rule 'react-compiler/react-compiler: [warn]' --ignore-path .eslintignore.react-compiler --max-warnings 28 .",
"check:test": "run-s test -- --silent",
"check:types": "tsc && turbo run check:types --filter='./packages/*' --filter='./packages/@sanity/*'",
"chore:format:fix": "prettier --cache --write .",
Expand Down Expand Up @@ -182,8 +182,11 @@
},
"overrides": {
"@npmcli/arborist": "^7.5.4",
"@sanity/ui": "2.6.7-canary.0",
"@typescript-eslint/eslint-plugin": "$@typescript-eslint/eslint-plugin",
"@typescript-eslint/parser": "$@typescript-eslint/parser"
"@typescript-eslint/parser": "$@typescript-eslint/parser",
"styled-components": "npm:[email protected]",
"framer-motion": "11.3.0"
}
},
"isSanityMonorepo": true
Expand Down
12 changes: 11 additions & 1 deletion packages/@repo/package.bundle/src/package.bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {version} from '../package.json'
export const defaultConfig: UserConfig = {
appType: 'custom',
define: {
'__DEV__': 'false',
'process.env.PKG_VERSION': JSON.stringify(version),
'process.env.NODE_ENV': '"production"',
'process.env': {},
Expand All @@ -19,7 +20,13 @@ export const defaultConfig: UserConfig = {
formats: ['es'],
},
rollupOptions: {
external: ['react', /^react-dom/, 'react/jsx-runtime', 'styled-components'],
external: [
'react',
/^react-dom/,
'react/jsx-runtime',
'styled-components',
'./checkoutPairWorker.ts',
],
output: {
exports: 'named',
dir: 'dist',
Expand All @@ -30,4 +37,7 @@ export const defaultConfig: UserConfig = {
},
},
},
worker: {
format: 'es',
},
}
3 changes: 3 additions & 0 deletions packages/@repo/package.config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"types": "./src/package.config.ts",
"scripts": {
"check:types": "tsc --noEmit --skipLibCheck src/package.config.ts"
},
"dependencies": {
"@web/rollup-plugin-import-meta-assets": "2.2.1"
}
}
14 changes: 14 additions & 0 deletions packages/@repo/package.config/src/package.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {defineConfig} from '@sanity/pkg-utils'
// @ts-expect-error -- missing types
import {importMetaAssets} from '@web/rollup-plugin-import-meta-assets'

export default defineConfig({
define: {
Expand Down Expand Up @@ -29,6 +31,18 @@ export default defineConfig({
legacyExports: true,
rollup: {
optimizeLodash: true,
plugins: ([t1, t2, t3, t4, t5, t6, ...plugins]) => [
t1,
t2,
t3,
t4,
t5,
t6,
importMetaAssets({
include: ['**/checkoutPair.mjs', '**/checkoutPair.ts'],
}),
...plugins,
],
},
tsconfig: 'tsconfig.lib.json',
strictOptions: {
Expand Down
16 changes: 9 additions & 7 deletions packages/@sanity/vision/src/components/PerspectivePopover.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import {HelpCircleIcon} from '@sanity/icons'
import {Badge, Button, Card, Inline, Popover, Stack, Text, useClickOutside} from '@sanity/ui'
import {useCallback, useState} from 'react'
import {useCallback, useRef, useState} from 'react'
import {useTranslation} from 'sanity'

import {visionLocaleNamespace} from '../i18n'
import {PerspectivePopoverContent, PerspectivePopoverLink} from './PerspectivePopover.styled'

export function PerspectivePopover() {
const [open, setOpen] = useState(false)
const [buttonEl, setButtonEl] = useState<HTMLElement | null>(null)
const [popoverEl, setPopoverEl] = useState<HTMLElement | null>(null)
const buttonRef = useRef<HTMLButtonElement | null>(null)
const popoverRef = useRef<HTMLDivElement | null>(null)

const handleClick = useCallback(() => setOpen((o) => !o), [])
const handleClickOutside = useCallback(() => setOpen(false), [])

const {t} = useTranslation(visionLocaleNamespace)

useClickOutside(handleClickOutside, [buttonEl, popoverEl])
useClickOutside(
() => setOpen(false),
() => [buttonRef.current, popoverRef.current],
)

return (
<Popover
Expand Down Expand Up @@ -45,7 +47,7 @@ export function PerspectivePopover() {
placement="bottom-start"
portal
padding={3}
ref={setPopoverEl}
ref={popoverRef}
open={open}
>
<Button
Expand All @@ -54,7 +56,7 @@ export function PerspectivePopover() {
padding={2}
tone="primary"
fontSize={1}
ref={setButtonEl}
ref={buttonRef}
onClick={handleClick}
selected={open}
/>
Expand Down
1 change: 1 addition & 0 deletions packages/sanity/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ presentation.js
router.js
structure.js
/migrate/*
/web-workers/*
3 changes: 3 additions & 0 deletions packages/sanity/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@
/migrate.js
/_internal.js
/cli.js

/web-workers/**/*.map
/web-workers/**/*.esm.js
1 change: 1 addition & 0 deletions packages/sanity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
"scripts": {
"build": "pkg-utils build --strict --check --clean",
"build:bundle": "vite build --config package.bundle.ts",
"build:web-workers": "vite build --config package.worker.ts",
"check:types": "tsc --project tsconfig.lib.json",
"clean": "rimraf _internal.js _singletons.js cli.js desk.js migrate.js presentation.js router.js structure.js lib",
"coverage": "jest --coverage",
Expand Down
32 changes: 32 additions & 0 deletions packages/sanity/package.worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {defineConfig, mergeConfig} from 'vite'

import {defaultConfig} from '../@repo/package.bundle/src/package.bundle'

export default defineConfig(() => {
return mergeConfig(defaultConfig, {
define: {
'window.process': 'undefined',
'window.location': 'location',
'window.console': 'console',
'window.addEventListener': 'addEventListener',
},
build: {
lib: {
entry: './src/core/store/_legacy/document/document-pair/checkoutPairWorker.ts',
fileName: 'checkoutPair',
},
rollupOptions: {
external: [],
output: {
exports: 'none',
dir: 'web-workers',
format: 'iife',
inlineDynamicImports: true,
},
},
},
worker: {
format: 'iife',
},
})
})
3 changes: 3 additions & 0 deletions packages/sanity/src/_internal/cli/server/getViteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ export async function getViteConfig(options: ViteOptions): Promise<InlineConfig>
'process.env.MODE': JSON.stringify(mode),
...getStudioEnvironmentVariables({prefix: 'process.env.', jsonEncode: true}),
},
worker: {
format: 'es',
},
}

if (mode === 'production') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export function CommentsListItemLayout(props: CommentsListItemLayoutProps) {

const [value, setValue] = useState<CommentMessage>(message)
const [isEditing, setIsEditing] = useState<boolean>(false)
const [rootElement, setRootElement] = useState<HTMLDivElement | null>(null)
const rootElementRef = useRef<HTMLDivElement | null>(null)
const startMessage = useRef<CommentMessage>(message)
const [menuOpen, setMenuOpen] = useState<boolean>(false)

Expand Down Expand Up @@ -315,12 +315,6 @@ export function CommentsListItemLayout(props: CommentsListItemLayoutProps) {

const handleCloseMenu = useCallback(() => setMenuOpen(false), [])

const handleClickOutside = useCallback(() => {
if (!hasChanges) {
cancelEdit()
}
}, [cancelEdit, hasChanges])

const handleRootKeyDown = useCallback(
(event: React.KeyboardEvent<HTMLDivElement>) => {
if (event.key === 'Escape' && !hasChanges) {
Expand All @@ -332,7 +326,14 @@ export function CommentsListItemLayout(props: CommentsListItemLayoutProps) {

useDidUpdate(isEditing, handleCloseMenu)

useClickOutside(handleClickOutside, [rootElement])
useClickOutside(
() => {
if (!hasChanges) {
cancelEdit()
}
},
() => [rootElementRef.current],
)

const name = user?.displayName ? (
<Text size={1} weight="medium" textOverflow="ellipsis" title={user.displayName}>
Expand All @@ -347,7 +348,7 @@ export function CommentsListItemLayout(props: CommentsListItemLayoutProps) {
data-menu-open={menuOpen ? 'true' : 'false'}
data-testid="comments-list-item-layout"
onKeyDown={handleRootKeyDown}
ref={setRootElement}
ref={rootElementRef}
space={4}
>
<InnerStack space={1} data-muted={displayError}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {useClickOutside} from '@sanity/ui'
// eslint-disable-next-line camelcase
import {getTheme_v2} from '@sanity/ui/theme'
import {isEqual} from 'lodash'
import {type KeyboardEvent, useCallback, useEffect, useMemo, useRef, useState} from 'react'
import {type KeyboardEvent, useCallback, useEffect, useMemo, useRef} from 'react'
import {css, styled} from 'styled-components'

import {Popover, type PopoverProps} from '../../../../../ui-components'
Expand Down Expand Up @@ -84,7 +84,7 @@ export function Editable(props: EditableProps) {
placeholder = t('compose.create-comment-placeholder'),
renderBlock,
} = props
const [popoverElement, setPopoverElement] = useState<HTMLDivElement | null>(null)
const popoverRef = useRef<HTMLDivElement | null>(null)
const rootElementRef = useRef<HTMLDivElement | null>(null)
const editableRef = useRef<HTMLDivElement | null>(null)
const mentionsMenuRef = useRef<MentionsMenuHandle | null>(null)
Expand Down Expand Up @@ -112,13 +112,14 @@ export function Editable(props: EditableProps) {
[placeholder],
)

const handleClickOutside = useCallback(() => {
if (mentionsMenuOpen) {
closeMentions()
}
}, [closeMentions, mentionsMenuOpen])

useClickOutside(handleClickOutside, [popoverElement])
useClickOutside(
() => {
if (mentionsMenuOpen) {
closeMentions()
}
},
() => [popoverRef.current],
)

const handleKeyDown = useCallback(
(event: KeyboardEvent) => {
Expand Down Expand Up @@ -219,7 +220,7 @@ export function Editable(props: EditableProps) {
fallbackPlacements={POPOVER_FALLBACK_PLACEMENTS}
open={mentionsMenuOpen}
placement="bottom"
ref={setPopoverElement}
ref={popoverRef}
referenceElement={cursorElement}
/>

Expand Down
Loading

0 comments on commit ae03801

Please sign in to comment.