@automattic/jetpack-ai-client 0.25.6 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (227) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/build/components/ai-icon/index.d.ts +10 -0
  3. package/build/components/ai-icon/index.js +16 -0
  4. package/build/components/ai-image/components/ai-image-modal.d.ts +57 -0
  5. package/build/components/ai-image/components/ai-image-modal.js +92 -0
  6. package/build/components/ai-image/components/carrousel.d.ts +28 -0
  7. package/build/components/ai-image/components/carrousel.js +69 -0
  8. package/build/components/ai-image/components/usage-counter.d.ts +16 -0
  9. package/build/components/ai-image/components/usage-counter.js +29 -0
  10. package/build/components/ai-image/featured-image.d.ts +17 -0
  11. package/build/components/ai-image/featured-image.js +278 -0
  12. package/build/components/ai-image/general-purpose-image.d.ts +23 -0
  13. package/build/components/ai-image/general-purpose-image.js +184 -0
  14. package/build/components/ai-image/hooks/use-ai-image.d.ts +48 -0
  15. package/build/components/ai-image/hooks/use-ai-image.js +219 -0
  16. package/build/components/ai-image/hooks/use-site-type.d.ts +6 -0
  17. package/build/components/ai-image/hooks/use-site-type.js +23 -0
  18. package/build/components/ai-image/index.d.ts +4 -0
  19. package/build/components/ai-image/index.js +4 -0
  20. package/build/components/ai-image/types.d.ts +16 -0
  21. package/build/components/ai-image/types.js +6 -0
  22. package/build/{ai-client/src/components → components}/index.d.ts +4 -0
  23. package/build/{ai-client/src/components → components}/index.js +4 -0
  24. package/build/components/modal/index.d.ts +18 -0
  25. package/build/components/modal/index.js +23 -0
  26. package/build/components/quota-exceeded-message/index.d.ts +13 -0
  27. package/build/components/quota-exceeded-message/index.js +152 -0
  28. package/build/components/quota-exceeded-message/light-nudge.d.ts +11 -0
  29. package/build/components/quota-exceeded-message/light-nudge.js +8 -0
  30. package/build/{ai-client/src/constants.d.ts → constants.d.ts} +3 -0
  31. package/build/{ai-client/src/constants.js → constants.js} +4 -0
  32. package/build/hooks/use-ai-checkout/index.d.ts +13 -0
  33. package/build/hooks/use-ai-checkout/index.js +41 -0
  34. package/build/hooks/use-ai-feature/index.d.ts +33 -0
  35. package/build/hooks/use-ai-feature/index.js +37 -0
  36. package/build/hooks/use-post-content.d.ts +5 -0
  37. package/build/hooks/use-post-content.js +20 -0
  38. package/build/hooks/use-save-to-media-library.d.ts +12 -0
  39. package/build/hooks/use-save-to-media-library.js +74 -0
  40. package/build/{ai-client/src/index.d.ts → index.d.ts} +3 -0
  41. package/build/{ai-client/src/index.js → index.js} +3 -0
  42. package/build/{ai-client/src/logo-generator → logo-generator}/components/upgrade-screen.js +1 -1
  43. package/build/{ai-client/src/logo-generator → logo-generator}/hooks/use-fair-usage-notice-message.js +1 -1
  44. package/package.json +20 -14
  45. package/src/components/ai-icon/index.tsx +39 -0
  46. package/src/components/ai-image/components/ai-image-modal.scss +88 -0
  47. package/src/components/ai-image/components/ai-image-modal.tsx +240 -0
  48. package/src/components/ai-image/components/carrousel.scss +163 -0
  49. package/src/components/ai-image/components/carrousel.tsx +217 -0
  50. package/src/components/ai-image/components/usage-counter.scss +19 -0
  51. package/src/components/ai-image/components/usage-counter.tsx +54 -0
  52. package/src/components/ai-image/featured-image.tsx +439 -0
  53. package/src/components/ai-image/general-purpose-image.tsx +303 -0
  54. package/src/components/ai-image/hooks/use-ai-image.ts +339 -0
  55. package/src/components/ai-image/hooks/use-site-type.ts +26 -0
  56. package/src/components/ai-image/index.ts +10 -0
  57. package/src/components/ai-image/style.scss +95 -0
  58. package/src/components/ai-image/types.ts +19 -0
  59. package/src/components/index.ts +12 -0
  60. package/src/components/modal/index.tsx +70 -0
  61. package/src/components/modal/style.scss +45 -0
  62. package/src/components/quota-exceeded-message/index.tsx +319 -0
  63. package/src/components/quota-exceeded-message/light-nudge.tsx +38 -0
  64. package/src/components/quota-exceeded-message/style.scss +35 -0
  65. package/src/constants.ts +5 -0
  66. package/src/hooks/use-ai-checkout/index.ts +65 -0
  67. package/src/hooks/use-ai-feature/Readme.md +20 -0
  68. package/src/hooks/use-ai-feature/index.ts +62 -0
  69. package/src/hooks/use-post-content.ts +27 -0
  70. package/src/hooks/use-save-to-media-library.ts +100 -0
  71. package/src/index.ts +3 -0
  72. package/src/logo-generator/components/upgrade-screen.tsx +1 -1
  73. package/src/logo-generator/hooks/use-fair-usage-notice-message.tsx +1 -1
  74. package/build/components/tools/jp-redirect/index.d.ts +0 -20
  75. package/build/components/tools/jp-redirect/index.js +0 -50
  76. package/build/components/tools/jp-redirect/types.d.ts +0 -39
  77. package/build/components/tools/jp-redirect/types.js +0 -1
  78. /package/build/{ai-client/src/api-fetch → api-fetch}/index.d.ts +0 -0
  79. /package/build/{ai-client/src/api-fetch → api-fetch}/index.js +0 -0
  80. /package/build/{ai-client/src/ask-question → ask-question}/index.d.ts +0 -0
  81. /package/build/{ai-client/src/ask-question → ask-question}/index.js +0 -0
  82. /package/build/{ai-client/src/ask-question → ask-question}/sync.d.ts +0 -0
  83. /package/build/{ai-client/src/ask-question → ask-question}/sync.js +0 -0
  84. /package/build/{ai-client/src/audio-transcription → audio-transcription}/index.d.ts +0 -0
  85. /package/build/{ai-client/src/audio-transcription → audio-transcription}/index.js +0 -0
  86. /package/build/{ai-client/src/components → components}/ai-control/ai-control.d.ts +0 -0
  87. /package/build/{ai-client/src/components → components}/ai-control/ai-control.js +0 -0
  88. /package/build/{ai-client/src/components → components}/ai-control/block-ai-control.d.ts +0 -0
  89. /package/build/{ai-client/src/components → components}/ai-control/block-ai-control.js +0 -0
  90. /package/build/{ai-client/src/components → components}/ai-control/extension-ai-control.d.ts +0 -0
  91. /package/build/{ai-client/src/components → components}/ai-control/extension-ai-control.js +0 -0
  92. /package/build/{ai-client/src/components → components}/ai-control/index.d.ts +0 -0
  93. /package/build/{ai-client/src/components → components}/ai-control/index.js +0 -0
  94. /package/build/{ai-client/src/components → components}/ai-feedback/index.d.ts +0 -0
  95. /package/build/{ai-client/src/components → components}/ai-feedback/index.js +0 -0
  96. /package/build/{ai-client/src/components → components}/ai-modal-footer/index.d.ts +0 -0
  97. /package/build/{ai-client/src/components → components}/ai-modal-footer/index.js +0 -0
  98. /package/build/{ai-client/src/components → components}/ai-status-indicator/index.d.ts +0 -0
  99. /package/build/{ai-client/src/components → components}/ai-status-indicator/index.js +0 -0
  100. /package/build/{ai-client/src/components → components}/audio-duration-display/index.d.ts +0 -0
  101. /package/build/{ai-client/src/components → components}/audio-duration-display/index.js +0 -0
  102. /package/build/{ai-client/src/components → components}/audio-duration-display/lib/media.d.ts +0 -0
  103. /package/build/{ai-client/src/components → components}/audio-duration-display/lib/media.js +0 -0
  104. /package/build/{ai-client/src/components → components}/message/index.d.ts +0 -0
  105. /package/build/{ai-client/src/components → components}/message/index.js +0 -0
  106. /package/build/{ai-client/src/data-flow → data-flow}/context.d.ts +0 -0
  107. /package/build/{ai-client/src/data-flow → data-flow}/context.js +0 -0
  108. /package/build/{ai-client/src/data-flow → data-flow}/index.d.ts +0 -0
  109. /package/build/{ai-client/src/data-flow → data-flow}/index.js +0 -0
  110. /package/build/{ai-client/src/data-flow → data-flow}/use-ai-context.d.ts +0 -0
  111. /package/build/{ai-client/src/data-flow → data-flow}/use-ai-context.js +0 -0
  112. /package/build/{ai-client/src/data-flow → data-flow}/with-ai-assistant-data.d.ts +0 -0
  113. /package/build/{ai-client/src/data-flow → data-flow}/with-ai-assistant-data.js +0 -0
  114. /package/build/{ai-client/src/hooks → hooks}/use-ai-suggestions/index.d.ts +0 -0
  115. /package/build/{ai-client/src/hooks → hooks}/use-ai-suggestions/index.js +0 -0
  116. /package/build/{ai-client/src/hooks → hooks}/use-audio-transcription/index.d.ts +0 -0
  117. /package/build/{ai-client/src/hooks → hooks}/use-audio-transcription/index.js +0 -0
  118. /package/build/{ai-client/src/hooks → hooks}/use-audio-validation/index.d.ts +0 -0
  119. /package/build/{ai-client/src/hooks → hooks}/use-audio-validation/index.js +0 -0
  120. /package/build/{ai-client/src/hooks → hooks}/use-image-generator/constants.d.ts +0 -0
  121. /package/build/{ai-client/src/hooks → hooks}/use-image-generator/constants.js +0 -0
  122. /package/build/{ai-client/src/hooks → hooks}/use-image-generator/index.d.ts +0 -0
  123. /package/build/{ai-client/src/hooks → hooks}/use-image-generator/index.js +0 -0
  124. /package/build/{ai-client/src/hooks → hooks}/use-media-recording/index.d.ts +0 -0
  125. /package/build/{ai-client/src/hooks → hooks}/use-media-recording/index.js +0 -0
  126. /package/build/{ai-client/src/hooks → hooks}/use-save-to-media-library/index.d.ts +0 -0
  127. /package/build/{ai-client/src/hooks → hooks}/use-save-to-media-library/index.js +0 -0
  128. /package/build/{ai-client/src/hooks → hooks}/use-transcription-post-processing/index.d.ts +0 -0
  129. /package/build/{ai-client/src/hooks → hooks}/use-transcription-post-processing/index.js +0 -0
  130. /package/build/{ai-client/src/icons → icons}/ai-assistant.d.ts +0 -0
  131. /package/build/{ai-client/src/icons → icons}/ai-assistant.js +0 -0
  132. /package/build/{ai-client/src/icons → icons}/error-exclamation.d.ts +0 -0
  133. /package/build/{ai-client/src/icons → icons}/error-exclamation.js +0 -0
  134. /package/build/{ai-client/src/icons → icons}/index.d.ts +0 -0
  135. /package/build/{ai-client/src/icons → icons}/index.js +0 -0
  136. /package/build/{ai-client/src/icons → icons}/mic.d.ts +0 -0
  137. /package/build/{ai-client/src/icons → icons}/mic.js +0 -0
  138. /package/build/{ai-client/src/icons → icons}/origami-plane.d.ts +0 -0
  139. /package/build/{ai-client/src/icons → icons}/origami-plane.js +0 -0
  140. /package/build/{ai-client/src/icons → icons}/player-pause.d.ts +0 -0
  141. /package/build/{ai-client/src/icons → icons}/player-pause.js +0 -0
  142. /package/build/{ai-client/src/icons → icons}/player-play.d.ts +0 -0
  143. /package/build/{ai-client/src/icons → icons}/player-play.js +0 -0
  144. /package/build/{ai-client/src/icons → icons}/player-stop.d.ts +0 -0
  145. /package/build/{ai-client/src/icons → icons}/player-stop.js +0 -0
  146. /package/build/{ai-client/src/icons → icons}/speak-tone.d.ts +0 -0
  147. /package/build/{ai-client/src/icons → icons}/speak-tone.js +0 -0
  148. /package/build/{ai-client/src/jwt → jwt}/index.d.ts +0 -0
  149. /package/build/{ai-client/src/jwt → jwt}/index.js +0 -0
  150. /package/build/{ai-client/src/libs → libs}/index.d.ts +0 -0
  151. /package/build/{ai-client/src/libs → libs}/index.js +0 -0
  152. /package/build/{ai-client/src/libs → libs}/map-action-to-human-text.d.ts +0 -0
  153. /package/build/{ai-client/src/libs → libs}/map-action-to-human-text.js +0 -0
  154. /package/build/{ai-client/src/libs → libs}/markdown/html-to-markdown.d.ts +0 -0
  155. /package/build/{ai-client/src/libs → libs}/markdown/html-to-markdown.js +0 -0
  156. /package/build/{ai-client/src/libs → libs}/markdown/index.d.ts +0 -0
  157. /package/build/{ai-client/src/libs → libs}/markdown/index.js +0 -0
  158. /package/build/{ai-client/src/libs → libs}/markdown/markdown-to-html.d.ts +0 -0
  159. /package/build/{ai-client/src/libs → libs}/markdown/markdown-to-html.js +0 -0
  160. /package/build/{ai-client/src/logo-generator → logo-generator}/assets/icons/ai.d.ts +0 -0
  161. /package/build/{ai-client/src/logo-generator → logo-generator}/assets/icons/ai.js +0 -0
  162. /package/build/{ai-client/src/logo-generator → logo-generator}/assets/icons/check.d.ts +0 -0
  163. /package/build/{ai-client/src/logo-generator → logo-generator}/assets/icons/check.js +0 -0
  164. /package/build/{ai-client/src/logo-generator → logo-generator}/assets/icons/logo.d.ts +0 -0
  165. /package/build/{ai-client/src/logo-generator → logo-generator}/assets/icons/logo.js +0 -0
  166. /package/build/{ai-client/src/logo-generator → logo-generator}/assets/icons/media.d.ts +0 -0
  167. /package/build/{ai-client/src/logo-generator → logo-generator}/assets/icons/media.js +0 -0
  168. /package/build/{ai-client/src/logo-generator → logo-generator}/components/fair-usage-notice.d.ts +0 -0
  169. /package/build/{ai-client/src/logo-generator → logo-generator}/components/fair-usage-notice.js +0 -0
  170. /package/build/{ai-client/src/logo-generator → logo-generator}/components/feature-fetch-failure-screen.d.ts +0 -0
  171. /package/build/{ai-client/src/logo-generator → logo-generator}/components/feature-fetch-failure-screen.js +0 -0
  172. /package/build/{ai-client/src/logo-generator → logo-generator}/components/first-load-screen.d.ts +0 -0
  173. /package/build/{ai-client/src/logo-generator → logo-generator}/components/first-load-screen.js +0 -0
  174. /package/build/{ai-client/src/logo-generator → logo-generator}/components/generator-modal.d.ts +0 -0
  175. /package/build/{ai-client/src/logo-generator → logo-generator}/components/generator-modal.js +0 -0
  176. /package/build/{ai-client/src/logo-generator → logo-generator}/components/history-carousel.d.ts +0 -0
  177. /package/build/{ai-client/src/logo-generator → logo-generator}/components/history-carousel.js +0 -0
  178. /package/build/{ai-client/src/logo-generator → logo-generator}/components/image-loader.d.ts +0 -0
  179. /package/build/{ai-client/src/logo-generator → logo-generator}/components/image-loader.js +0 -0
  180. /package/build/{ai-client/src/logo-generator → logo-generator}/components/logo-presenter.d.ts +0 -0
  181. /package/build/{ai-client/src/logo-generator → logo-generator}/components/logo-presenter.js +0 -0
  182. /package/build/{ai-client/src/logo-generator → logo-generator}/components/prompt.d.ts +0 -0
  183. /package/build/{ai-client/src/logo-generator → logo-generator}/components/prompt.js +0 -0
  184. /package/build/{ai-client/src/logo-generator → logo-generator}/components/upgrade-nudge.d.ts +0 -0
  185. /package/build/{ai-client/src/logo-generator → logo-generator}/components/upgrade-nudge.js +0 -0
  186. /package/build/{ai-client/src/logo-generator → logo-generator}/components/upgrade-screen.d.ts +0 -0
  187. /package/build/{ai-client/src/logo-generator → logo-generator}/components/visit-site-banner.d.ts +0 -0
  188. /package/build/{ai-client/src/logo-generator → logo-generator}/components/visit-site-banner.js +0 -0
  189. /package/build/{ai-client/src/logo-generator → logo-generator}/constants.d.ts +0 -0
  190. /package/build/{ai-client/src/logo-generator → logo-generator}/constants.js +0 -0
  191. /package/build/{ai-client/src/logo-generator → logo-generator}/hooks/use-checkout.d.ts +0 -0
  192. /package/build/{ai-client/src/logo-generator → logo-generator}/hooks/use-checkout.js +0 -0
  193. /package/build/{ai-client/src/logo-generator → logo-generator}/hooks/use-fair-usage-notice-message.d.ts +0 -0
  194. /package/build/{ai-client/src/logo-generator → logo-generator}/hooks/use-logo-generator.d.ts +0 -0
  195. /package/build/{ai-client/src/logo-generator → logo-generator}/hooks/use-logo-generator.js +0 -0
  196. /package/build/{ai-client/src/logo-generator → logo-generator}/hooks/use-request-errors.d.ts +0 -0
  197. /package/build/{ai-client/src/logo-generator → logo-generator}/hooks/use-request-errors.js +0 -0
  198. /package/build/{ai-client/src/logo-generator → logo-generator}/index.d.ts +0 -0
  199. /package/build/{ai-client/src/logo-generator → logo-generator}/index.js +0 -0
  200. /package/build/{ai-client/src/logo-generator → logo-generator}/lib/logo-storage.d.ts +0 -0
  201. /package/build/{ai-client/src/logo-generator → logo-generator}/lib/logo-storage.js +0 -0
  202. /package/build/{ai-client/src/logo-generator → logo-generator}/lib/media-exists.d.ts +0 -0
  203. /package/build/{ai-client/src/logo-generator → logo-generator}/lib/media-exists.js +0 -0
  204. /package/build/{ai-client/src/logo-generator → logo-generator}/lib/set-site-logo.d.ts +0 -0
  205. /package/build/{ai-client/src/logo-generator → logo-generator}/lib/set-site-logo.js +0 -0
  206. /package/build/{ai-client/src/logo-generator → logo-generator}/lib/wpcom-limited-request.d.ts +0 -0
  207. /package/build/{ai-client/src/logo-generator → logo-generator}/lib/wpcom-limited-request.js +0 -0
  208. /package/build/{ai-client/src/logo-generator → logo-generator}/store/actions.d.ts +0 -0
  209. /package/build/{ai-client/src/logo-generator → logo-generator}/store/actions.js +0 -0
  210. /package/build/{ai-client/src/logo-generator → logo-generator}/store/constants.d.ts +0 -0
  211. /package/build/{ai-client/src/logo-generator → logo-generator}/store/constants.js +0 -0
  212. /package/build/{ai-client/src/logo-generator → logo-generator}/store/index.d.ts +0 -0
  213. /package/build/{ai-client/src/logo-generator → logo-generator}/store/index.js +0 -0
  214. /package/build/{ai-client/src/logo-generator → logo-generator}/store/initial-state.d.ts +0 -0
  215. /package/build/{ai-client/src/logo-generator → logo-generator}/store/initial-state.js +0 -0
  216. /package/build/{ai-client/src/logo-generator → logo-generator}/store/reducer.d.ts +0 -0
  217. /package/build/{ai-client/src/logo-generator → logo-generator}/store/reducer.js +0 -0
  218. /package/build/{ai-client/src/logo-generator → logo-generator}/store/selectors.d.ts +0 -0
  219. /package/build/{ai-client/src/logo-generator → logo-generator}/store/selectors.js +0 -0
  220. /package/build/{ai-client/src/logo-generator → logo-generator}/store/types.d.ts +0 -0
  221. /package/build/{ai-client/src/logo-generator → logo-generator}/store/types.js +0 -0
  222. /package/build/{ai-client/src/logo-generator → logo-generator}/types.d.ts +0 -0
  223. /package/build/{ai-client/src/logo-generator → logo-generator}/types.js +0 -0
  224. /package/build/{ai-client/src/suggestions-event-source → suggestions-event-source}/index.d.ts +0 -0
  225. /package/build/{ai-client/src/suggestions-event-source → suggestions-event-source}/index.js +0 -0
  226. /package/build/{ai-client/src/types.d.ts → types.d.ts} +0 -0
  227. /package/build/{ai-client/src/types.js → types.js} +0 -0
