@canva/cli 0.0.1-beta.2 → 0.0.1-beta.20
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/README.md +184 -108
- package/cli.js +467 -394
- package/lib/cjs/index.cjs +388 -0
- package/lib/esm/index.mjs +388 -0
- package/package.json +12 -3
- package/templates/base/backend/routers/oauth.ts +393 -0
- package/templates/base/eslint.config.mjs +2 -4
- package/templates/base/package.json +31 -25
- package/templates/base/scripts/copy_env.ts +10 -0
- package/templates/base/scripts/start/app_runner.ts +39 -2
- package/templates/base/scripts/start/context.ts +12 -6
- package/templates/base/scripts/start/start.ts +11 -0
- package/templates/base/scripts/start/tests/start.tests.ts +61 -0
- package/templates/base/utils/backend/bearer_middleware/bearer_middleware.ts +101 -0
- package/templates/base/utils/backend/bearer_middleware/index.ts +1 -0
- package/templates/base/utils/backend/bearer_middleware/tests/bearer_middleware.tests.ts +192 -0
- package/templates/base/utils/use_add_element.ts +48 -0
- package/templates/base/utils/use_feature_support.ts +28 -0
- package/templates/base/{webpack.config.cjs → webpack.config.ts} +34 -42
- package/templates/common/.gitignore.template +5 -6
- package/templates/common/.nvmrc +1 -0
- package/templates/common/.prettierrc +21 -0
- package/templates/common/.vscode/extensions.json +6 -0
- package/templates/common/README.md +4 -74
- package/templates/common/conf/eslint-local-i18n-rules/index.mjs +181 -0
- package/templates/common/conf/{eslint-general.mjs → eslint_general.mjs} +40 -0
- package/templates/common/conf/eslint_i18n.mjs +41 -0
- package/templates/common/jest.config.mjs +29 -2
- package/templates/common/jest.setup.ts +20 -0
- package/templates/common/utils/backend/base_backend/create.ts +104 -0
- package/templates/common/utils/table_wrapper.ts +477 -0
- package/templates/common/utils/tests/table_wrapper.tests.ts +252 -0
- package/templates/common/utils/use_add_element.ts +48 -0
- package/templates/common/utils/use_feature_support.ts +28 -0
- package/templates/common/utils/use_overlay_hook.ts +74 -0
- package/templates/common/utils/use_selection_hook.ts +37 -0
- package/templates/dam/backend/routers/dam.ts +17 -12
- package/templates/dam/backend/server.ts +0 -7
- package/templates/dam/eslint.config.mjs +2 -2
- package/templates/dam/package.json +51 -39
- package/templates/dam/scripts/copy_env.ts +10 -0
- package/templates/dam/scripts/start/app_runner.ts +39 -2
- package/templates/dam/scripts/start/context.ts +12 -6
- package/templates/dam/scripts/start/start.ts +11 -0
- package/templates/dam/scripts/start/tests/start.tests.ts +61 -0
- package/templates/dam/src/app.tsx +26 -138
- package/templates/dam/src/config.ts +209 -87
- package/templates/{hello_world/webpack.config.cjs → dam/webpack.config.ts} +34 -42
- package/templates/gen_ai/README.md +1 -1
- package/templates/gen_ai/backend/routers/image.ts +3 -3
- package/templates/gen_ai/backend/server.ts +0 -7
- package/templates/gen_ai/eslint.config.mjs +2 -4
- package/templates/gen_ai/package.json +56 -42
- package/templates/gen_ai/scripts/copy_env.ts +10 -0
- package/templates/gen_ai/scripts/start/app_runner.ts +39 -2
- package/templates/gen_ai/scripts/start/context.ts +12 -6
- package/templates/gen_ai/scripts/start/start.ts +11 -0
- package/templates/gen_ai/scripts/start/tests/start.tests.ts +61 -0
- package/templates/gen_ai/src/api/api.ts +1 -39
- package/templates/gen_ai/src/app.tsx +16 -10
- package/templates/gen_ai/src/components/footer.messages.ts +0 -5
- package/templates/gen_ai/src/components/footer.tsx +2 -16
- package/templates/gen_ai/src/components/image_grid.tsx +8 -6
- package/templates/gen_ai/src/components/index.ts +0 -1
- package/templates/gen_ai/src/components/tests/remaining_credit.tests.tsx +43 -0
- package/templates/gen_ai/src/context/app_context.tsx +4 -26
- package/templates/gen_ai/src/context/context.messages.ts +1 -12
- package/templates/gen_ai/src/home.tsx +13 -0
- package/templates/gen_ai/src/index.tsx +2 -18
- package/templates/gen_ai/src/routes/routes.tsx +2 -2
- package/templates/gen_ai/utils/backend/bearer_middleware/bearer_middleware.ts +101 -0
- package/templates/gen_ai/utils/backend/bearer_middleware/index.ts +1 -0
- package/templates/gen_ai/utils/backend/bearer_middleware/tests/bearer_middleware.tests.ts +192 -0
- package/templates/gen_ai/{webpack.config.cjs → webpack.config.ts} +34 -42
- package/templates/hello_world/eslint.config.mjs +2 -4
- package/templates/hello_world/package.json +55 -33
- package/templates/hello_world/scripts/copy_env.ts +10 -0
- package/templates/hello_world/scripts/start/app_runner.ts +39 -2
- package/templates/hello_world/scripts/start/context.ts +12 -6
- package/templates/hello_world/scripts/start/start.ts +11 -0
- package/templates/hello_world/scripts/start/tests/start.tests.ts +61 -0
- package/templates/hello_world/src/app.tsx +24 -2
- package/templates/hello_world/src/tests/__snapshots__/app.tests.tsx.snap +45 -0
- package/templates/hello_world/src/tests/app.tests.tsx +86 -0
- package/templates/hello_world/utils/use_add_element.ts +48 -0
- package/templates/hello_world/utils/use_feature_support.ts +28 -0
- package/templates/{dam/webpack.config.cjs → hello_world/webpack.config.ts} +34 -42
- package/templates/common/conf/eslint-i18n.mjs +0 -23
- package/templates/dam/backend/database/database.ts +0 -42
- package/templates/dam/backend/routers/auth.ts +0 -285
- package/templates/gen_ai/backend/routers/auth.ts +0 -285
- package/templates/gen_ai/src/components/logged_in_status.tsx +0 -44
- package/templates/gen_ai/src/services/auth.tsx +0 -31
- package/templates/gen_ai/src/services/index.ts +0 -1
- /package/templates/{gen_ai → common}/utils/backend/jwt_middleware/index.ts +0 -0
- /package/templates/{gen_ai → common}/utils/backend/jwt_middleware/jwt_middleware.ts +0 -0
- /package/templates/{gen_ai → common}/utils/backend/jwt_middleware/tests/jwt_middleware.tests.ts +0 -0
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
/* eslint-disable no-console */
|
|
2
2
|
import type { Context } from "./context";
|
|
3
3
|
import * as chalk from "chalk";
|
|
4
|
-
import { buildConfig } from "../../webpack.config
|
|
4
|
+
import { buildConfig } from "../../webpack.config";
|
|
5
5
|
import * as ngrok from "@ngrok/ngrok";
|
|
6
6
|
import * as nodemon from "nodemon";
|
|
7
7
|
import * as Table from "cli-table3";
|
|
8
8
|
import * as webpack from "webpack";
|
|
9
9
|
import * as WebpackDevServer from "webpack-dev-server";
|
|
10
|
+
import * as open from "open";
|
|
11
|
+
import { generatePreviewUrl } from "@canva/cli";
|
|
10
12
|
import type { Certificate } from "../ssl/ssl";
|
|
11
13
|
import { createOrRetrieveCertificate } from "../ssl/ssl";
|
|
12
14
|
|
|
@@ -46,12 +48,18 @@ export class AppRunner {
|
|
|
46
48
|
}
|
|
47
49
|
}
|
|
48
50
|
|
|
49
|
-
const table = new Table(
|
|
51
|
+
const table = new Table({
|
|
52
|
+
colWidths: [30, 80],
|
|
53
|
+
wordWrap: true,
|
|
54
|
+
wrapOnWordBoundary: true,
|
|
55
|
+
});
|
|
50
56
|
|
|
51
57
|
const server = await this.runWebpackDevServer(ctx, table, cert);
|
|
52
58
|
|
|
53
59
|
await this.maybeRunBackendServer(ctx, table, cert, server);
|
|
54
60
|
|
|
61
|
+
await this.generateAndOpenPreviewUrl(ctx.openPreview, table);
|
|
62
|
+
|
|
55
63
|
console.log(table.toString(), "\n");
|
|
56
64
|
|
|
57
65
|
console.log(
|
|
@@ -161,4 +169,33 @@ export class AppRunner {
|
|
|
161
169
|
|
|
162
170
|
return server;
|
|
163
171
|
};
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Calls the Canva CLI to generate a preview URL for the app
|
|
175
|
+
*/
|
|
176
|
+
private readonly generateAndOpenPreviewUrl = async (
|
|
177
|
+
openPreview: boolean,
|
|
178
|
+
table: Table.Table,
|
|
179
|
+
) => {
|
|
180
|
+
const previewCellHeader = { content: "Preview your app in Canva" };
|
|
181
|
+
|
|
182
|
+
const generatePreviewResult = await generatePreviewUrl();
|
|
183
|
+
|
|
184
|
+
if (!generatePreviewResult.success) {
|
|
185
|
+
table.push([
|
|
186
|
+
previewCellHeader,
|
|
187
|
+
{ content: warnChalk(generatePreviewResult.message) },
|
|
188
|
+
]);
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
table.push([
|
|
193
|
+
previewCellHeader,
|
|
194
|
+
{ content: "Preview URL", href: generatePreviewResult.data },
|
|
195
|
+
]);
|
|
196
|
+
|
|
197
|
+
if (openPreview) {
|
|
198
|
+
open(generatePreviewResult.data);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
164
201
|
}
|
|
@@ -3,8 +3,10 @@ import * as path from "path";
|
|
|
3
3
|
|
|
4
4
|
interface CliArgs {
|
|
5
5
|
example?: string;
|
|
6
|
-
useHttps
|
|
7
|
-
ngrok
|
|
6
|
+
useHttps: boolean;
|
|
7
|
+
ngrok: boolean;
|
|
8
|
+
preview: boolean;
|
|
9
|
+
overrideFrontendPort?: number;
|
|
8
10
|
}
|
|
9
11
|
|
|
10
12
|
interface EnvVars {
|
|
@@ -45,7 +47,7 @@ export class Context {
|
|
|
45
47
|
}
|
|
46
48
|
|
|
47
49
|
get ngrokEnabled() {
|
|
48
|
-
return
|
|
50
|
+
return this.args.ngrok;
|
|
49
51
|
}
|
|
50
52
|
|
|
51
53
|
get hmrEnabled() {
|
|
@@ -53,7 +55,7 @@ export class Context {
|
|
|
53
55
|
}
|
|
54
56
|
|
|
55
57
|
get httpsEnabled() {
|
|
56
|
-
return
|
|
58
|
+
return this.args.useHttps;
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
get frontendEntryPath() {
|
|
@@ -69,11 +71,11 @@ export class Context {
|
|
|
69
71
|
}
|
|
70
72
|
|
|
71
73
|
get frontendUrl() {
|
|
72
|
-
return `${this.protocol}://localhost:${this.
|
|
74
|
+
return `${this.protocol}://localhost:${this.frontendPort}`;
|
|
73
75
|
}
|
|
74
76
|
|
|
75
77
|
get frontendPort() {
|
|
76
|
-
return this.envVars.frontendPort;
|
|
78
|
+
return this.args.overrideFrontendPort || this.envVars.frontendPort;
|
|
77
79
|
}
|
|
78
80
|
|
|
79
81
|
get developerBackendEntryPath(): string | undefined {
|
|
@@ -117,6 +119,10 @@ export class Context {
|
|
|
117
119
|
return this.envVars.appId;
|
|
118
120
|
}
|
|
119
121
|
|
|
122
|
+
get openPreview(): boolean {
|
|
123
|
+
return this.args.preview;
|
|
124
|
+
}
|
|
125
|
+
|
|
120
126
|
private get protocol(): "https" | "http" {
|
|
121
127
|
return this.httpsEnabled ? "https" : "http";
|
|
122
128
|
}
|
|
@@ -23,6 +23,17 @@ yargs(hideBin(process.argv))
|
|
|
23
23
|
default:
|
|
24
24
|
process.env.npm_config_use_https?.toLocaleLowerCase().trim() === "true",
|
|
25
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
|
+
})
|
|
26
37
|
.command(
|
|
27
38
|
"$0",
|
|
28
39
|
"Starts local development",
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
|
|
3
|
+
import type { ChildProcess } from "child_process";
|
|
4
|
+
import { spawn } from "child_process";
|
|
5
|
+
import * as treeKill from "tree-kill";
|
|
6
|
+
|
|
7
|
+
describe("start script", () => {
|
|
8
|
+
let serverProcess: ChildProcess;
|
|
9
|
+
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
if (serverProcess?.pid && !serverProcess.exitCode) {
|
|
12
|
+
treeKill(serverProcess.pid);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("should execute 'npm run start' and start a dev server", async () => {
|
|
17
|
+
const testServerPort = 8089;
|
|
18
|
+
serverProcess = await spawn(
|
|
19
|
+
`npm run start -- -p ${testServerPort} --no-preview`,
|
|
20
|
+
{
|
|
21
|
+
shell: true,
|
|
22
|
+
},
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
if (!serverProcess.pid) {
|
|
26
|
+
throw new Error("Unable to start server");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// wait for the server to start and collect output until it reports the running URL
|
|
30
|
+
let output = "";
|
|
31
|
+
await new Promise<void>((resolve, reject) => {
|
|
32
|
+
serverProcess.stdout?.on("data", (data) => {
|
|
33
|
+
output += data.toString();
|
|
34
|
+
if (output.includes("Development URL")) {
|
|
35
|
+
resolve();
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
serverProcess.stderr?.on("data", (data) => {
|
|
40
|
+
console.error("Server process error: ", data.toString());
|
|
41
|
+
reject();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// timeout and fail test if the server hasn't correctly started in 10 seconds
|
|
45
|
+
setTimeout(() => {
|
|
46
|
+
if (serverProcess?.pid && !serverProcess.exitCode) {
|
|
47
|
+
treeKill(serverProcess.pid);
|
|
48
|
+
}
|
|
49
|
+
reject(new Error("Test timed out"));
|
|
50
|
+
}, 10000);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// check that the server is running and accessible
|
|
54
|
+
const expectedServerURL = `http://localhost:${testServerPort}`;
|
|
55
|
+
expect(output).toContain(expectedServerURL);
|
|
56
|
+
expect(serverProcess.exitCode).toBeNull();
|
|
57
|
+
|
|
58
|
+
// clean up
|
|
59
|
+
treeKill(serverProcess.pid);
|
|
60
|
+
}, 15000); // 15 seconds timeout to allow for the server to start
|
|
61
|
+
});
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { auth } from "@canva/user";
|
|
2
1
|
import { POLLING_INTERVAL_IN_SECONDS } from "src/config";
|
|
3
2
|
|
|
4
3
|
/**
|
|
@@ -41,22 +40,12 @@ interface RemainingCreditsResult {
|
|
|
41
40
|
credits: number;
|
|
42
41
|
}
|
|
43
42
|
|
|
44
|
-
/**
|
|
45
|
-
* Represents the result of retrieving the logged in status of the user.
|
|
46
|
-
*/
|
|
47
|
-
export type LoggedInState =
|
|
48
|
-
| "authenticated"
|
|
49
|
-
| "not_authenticated"
|
|
50
|
-
| "checking"
|
|
51
|
-
| "error";
|
|
52
|
-
|
|
53
43
|
const endpoints = {
|
|
54
44
|
queueImageGeneration: "/api/queue-image-generation",
|
|
55
45
|
getImageGenerationJobStatus: "/api/job-status",
|
|
56
46
|
cancelImageGenerationJob: "/api/job-status/cancel",
|
|
57
47
|
getRemainingCredits: "/api/credits",
|
|
58
48
|
purchaseCredits: "/api/purchase-credits",
|
|
59
|
-
getLoggedInStatus: "/api/authentication/status",
|
|
60
49
|
};
|
|
61
50
|
|
|
62
51
|
/**
|
|
@@ -172,42 +161,15 @@ export const purchaseCredits = async (): Promise<RemainingCreditsResult> => {
|
|
|
172
161
|
};
|
|
173
162
|
|
|
174
163
|
/**
|
|
175
|
-
*
|
|
176
|
-
* This is example code, intended to convey the basic idea. When implementing this in your app, you might want more advanced checks.
|
|
177
|
-
*
|
|
178
|
-
* Note: You must register the provided endpoint via the Developer Portal.
|
|
179
|
-
*/
|
|
180
|
-
export const checkAuthenticationStatus = async (): Promise<LoggedInState> => {
|
|
181
|
-
try {
|
|
182
|
-
const url = new URL(endpoints.getLoggedInStatus, BACKEND_HOST);
|
|
183
|
-
const result: { isAuthenticated: string } = await sendRequest(url, {
|
|
184
|
-
method: "POST",
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
if (result?.isAuthenticated) {
|
|
188
|
-
return "authenticated";
|
|
189
|
-
} else {
|
|
190
|
-
return "not_authenticated";
|
|
191
|
-
}
|
|
192
|
-
} catch (error) {
|
|
193
|
-
// eslint-disable-next-line no-console
|
|
194
|
-
console.error(error);
|
|
195
|
-
return "error";
|
|
196
|
-
}
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Sends a request to the specified URL with authorization headers.
|
|
164
|
+
* Sends a request to the specified URL.
|
|
201
165
|
* @param {URL} url - The URL to send the request to.
|
|
202
166
|
* @param {RequestInit} [options] - Optional fetch options to be passed to the fetch function.
|
|
203
167
|
* @returns {Promise<Object>} - A promise that resolves to the response body.
|
|
204
168
|
*/
|
|
205
169
|
const sendRequest = async <T>(url: URL, options?: RequestInit): Promise<T> => {
|
|
206
|
-
const token = await auth.getCanvaUserToken();
|
|
207
170
|
const res = await fetch(url, {
|
|
208
171
|
headers: {
|
|
209
172
|
...options?.headers,
|
|
210
|
-
Authorization: `Bearer ${token}`,
|
|
211
173
|
},
|
|
212
174
|
...options,
|
|
213
175
|
});
|
|
@@ -1,13 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
1
|
+
import { createHashRouter, RouterProvider } from "react-router-dom";
|
|
2
|
+
import { ContextProvider } from "./context";
|
|
3
|
+
import { routes } from "./routes";
|
|
4
|
+
import { AppI18nProvider } from "@canva/app-i18n-kit";
|
|
5
|
+
import { AppUiProvider } from "@canva/app-ui-kit";
|
|
6
|
+
import { ErrorBoundary } from "react-error-boundary";
|
|
7
|
+
import { ErrorPage } from "./pages";
|
|
5
8
|
|
|
6
9
|
export const App = () => (
|
|
7
|
-
<
|
|
8
|
-
<
|
|
9
|
-
<
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
<AppI18nProvider>
|
|
11
|
+
<AppUiProvider>
|
|
12
|
+
<ErrorBoundary fallback={<ErrorPage />}>
|
|
13
|
+
<ContextProvider>
|
|
14
|
+
<RouterProvider router={createHashRouter(routes)} />
|
|
15
|
+
</ContextProvider>
|
|
16
|
+
</ErrorBoundary>
|
|
17
|
+
</AppUiProvider>
|
|
18
|
+
</AppI18nProvider>
|
|
13
19
|
);
|
|
@@ -11,11 +11,6 @@ export const FooterMessages = defineMessages({
|
|
|
11
11
|
defaultMessage: "Generate image",
|
|
12
12
|
description: "A button label to generate an image from a prompt",
|
|
13
13
|
},
|
|
14
|
-
signUpOrLogin: {
|
|
15
|
-
defaultMessage: "Sign up or log in to generate",
|
|
16
|
-
description:
|
|
17
|
-
"A button label to sign up or log in, before the user is able to generate an image",
|
|
18
|
-
},
|
|
19
14
|
purchaseMoreCredits: {
|
|
20
15
|
defaultMessage: "Purchase more credits",
|
|
21
16
|
description:
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { useNavigate, useLocation } from "react-router-dom";
|
|
2
2
|
import { Rows, Button } from "@canva/app-ui-kit";
|
|
3
3
|
import { queueImageGeneration, purchaseCredits } from "src/api";
|
|
4
|
-
import {
|
|
4
|
+
import { RemainingCredits } from "src/components";
|
|
5
5
|
import { NUMBER_OF_IMAGES_TO_GENERATE } from "src/config";
|
|
6
6
|
import { useAppContext } from "src/context";
|
|
7
7
|
import { Paths } from "src/routes";
|
|
8
|
-
import { useAuth } from "src/services";
|
|
9
8
|
import { getObsceneWords } from "src/utils";
|
|
10
9
|
import { useIntl } from "react-intl";
|
|
11
10
|
import { FooterMessages as Messages } from "./footer.messages";
|
|
@@ -13,10 +12,8 @@ import { FooterMessages as Messages } from "./footer.messages";
|
|
|
13
12
|
export const Footer = () => {
|
|
14
13
|
const navigate = useNavigate();
|
|
15
14
|
const { pathname } = useLocation();
|
|
16
|
-
const { requestAuthentication } = useAuth();
|
|
17
15
|
const isRootRoute = pathname === Paths.HOME;
|
|
18
16
|
const {
|
|
19
|
-
loggedInState,
|
|
20
17
|
setAppError,
|
|
21
18
|
promptInput,
|
|
22
19
|
setPromptInput,
|
|
@@ -86,10 +83,6 @@ export const Footer = () => {
|
|
|
86
83
|
navigate(Paths.RESULTS);
|
|
87
84
|
};
|
|
88
85
|
|
|
89
|
-
const onSignUpOrLogInClick = async () => {
|
|
90
|
-
await requestAuthentication();
|
|
91
|
-
};
|
|
92
|
-
|
|
93
86
|
const onPurchaseMoreCredits = async () => {
|
|
94
87
|
const { credits } = await purchaseCredits();
|
|
95
88
|
|
|
@@ -110,17 +103,11 @@ export const Footer = () => {
|
|
|
110
103
|
: intl.formatMessage(Messages.generateAgain),
|
|
111
104
|
visible: hasRemainingCredits,
|
|
112
105
|
},
|
|
113
|
-
{
|
|
114
|
-
variant: "primary" as const,
|
|
115
|
-
onClick: onSignUpOrLogInClick,
|
|
116
|
-
value: intl.formatMessage(Messages.signUpOrLogin),
|
|
117
|
-
visible: loggedInState === "not_authenticated" && !hasRemainingCredits,
|
|
118
|
-
},
|
|
119
106
|
{
|
|
120
107
|
variant: "primary" as const,
|
|
121
108
|
onClick: onPurchaseMoreCredits,
|
|
122
109
|
value: intl.formatMessage(Messages.purchaseMoreCredits),
|
|
123
|
-
visible:
|
|
110
|
+
visible: !hasRemainingCredits,
|
|
124
111
|
},
|
|
125
112
|
{
|
|
126
113
|
variant: "secondary" as const,
|
|
@@ -151,7 +138,6 @@ export const Footer = () => {
|
|
|
151
138
|
),
|
|
152
139
|
)}
|
|
153
140
|
<RemainingCredits />
|
|
154
|
-
<LoggedInStatus />
|
|
155
141
|
</Rows>
|
|
156
142
|
);
|
|
157
143
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Grid, ImageCard, Rows, Text } from "@canva/app-ui-kit";
|
|
2
2
|
import type { QueuedImage } from "@canva/asset";
|
|
3
3
|
import { upload } from "@canva/asset";
|
|
4
|
-
import {
|
|
4
|
+
import { addElementAtPoint, ui } from "@canva/design";
|
|
5
5
|
import type { ImageType } from "src/api";
|
|
6
6
|
import { useAppContext } from "src/context";
|
|
7
7
|
import * as styles from "styles/utils.css";
|
|
@@ -12,12 +12,13 @@ const THUMBNAIL_HEIGHT = 150;
|
|
|
12
12
|
const uploadImage = async (image: ImageType): Promise<QueuedImage> => {
|
|
13
13
|
// Upload the image using @canva/asset.
|
|
14
14
|
const queuedImage = await upload({
|
|
15
|
-
type: "
|
|
15
|
+
type: "image",
|
|
16
16
|
mimeType: "image/jpeg",
|
|
17
17
|
thumbnailUrl: image.thumbnail.url,
|
|
18
18
|
url: image.fullsize.url,
|
|
19
19
|
width: image.fullsize.width,
|
|
20
20
|
height: image.fullsize.height,
|
|
21
|
+
aiDisclosure: "app_generated",
|
|
21
22
|
});
|
|
22
23
|
|
|
23
24
|
return queuedImage;
|
|
@@ -34,8 +35,8 @@ export const ImageGrid = () => {
|
|
|
34
35
|
try {
|
|
35
36
|
parentNode?.classList.add(styles.hidden);
|
|
36
37
|
|
|
37
|
-
await ui.
|
|
38
|
-
type: "
|
|
38
|
+
await ui.startDragToPoint(event, {
|
|
39
|
+
type: "image",
|
|
39
40
|
resolveImageRef: () => uploadImage(image),
|
|
40
41
|
previewUrl: image.thumbnail.url,
|
|
41
42
|
previewSize: {
|
|
@@ -55,8 +56,9 @@ export const ImageGrid = () => {
|
|
|
55
56
|
const onImageClick = async (image: ImageType) => {
|
|
56
57
|
const queuedImage = await uploadImage(image);
|
|
57
58
|
|
|
58
|
-
await
|
|
59
|
-
type: "
|
|
59
|
+
await addElementAtPoint({
|
|
60
|
+
type: "image",
|
|
61
|
+
altText: { text: image.label, decorative: false },
|
|
60
62
|
ref: queuedImage.ref,
|
|
61
63
|
});
|
|
62
64
|
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/* eslint-disable formatjs/no-literal-string-in-jsx */
|
|
2
|
+
import { TestAppUiProvider } from "@canva/app-ui-kit";
|
|
3
|
+
import { TestAppI18nProvider } from "@canva/app-i18n-kit";
|
|
4
|
+
import type { RenderResult } from "@testing-library/react";
|
|
5
|
+
import { fireEvent, render } from "@testing-library/react";
|
|
6
|
+
import React from "react";
|
|
7
|
+
import { RemainingCredits } from "../remaining_credits";
|
|
8
|
+
import { requestOpenExternalUrl } from "@canva/platform";
|
|
9
|
+
|
|
10
|
+
function renderInTestProvider(node: React.ReactNode): RenderResult {
|
|
11
|
+
return render(
|
|
12
|
+
// In a test environment, you should wrap your apps in `TestAppI18nProvider` and `TestAppUiProvider`, rather than `AppI18nProvider` and `AppUiProvider`
|
|
13
|
+
<TestAppI18nProvider>
|
|
14
|
+
<TestAppUiProvider>{node}</TestAppUiProvider>,
|
|
15
|
+
</TestAppI18nProvider>,
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// This test demonstrates how to test code that uses functions from the Canva Apps SDK
|
|
20
|
+
// For more information on testing with the Canva Apps SDK, see https://www.canva.dev/docs/apps/testing/
|
|
21
|
+
describe("Remaining Credit Tests", () => {
|
|
22
|
+
const mockRequestOpenExternalUrl = jest.mocked(requestOpenExternalUrl);
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
jest.resetAllMocks();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("should call requestOpenExternalUrl when the link is clicked", () => {
|
|
29
|
+
// assert that the mock is in the expected clean state
|
|
30
|
+
expect(mockRequestOpenExternalUrl).not.toHaveBeenCalled();
|
|
31
|
+
|
|
32
|
+
const result = renderInTestProvider(<RemainingCredits />);
|
|
33
|
+
|
|
34
|
+
// get a reference to the link to purchase more credits
|
|
35
|
+
const purchaseMoreLink = result.getByRole("button");
|
|
36
|
+
|
|
37
|
+
// programmatically simulate clicking the button
|
|
38
|
+
fireEvent.click(purchaseMoreLink);
|
|
39
|
+
|
|
40
|
+
// we expect that requestOpenExternalUrl has been called
|
|
41
|
+
expect(mockRequestOpenExternalUrl).toHaveBeenCalled();
|
|
42
|
+
});
|
|
43
|
+
});
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { createContext, useEffect, useState } from "react";
|
|
2
2
|
import { useIntl } from "react-intl";
|
|
3
|
-
import type { ImageType
|
|
4
|
-
import {
|
|
3
|
+
import type { ImageType } from "src/api";
|
|
4
|
+
import { getRemainingCredits } from "src/api";
|
|
5
5
|
import { ContextMessages as Messages } from "./context.messages";
|
|
6
6
|
|
|
7
7
|
export interface AppContextType {
|
|
8
|
-
loggedInState: LoggedInState;
|
|
9
|
-
setLoggedInState: (value: LoggedInState) => void;
|
|
10
8
|
appError: string;
|
|
11
9
|
setAppError: (value: string) => void;
|
|
12
10
|
creditsError: string;
|
|
@@ -28,8 +26,6 @@ export interface AppContextType {
|
|
|
28
26
|
}
|
|
29
27
|
|
|
30
28
|
export const AppContext = createContext<AppContextType>({
|
|
31
|
-
loggedInState: "not_authenticated",
|
|
32
|
-
setLoggedInState: () => {},
|
|
33
29
|
appError: "",
|
|
34
30
|
setAppError: () => {},
|
|
35
31
|
creditsError: "",
|
|
@@ -65,8 +61,6 @@ export const ContextProvider = ({
|
|
|
65
61
|
}: {
|
|
66
62
|
children: React.ReactNode;
|
|
67
63
|
}): JSX.Element => {
|
|
68
|
-
const [loggedInState, setLoggedInState] =
|
|
69
|
-
useState<LoggedInState>("not_authenticated");
|
|
70
64
|
const [appError, setAppError] = useState<string>("");
|
|
71
65
|
const [loadingApp, setLoadingApp] = useState<boolean>(true); // set to true to prevent ui flash on load
|
|
72
66
|
const [isLoadingImages, setIsLoadingImages] = useState<boolean>(false);
|
|
@@ -95,17 +89,6 @@ export const ContextProvider = ({
|
|
|
95
89
|
// eslint-disable-next-line no-console
|
|
96
90
|
console.error("Error fetching remaining credits:", error);
|
|
97
91
|
}
|
|
98
|
-
|
|
99
|
-
// Fetch login status
|
|
100
|
-
try {
|
|
101
|
-
checkAuthenticationStatus();
|
|
102
|
-
} catch (error) {
|
|
103
|
-
setAppError(
|
|
104
|
-
intl.formatMessage(Messages.appErrorGetLoggedInStatusFailed),
|
|
105
|
-
);
|
|
106
|
-
// eslint-disable-next-line no-console
|
|
107
|
-
console.error("Error fetching login status:", error);
|
|
108
|
-
}
|
|
109
92
|
} catch (error) {
|
|
110
93
|
setAppError(intl.formatMessage(Messages.appErrorGeneral));
|
|
111
94
|
// eslint-disable-next-line no-console
|
|
@@ -125,13 +108,10 @@ export const ContextProvider = ({
|
|
|
125
108
|
return;
|
|
126
109
|
}
|
|
127
110
|
|
|
128
|
-
const errorMessage =
|
|
129
|
-
loggedInState === "authenticated"
|
|
130
|
-
? intl.formatMessage(Messages.alertNotEnoughCreditsLoggedIn)
|
|
131
|
-
: intl.formatMessage(Messages.alertNotEnoughCreditsLoggedOut);
|
|
111
|
+
const errorMessage = intl.formatMessage(Messages.alertNotEnoughCredits);
|
|
132
112
|
|
|
133
113
|
setCreditsError(errorMessage);
|
|
134
|
-
}, [loadingApp, remainingCredits
|
|
114
|
+
}, [loadingApp, remainingCredits]);
|
|
135
115
|
|
|
136
116
|
const setPromptInputHandler = (value: string) => {
|
|
137
117
|
if (
|
|
@@ -148,8 +128,6 @@ export const ContextProvider = ({
|
|
|
148
128
|
};
|
|
149
129
|
|
|
150
130
|
const value: AppContextType = {
|
|
151
|
-
loggedInState,
|
|
152
|
-
setLoggedInState,
|
|
153
131
|
appError,
|
|
154
132
|
setAppError,
|
|
155
133
|
creditsError,
|
|
@@ -12,11 +12,6 @@ export const ContextMessages = defineMessages({
|
|
|
12
12
|
description:
|
|
13
13
|
"A message to indicate that there was a failure to get the number of credits the user has",
|
|
14
14
|
},
|
|
15
|
-
appErrorGetLoggedInStatusFailed: {
|
|
16
|
-
defaultMessage: "Retrieving logged in status has failed.",
|
|
17
|
-
description:
|
|
18
|
-
"A message to indicate that due to an unexpected problem, the app was unable to determine if the user is logged in",
|
|
19
|
-
},
|
|
20
15
|
|
|
21
16
|
/** Messages related to prompts and user input validation. */
|
|
22
17
|
promptMissingErrorMessage: {
|
|
@@ -26,16 +21,10 @@ export const ContextMessages = defineMessages({
|
|
|
26
21
|
},
|
|
27
22
|
|
|
28
23
|
/** Messages related to credits, including their availability and purchasing options. */
|
|
29
|
-
|
|
24
|
+
alertNotEnoughCredits: {
|
|
30
25
|
defaultMessage:
|
|
31
26
|
"You don’t have enough credits left to generate an image. Please purchase more.",
|
|
32
27
|
description:
|
|
33
28
|
"A message to indicate that the user doesn't have enough credits to generate an image, and will need to buy more to continue",
|
|
34
29
|
},
|
|
35
|
-
alertNotEnoughCreditsLoggedOut: {
|
|
36
|
-
defaultMessage:
|
|
37
|
-
"You don’t have enough credits left to generate an image. Please sign up or log in to purchase more.",
|
|
38
|
-
description:
|
|
39
|
-
"A message to indicate that the user doesn't have enough credits to generate an image, and will need to sign up or log in to buy more",
|
|
40
|
-
},
|
|
41
30
|
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Outlet } from "react-router-dom";
|
|
2
|
+
import { Rows } from "@canva/app-ui-kit";
|
|
3
|
+
import { Footer } from "./components";
|
|
4
|
+
import * as styles from "styles/components.css";
|
|
5
|
+
|
|
6
|
+
export const Home = () => (
|
|
7
|
+
<div className={styles.scrollContainer}>
|
|
8
|
+
<Rows spacing="3u">
|
|
9
|
+
<Outlet />
|
|
10
|
+
<Footer />
|
|
11
|
+
</Rows>
|
|
12
|
+
</div>
|
|
13
|
+
);
|
|
@@ -1,27 +1,11 @@
|
|
|
1
1
|
import { createRoot } from "react-dom/client";
|
|
2
|
-
import { createHashRouter, RouterProvider } from "react-router-dom";
|
|
3
|
-
import { ErrorBoundary } from "react-error-boundary";
|
|
4
|
-
import { AppUiProvider } from "@canva/app-ui-kit";
|
|
5
|
-
import { routes } from "./routes";
|
|
6
|
-
import { ErrorPage } from "./pages";
|
|
7
|
-
import { ContextProvider } from "./context";
|
|
8
2
|
import "@canva/app-ui-kit/styles.css";
|
|
9
|
-
import {
|
|
3
|
+
import { App } from "./app";
|
|
10
4
|
|
|
11
5
|
const root = createRoot(document.getElementById("root") as Element);
|
|
12
6
|
|
|
13
7
|
function render() {
|
|
14
|
-
root.render(
|
|
15
|
-
<AppI18nProvider>
|
|
16
|
-
<AppUiProvider>
|
|
17
|
-
<ErrorBoundary fallback={<ErrorPage />}>
|
|
18
|
-
<ContextProvider>
|
|
19
|
-
<RouterProvider router={createHashRouter(routes)} />
|
|
20
|
-
</ContextProvider>
|
|
21
|
-
</ErrorBoundary>
|
|
22
|
-
</AppUiProvider>
|
|
23
|
-
</AppI18nProvider>,
|
|
24
|
-
);
|
|
8
|
+
root.render(<App />);
|
|
25
9
|
}
|
|
26
10
|
|
|
27
11
|
render();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Home } from "src/home";
|
|
2
2
|
import { ErrorPage, GeneratePage, ResultsPage } from "src/pages";
|
|
3
3
|
|
|
4
4
|
export enum Paths {
|
|
@@ -9,7 +9,7 @@ export enum Paths {
|
|
|
9
9
|
export const routes = [
|
|
10
10
|
{
|
|
11
11
|
path: Paths.HOME,
|
|
12
|
-
element: <
|
|
12
|
+
element: <Home />,
|
|
13
13
|
errorElement: <ErrorPage />,
|
|
14
14
|
children: [
|
|
15
15
|
{
|