@canva/cli 1.19.0 → 1.21.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.
- package/CHANGELOG.md +26 -0
- package/README.md +1 -9
- package/cli.js +625 -626
- package/package.json +2 -3
- package/templates/base/backend/base_backend/create.ts +0 -114
- package/templates/base/backend/database/database.ts +0 -42
- package/templates/base/backend/routers/auth.ts +0 -288
- package/templates/base/declarations/declarations.d.ts +0 -29
- package/templates/base/eslint.config.mjs +0 -14
- package/templates/base/package.json +0 -91
- package/templates/base/scripts/copy_env.ts +0 -13
- package/templates/base/scripts/ssl/ssl.ts +0 -131
- package/templates/base/scripts/start/app_runner.ts +0 -223
- package/templates/base/scripts/start/context.ts +0 -171
- package/templates/base/scripts/start/start.ts +0 -46
- package/templates/base/scripts/start.tests.ts +0 -61
- package/templates/base/styles/components.css +0 -38
- package/templates/base/tsconfig.json +0 -56
- package/templates/base/webpack.config.ts +0 -247
- package/templates/common/.env.template +0 -6
- package/templates/common/.gitignore.template +0 -8
- package/templates/common/.nvmrc +0 -1
- package/templates/common/.prettierrc +0 -21
- package/templates/common/LICENSE.md +0 -48
- package/templates/common/README.md +0 -179
- package/templates/common/jest.config.mjs +0 -35
- package/templates/common/jest.setup.ts +0 -35
- package/templates/content_publisher/README.md +0 -58
- package/templates/content_publisher/canva-app.json +0 -17
- package/templates/content_publisher/declarations/declarations.d.ts +0 -29
- package/templates/content_publisher/eslint.config.mjs +0 -14
- package/templates/content_publisher/package.json +0 -90
- package/templates/content_publisher/scripts/copy_env.ts +0 -13
- package/templates/content_publisher/scripts/ssl/ssl.ts +0 -131
- package/templates/content_publisher/scripts/start/app_runner.ts +0 -223
- package/templates/content_publisher/scripts/start/context.ts +0 -171
- package/templates/content_publisher/scripts/start/start.ts +0 -46
- package/templates/content_publisher/src/index.tsx +0 -4
- package/templates/content_publisher/src/intents/content_publisher/index.tsx +0 -107
- package/templates/content_publisher/src/intents/content_publisher/post_preview.tsx +0 -240
- package/templates/content_publisher/src/intents/content_publisher/preview_ui.tsx +0 -62
- package/templates/content_publisher/src/intents/content_publisher/settings_ui.tsx +0 -81
- package/templates/content_publisher/src/intents/content_publisher/types.ts +0 -29
- package/templates/content_publisher/styles/components.css +0 -38
- package/templates/content_publisher/styles/preview_ui.css +0 -49
- package/templates/content_publisher/tsconfig.json +0 -56
- package/templates/content_publisher/webpack.config.ts +0 -247
- package/templates/dam/backend/routers/dam.ts +0 -108
- package/templates/dam/backend/server.ts +0 -65
- package/templates/dam/canva-app.json +0 -25
- package/templates/dam/declarations/declarations.d.ts +0 -29
- package/templates/dam/eslint.config.mjs +0 -14
- package/templates/dam/package.json +0 -97
- package/templates/dam/scripts/copy_env.ts +0 -13
- package/templates/dam/scripts/ssl/ssl.ts +0 -131
- package/templates/dam/scripts/start/app_runner.ts +0 -223
- package/templates/dam/scripts/start/context.ts +0 -171
- package/templates/dam/scripts/start/start.ts +0 -46
- package/templates/dam/src/index.tsx +0 -4
- package/templates/dam/src/intents/design_editor/adapter.ts +0 -44
- package/templates/dam/src/intents/design_editor/app.tsx +0 -35
- package/templates/dam/src/intents/design_editor/config.ts +0 -220
- package/templates/dam/src/intents/design_editor/index.css +0 -3
- package/templates/dam/src/intents/design_editor/index.tsx +0 -25
- package/templates/dam/tsconfig.json +0 -56
- package/templates/dam/utils/backend/base_backend/create.ts +0 -114
- package/templates/dam/webpack.config.ts +0 -247
- package/templates/data_connector/README.md +0 -84
- package/templates/data_connector/canva-app.json +0 -21
- package/templates/data_connector/declarations/declarations.d.ts +0 -29
- package/templates/data_connector/eslint.config.mjs +0 -14
- package/templates/data_connector/package.json +0 -92
- package/templates/data_connector/scripts/copy_env.ts +0 -13
- package/templates/data_connector/scripts/ssl/ssl.ts +0 -131
- package/templates/data_connector/scripts/start/app_runner.ts +0 -223
- package/templates/data_connector/scripts/start/context.ts +0 -171
- package/templates/data_connector/scripts/start/start.ts +0 -46
- package/templates/data_connector/src/api/connect_client.ts +0 -6
- package/templates/data_connector/src/api/data_source.ts +0 -97
- package/templates/data_connector/src/api/data_sources/designs.tsx +0 -296
- package/templates/data_connector/src/api/data_sources/index.ts +0 -4
- package/templates/data_connector/src/api/data_sources/templates.tsx +0 -328
- package/templates/data_connector/src/api/fetch_data_table.ts +0 -55
- package/templates/data_connector/src/api/index.ts +0 -4
- package/templates/data_connector/src/api/oauth.ts +0 -8
- package/templates/data_connector/src/api/tests/data_source.test.tsx +0 -99
- package/templates/data_connector/src/components/app_error.tsx +0 -15
- package/templates/data_connector/src/components/footer.tsx +0 -26
- package/templates/data_connector/src/components/header.tsx +0 -40
- package/templates/data_connector/src/components/index.ts +0 -3
- package/templates/data_connector/src/components/inputs/messages.tsx +0 -95
- package/templates/data_connector/src/components/inputs/search_filter.tsx +0 -109
- package/templates/data_connector/src/components/inputs/select_field.tsx +0 -26
- package/templates/data_connector/src/context/app_context.tsx +0 -125
- package/templates/data_connector/src/context/index.ts +0 -2
- package/templates/data_connector/src/context/use_app_context.ts +0 -17
- package/templates/data_connector/src/index.tsx +0 -4
- package/templates/data_connector/src/intents/data_connector/app.tsx +0 -20
- package/templates/data_connector/src/intents/data_connector/entrypoint.tsx +0 -70
- package/templates/data_connector/src/intents/data_connector/home.tsx +0 -21
- package/templates/data_connector/src/intents/data_connector/index.tsx +0 -56
- package/templates/data_connector/src/pages/data_source_config.tsx +0 -9
- package/templates/data_connector/src/pages/error.tsx +0 -37
- package/templates/data_connector/src/pages/index.ts +0 -4
- package/templates/data_connector/src/pages/login.tsx +0 -145
- package/templates/data_connector/src/pages/select_source.tsx +0 -24
- package/templates/data_connector/src/routes/index.ts +0 -2
- package/templates/data_connector/src/routes/paths.ts +0 -7
- package/templates/data_connector/src/routes/protected_route.tsx +0 -26
- package/templates/data_connector/src/routes/routes.tsx +0 -42
- package/templates/data_connector/src/utils/data_params.ts +0 -17
- package/templates/data_connector/src/utils/data_table.ts +0 -116
- package/templates/data_connector/src/utils/fetch_result.ts +0 -36
- package/templates/data_connector/src/utils/index.ts +0 -2
- package/templates/data_connector/src/utils/tests/data_table.test.ts +0 -133
- package/templates/data_connector/styles/components.css +0 -38
- package/templates/data_connector/tsconfig.json +0 -56
- package/templates/data_connector/webpack.config.ts +0 -247
- package/templates/gen_ai/README.md +0 -27
- package/templates/gen_ai/backend/routers/image.ts +0 -232
- package/templates/gen_ai/backend/server.ts +0 -65
- package/templates/gen_ai/canva-app.json +0 -25
- package/templates/gen_ai/declarations/declarations.d.ts +0 -29
- package/templates/gen_ai/eslint.config.mjs +0 -14
- package/templates/gen_ai/package.json +0 -101
- package/templates/gen_ai/scripts/copy_env.ts +0 -13
- package/templates/gen_ai/scripts/ssl/ssl.ts +0 -131
- package/templates/gen_ai/scripts/start/app_runner.ts +0 -223
- package/templates/gen_ai/scripts/start/context.ts +0 -171
- package/templates/gen_ai/scripts/start/start.ts +0 -46
- package/templates/gen_ai/src/api/api.ts +0 -194
- package/templates/gen_ai/src/api/index.ts +0 -1
- package/templates/gen_ai/src/components/app_error.tsx +0 -18
- package/templates/gen_ai/src/components/footer.messages.ts +0 -48
- package/templates/gen_ai/src/components/footer.tsx +0 -156
- package/templates/gen_ai/src/components/image_grid.tsx +0 -103
- package/templates/gen_ai/src/components/index.ts +0 -7
- package/templates/gen_ai/src/components/loading_results.tsx +0 -169
- package/templates/gen_ai/src/components/prompt_input.messages.ts +0 -14
- package/templates/gen_ai/src/components/prompt_input.tsx +0 -154
- package/templates/gen_ai/src/components/remaining_credits.tsx +0 -84
- package/templates/gen_ai/src/components/report_box.tsx +0 -54
- package/templates/gen_ai/src/components/tests/remaining_credit.tests.tsx +0 -47
- package/templates/gen_ai/src/config.ts +0 -21
- package/templates/gen_ai/src/context/app_context.tsx +0 -153
- package/templates/gen_ai/src/context/context.messages.ts +0 -30
- package/templates/gen_ai/src/context/index.ts +0 -2
- package/templates/gen_ai/src/context/use_app_context.ts +0 -17
- package/templates/gen_ai/src/index.tsx +0 -4
- package/templates/gen_ai/src/intents/design_editor/app.tsx +0 -19
- package/templates/gen_ai/src/intents/design_editor/home.tsx +0 -13
- package/templates/gen_ai/src/intents/design_editor/index.tsx +0 -17
- package/templates/gen_ai/src/pages/error.tsx +0 -41
- package/templates/gen_ai/src/pages/generate.tsx +0 -9
- package/templates/gen_ai/src/pages/index.ts +0 -3
- package/templates/gen_ai/src/pages/results.tsx +0 -31
- package/templates/gen_ai/src/routes/index.ts +0 -1
- package/templates/gen_ai/src/routes/paths.ts +0 -4
- package/templates/gen_ai/src/routes/routes.tsx +0 -24
- package/templates/gen_ai/src/utils/index.ts +0 -1
- package/templates/gen_ai/src/utils/obscenity_filter.ts +0 -33
- package/templates/gen_ai/styles/components.css +0 -38
- package/templates/gen_ai/styles/utils.css +0 -3
- package/templates/gen_ai/tsconfig.json +0 -56
- package/templates/gen_ai/utils/backend/base_backend/create.ts +0 -114
- package/templates/gen_ai/webpack.config.ts +0 -247
- package/templates/hello_world/canva-app.json +0 -21
- package/templates/hello_world/declarations/declarations.d.ts +0 -29
- package/templates/hello_world/eslint.config.mjs +0 -14
- package/templates/hello_world/package.json +0 -90
- package/templates/hello_world/scripts/copy_env.ts +0 -13
- package/templates/hello_world/scripts/ssl/ssl.ts +0 -131
- package/templates/hello_world/scripts/start/app_runner.ts +0 -223
- package/templates/hello_world/scripts/start/context.ts +0 -171
- package/templates/hello_world/scripts/start/start.ts +0 -46
- package/templates/hello_world/src/index.tsx +0 -4
- package/templates/hello_world/src/intents/design_editor/app.tsx +0 -86
- package/templates/hello_world/src/intents/design_editor/index.tsx +0 -25
- package/templates/hello_world/src/intents/design_editor/tests/__snapshots__/app.tests.tsx.snap +0 -45
- package/templates/hello_world/src/intents/design_editor/tests/app.tests.tsx +0 -92
- package/templates/hello_world/styles/components.css +0 -38
- package/templates/hello_world/tsconfig.json +0 -56
- package/templates/hello_world/webpack.config.ts +0 -247
- package/templates/mls/README.md +0 -81
- package/templates/mls/canva-app.json +0 -25
- package/templates/mls/declarations/declarations.d.ts +0 -29
- package/templates/mls/eslint.config.mjs +0 -14
- package/templates/mls/jest.config.mjs +0 -36
- package/templates/mls/jest.setup.ts +0 -39
- package/templates/mls/package.json +0 -117
- package/templates/mls/scripts/copy_env.ts +0 -13
- package/templates/mls/scripts/ssl/ssl.ts +0 -131
- package/templates/mls/scripts/start/app_runner.ts +0 -223
- package/templates/mls/scripts/start/context.ts +0 -171
- package/templates/mls/scripts/start/start.ts +0 -46
- package/templates/mls/src/__tests__/app.tests.tsx +0 -11
- package/templates/mls/src/__tests__/office_selection_page.tests.tsx +0 -72
- package/templates/mls/src/__tests__/utils.tsx +0 -19
- package/templates/mls/src/adapter.ts +0 -126
- package/templates/mls/src/components/agent/agent_card.tsx +0 -57
- package/templates/mls/src/components/agent/agent_grid.tsx +0 -37
- package/templates/mls/src/components/agent/agent_list.tsx +0 -17
- package/templates/mls/src/components/agent/agent_search_filters.tsx +0 -88
- package/templates/mls/src/components/breadcrumb/breadcrumb.tsx +0 -40
- package/templates/mls/src/components/listing/listing_card.tsx +0 -64
- package/templates/mls/src/components/listing/listing_grid.tsx +0 -37
- package/templates/mls/src/components/listing/listing_list.tsx +0 -21
- package/templates/mls/src/components/listing/listing_search_filters.tsx +0 -145
- package/templates/mls/src/components/placeholders/placeholders.tsx +0 -65
- package/templates/mls/src/data.ts +0 -359
- package/templates/mls/src/index.tsx +0 -4
- package/templates/mls/src/intents/design_editor/app.tsx +0 -44
- package/templates/mls/src/intents/design_editor/index.tsx +0 -25
- package/templates/mls/src/pages/agent_details_page/agent_details_page.tsx +0 -175
- package/templates/mls/src/pages/list_page/agent_tab_panel.tsx +0 -126
- package/templates/mls/src/pages/list_page/list_page.tsx +0 -67
- package/templates/mls/src/pages/list_page/listing_tab_panel.tsx +0 -135
- package/templates/mls/src/pages/listing_details_page/listing_details_page.tsx +0 -418
- package/templates/mls/src/pages/loading_page/loading_page.tsx +0 -152
- package/templates/mls/src/pages/office_selection_page/office_selection_page.tsx +0 -144
- package/templates/mls/src/real_estate.type.ts +0 -44
- package/templates/mls/src/util/use_add_element.tsx +0 -62
- package/templates/mls/src/util/use_drag_element.tsx +0 -68
- package/templates/mls/styles/components.css +0 -38
- package/templates/mls/tsconfig.json +0 -54
- package/templates/mls/webpack.config.ts +0 -248
- package/templates/optional/.cursor/mcp.json +0 -8
- package/templates/optional/.vscode/extensions.json +0 -6
- package/templates/optional/.vscode/mcp.json +0 -9
- package/templates/optional/AGENTS.md +0 -154
- 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
|
-
};
|