Unblock React Compiler for 23 components across ten small causes (#11542)
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,14 +1,20 @@
|
||||
import {useEffect, useState} from 'react'
|
||||
import type tldts from 'tldts'
|
||||
|
||||
/**
|
||||
* Module scope because React Compiler cannot lower an `import()` expression
|
||||
* inside a component or hook body.
|
||||
*/
|
||||
function loadTLDs(): Promise<typeof tldts> {
|
||||
// @ts-expect-error - valid path
|
||||
return import('tldts/dist/index.cjs.min.js')
|
||||
}
|
||||
|
||||
export function useTLDs() {
|
||||
const [tlds, setTlds] = useState<typeof tldts>()
|
||||
|
||||
useEffect(() => {
|
||||
// @ts-expect-error - valid path
|
||||
import('tldts/dist/index.cjs.min.js').then(tlds => {
|
||||
setTlds(tlds)
|
||||
})
|
||||
loadTLDs().then(setTlds)
|
||||
}, [])
|
||||
|
||||
return tlds
|
||||
|
||||
@@ -29,6 +29,16 @@ const E_SAME_AS_SOURCE_LANGUAGE =
|
||||
const E_EMPTY_RESULT = 'Translation result is empty.'
|
||||
const E_INVALID_SOURCE_LANGUAGE = 'Invalid source language'
|
||||
|
||||
/**
|
||||
* Returns a copy of `obj` without `key`. A computed property in a destructuring
|
||||
* pattern is syntax React Compiler cannot lower, so this stays out of the hook.
|
||||
*/
|
||||
function omitKey<T extends Record<string, unknown>>(obj: T, key: string): T {
|
||||
const next = {...obj}
|
||||
delete next[key]
|
||||
return next
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts on-device translation via @bsky.app/expo-translate-text.
|
||||
* Uses a lazy import to avoid crashing if the native module isn't linked into
|
||||
@@ -199,8 +209,7 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
|
||||
setRefCounts(prev => {
|
||||
const newCount = (prev[key] ?? 1) - 1
|
||||
if (newCount <= 0) {
|
||||
const {[key]: _, ...rest} = prev
|
||||
return rest
|
||||
return omitKey(prev, key)
|
||||
}
|
||||
return {...prev, [key]: newCount}
|
||||
})
|
||||
|
||||
@@ -6,7 +6,9 @@ let emojis: Awaited<ReturnType<typeof getEmojis>> | null = null
|
||||
|
||||
export function useGetEmojis() {
|
||||
return useCallback(async () => {
|
||||
emojis ??= await getEmojis()
|
||||
if (emojis == null) {
|
||||
emojis = await getEmojis()
|
||||
}
|
||||
return emojis
|
||||
}, [])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user