mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-12 21:23:45 +01:00
* feat: cleaning up ai quota leftovers fix: removing more straggler invos * fix: featureUpdater still need analyticsId in the user projection * feat: updating cypress tests to also check for fair usage text GitOrigin-RevId: 09c1167106c144084c111beea56867f6aa4a0906
73 lines
1.8 KiB
TypeScript
73 lines
1.8 KiB
TypeScript
import {
|
|
createContext,
|
|
FC,
|
|
useCallback,
|
|
useContext,
|
|
useEffect,
|
|
useState,
|
|
} from 'react'
|
|
import { User } from '../../../../types/user'
|
|
import { useUserContext } from './user-context'
|
|
import { useReceiveUser } from '../hooks/user-channel/use-receive-user'
|
|
import { getJSON } from '@/infrastructure/fetch-json'
|
|
import { useEditorContext } from './editor-context'
|
|
import getMeta from '@/utils/meta'
|
|
|
|
export const UserFeaturesContext = createContext<User['features']>(undefined)
|
|
|
|
const hasUnlimitedAi = getMeta('ol-hasUnlimitedAi')
|
|
|
|
export const UserFeaturesProvider: FC<React.PropsWithChildren> = ({
|
|
children,
|
|
}) => {
|
|
const user = useUserContext()
|
|
const { writefullInstance } = useEditorContext()
|
|
const [features, setFeatures] = useState(user.features || {})
|
|
|
|
useReceiveUser(
|
|
useCallback(data => {
|
|
if (data?.features) {
|
|
setFeatures(data.features)
|
|
}
|
|
}, [])
|
|
)
|
|
|
|
useEffect(() => {
|
|
const listener = async ({ isPremium }: { isPremium: boolean }) => {
|
|
if (hasUnlimitedAi === isPremium) {
|
|
// the user is premium on writefull and has the AI assist, no need to refresh the features
|
|
return
|
|
}
|
|
const newFeatures = await getJSON('/user/features')
|
|
setFeatures(newFeatures)
|
|
}
|
|
|
|
writefullInstance?.addEventListener('writefull-login-complete', listener)
|
|
|
|
return () => {
|
|
writefullInstance?.removeEventListener(
|
|
'writefull-login-complete',
|
|
listener
|
|
)
|
|
}
|
|
}, [writefullInstance])
|
|
|
|
return (
|
|
<UserFeaturesContext.Provider value={features}>
|
|
{children}
|
|
</UserFeaturesContext.Provider>
|
|
)
|
|
}
|
|
|
|
export function useUserFeaturesContext() {
|
|
const context = useContext(UserFeaturesContext)
|
|
|
|
if (!context) {
|
|
throw new Error(
|
|
'useUserFeaturesContext is only available inside UserFeaturesContext'
|
|
)
|
|
}
|
|
|
|
return context
|
|
}
|