@@ -0,0 +1,65 @@
1
+ /*
2
+ * External dependencies
3
+ */
4
+ import getRedirectUrl from '@automattic/jetpack-components/tools/jp-redirect';
5
+ import {
6
+ isAtomicSite,
7
+ isSimpleSite,
8
+ getSiteFragment,
9
+ useAutosaveAndRedirect,
10
+ } from '@automattic/jetpack-shared-extension-utils';
11
+ import useAiFeature from '../use-ai-feature/index.js';
12
+ /*
13
+ * Types
14
+ */
15
+ import type { MouseEvent } from 'react';
16
+
17
+ const getWPComRedirectToURL = () => {
18
+ const searchParams = new URLSearchParams( window.location.search );
19
+ const site = getSiteFragment();
20
+
21
+ if ( isSimpleSite() && searchParams.has( 'post' ) ) {
22
+ // When there is an explicit post, use it as the destination
23
+ return `https://wordpress.com/post/${ site }/${ searchParams.get( 'post' ) }`;
24
+ }
25
+ // When there is no explicit post, or the site is not Simple, use the home page as the destination
26
+ return `https://wordpress.com/home/${ site }`;
27
+ };
28
+
29
+ type UseAICheckoutReturn = {
30
+ checkoutUrl: string;
31
+ autosaveAndRedirect: ( event: MouseEvent< HTMLButtonElement > ) => void;
32
+ isRedirecting: boolean;
33
+ };
34
+
35
+ /**
36
+ * The hook to get properties for AICheckout
37
+ *
38
+ * @return {UseAICheckoutReturn} - Object containing properties for AICheckout.
39
+ */
40
+ export default function useAICheckout(): UseAICheckoutReturn {
41
+ const { nextTier, tierPlansEnabled } = useAiFeature();
42
+
43
+ const wpcomRedirectToURL = getWPComRedirectToURL();
44
+
45
+ const wpcomCheckoutUrl = getRedirectUrl( 'jetpack-ai-yearly-tier-upgrade-nudge', {
46
+ site: getSiteFragment() as string,
47
+ path: tierPlansEnabled ? `jetpack_ai_yearly:-q-${ nextTier?.limit }` : 'jetpack_ai_yearly',
48
+ query: `redirect_to=${ encodeURIComponent( wpcomRedirectToURL ) }`,
49
+ } );
50
+
51
+ const jetpackCheckoutUrl = getRedirectUrl( 'jetpack-ai-upgrade-url-for-jetpack-sites', {
52
+ site: getSiteFragment() as string,
53
+ path: 'jetpack_ai_yearly',
54
+ } );
55
+
56
+ const checkoutUrl = isAtomicSite() || isSimpleSite() ? wpcomCheckoutUrl : jetpackCheckoutUrl;
57
+
58
+ const { autosaveAndRedirect, isRedirecting } = useAutosaveAndRedirect( checkoutUrl );
59
+
60
+ return {
61
+ checkoutUrl,
62
+ autosaveAndRedirect,
63
+ isRedirecting,
64
+ };
65
+ }
@@ -0,0 +1,20 @@
1
+ # useAiFeature()
2
+
3
+ React custom hook that provides valuable data about AI requests for the site.
4
+
5
+ ```es6
6
+ function UpgradePlan() {
7
+ const { hasFeature, count, refresh } = useAiFeature();
8
+ if ( ! hasFeature ) {
9
+ return null;
10
+ }
11
+
12
+ return (
13
+ <div>
14
+ { `You have made ${ count } requests so far.` }
15
+ <Button>Upgrade</Button>
16
+ <Button onClick={ refresh}>Refresh Data!</Button>
17
+ </div>
18
+ );
19
+ }
20
+ ```
@@ -0,0 +1,62 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import {
5
+ PLAN_TYPE_FREE,
6
+ usePlanType as getPlanType,
7
+ } from '@automattic/jetpack-shared-extension-utils';
8
+ import { useDispatch, useSelect } from '@wordpress/data';
9
+ import type { WordPressPlansSelectors } from '@automattic/jetpack-shared-extension-utils/store/wordpress-com';
10
+
11
+ /**
12
+ * Hook to get properties for AiFeature
13
+ * @return {object} - Object containing properties for AiFeature.
14
+ */
15
+ export default function useAiFeature() {
16
+ const { data, loading, requestsLimit, requestsCount } = useSelect( select => {
17
+ const { getAiAssistantFeature, getIsRequestingAiAssistantFeature } = select(
18
+ 'wordpress-com/plans'
19
+ ) as WordPressPlansSelectors;
20
+
21
+ const featureData = getAiAssistantFeature();
22
+
23
+ const {
24
+ currentTier,
25
+ usagePeriod,
26
+ requestsCount: allTimeRequestsCount,
27
+ requestsLimit: freeRequestsLimit,
28
+ } = featureData;
29
+
30
+ const planType = getPlanType( currentTier );
31
+
32
+ const currentTierLimit = currentTier?.limit || freeRequestsLimit;
33
+
34
+ const actualRequestsCount =
35
+ planType === PLAN_TYPE_FREE ? allTimeRequestsCount : usagePeriod?.requestsCount;
36
+ const actualRequestsLimit = planType === PLAN_TYPE_FREE ? freeRequestsLimit : currentTierLimit;
37
+
38
+ return {
39
+ data: featureData,
40
+ loading: getIsRequestingAiAssistantFeature(),
41
+ requestsCount: actualRequestsCount,
42
+ requestsLimit: actualRequestsLimit,
43
+ };
44
+ }, [] );
45
+
46
+ const {
47
+ fetchAiAssistantFeature: loadFeatures,
48
+ increaseAiAssistantRequestsCount: increaseRequestsCount,
49
+ dequeueAiAssistantFeatureAsyncRequest: dequeueAsyncRequest,
50
+ } = useDispatch( 'wordpress-com/plans' );
51
+
52
+ return {
53
+ ...data,
54
+ requestsCount,
55
+ requestsLimit,
56
+ loading,
57
+ error: null, // @todo: handle error at store level
58
+ refresh: loadFeatures,
59
+ increaseRequestsCount,
60
+ dequeueAsyncRequest,
61
+ };
62
+ }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { serialize } from '@wordpress/blocks';
5
+ import { useSelect } from '@wordpress/data';
6
+ /**
7
+ * Types
8
+ */
9
+ import { renderMarkdownFromHTML } from '../libs/markdown/index.js';
10
+ import type * as BlockEditorSelectors from '@wordpress/block-editor/store/selectors.js';
11
+ /**
12
+ * Internal dependencies
13
+ */
14
+
15
+ /*
16
+ * Simple helper to get the post content as markdown
17
+ */
18
+ const usePostContent = () => {
19
+ const blocks = useSelect(
20
+ select => ( select( 'core/block-editor' ) as typeof BlockEditorSelectors ).getBlocks(),
21
+ []
22
+ );
23
+
24
+ return blocks?.length ? renderMarkdownFromHTML( { content: serialize( blocks ) } ) : '';
25
+ };
26
+
27
+ export default usePostContent;
@@ -0,0 +1,100 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { isBlobURL } from '@wordpress/blob';
5
+ import { useSelect } from '@wordpress/data';
6
+ import { useState } from '@wordpress/element';
7
+ import debugFactory from 'debug';
8
+ /**
9
+ * Types
10
+ */
11
+ import type { BlockEditorStore } from '../types.js';
12
+
13
+ const debug = debugFactory( 'jetpack-ai-client:save-to-media-library' );
14
+
15
+ type UseSaveToMediaLibraryReturn = {
16
+ isLoading: boolean;
17
+ saveToMediaLibrary: ( url: string, name: string ) => Promise< { id: string; url: string } >;
18
+ };
19
+
20
+ /**
21
+ * Hook to save data to media library
22
+ *
23
+ * @return {UseSaveToMediaLibraryReturn} - Object containing properties to save data to media library.
24
+ */
25
+ export default function useSaveToMediaLibrary() {
26
+ const [ isLoading, setIsLoading ] = useState( false );
27
+ const { getSettings } = useSelect(
28
+ select => select( 'core/block-editor' ),
29
+ []
30
+ ) as BlockEditorStore[ 'selectors' ];
31
+
32
+ const saveToMediaLibrary = (
33
+ url: string,
34
+ name: string = null
35
+ ): Promise< { id: string; url: string } > => {
36
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
+ const settings = getSettings() as any;
38
+
39
+ return new Promise( ( resolve, reject ) => {
40
+ setIsLoading( true );
41
+
42
+ debug( 'Fetching image from URL' );
43
+
44
+ fetch( url )
45
+ .then( response => {
46
+ debug( 'Transforming response to blob' );
47
+
48
+ response
49
+ .blob()
50
+ .then( ( blob: Blob ) => {
51
+ debug( 'Uploading blob to media library' );
52
+ const filesList = [];
53
+
54
+ if ( name ) {
55
+ filesList.push( new File( [ blob ], name ) );
56
+ } else {
57
+ filesList.push( blob );
58
+ }
59
+
60
+ settings.mediaUpload( {
61
+ allowedTypes: [ 'image' ],
62
+ filesList,
63
+ onFileChange( [ image ] ) {
64
+ if ( isBlobURL( image?.url ) ) {
65
+ return;
66
+ }
67
+
68
+ if ( image ) {
69
+ debug( 'Image uploaded to media library', image );
70
+ resolve( image );
71
+ }
72
+
73
+ setIsLoading( false );
74
+ },
75
+ onError( message ) {
76
+ debug( 'Error uploading image to media library:', message );
77
+ reject( message );
78
+ setIsLoading( false );
79
+ },
80
+ } );
81
+ } )
82
+ .catch( e => {
83
+ debug( 'Error transforming response to blob:', e?.message );
84
+ reject( e?.message );
85
+ setIsLoading( false );
86
+ } );
87
+ } )
88
+ .catch( e => {
89
+ debug( 'Error fetching image from URL:', e?.message );
90
+ reject( e?.message );
91
+ setIsLoading( false );
92
+ } );
93
+ } );
94
+ };
95
+
96
+ return {
97
+ isLoading,
98
+ saveToMediaLibrary,
99
+ };
100
+ }
package/src/index.ts CHANGED
@@ -10,12 +10,15 @@ export { default as transcribeAudio } from './audio-transcription/index.js';
10
10
  /*
11
11
  * Hooks
12
12
  */
