ai-paywall-notification.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import Notification from '@/shared/components/notification'
  2. import UpgradeButton from '@/features/ide-react/components/toolbar/upgrade-button'
  3. import { useEditorContext } from '@/shared/context/editor-context'
  4. import { useUserFeaturesContext } from '@/shared/context/user-features-context'
  5. import { useTranslation } from 'react-i18next'
  6. import { formatSecondsToHoursAndMinutes } from '@/shared/utils/time'
  7. import { useFeatureFlag } from '@/shared/context/split-test-context'
  8. import getMeta from '@/utils/meta'
  9. const hasUnlimitedAi = getMeta('ol-hasUnlimitedAi')
  10. type aiFeatureLocations = 'errorAssist' | 'workbench'
  11. function AiPaywallNotification({
  12. isActionBelowContent = false,
  13. featureLocation,
  14. }: {
  15. isActionBelowContent?: boolean
  16. featureLocation: aiFeatureLocations
  17. }) {
  18. const {
  19. hasSuggestionsLeft,
  20. hasTokensLeft,
  21. tokenResetDate,
  22. premiumSuggestionResetDate,
  23. } = useEditorContext()
  24. const { t } = useTranslation()
  25. const features = useUserFeaturesContext()
  26. const inQuotaRollout = useFeatureFlag('plans-2026-phase-1')
  27. const user = getMeta('ol-user')
  28. const isCommons = user.hasInstitutionLicence
  29. const isGroupUser = user.isMemberOfGroupSubscription
  30. if (!getMeta('ol-showAiFeatures')) {
  31. return null
  32. }
  33. // todo: quota clean-up: remove once we are transitioned off aiErrorAssistant naming and replace with just hasUnlimitedAi, also remove null FF check
  34. const hasAddOn = hasUnlimitedAi || Boolean(features?.aiErrorAssistant)
  35. // error assist only needs usage quota
  36. const canUseErrorAssist = hasSuggestionsLeft
  37. if (canUseErrorAssist && featureLocation === 'errorAssist') {
  38. return null
  39. }
  40. // workbench needs both tokens and usage quota
  41. const canUseWorkbench = hasSuggestionsLeft && hasTokensLeft
  42. if (canUseWorkbench && featureLocation === 'workbench') {
  43. return null
  44. }
  45. const exceededQuotaDates = [
  46. ...(hasSuggestionsLeft ? [] : [premiumSuggestionResetDate]),
  47. ...(hasTokensLeft ? [] : [tokenResetDate]),
  48. ]
  49. const longestResetDate = exceededQuotaDates.reduce((latest, date) =>
  50. date > latest ? date : latest
  51. )
  52. const secondsTillReset =
  53. (longestResetDate.getTime() - new Date().getTime()) / 1000
  54. // if we should have refreshed already remove paywall
  55. if (secondsTillReset <= 0) {
  56. return null
  57. }
  58. // if not in rollout, use old paywalls for free users
  59. if (!inQuotaRollout && !hasAddOn) {
  60. return null
  61. }
  62. if (!inQuotaRollout) {
  63. // if not in rollout, we can still use our fair usage messages
  64. const chattingMessage = t(
  65. 'youve_reached_the_fair_usage_limit_on_your_plan_you_can_start_chatting_again_in_time',
  66. {
  67. time: formatSecondsToHoursAndMinutes(t, secondsTillReset),
  68. }
  69. )
  70. const assistMessage = t('youve_reached_the_fair_usage')
  71. return (
  72. <Notification
  73. type="info"
  74. title={t('usage_limit_reached')}
  75. content={
  76. featureLocation === 'workbench' ? chattingMessage : assistMessage
  77. }
  78. isDismissible={false}
  79. customIcon={null}
  80. className="ai-paywall-notification"
  81. />
  82. )
  83. }
  84. if (hasAddOn) {
  85. return (
  86. <FairUseLimit
  87. secondsTillReset={secondsTillReset}
  88. featureLocation={featureLocation}
  89. />
  90. )
  91. }
  92. if (isGroupUser) {
  93. return (
  94. <GroupsPaywall
  95. secondsTillReset={secondsTillReset}
  96. featureLocation={featureLocation}
  97. />
  98. )
  99. }
  100. if (isCommons) {
  101. return (
  102. <CommonsPaywall
  103. secondsTillReset={secondsTillReset}
  104. featureLocation={featureLocation}
  105. />
  106. )
  107. }
  108. const message = t('upgrade_for_unlimited_access_to_ai', {
  109. time: formatSecondsToHoursAndMinutes(t, secondsTillReset),
  110. })
  111. return (
  112. <>
  113. <Notification
  114. type="info"
  115. title={t('youve_hit_your_daily_ai_limit')}
  116. content={message}
  117. isDismissible={false}
  118. customIcon={null}
  119. isActionBelowContent={isActionBelowContent}
  120. action={
  121. <UpgradeButton
  122. className="px-2.5 py-2"
  123. referrer="ai"
  124. source={featureLocation}
  125. />
  126. }
  127. className="ai-upgrade-paywall-btn ai-paywall-notification"
  128. />
  129. </>
  130. )
  131. }
  132. function GroupsPaywall({
  133. secondsTillReset,
  134. featureLocation,
  135. }: {
  136. secondsTillReset: number
  137. featureLocation: aiFeatureLocations
  138. }) {
  139. const { t } = useTranslation()
  140. const message = t('your_limit_will_reset_in_time_or_speak_to_admin', {
  141. time: formatSecondsToHoursAndMinutes(t, secondsTillReset),
  142. })
  143. const title =
  144. featureLocation === 'workbench'
  145. ? t('youve_reached_your_daily_ai_limit')
  146. : t('youve_hit_your_daily_ai_limit')
  147. return (
  148. <>
  149. <Notification
  150. type="info"
  151. title={title}
  152. content={message}
  153. isDismissible={false}
  154. customIcon={null}
  155. className="ai-paywall-notification"
  156. />
  157. </>
  158. )
  159. }
  160. function CommonsPaywall({
  161. secondsTillReset,
  162. featureLocation,
  163. }: {
  164. secondsTillReset: number
  165. featureLocation: aiFeatureLocations
  166. }) {
  167. const { t } = useTranslation()
  168. // workbench isnt available on commons plans, so dont show it here and let upgrade-notification handle it
  169. if (featureLocation === 'workbench') {
  170. return null
  171. }
  172. const message = t('this_will_reset_in', {
  173. time: formatSecondsToHoursAndMinutes(t, secondsTillReset),
  174. })
  175. return (
  176. <>
  177. <Notification
  178. type="info"
  179. title={t('youve_reached_your_ai_usage_limit')}
  180. content={message}
  181. isDismissible={false}
  182. customIcon={null}
  183. className="ai-paywall-notification"
  184. />
  185. </>
  186. )
  187. }
  188. function FairUseLimit({
  189. secondsTillReset,
  190. featureLocation,
  191. }: {
  192. secondsTillReset: number
  193. featureLocation: aiFeatureLocations
  194. }) {
  195. const { t } = useTranslation()
  196. const title =
  197. featureLocation === 'workbench'
  198. ? t('usage_limit_reached')
  199. : t('youve_reached_the_fair_usage')
  200. const workbenchMessage = t(
  201. 'youve_reached_the_fair_usage_limit_on_your_plan_you_can_start_chatting_again_in_time',
  202. {
  203. time: formatSecondsToHoursAndMinutes(t, secondsTillReset),
  204. }
  205. )
  206. const assistMessage = t('this_will_reset_in', {
  207. time: formatSecondsToHoursAndMinutes(t, secondsTillReset),
  208. })
  209. const message =
  210. featureLocation === 'workbench' ? workbenchMessage : assistMessage
  211. return (
  212. <>
  213. <Notification
  214. type="info"
  215. title={title}
  216. content={message}
  217. isDismissible={false}
  218. customIcon={null}
  219. className="ai-paywall-notification"
  220. />
  221. </>
  222. )
  223. }
  224. export default AiPaywallNotification