@canva/cli 1.19.0 → 1.20.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 (231) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +1 -9
  3. package/cli.js +408 -409
  4. package/package.json +1 -2
  5. package/templates/base/backend/base_backend/create.ts +0 -114
  6. package/templates/base/backend/database/database.ts +0 -42
  7. package/templates/base/backend/routers/auth.ts +0 -288
  8. package/templates/base/declarations/declarations.d.ts +0 -29
  9. package/templates/base/eslint.config.mjs +0 -14
  10. package/templates/base/package.json +0 -91
  11. package/templates/base/scripts/copy_env.ts +0 -13
  12. package/templates/base/scripts/ssl/ssl.ts +0 -131
  13. package/templates/base/scripts/start/app_runner.ts +0 -223
  14. package/templates/base/scripts/start/context.ts +0 -171
  15. package/templates/base/scripts/start/start.ts +0 -46
  16. package/templates/base/scripts/start.tests.ts +0 -61
  17. package/templates/base/styles/components.css +0 -38
  18. package/templates/base/tsconfig.json +0 -56
  19. package/templates/base/webpack.config.ts +0 -247
  20. package/templates/common/.env.template +0 -6
  21. package/templates/common/.gitignore.template +0 -8
  22. package/templates/common/.nvmrc +0 -1
  23. package/templates/common/.prettierrc +0 -21
  24. package/templates/common/LICENSE.md +0 -48
  25. package/templates/common/README.md +0 -179
  26. package/templates/common/jest.config.mjs +0 -35
  27. package/templates/common/jest.setup.ts +0 -35
  28. package/templates/content_publisher/README.md +0 -58
  29. package/templates/content_publisher/canva-app.json +0 -17
  30. package/templates/content_publisher/declarations/declarations.d.ts +0 -29
  31. package/templates/content_publisher/eslint.config.mjs +0 -14
  32. package/templates/content_publisher/package.json +0 -90
  33. package/templates/content_publisher/scripts/copy_env.ts +0 -13
  34. package/templates/content_publisher/scripts/ssl/ssl.ts +0 -131
  35. package/templates/content_publisher/scripts/start/app_runner.ts +0 -223
  36. package/templates/content_publisher/scripts/start/context.ts +0 -171
  37. package/templates/content_publisher/scripts/start/start.ts +0 -46
  38. package/templates/content_publisher/src/index.tsx +0 -4
  39. package/templates/content_publisher/src/intents/content_publisher/index.tsx +0 -107
  40. package/templates/content_publisher/src/intents/content_publisher/post_preview.tsx +0 -240
  41. package/templates/content_publisher/src/intents/content_publisher/preview_ui.tsx +0 -62
  42. package/templates/content_publisher/src/intents/content_publisher/settings_ui.tsx +0 -81
  43. package/templates/content_publisher/src/intents/content_publisher/types.ts +0 -29
  44. package/templates/content_publisher/styles/components.css +0 -38
  45. package/templates/content_publisher/styles/preview_ui.css +0 -49
  46. package/templates/content_publisher/tsconfig.json +0 -56
  47. package/templates/content_publisher/webpack.config.ts +0 -247
  48. package/templates/dam/backend/routers/dam.ts +0 -108
  49. package/templates/dam/backend/server.ts +0 -65
  50. package/templates/dam/canva-app.json +0 -25
  51. package/templates/dam/declarations/declarations.d.ts +0 -29
  52. package/templates/dam/eslint.config.mjs +0 -14
  53. package/templates/dam/package.json +0 -97
  54. package/templates/dam/scripts/copy_env.ts +0 -13
  55. package/templates/dam/scripts/ssl/ssl.ts +0 -131
  56. package/templates/dam/scripts/start/app_runner.ts +0 -223
  57. package/templates/dam/scripts/start/context.ts +0 -171
  58. package/templates/dam/scripts/start/start.ts +0 -46
  59. package/templates/dam/src/index.tsx +0 -4
  60. package/templates/dam/src/intents/design_editor/adapter.ts +0 -44
  61. package/templates/dam/src/intents/design_editor/app.tsx +0 -35
  62. package/templates/dam/src/intents/design_editor/config.ts +0 -220
  63. package/templates/dam/src/intents/design_editor/index.css +0 -3
  64. package/templates/dam/src/intents/design_editor/index.tsx +0 -25
  65. package/templates/dam/tsconfig.json +0 -56
  66. package/templates/dam/utils/backend/base_backend/create.ts +0 -114
  67. package/templates/dam/webpack.config.ts +0 -247
  68. package/templates/data_connector/README.md +0 -84
  69. package/templates/data_connector/canva-app.json +0 -21
  70. package/templates/data_connector/declarations/declarations.d.ts +0 -29
  71. package/templates/data_connector/eslint.config.mjs +0 -14
  72. package/templates/data_connector/package.json +0 -92
  73. package/templates/data_connector/scripts/copy_env.ts +0 -13
  74. package/templates/data_connector/scripts/ssl/ssl.ts +0 -131
  75. package/templates/data_connector/scripts/start/app_runner.ts +0 -223
  76. package/templates/data_connector/scripts/start/context.ts +0 -171
  77. package/templates/data_connector/scripts/start/start.ts +0 -46
  78. package/templates/data_connector/src/api/connect_client.ts +0 -6
  79. package/templates/data_connector/src/api/data_source.ts +0 -97
  80. package/templates/data_connector/src/api/data_sources/designs.tsx +0 -296
  81. package/templates/data_connector/src/api/data_sources/index.ts +0 -4
  82. package/templates/data_connector/src/api/data_sources/templates.tsx +0 -328
  83. package/templates/data_connector/src/api/fetch_data_table.ts +0 -55
  84. package/templates/data_connector/src/api/index.ts +0 -4
  85. package/templates/data_connector/src/api/oauth.ts +0 -8
  86. package/templates/data_connector/src/api/tests/data_source.test.tsx +0 -99
  87. package/templates/data_connector/src/components/app_error.tsx +0 -15
  88. package/templates/data_connector/src/components/footer.tsx +0 -26
  89. package/templates/data_connector/src/components/header.tsx +0 -40
  90. package/templates/data_connector/src/components/index.ts +0 -3
  91. package/templates/data_connector/src/components/inputs/messages.tsx +0 -95
  92. package/templates/data_connector/src/components/inputs/search_filter.tsx +0 -109
  93. package/templates/data_connector/src/components/inputs/select_field.tsx +0 -26
  94. package/templates/data_connector/src/context/app_context.tsx +0 -125
  95. package/templates/data_connector/src/context/index.ts +0 -2
  96. package/templates/data_connector/src/context/use_app_context.ts +0 -17
  97. package/templates/data_connector/src/index.tsx +0 -4
  98. package/templates/data_connector/src/intents/data_connector/app.tsx +0 -20
  99. package/templates/data_connector/src/intents/data_connector/entrypoint.tsx +0 -70
  100. package/templates/data_connector/src/intents/data_connector/home.tsx +0 -21
  101. package/templates/data_connector/src/intents/data_connector/index.tsx +0 -56
  102. package/templates/data_connector/src/pages/data_source_config.tsx +0 -9
  103. package/templates/data_connector/src/pages/error.tsx +0 -37
  104. package/templates/data_connector/src/pages/index.ts +0 -4
  105. package/templates/data_connector/src/pages/login.tsx +0 -145
  106. package/templates/data_connector/src/pages/select_source.tsx +0 -24
  107. package/templates/data_connector/src/routes/index.ts +0 -2
  108. package/templates/data_connector/src/routes/paths.ts +0 -7
  109. package/templates/data_connector/src/routes/protected_route.tsx +0 -26
  110. package/templates/data_connector/src/routes/routes.tsx +0 -42
  111. package/templates/data_connector/src/utils/data_params.ts +0 -17
  112. package/templates/data_connector/src/utils/data_table.ts +0 -116
  113. package/templates/data_connector/src/utils/fetch_result.ts +0 -36
  114. package/templates/data_connector/src/utils/index.ts +0 -2
  115. package/templates/data_connector/src/utils/tests/data_table.test.ts +0 -133
  116. package/templates/data_connector/styles/components.css +0 -38
  117. package/templates/data_connector/tsconfig.json +0 -56
  118. package/templates/data_connector/webpack.config.ts +0 -247
  119. package/templates/gen_ai/README.md +0 -27
  120. package/templates/gen_ai/backend/routers/image.ts +0 -232
  121. package/templates/gen_ai/backend/server.ts +0 -65
  122. package/templates/gen_ai/canva-app.json +0 -25
  123. package/templates/gen_ai/declarations/declarations.d.ts +0 -29
  124. package/templates/gen_ai/eslint.config.mjs +0 -14
  125. package/templates/gen_ai/package.json +0 -101
  126. package/templates/gen_ai/scripts/copy_env.ts +0 -13
  127. package/templates/gen_ai/scripts/ssl/ssl.ts +0 -131
  128. package/templates/gen_ai/scripts/start/app_runner.ts +0 -223
  129. package/templates/gen_ai/scripts/start/context.ts +0 -171
  130. package/templates/gen_ai/scripts/start/start.ts +0 -46
  131. package/templates/gen_ai/src/api/api.ts +0 -194
  132. package/templates/gen_ai/src/api/index.ts +0 -1
  133. package/templates/gen_ai/src/components/app_error.tsx +0 -18
  134. package/templates/gen_ai/src/components/footer.messages.ts +0 -48
  135. package/templates/gen_ai/src/components/footer.tsx +0 -156
  136. package/templates/gen_ai/src/components/image_grid.tsx +0 -103
  137. package/templates/gen_ai/src/components/index.ts +0 -7
  138. package/templates/gen_ai/src/components/loading_results.tsx +0 -169
  139. package/templates/gen_ai/src/components/prompt_input.messages.ts +0 -14
  140. package/templates/gen_ai/src/components/prompt_input.tsx +0 -154
  141. package/templates/gen_ai/src/components/remaining_credits.tsx +0 -84
  142. package/templates/gen_ai/src/components/report_box.tsx +0 -54
  143. package/templates/gen_ai/src/components/tests/remaining_credit.tests.tsx +0 -47
  144. package/templates/gen_ai/src/config.ts +0 -21
  145. package/templates/gen_ai/src/context/app_context.tsx +0 -153
  146. package/templates/gen_ai/src/context/context.messages.ts +0 -30
  147. package/templates/gen_ai/src/context/index.ts +0 -2
  148. package/templates/gen_ai/src/context/use_app_context.ts +0 -17
  149. package/templates/gen_ai/src/index.tsx +0 -4
  150. package/templates/gen_ai/src/intents/design_editor/app.tsx +0 -19
  151. package/templates/gen_ai/src/intents/design_editor/home.tsx +0 -13
  152. package/templates/gen_ai/src/intents/design_editor/index.tsx +0 -17
  153. package/templates/gen_ai/src/pages/error.tsx +0 -41
  154. package/templates/gen_ai/src/pages/generate.tsx +0 -9
  155. package/templates/gen_ai/src/pages/index.ts +0 -3
  156. package/templates/gen_ai/src/pages/results.tsx +0 -31
  157. package/templates/gen_ai/src/routes/index.ts +0 -1
  158. package/templates/gen_ai/src/routes/paths.ts +0 -4
  159. package/templates/gen_ai/src/routes/routes.tsx +0 -24
  160. package/templates/gen_ai/src/utils/index.ts +0 -1
  161. package/templates/gen_ai/src/utils/obscenity_filter.ts +0 -33
  162. package/templates/gen_ai/styles/components.css +0 -38
  163. package/templates/gen_ai/styles/utils.css +0 -3
  164. package/templates/gen_ai/tsconfig.json +0 -56
  165. package/templates/gen_ai/utils/backend/base_backend/create.ts +0 -114
  166. package/templates/gen_ai/webpack.config.ts +0 -247
  167. package/templates/hello_world/canva-app.json +0 -21
  168. package/templates/hello_world/declarations/declarations.d.ts +0 -29
  169. package/templates/hello_world/eslint.config.mjs +0 -14
  170. package/templates/hello_world/package.json +0 -90
  171. package/templates/hello_world/scripts/copy_env.ts +0 -13
  172. package/templates/hello_world/scripts/ssl/ssl.ts +0 -131
  173. package/templates/hello_world/scripts/start/app_runner.ts +0 -223
  174. package/templates/hello_world/scripts/start/context.ts +0 -171
  175. package/templates/hello_world/scripts/start/start.ts +0 -46
  176. package/templates/hello_world/src/index.tsx +0 -4
  177. package/templates/hello_world/src/intents/design_editor/app.tsx +0 -86
  178. package/templates/hello_world/src/intents/design_editor/index.tsx +0 -25
  179. package/templates/hello_world/src/intents/design_editor/tests/__snapshots__/app.tests.tsx.snap +0 -45
  180. package/templates/hello_world/src/intents/design_editor/tests/app.tests.tsx +0 -92
  181. package/templates/hello_world/styles/components.css +0 -38
  182. package/templates/hello_world/tsconfig.json +0 -56
  183. package/templates/hello_world/webpack.config.ts +0 -247
  184. package/templates/mls/README.md +0 -81
  185. package/templates/mls/canva-app.json +0 -25
  186. package/templates/mls/declarations/declarations.d.ts +0 -29
  187. package/templates/mls/eslint.config.mjs +0 -14
  188. package/templates/mls/jest.config.mjs +0 -36
  189. package/templates/mls/jest.setup.ts +0 -39
  190. package/templates/mls/package.json +0 -117
  191. package/templates/mls/scripts/copy_env.ts +0 -13
  192. package/templates/mls/scripts/ssl/ssl.ts +0 -131
  193. package/templates/mls/scripts/start/app_runner.ts +0 -223
  194. package/templates/mls/scripts/start/context.ts +0 -171
  195. package/templates/mls/scripts/start/start.ts +0 -46
  196. package/templates/mls/src/__tests__/app.tests.tsx +0 -11
  197. package/templates/mls/src/__tests__/office_selection_page.tests.tsx +0 -72
  198. package/templates/mls/src/__tests__/utils.tsx +0 -19
  199. package/templates/mls/src/adapter.ts +0 -126
  200. package/templates/mls/src/components/agent/agent_card.tsx +0 -57
  201. package/templates/mls/src/components/agent/agent_grid.tsx +0 -37
  202. package/templates/mls/src/components/agent/agent_list.tsx +0 -17
  203. package/templates/mls/src/components/agent/agent_search_filters.tsx +0 -88
  204. package/templates/mls/src/components/breadcrumb/breadcrumb.tsx +0 -40
  205. package/templates/mls/src/components/listing/listing_card.tsx +0 -64
  206. package/templates/mls/src/components/listing/listing_grid.tsx +0 -37
  207. package/templates/mls/src/components/listing/listing_list.tsx +0 -21
  208. package/templates/mls/src/components/listing/listing_search_filters.tsx +0 -145
  209. package/templates/mls/src/components/placeholders/placeholders.tsx +0 -65
  210. package/templates/mls/src/data.ts +0 -359
  211. package/templates/mls/src/index.tsx +0 -4
  212. package/templates/mls/src/intents/design_editor/app.tsx +0 -44
  213. package/templates/mls/src/intents/design_editor/index.tsx +0 -25
  214. package/templates/mls/src/pages/agent_details_page/agent_details_page.tsx +0 -175
  215. package/templates/mls/src/pages/list_page/agent_tab_panel.tsx +0 -126
  216. package/templates/mls/src/pages/list_page/list_page.tsx +0 -67
  217. package/templates/mls/src/pages/list_page/listing_tab_panel.tsx +0 -135
  218. package/templates/mls/src/pages/listing_details_page/listing_details_page.tsx +0 -418
  219. package/templates/mls/src/pages/loading_page/loading_page.tsx +0 -152
  220. package/templates/mls/src/pages/office_selection_page/office_selection_page.tsx +0 -144
  221. package/templates/mls/src/real_estate.type.ts +0 -44
  222. package/templates/mls/src/util/use_add_element.tsx +0 -62
  223. package/templates/mls/src/util/use_drag_element.tsx +0 -68
  224. package/templates/mls/styles/components.css +0 -38
  225. package/templates/mls/tsconfig.json +0 -54
  226. package/templates/mls/webpack.config.ts +0 -248
  227. package/templates/optional/.cursor/mcp.json +0 -8
  228. package/templates/optional/.vscode/extensions.json +0 -6
  229. package/templates/optional/.vscode/mcp.json +0 -9
  230. package/templates/optional/AGENTS.md +0 -154
  231. package/templates/optional/CLAUDE.md +0 -154