13
+ export { default as useAICheckout } from './hooks/use-ai-checkout/index.js';
14
+ export { default as useAiFeature } from './hooks/use-ai-feature/index.js';
13
15
  export { default as useAiSuggestions, getErrorData } from './hooks/use-ai-suggestions/index.js';
14
16
  export { default as useMediaRecording } from './hooks/use-media-recording/index.js';
15
17
  export { default as useAudioTranscription } from './hooks/use-audio-transcription/index.js';
16
18
  export { default as useTranscriptionPostProcessing } from './hooks/use-transcription-post-processing/index.js';
17
19
  export { default as useAudioValidation } from './hooks/use-audio-validation/index.js';
18
20
  export { default as useImageGenerator } from './hooks/use-image-generator/index.js';
21
+ export { default as usePostContent } from './hooks/use-post-content.js';
19
22
  export * from './hooks/use-image-generator/constants.js';
20
23
 
21
24
  /*
@@ -1,13 +1,13 @@
1
1
  /**
2
2
  * External dependencies
3
3
  */
4
+ import getRedirectUrl from '@automattic/jetpack-components/tools/jp-redirect';
4
5
  import { useAnalytics } from '@automattic/jetpack-shared-extension-utils';
5
6
  import { Button } from '@wordpress/components';
6
7
  import { __ } from '@wordpress/i18n';
