Bluesky app fork with some witchin' additions 馃挮
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 95 lines 2.3 kB view raw
1import {useCallback, useContext, useMemo} from 'react' 2 3import {useGoogleTranslate} from '#/lib/hooks/useGoogleTranslate' 4import {useAnalytics} from '#/analytics' 5import {Context} from './context' 6import { 7 type ContextType, 8 type TranslationFunctionParams, 9 type TranslationOptions, 10 type TranslationState, 11} from './types' 12 13export * from './types' 14export * from './utils' 15 16const translationState: Record<string, TranslationState> = {} 17const acquireTranslation = (_key: string) => { 18 return () => {} 19} 20const clearTranslation = (_key: string) => {} 21 22/** 23 * Web always opens Google Translate. 24 */ 25export function useTranslate({key}: TranslationOptions) { 26 const context = useContext(Context) 27 if (!context) { 28 throw new Error( 29 'useTranslate must be used within a TranslateOnDeviceProvider', 30 ) 31 } 32 33 // Always call hooks in consistent order 34 const translate = useCallback( 35 async (params: TranslationFunctionParams) => { 36 return context.translate( 37 { 38 ...params, 39 }, 40 { 41 key, 42 forceGoogleTranslate: true, 43 }, 44 ) 45 }, 46 [key, context], 47 ) 48 49 const clearTranslation = useCallback(() => { 50 return context.clearTranslation(key) 51 }, [key, context]) 52 53 return { 54 translationState: context.translationState[key] ?? { 55 status: 'idle' as const, 56 }, 57 translate, 58 clearTranslation, 59 } 60} 61 62export function Provider({children}: React.PropsWithChildren<unknown>) { 63 const ax = useAnalytics() 64 const googleTranslate = useGoogleTranslate() 65 66 const translate = useCallback<ContextType['translate']>( 67 async ({ 68 text, 69 expectedTargetLanguage, 70 expectedSourceLanguage, 71 possibleSourceLanguages, 72 }) => { 73 ax.metric('translate', { 74 os: 'web', 75 possibleSourceLanguages, 76 expectedTargetLanguage, 77 textLength: text.length, 78 googleTranslate: true, 79 }) 80 await googleTranslate( 81 text, 82 expectedTargetLanguage, 83 expectedSourceLanguage, 84 ) 85 }, 86 [ax, googleTranslate], 87 ) 88 89 const ctx = useMemo( 90 () => ({acquireTranslation, clearTranslation, translate, translationState}), 91 [translate], 92 ) 93 94 return <Context.Provider value={ctx}>{children}</Context.Provider> 95}