@@ -1,46 +0,0 @@
1
- #!/usr/bin/env node
2
- import yargs from "yargs";
3
- import { hideBin } from "yargs/helpers";
4
- import { AppRunner } from "./app_runner";
5
- import { Context } from "./context";
6
-
7
- const appRunner = new AppRunner();
8
-
9
- yargs(hideBin(process.argv))
10
- .version(false)
11
- .help()
12
- .option("ngrok", {
13
- description: "Run backend server via ngrok.",
14
- type: "boolean",
15
- // npm swallows command line args instead of forwarding to the script
16
- default:
17
- process.env.npm_config_ngrok?.toLocaleLowerCase().trim() === "true",
18
- })
19
- .option("use-https", {
20
- description: "Start local development server on HTTPS.",
21
- type: "boolean",
22
- // npm swallows commands line args instead of forwarding to the script
23
- default:
24
- process.env.npm_config_use_https?.toLocaleLowerCase().trim() === "true",
25
- })
26
- .option("override-frontend-port", {
27
- description:
28
- "Port to run the local development server on. Overrides the frontend port set in the .env file.",
29
- type: "number",
30
- alias: "p",
31
- })
32
- .option("preview", {
33
- description: "Open the app in Canva.",
34
- type: "boolean",
35
- default: false,
36
- })
37
- .command(
38
- "$0",
39
- "Starts local development",
40
- () => {},
41
- (args) => {
42
- const ctx = new Context(process.env, args);
43
- appRunner.run(ctx);
44
- },
45
- )
46
- .parse();
@@ -1,194 +0,0 @@
1
- import { auth } from "@canva/user";
2
- import { POLLING_INTERVAL_IN_SECONDS } from "src/config";
3
-
4
- /**
5
- * Represents the structure of an image.
6
- */
7
- export interface ImageType {
8
- /** The label of the image. */
9
- label: string;
10
- /** Information about the full-size version of the image. */
11
- fullsize: { width: number; height: number; url: string };
12
- /** Information about the thumbnail version of the image. */
13
- thumbnail: { width: number; height: number; url: string };
14
- }
15
-
16
- /**
17
- * Represents the result of generating images.
18
- */
19
- interface ImageGenerationResult {
20
- /** An array of generated images. */
21
- images: ImageType[];
22
- /** The remaining credits. */
23
- credits: number;
24
- }
25
-
26
- interface ImageGenerationJobStatusResponse {
27
- status: "completed" | "processing";
28
- images: ImageType[];
29
- credits: number;
30
- }
31
-
32
- interface QueueImageGenerationResponse {
33
- jobId: string;
34
- }
35
-
36
- /**
37
- * Represents the result of retrieving remaining credits.
38
- */
39
- interface RemainingCreditsResult {
40
- /** The remaining credits. */
41
- credits: number;
42
- }
43
-
44
- const endpoints = {
45
- queueImageGeneration: "/api/queue-image-generation",
46
- getImageGenerationJobStatus: "/api/job-status",
47
- cancelImageGenerationJob: "/api/job-status/cancel",
48
- getRemainingCredits: "/api/credits",
49
- purchaseCredits: "/api/purchase-credits",
50
- };
51
-
52
- /**
53
- * queues image generation based on the provided prompt and number of images.
54
- * @param {Object} options - The options for generating images.
55
- * @param {string} options.prompt - The prompt for generating images.
56
- * @param {number} options.numberOfImages - The number of images to generate.
57
- * @returns {Promise<QueueImageGenerationResponse>} A promise that resolves to the created job ID.
58
- */
59
- export const queueImageGeneration = async ({
60
- prompt,
61
- numberOfImages,
62
- }: {
63
- prompt: string;
64
- numberOfImages: number;
65
- }): Promise<QueueImageGenerationResponse> => {
66
- const url = new URL(endpoints.queueImageGeneration, BACKEND_HOST);
67
- url.searchParams.append("count", numberOfImages.toString());
68
- url.searchParams.append("prompt", prompt);
69
-
70
- const result: QueueImageGenerationResponse = await sendRequest(url);
71
-
72
- return result;
73
- };
74
-
75
- /**
76
- * Retrieves the status of an image generation job.
77
- * @param {string} jobId - The ID of the job to retrieve status for.
78
- * @returns {Promise<ImageGenerationResult>} A promise that resolves to the image generation result.
79
- */
80
- export const getImageGenerationJobStatus = async ({
81
- jobId,
82
- }: {
83
- jobId: string;
84
- }): Promise<ImageGenerationResult> => {
85
- const url = new URL(endpoints.getImageGenerationJobStatus, BACKEND_HOST);
86
- url.searchParams.append("jobId", jobId);
87
-
88
- // Define a maximum number of polling attempts
89
- const maxAttempts = 10;
90
- let attempts = 0;
91
-
92
- while (attempts < maxAttempts) {
93
- try {
94
- const response = (await sendRequest(
95
- url,
96
- )) as ImageGenerationJobStatusResponse;
97
- if (response.status === "completed") {
98
- return { images: response.images, credits: response.credits };
99
- } else if (response.status === "processing") {
100
- // Job is still processing, wait and continue polling
101
- await new Promise((resolve) =>
102
- setTimeout(resolve, POLLING_INTERVAL_IN_SECONDS * 1000),
103
- );
104
- attempts++;
105
- } else if (response.status === "cancelled") {
106
- throw new Error("Job not found");
107
- }
108
- } catch (error) {
109
- // Handle errors here
110
- throw new Error(`Error while polling job status ${error}`);
111
- }
112
- }
113
-
114
- throw new Error("Maximum polling attempts reached");
115
- };
116
-
117
- /**
118
- * Cancels an image generation job based on the provided job ID.
119
- * @param {string} jobId - The ID of the job to cancel.
120
- * @returns {Promise<void>} - A promise that resolves if the job is successfully cancelled, otherwise rejects with an error.
121
- */
122
- export const cancelImageGenerationJob = async (
123
- jobId: string,
124
- ): Promise<void> => {
125
- const url = new URL(endpoints.cancelImageGenerationJob, BACKEND_HOST);
126
- url.searchParams.append("jobId", jobId);
127
-
128
- try {
129
- await sendRequest(url, {
130
- method: "POST",
131
- });
132
- } catch {
133
- throw new Error("Failed to cancel job.");
134
- }
135
- };
136
-
137
- /**
138
- * Retrieves the remaining credits.
139
- * @returns {Promise<RemainingCreditsResult>} - A promise that resolves to the remaining credits.
140
- */
141
- export const getRemainingCredits =
142
- async (): Promise<RemainingCreditsResult> => {
143
- const url = new URL(endpoints.getRemainingCredits, BACKEND_HOST);
144
-
145
- const result: RemainingCreditsResult = await sendRequest(url);
146
-
147
- return result;
148
- };
149
-
150
- /**
151
- * Purchases credits for the user.
152
- * @returns {Promise<RemainingCreditsResult>} - A promise that resolves to the remaining credits after purchasing.
153
- */
154
- export const purchaseCredits = async (): Promise<RemainingCreditsResult> => {
155
- const url = new URL(endpoints.purchaseCredits, BACKEND_HOST);
156
-
157
- const result: RemainingCreditsResult = await sendRequest(url, {
158
- method: "POST",
159
- });
160
-
161
- return result;
162
- };
163
-
164
- /**
165
- * Sends a request to the specified URL.
166
- * @param {URL} url - The URL to send the request to.
167
- * @param {RequestInit} [options] - Optional fetch options to be passed to the fetch function.
168
- * @returns {Promise<Object>} - A promise that resolves to the response body.
169
- */
170
- const sendRequest = async <T>(url: URL, options?: RequestInit): Promise<T> => {
171
- const userToken = await auth.getCanvaUserToken();
172
-
173
- const res = await fetch(url, {
174
- headers: {
175
- Authorization: `Bearer ${userToken}`,
176
- ...options?.headers,
177
- },
178
- ...options,
179
- });
180
-
181
- if (!res.ok) {
182
- throw new Error(`Request failed with status ${res.status}`);
183
- }
184
-
185
- // Check Content-Type header to determine how to parse response body
186
- const contentType = res.headers.get("content-type");
187
- if (contentType && contentType.includes("application/json")) {
188
- // Parse response body as JSON
189
- return (await res.json()) as T;
190
- } else {
191
- // Parse response body as text
192
- return (await res.text()) as unknown as T;
193
- }
194
- };
@@ -1 +0,0 @@
1
- export * from "./api";
@@ -1,18 +0,0 @@
1
- import { Alert } from "@canva/app-ui-kit";
2
- import { useAppContext } from "src/context";
3
-
4
- export const AppError = () => {
5
- const { loadingApp, creditsError, appError, setAppError } = useAppContext();
6
- if (loadingApp || (!appError && !creditsError)) {
7
- return null;
8
- }
9
-
10
- return (
11
- <Alert
12
- tone="critical"
13
- onDismiss={creditsError ? undefined : () => setAppError("")}
14
- >
15
- {creditsError || appError}
16
- </Alert>
17
- );
18
- };
@@ -1,48 +0,0 @@
1
- import { defineMessages } from "react-intl";
2
-
3
- export const FooterMessages = defineMessages({
4
- /** Indicates actions users can take or instructions provided to the user. */
5
- generateAgain: {
6
- defaultMessage: "Generate again",
7
- description:
8
- "A button label to generate another image based on the previous prompt",
9
- },
10
- generateImage: {
11
- defaultMessage: "Generate image",
12
- description: "A button label to generate an image from a prompt",
13
- },
14
- purchaseMoreCredits: {
15
- defaultMessage: "Purchase more credits",
16
- description:
17
- "A button label to open a page where the user can purchase more credits",
18
- },
19
- startOver: {
20
- defaultMessage: "Start over",
21
- description: "A button label to start the image generation process over",
22
- },
23
-
24
- /** Messages related to handling errors that occur during operations. */
25
- appErrorGeneratingImagesFailed: {
26
- defaultMessage: "Generating images has failed, please try again.",
27
- description:
28
- "A message to indicate that generating images has failed, but the user is able to make another attempt",
29
- },
30
-
31
- /** Messages related to prompts and user input validation. */
32
- promptMissingErrorMessage: {
33
- defaultMessage: "Please describe what you want to create",
34
- description:
35
- "An error message to indicate that the user did not supply a prompt to generate an image, and this has to be provided before generating",
36
- },
37
- promptNoCreditsRemaining: {
38
- defaultMessage: "No credits remaining.",
39
- description:
40
- "A message to indicate that the user has no credits remaining, and is unable to generate an image",
41
- },
42
- promptObscenityErrorMessage: {
43
- defaultMessage:
44
- "Something you typed may result in content that doesn’t meet our policies.",
45
- description:
46
- "An error message to indicate that the user typed something that may result in content that for example could be offensive or violent",
47
- },
48
- });
@@ -1,156 +0,0 @@
1
- import { Button, Rows } from "@canva/app-ui-kit";
2
- import { getPlatformInfo } from "@canva/platform";
3
- import { useIntl } from "react-intl";
4
- import { useLocation, useNavigate } from "react-router-dom";
5
- import { purchaseCredits, queueImageGeneration } from "src/api";
6
- import { RemainingCredits } from "src/components";
7
- import { NUMBER_OF_IMAGES_TO_GENERATE } from "src/config";
8
- import { useAppContext } from "src/context";
9
- import { Paths } from "src/routes/paths";
10
- import { getObsceneWords } from "src/utils";
11
- import { FooterMessages as Messages } from "./footer.messages";
12
-
13
- export const Footer = () => {
14
- const navigate = useNavigate();
15
- const { pathname } = useLocation();
16
- const isRootRoute = pathname === Paths.HOME;
17
- const platformInfo = getPlatformInfo();
18
- const {
19
- setAppError,
20
- promptInput,
21
- setPromptInput,
22
- setPromptInputError,
23
- loadingApp,
24
- isLoadingImages,
25
- setJobId,
26
- setIsLoadingImages,
27
- remainingCredits,
28
- setRemainingCredits,
29
- } = useAppContext();
30
- const intl = useIntl();
31
-
32
- const hasRemainingCredits = remainingCredits > 0;
33
-
34
- const isCreditRemaining = () => {
35
- if (!hasRemainingCredits) {
36
- setPromptInputError(
37
- intl.formatMessage(Messages.promptNoCreditsRemaining),
38
- );
39
- return false;
40
- }
41
- return true;
42
- };
43
-
44
- const isPromptInputFilled = () => {
45
- if (!promptInput) {
46
- setPromptInputError(
47
- intl.formatMessage(Messages.promptMissingErrorMessage),
48
- );
49
- return false;
50
- }
51
- return true;
52
- };
53
-
54
- const isPromptInputClean = () => {
55
- const obsceneWords = getObsceneWords(promptInput);
56
- if (obsceneWords.length > 0) {
57
- setPromptInputError(
58
- intl.formatMessage(Messages.promptObscenityErrorMessage),
59
- );
60
- return false;
61
- }
62
- return true;
63
- };
64
-
65
- const onGenerateClick = async () => {
66
- if (
67
- !isCreditRemaining() ||
68
- !isPromptInputFilled() ||
69
- !isPromptInputClean()
70
- ) {
71
- return;
72
- }
73
-
74
- setIsLoadingImages(true);
75
- try {
76
- const { jobId } = await queueImageGeneration({
77
- prompt: promptInput,
78
- numberOfImages: NUMBER_OF_IMAGES_TO_GENERATE,
79
- });
80
-
81
- setJobId(jobId);
82
- } catch {
83
- setAppError(intl.formatMessage(Messages.appErrorGeneratingImagesFailed));
84
- }
85
- navigate(Paths.RESULTS);
86
- };
87
-
88
- const onPurchaseMoreCredits = async () => {
89
- if (!platformInfo.canAcceptPayments) {
90
- return;
91
- }
92
-
93
- const { credits } = await purchaseCredits();
94
- setRemainingCredits(credits);
95
- };
96
-
97
- const reset = () => {
98
- setPromptInput("");
99
- navigate(Paths.HOME);
100
- };
101
-
102
- const footerButtons = [
103
- {
104
- variant: "primary" as const,
105
- onClick: onGenerateClick,
106
- value: isRootRoute
107
- ? intl.formatMessage(Messages.generateImage)
108
- : intl.formatMessage(Messages.generateAgain),
109
- visible: hasRemainingCredits,
110
- },
111
- {
112
- variant: "primary" as const,
113
- onClick: onPurchaseMoreCredits,
114
- value: intl.formatMessage(Messages.purchaseMoreCredits),
115
- visible: !hasRemainingCredits,
116
- disabled: !platformInfo.canAcceptPayments,
117
- tooltip: intl.formatMessage({
118
- defaultMessage: "Payment not available on this platform.",
119
- description:
120
- "Tooltip text when payment functionality is not available on the current platform. ",
121
- }),
122
- },
123
- {
124
- variant: "secondary" as const,
125
- onClick: reset,
126
- value: intl.formatMessage(Messages.startOver),
127
- visible: !isRootRoute,
128
- },
129
- ];
130
-
131
- if (isLoadingImages) {
132
- return null;
133
- }
134
-
135
- return (
136
- <Rows spacing="1u">
137
- {footerButtons.map(
138
- ({ visible, variant, onClick, value, disabled, tooltip }) =>
139
- visible && (
140
- <Button
141
- key={value}
142
- variant={variant}
143
- onClick={onClick}
144
- loading={loadingApp}
145
- stretch={true}
146
- disabled={disabled}
147
- tooltipLabel={tooltip}
148
- >
149
- {value}
150
- </Button>
151
- ),
152
- )}
153
- <RemainingCredits />
154
- </Rows>
155
- );
156
- };
@@ -1,103 +0,0 @@
1
- import { Grid, ImageCard, Rows, Text } from "@canva/app-ui-kit";
2
- import type { QueuedImage } from "@canva/asset";
3
- import { upload } from "@canva/asset";
4
- import { addElementAtPoint, ui } from "@canva/design";
5
- import type { DragEvent } from "react";
6
- import { FormattedMessage, useIntl } from "react-intl";
7
- import type { ImageType } from "src/api";
8
- import { useAppContext } from "src/context";
9
- import * as styles from "styles/utils.css";
10
-
11
- const THUMBNAIL_HEIGHT = 150;
12
-
13
- const uploadImage = async (image: ImageType): Promise<QueuedImage> => {
14
- // Upload the image using @canva/asset.
15
- const queuedImage = await upload({
16
- type: "image",
17
- mimeType: "image/jpeg",
18
- thumbnailUrl: image.thumbnail.url,
19
- url: image.fullsize.url,
20
- width: image.fullsize.width,
21
- height: image.fullsize.height,
22
- aiDisclosure: "app_generated",
23
- });
24
-
25
- return queuedImage;
26
- };
27
-
28
- export const ImageGrid = () => {
29
- const { generatedImages } = useAppContext();
30
-
31
- const onDragStart = async (
32
- event: DragEvent<HTMLElement>,
33
- image: ImageType,
34
- ) => {
35
- const parentNode = event.currentTarget.parentElement;
36
- try {
37
- if (styles.hidden) {
38
- parentNode?.classList.add(styles.hidden);
39
- }
40
-
41
- await ui.startDragToPoint(event, {
42
- type: "image",
43
- resolveImageRef: () => uploadImage(image),
44
- previewUrl: image.thumbnail.url,
45
- previewSize: {
46
- width: image.thumbnail.width,
47
- height: image.thumbnail.height,
48
- },
49
- fullSize: {
50
- width: image.fullsize.width,
51
- height: image.fullsize.height,
52
- },
53
- });
54
- } finally {
55
- if (styles.hidden) {
56
- parentNode?.classList.remove(styles.hidden);
57
- }
58
- }
59
- };
60
-
61
- const onImageClick = async (image: ImageType) => {
62
- const queuedImage = await uploadImage(image);
63
-
64
- await addElementAtPoint({
65
- type: "image",
66
- altText: { text: image.label, decorative: false },
67
- ref: queuedImage.ref,
68
- });
69
- };
70
-
71
- const intl = useIntl();
72
-
73
- return (
74
- <Rows spacing="1u">
75
- <Text size="medium" variant="bold">
76
- <FormattedMessage
77
- defaultMessage="Select or drag to add to design"
78
- description="Instruction to the user on how they can add the generated image to their design"
79
- />
80
- </Text>
81
- <Grid columns={2} spacing="2u">
82
- {generatedImages.map((image, index) => (
83
- <ImageCard
84
- key={index}
85
- thumbnailUrl={image.thumbnail.url}
86
- onClick={() => onImageClick(image)}
87
- ariaLabel={intl.formatMessage({
88
- defaultMessage: "Add image to design",
89
- description:
90
- "Aria label for the image card. When the image card is pressed, it will add the image to the design",
91
- })}
92
- alt={image.label}
93
- thumbnailHeight={THUMBNAIL_HEIGHT}
94
- borderRadius="standard"
95
- onDragStart={(event: DragEvent<HTMLElement>) =>
96
- onDragStart(event, image)
97
- }
98
- />
99
- ))}
100
- </Grid>
101
- </Rows>
102
- );
103
- };
@@ -1,7 +0,0 @@
1
- export * from "./app_error";
2
- export * from "./footer";
3
- export * from "./image_grid";
4
- export * from "./loading_results";
5
- export * from "./prompt_input";
6
- export * from "./report_box";
7
- export * from "./remaining_credits";