7
8
  /**
8
9
  * Internal dependencies
9
10
  */
10
- import getRedirectUrl from '../../../../components/tools/jp-redirect/index.js';
11
11
  import { EVENT_PLACEMENT_FREE_USER_SCREEN, EVENT_UPGRADE } from '../constants.js';
12
12
  import useLogoGenerator from '../hooks/use-logo-generator.js';
13
13
  /**
@@ -1,7 +1,7 @@
1
+ import getRedirectUrl from '@automattic/jetpack-components/tools/jp-redirect';
1
2
  import { useSelect } from '@wordpress/data';
2
3
  import { createInterpolateElement, type Element } from '@wordpress/element';
3
4
  import { __, sprintf } from '@wordpress/i18n';
4
- import getRedirectUrl from '../../../../components/tools/jp-redirect/index.js';
5
5
  /**
6
6
  * Internal dependencies
7
7
  */
@@ -1,20 +0,0 @@
1
- import { GetRedirectUrlArgs } from './types.js';
2
- /**
3
- * Builds an URL using the jetpack.com/redirect/ service
4
- *
5
- * If source is a simple slug, it will be sent using the source query parameter. e.g. jetpack.com/redirect/?source=slug
6
- *
7
- * If source is a full URL, starting with https://, it will be sent using the url query parameter. e.g. jetpack.com/redirect/?url=https://wordpress.com
8
- *
9
- * Note: if using full URL, query parameters and anchor must be passed in args. Any querystring of url fragment in the URL will be discarded.
10
- *
11
- * @since 0.2.0
12
- * @param {string} source - The URL handler registered in the server or the full destination URL (starting with https://).
13
- * @param {GetRedirectUrlArgs} args - Additional arguments to build the url.
14
- * This is not a complete list as any argument passed here
15
- * will be sent to as a query parameter to the Redirect server.
16
- * These parameters will not necessarily be passed over to the final destination URL.
17
- * If you want to add a parameter to the final destination URL, use the `query` argument.
18
- * @return {string} The redirect URL
19
- */
20
- export default function getRedirectUrl(source: string, args?: GetRedirectUrlArgs): string;
@@ -1,50 +0,0 @@
1
- /* global jetpack_redirects */
2
- /**
3
- * Builds an URL using the jetpack.com/redirect/ service
4
- *
5
- * If source is a simple slug, it will be sent using the source query parameter. e.g. jetpack.com/redirect/?source=slug
6
- *
7
- * If source is a full URL, starting with https://, it will be sent using the url query parameter. e.g. jetpack.com/redirect/?url=https://wordpress.com
8
- *
9
- * Note: if using full URL, query parameters and anchor must be passed in args. Any querystring of url fragment in the URL will be discarded.
10
- *
11
- * @since 0.2.0
12
- * @param {string} source - The URL handler registered in the server or the full destination URL (starting with https://).
13
- * @param {GetRedirectUrlArgs} args - Additional arguments to build the url.
14
- * This is not a complete list as any argument passed here
15
- * will be sent to as a query parameter to the Redirect server.
16
- * These parameters will not necessarily be passed over to the final destination URL.
17
- * If you want to add a parameter to the final destination URL, use the `query` argument.
18
- * @return {string} The redirect URL
19
- */
20
- export default function getRedirectUrl(source, args = {}) {
21
- const queryVars = {};
22
- let calypsoEnv;
23
- if (typeof window !== 'undefined') {
24
- calypsoEnv = window?.JP_CONNECTION_INITIAL_STATE?.calypsoEnv;
25
- }
26
- if (source.search('https://') === 0) {
27
- const parsedUrl = new URL(source);
28
- // discard any query and fragments.
29
- source = `https://${parsedUrl.host}${parsedUrl.pathname}`;
30
- queryVars.url = encodeURIComponent(source);
31
- }
32
- else {
33
- queryVars.source = encodeURIComponent(source);
34
- }
35
- for (const argName in args) {
36
- queryVars[argName] = encodeURIComponent(args[argName]);
37
- }
38
- if (!Object.keys(queryVars).includes('site') &&
39
- typeof jetpack_redirects !== 'undefined' &&
40
- Object.hasOwn(jetpack_redirects, 'currentSiteRawUrl')) {
41
- queryVars.site = jetpack_redirects.currentBlogID ?? jetpack_redirects.currentSiteRawUrl;
42
- }
43
- if (calypsoEnv) {
44
- queryVars.calypso_env = calypsoEnv;
45
- }
46
- const queryString = Object.keys(queryVars)
47
- .map(key => key + '=' + queryVars[key])
48
- .join('&');
49
- return `https://jetpack.com/redirect/?` + queryString;
50
- }
@@ -1,39 +0,0 @@
1
- export type GetRedirectUrlArgs = {
2
- /**
3
- * URL of the current site. Will default to the value of
4
- * jetpack_redirects.currentSiteRawUrl, if available.
5
- * Used to fill in the `[site]` placeholder in the target.
6
- */
7
- site?: string;
8
- /**
9
- * Additional path to be appended to the URL.
10
- * Used to fill in the `[path]` placeholder in the target.
11
- */
12
- path?: string;
13
- /**
14
- * Query parameters to be added to the final destination URL.
15
- * Should be in query string format (e.g. 'key=value&foo=bar').
16
- */
17
- query?: string;
18
- /**
19
- * Anchor to be added to the URL. Must be a single string.
20
- * Example: `section1`.
21
- */
22
- anchor?: string;
23
- /**
24
- * Any other custom arguments to be added to the final destination URL.
25
- */
26
- [key: string]: string;
27
- };
28
- export type QueryVars = {
29
- url?: string;
30
- site?: string;
31
- source?: string;
32
- calypso_env?: string;
33
- };
34
- declare global {
35
- const jetpack_redirects: {
36
- currentSiteRawUrl?: string;
37
- currentBlogID?: string;
38
- };
39
- }
@@ -1 +0,0 @@
1
- export {};
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes