@canva/cli 0.0.1-beta.2 → 0.0.1-beta.4
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 +177 -109
- package/cli.js +267 -267
- package/package.json +1 -1
- package/templates/base/backend/routers/oauth.ts +393 -0
- package/templates/base/eslint.config.mjs +0 -2
- package/templates/base/package.json +22 -19
- 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/common/.gitignore.template +5 -6
- package/templates/common/README.md +4 -74
- package/templates/common/conf/eslint-general.mjs +26 -0
- package/templates/common/conf/eslint-i18n.mjs +18 -3
- package/templates/dam/backend/server.ts +0 -7
- package/templates/dam/package.json +27 -27
- package/templates/dam/src/app.tsx +2 -135
- 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 +0 -2
- package/templates/gen_ai/package.json +28 -28
- package/templates/gen_ai/src/api/api.ts +1 -39
- 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/context/app_context.tsx +4 -26
- package/templates/gen_ai/src/context/context.messages.ts +1 -12
- 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/hello_world/eslint.config.mjs +0 -2
- package/templates/hello_world/package.json +20 -19
- package/templates/hello_world/src/app.tsx +4 -2
- 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/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/utils/backend/jwt_middleware/index.ts +0 -1
- package/templates/gen_ai/utils/backend/jwt_middleware/jwt_middleware.ts +0 -229
- package/templates/gen_ai/utils/backend/jwt_middleware/tests/jwt_middleware.tests.ts +0 -630
|
@@ -1,147 +1,14 @@
|
|
|
1
|
-
import { useEffect, useState } from "react";
|
|
2
1
|
import { SearchableListView } from "@canva/app-components";
|
|
3
|
-
import {
|
|
4
|
-
import type { Authentication } from "@canva/user";
|
|
5
|
-
import { auth } from "@canva/user";
|
|
2
|
+
import { Box } from "@canva/app-ui-kit";
|
|
6
3
|
import { findResources } from "./adapter";
|
|
7
4
|
import { config } from "./config";
|
|
8
5
|
import * as styles from "./index.css";
|
|
9
6
|
import "@canva/app-ui-kit/styles.css";
|
|
10
7
|
|
|
11
|
-
type AuthenticationState =
|
|
12
|
-
| "authenticated"
|
|
13
|
-
| "not_authenticated"
|
|
14
|
-
| "checking"
|
|
15
|
-
| "cancelled"
|
|
16
|
-
| "error";
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* This endpoint is defined in the ./backend/server.ts file. You need to
|
|
20
|
-
* register the endpoint in the Developer Portal before sending requests.
|
|
21
|
-
*
|
|
22
|
-
* BACKEND_HOST is configured in the root .env file, for more information,
|
|
23
|
-
* refer to the README.md.
|
|
24
|
-
*/
|
|
25
|
-
const AUTHENTICATION_CHECK_URL = `${BACKEND_HOST}/api/authentication/status`;
|
|
26
|
-
|
|
27
|
-
const checkAuthenticationStatus = async (
|
|
28
|
-
auth: Authentication,
|
|
29
|
-
): Promise<AuthenticationState> => {
|
|
30
|
-
/**
|
|
31
|
-
* Send a request to an endpoint that checks if the user is authenticated.
|
|
32
|
-
* This is example code, intended to convey the basic idea. When implementing this in your app, you might want more advanced checks.
|
|
33
|
-
*
|
|
34
|
-
* Note: You must register the provided endpoint via the Developer Portal.
|
|
35
|
-
*/
|
|
36
|
-
try {
|
|
37
|
-
const token = await auth.getCanvaUserToken();
|
|
38
|
-
const res = await fetch(AUTHENTICATION_CHECK_URL, {
|
|
39
|
-
headers: {
|
|
40
|
-
Authorization: `Bearer ${token}`,
|
|
41
|
-
},
|
|
42
|
-
method: "POST",
|
|
43
|
-
});
|
|
44
|
-
const body = await res.json();
|
|
45
|
-
|
|
46
|
-
if (body?.isAuthenticated) {
|
|
47
|
-
return "authenticated";
|
|
48
|
-
} else {
|
|
49
|
-
return "not_authenticated";
|
|
50
|
-
}
|
|
51
|
-
} catch (error) {
|
|
52
|
-
// eslint-disable-next-line no-console
|
|
53
|
-
console.error(error);
|
|
54
|
-
return "error";
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
|
|
58
8
|
export function App() {
|
|
59
|
-
|
|
60
|
-
const [authState, setAuthState] = useState<AuthenticationState>("checking");
|
|
61
|
-
|
|
62
|
-
useEffect(() => {
|
|
63
|
-
setAuthState("checking");
|
|
64
|
-
checkAuthenticationStatus(auth).then((status) => {
|
|
65
|
-
setAuthState(status);
|
|
66
|
-
});
|
|
67
|
-
}, []);
|
|
68
|
-
|
|
69
|
-
useEffect(() => {
|
|
70
|
-
if (authState === "not_authenticated") {
|
|
71
|
-
startAuthenticationFlow();
|
|
72
|
-
}
|
|
73
|
-
}, [authState]);
|
|
74
|
-
|
|
75
|
-
const startAuthenticationFlow = async () => {
|
|
76
|
-
try {
|
|
77
|
-
const response = await auth.requestAuthentication();
|
|
78
|
-
switch (response.status) {
|
|
79
|
-
case "COMPLETED":
|
|
80
|
-
setAuthState("authenticated");
|
|
81
|
-
break;
|
|
82
|
-
case "ABORTED":
|
|
83
|
-
// eslint-disable-next-line no-console
|
|
84
|
-
console.warn("Authentication aborted by user.");
|
|
85
|
-
setAuthState("cancelled");
|
|
86
|
-
break;
|
|
87
|
-
case "DENIED":
|
|
88
|
-
// eslint-disable-next-line no-console
|
|
89
|
-
console.warn("Authentication denied by user", response.details);
|
|
90
|
-
setAuthState("cancelled");
|
|
91
|
-
break;
|
|
92
|
-
default:
|
|
93
|
-
// eslint-disable-next-line no-console
|
|
94
|
-
console.log("Unknown auth state");
|
|
95
|
-
break;
|
|
96
|
-
}
|
|
97
|
-
} catch (e) {
|
|
98
|
-
// eslint-disable-next-line no-console
|
|
99
|
-
console.error(e);
|
|
100
|
-
setAuthState("error");
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
if (authState === "error") {
|
|
105
|
-
// eslint-disable-next-line no-console
|
|
106
|
-
console.warn(
|
|
107
|
-
"Warning: authentication not enabled on this app. Please enable auth with the instructions in README",
|
|
108
|
-
);
|
|
109
|
-
// Comment this next line out for production apps
|
|
110
|
-
setAuthState("authenticated");
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// If user has denied or aborted auth flow
|
|
114
|
-
if (authState === "cancelled") {
|
|
115
|
-
return (
|
|
116
|
-
<Box paddingEnd="2u" height="full" className={styles.centerInPage}>
|
|
117
|
-
<Rows spacing="2u" align="center">
|
|
118
|
-
<Alert tone="critical">
|
|
119
|
-
Something went wrong while authenticating
|
|
120
|
-
</Alert>
|
|
121
|
-
<Button
|
|
122
|
-
variant="primary"
|
|
123
|
-
onClick={startAuthenticationFlow}
|
|
124
|
-
stretch={true}
|
|
125
|
-
>
|
|
126
|
-
Start authentication flow
|
|
127
|
-
</Button>
|
|
128
|
-
</Rows>
|
|
129
|
-
</Box>
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return authState === "authenticated" ? (
|
|
9
|
+
return (
|
|
134
10
|
<Box className={styles.rootWrapper}>
|
|
135
11
|
<SearchableListView config={config} findResources={findResources} />
|
|
136
12
|
</Box>
|
|
137
|
-
) : (
|
|
138
|
-
<Box
|
|
139
|
-
width="full"
|
|
140
|
-
height="full"
|
|
141
|
-
paddingTop="1u"
|
|
142
|
-
className={styles.centerInPage}
|
|
143
|
-
>
|
|
144
|
-
<LoadingIndicator size="large" />
|
|
145
|
-
</Box>
|
|
146
13
|
);
|
|
147
14
|
}
|
|
@@ -20,7 +20,7 @@ In this template, we've included a basic obscenity filter to stop users from cre
|
|
|
20
20
|
|
|
21
21
|
### Backend
|
|
22
22
|
|
|
23
|
-
This template includes a simple Express server as a sample backend. Please note that this server is not production-ready, and we advise using it solely for instructional purposes to demonstrate API calls.
|
|
23
|
+
This template includes a simple Express server as a sample backend. Please note that this server is not production-ready, and we advise using it solely for instructional purposes to demonstrate API calls. If you require authentication for your app, we recommend looking at the authentication example provided in the [starter kit](https://github.com/canva-sdks/canva-apps-sdk-starter-kit).
|
|
24
24
|
|
|
25
25
|
### Thumbnails
|
|
26
26
|
|
|
@@ -93,7 +93,7 @@ export const createImageRouter = () => {
|
|
|
93
93
|
|
|
94
94
|
/**
|
|
95
95
|
* GET endpoint to retrieve user credits.
|
|
96
|
-
*
|
|
96
|
+
* Returns the current number of credits available to the user.
|
|
97
97
|
*/
|
|
98
98
|
router.get(Routes.CREDITS, async (req, res) => {
|
|
99
99
|
res.status(200).send({
|
|
@@ -103,7 +103,7 @@ export const createImageRouter = () => {
|
|
|
103
103
|
|
|
104
104
|
/**
|
|
105
105
|
* POST endpoint to purchase credits.
|
|
106
|
-
*
|
|
106
|
+
* Increments the user's credits by the number of credits in a bundle.
|
|
107
107
|
* This endpoint should be backed by proper input validation to prevent misuse.
|
|
108
108
|
*/
|
|
109
109
|
router.post(Routes.PURCHASE_CREDITS, async (req, res) => {
|
|
@@ -115,7 +115,7 @@ export const createImageRouter = () => {
|
|
|
115
115
|
|
|
116
116
|
/**
|
|
117
117
|
* GET endpoint to generate images based on a prompt.
|
|
118
|
-
*
|
|
118
|
+
* Generates images based on the provided prompt and adds a job to the processing queue.
|
|
119
119
|
* If there are not enough credits, it returns a 403 error.
|
|
120
120
|
* If the prompt parameter is missing, it returns a 400 error.
|
|
121
121
|
* Once the job is added to the queue, it returns a jobId that can be used to check the job status.
|
|
@@ -2,7 +2,6 @@ import * as express from "express";
|
|
|
2
2
|
import * as cors from "cors";
|
|
3
3
|
import { createBaseServer } from "../utils/backend/base_backend/create";
|
|
4
4
|
import { createImageRouter } from "./routers/image";
|
|
5
|
-
import { createAuthRouter } from "./routers/auth";
|
|
6
5
|
|
|
7
6
|
async function main() {
|
|
8
7
|
const router = express.Router();
|
|
@@ -37,12 +36,6 @@ async function main() {
|
|
|
37
36
|
*/
|
|
38
37
|
router.use(cors());
|
|
39
38
|
|
|
40
|
-
/**
|
|
41
|
-
* Add routes for authorisation.
|
|
42
|
-
*/
|
|
43
|
-
const authRouter = createAuthRouter();
|
|
44
|
-
router.use(authRouter);
|
|
45
|
-
|
|
46
39
|
/**
|
|
47
40
|
* Add routes for image generation.
|
|
48
41
|
*/
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"name": "gen_ai",
|
|
4
4
|
"description": "An example app demonstrating common patterns that you might use in a generative AI app.",
|
|
5
5
|
"scripts": {
|
|
6
|
-
"extract": "formatjs extract
|
|
6
|
+
"extract": "formatjs extract src/**/*.{ts,tsx} --out-file dist/messages_en.json",
|
|
7
7
|
"build": "webpack --config webpack.config.cjs --mode production && npm run extract",
|
|
8
8
|
"format": "prettier '**/*.{css,ts,tsx}' --no-config --write",
|
|
9
9
|
"format:check": "prettier '**/*.{css,ts,tsx}' --no-config --check --ignore-path",
|
|
@@ -16,57 +16,57 @@
|
|
|
16
16
|
"test:watch": "jest --watchAll"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@canva/app-i18n-kit": "^0.0
|
|
20
|
-
"@canva/app-ui-kit": "^
|
|
21
|
-
"@canva/asset": "^
|
|
22
|
-
"@canva/design": "^1.
|
|
23
|
-
"@canva/platform": "^
|
|
24
|
-
"@canva/user": "^
|
|
25
|
-
"cookie-parser": "1.4.
|
|
19
|
+
"@canva/app-i18n-kit": "^1.0.0",
|
|
20
|
+
"@canva/app-ui-kit": "^4.1.0",
|
|
21
|
+
"@canva/asset": "^2.0.0",
|
|
22
|
+
"@canva/design": "^2.1.0",
|
|
23
|
+
"@canva/platform": "^2.0.0",
|
|
24
|
+
"@canva/user": "^2.0.0",
|
|
25
|
+
"cookie-parser": "1.4.7",
|
|
26
26
|
"cors": "2.8.5",
|
|
27
|
-
"html-react-parser": "5.1.
|
|
27
|
+
"html-react-parser": "5.1.18",
|
|
28
28
|
"obscenity": "0.4.0",
|
|
29
29
|
"react": "18.3.1",
|
|
30
30
|
"react-dom": "18.3.1",
|
|
31
31
|
"react-error-boundary": "4.0.13",
|
|
32
32
|
"react-intl": "6.6.8",
|
|
33
|
-
"react-router-dom": "6.
|
|
33
|
+
"react-router-dom": "6.27.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@eslint/eslintrc": "3.1.0",
|
|
37
|
-
"@eslint/js": "9.
|
|
37
|
+
"@eslint/js": "9.12.0",
|
|
38
38
|
"@formatjs/cli": "6.2.12",
|
|
39
39
|
"@formatjs/ts-transformer": "3.13.14",
|
|
40
40
|
"@ngrok/ngrok": "1.4.1",
|
|
41
41
|
"@svgr/webpack": "8.1.0",
|
|
42
42
|
"@types/debug": "4.1.12",
|
|
43
43
|
"@types/express": "4.17.21",
|
|
44
|
-
"@types/express-serve-static-core": "4.19.
|
|
45
|
-
"@types/jest": "29.5.
|
|
46
|
-
"@types/jsonwebtoken": "9.0.
|
|
44
|
+
"@types/express-serve-static-core": "4.19.6",
|
|
45
|
+
"@types/jest": "29.5.13",
|
|
46
|
+
"@types/jsonwebtoken": "9.0.7",
|
|
47
47
|
"@types/node": "20.10.0",
|
|
48
48
|
"@types/node-fetch": "2.6.11",
|
|
49
49
|
"@types/node-forge": "1.3.11",
|
|
50
50
|
"@types/nodemon": "1.19.6",
|
|
51
51
|
"@types/prompts": "2.4.9",
|
|
52
|
-
"@types/react": "18.3.
|
|
53
|
-
"@types/react-dom": "18.3.
|
|
52
|
+
"@types/react": "18.3.11",
|
|
53
|
+
"@types/react-dom": "18.3.1",
|
|
54
54
|
"@types/webpack-env": "1.18.5",
|
|
55
|
-
"@typescript-eslint/eslint-plugin": "8.
|
|
56
|
-
"@typescript-eslint/parser": "8.
|
|
55
|
+
"@typescript-eslint/eslint-plugin": "8.9.0",
|
|
56
|
+
"@typescript-eslint/parser": "8.9.0",
|
|
57
57
|
"chalk": "4.1.2",
|
|
58
58
|
"cli-table3": "0.6.5",
|
|
59
59
|
"css-loader": "7.1.2",
|
|
60
60
|
"css-modules-typescript-loader": "4.0.1",
|
|
61
|
-
"cssnano": "7.0.
|
|
62
|
-
"debug": "4.3.
|
|
61
|
+
"cssnano": "7.0.6",
|
|
62
|
+
"debug": "4.3.7",
|
|
63
63
|
"dotenv": "16.4.5",
|
|
64
|
-
"eslint": "
|
|
65
|
-
"eslint-plugin-formatjs": "
|
|
66
|
-
"eslint-plugin-jest": "28.8.
|
|
67
|
-
"eslint-plugin-react": "7.
|
|
64
|
+
"eslint": "9.12.0",
|
|
65
|
+
"eslint-plugin-formatjs": "5.0.0",
|
|
66
|
+
"eslint-plugin-jest": "28.8.3",
|
|
67
|
+
"eslint-plugin-react": "7.37.1",
|
|
68
68
|
"exponential-backoff": "3.1.1",
|
|
69
|
-
"express": "4.
|
|
69
|
+
"express": "4.21.1",
|
|
70
70
|
"express-basic-auth": "1.2.1",
|
|
71
71
|
"jest": "29.7.0",
|
|
72
72
|
"jsonwebtoken": "9.0.2",
|
|
@@ -80,14 +80,14 @@
|
|
|
80
80
|
"prompts": "2.4.2",
|
|
81
81
|
"style-loader": "4.0.0",
|
|
82
82
|
"terser-webpack-plugin": "5.3.10",
|
|
83
|
-
"ts-jest": "29.2.
|
|
83
|
+
"ts-jest": "29.2.5",
|
|
84
84
|
"ts-loader": "9.5.1",
|
|
85
85
|
"ts-node": "10.9.2",
|
|
86
86
|
"typescript": "5.5.4",
|
|
87
87
|
"url-loader": "4.1.1",
|
|
88
|
-
"webpack": "5.
|
|
88
|
+
"webpack": "5.95.0",
|
|
89
89
|
"webpack-cli": "5.1.4",
|
|
90
|
-
"webpack-dev-server": "5.0
|
|
90
|
+
"webpack-dev-server": "5.1.0",
|
|
91
91
|
"yargs": "17.7.2"
|
|
92
92
|
}
|
|
93
93
|
}
|
|
@@ -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
|
});
|
|
@@ -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
|
};
|
|
@@ -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
|
});
|