@canva/cli 0.0.1-beta.28 → 0.0.1-beta.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/cli.js +257 -257
  2. package/package.json +1 -1
  3. package/templates/data_connector/README.md +84 -0
  4. package/templates/data_connector/declarations/declarations.d.ts +29 -0
  5. package/templates/data_connector/eslint.config.mjs +14 -0
  6. package/templates/data_connector/package.json +91 -0
  7. package/templates/data_connector/scripts/copy_env.ts +10 -0
  8. package/templates/data_connector/scripts/ssl/ssl.ts +131 -0
  9. package/templates/data_connector/scripts/start/app_runner.ts +201 -0
  10. package/templates/data_connector/scripts/start/context.ts +171 -0
  11. package/templates/data_connector/scripts/start/start.ts +46 -0
  12. package/templates/data_connector/scripts/start/tests/start.tests.ts +61 -0
  13. package/templates/data_connector/src/api/connect_client.ts +6 -0
  14. package/templates/data_connector/src/api/data_source.ts +96 -0
  15. package/templates/data_connector/src/api/data_sources/designs.tsx +253 -0
  16. package/templates/data_connector/src/api/data_sources/index.ts +4 -0
  17. package/templates/data_connector/src/api/data_sources/templates.tsx +287 -0
  18. package/templates/data_connector/src/api/fetch_data_table.ts +51 -0
  19. package/templates/data_connector/src/api/index.ts +4 -0
  20. package/templates/data_connector/src/api/oauth.ts +8 -0
  21. package/templates/data_connector/src/api/tests/data_source.test.tsx +99 -0
  22. package/templates/data_connector/src/app.tsx +24 -0
  23. package/templates/data_connector/src/components/app_error.tsx +15 -0
  24. package/templates/data_connector/src/components/footer.tsx +26 -0
  25. package/templates/data_connector/src/components/header.tsx +40 -0
  26. package/templates/data_connector/src/components/index.ts +3 -0
  27. package/templates/data_connector/src/components/inputs/messages.tsx +80 -0
  28. package/templates/data_connector/src/components/inputs/select_field.tsx +26 -0
  29. package/templates/data_connector/src/context/app_context.tsx +124 -0
  30. package/templates/data_connector/src/context/index.ts +2 -0
  31. package/templates/data_connector/src/context/use_app_context.ts +17 -0
  32. package/templates/data_connector/src/entrypoint.tsx +73 -0
  33. package/templates/data_connector/src/home.tsx +21 -0
  34. package/templates/data_connector/src/index.tsx +69 -0
  35. package/templates/data_connector/src/pages/data_source_config.tsx +9 -0
  36. package/templates/data_connector/src/pages/error.tsx +37 -0
  37. package/templates/data_connector/src/pages/index.ts +4 -0
  38. package/templates/data_connector/src/pages/login.tsx +145 -0
  39. package/templates/data_connector/src/pages/select_source.tsx +24 -0
  40. package/templates/data_connector/src/routes/index.ts +2 -0
  41. package/templates/data_connector/src/routes/protected_route.tsx +25 -0
  42. package/templates/data_connector/src/routes/routes.tsx +46 -0
  43. package/templates/data_connector/src/utils/data_params.ts +17 -0
  44. package/templates/data_connector/src/utils/data_table.ts +100 -0
  45. package/templates/data_connector/src/utils/fetch_result.ts +36 -0
  46. package/templates/data_connector/src/utils/index.ts +2 -0
  47. package/templates/data_connector/src/utils/tests/data_table.test.ts +133 -0
  48. package/templates/data_connector/styles/components.css +38 -0
  49. package/templates/data_connector/tsconfig.json +54 -0
  50. package/templates/data_connector/webpack.config.ts +270 -0
@@ -0,0 +1,124 @@
1
+ import { createContext, useState, useCallback } from "react";
2
+ import type { RenderSelectionUiParams } from "@canva/intents/data";
3
+ import type { AccessTokenResponse } from "@canva/user";
4
+ import { auth } from "@canva/user";
5
+ import type { APIResponseItem, DataSourceHandler } from "src/api/data_source";
6
+ import { type DataSourceConfig } from "src/api/data_source";
7
+
8
+ export interface AppContextType {
9
+ appError: string;
10
+ setAppError: (value: string) => void;
11
+ dataParams: RenderSelectionUiParams;
12
+ isAuthenticated: boolean;
13
+ accessToken: AccessTokenResponse | undefined;
14
+ setAccessToken: (token: AccessTokenResponse | undefined) => void;
15
+ oauth: ReturnType<typeof auth.initOauth>;
16
+ logout: () => Promise<void>;
17
+ dataSourceHandler?: DataSourceHandler<DataSourceConfig, APIResponseItem>;
18
+ setDataSourceHandler: (
19
+ value: DataSourceHandler<DataSourceConfig, APIResponseItem>,
20
+ ) => void;
21
+
22
+ dataSourceConfig?: DataSourceConfig;
23
+ setDataSourceConfig: (value: DataSourceConfig) => void;
24
+ loadDataSource: (title: string, source: DataSourceConfig) => Promise<void>;
25
+ }
26
+
27
+ export const AppContext = createContext<AppContextType>({
28
+ appError: "",
29
+ setAppError: () => {},
30
+ dataParams: {} as RenderSelectionUiParams,
31
+ isAuthenticated: false,
32
+ accessToken: undefined,
33
+ setAccessToken: () => {},
34
+ oauth: auth.initOauth(),
35
+ logout: async () => {},
36
+ dataSourceHandler: {} as DataSourceHandler<DataSourceConfig, APIResponseItem>,
37
+ setDataSourceHandler: () => {},
38
+ dataSourceConfig: {} as DataSourceConfig,
39
+ setDataSourceConfig: () => {},
40
+ loadDataSource: async () => {},
41
+ });
42
+
43
+ /**
44
+ * Provides application-wide state and methods using React Context.
45
+ * @param {object} props - The props object.
46
+ * @param {React.ReactNode} props.children - The children components wrapped by the provider.
47
+ * @returns {JSX.Element} The provider component.
48
+ * @description This provider component wraps the entire application to provide application-wide state and methods using React Context.
49
+ * It manages state related to app errors, filter parameters, and authentication.
50
+ * It exposes these state values and setter methods to its child components via the AppContext.
51
+ * For more information on React Context, refer to the official React documentation: {@link https://react.dev/learn/passing-data-deeply-with-context}.
52
+ */
53
+ export const ContextProvider = ({
54
+ renderSelectionUiParams,
55
+ children,
56
+ }: {
57
+ renderSelectionUiParams: RenderSelectionUiParams;
58
+ children: React.ReactNode;
59
+ }): JSX.Element => {
60
+ const [appError, setAppError] = useState<string>("");
61
+ const [dataParams] = useState<RenderSelectionUiParams>(
62
+ renderSelectionUiParams,
63
+ );
64
+
65
+ // authentication
66
+ const [accessToken, setAccessToken] = useState<
67
+ AccessTokenResponse | undefined
68
+ >(undefined);
69
+ const oauth = auth.initOauth();
70
+ const isAuthenticated = !!accessToken;
71
+
72
+ // data handlers
73
+ const [dataSourceHandler, setDataSourceHandler] =
74
+ useState<DataSourceHandler<DataSourceConfig, APIResponseItem>>();
75
+ const [dataSourceConfig, setDataSourceConfig] = useState<DataSourceConfig>();
76
+
77
+ // data connection
78
+ const loadDataSource = useCallback(
79
+ async (title: string, source: DataSourceConfig) => {
80
+ const result = await dataParams.updateDataRef({
81
+ title,
82
+ source: JSON.stringify(source),
83
+ });
84
+ if (result.status === "remote_request_failed") {
85
+ setAppError(`Failed to load data source: uanble to connect to the API`);
86
+ } else if (result.status === "app_error") {
87
+ setAppError(
88
+ `Failed to load data source: ${result.message || result.status}`,
89
+ );
90
+ } else {
91
+ setAppError("");
92
+ }
93
+ },
94
+ [dataParams],
95
+ );
96
+
97
+ const logout = useCallback(async () => {
98
+ try {
99
+ setAccessToken(undefined);
100
+ await oauth.deauthorize();
101
+ setAccessToken(null);
102
+ } catch (error) {
103
+ setAppError(error instanceof Error ? error.message : "Logout failed");
104
+ }
105
+ }, [oauth]);
106
+
107
+ const value: AppContextType = {
108
+ appError,
109
+ setAppError,
110
+ dataParams,
111
+ isAuthenticated,
112
+ accessToken,
113
+ setAccessToken,
114
+ oauth,
115
+ logout,
116
+ dataSourceHandler,
117
+ setDataSourceHandler,
118
+ dataSourceConfig,
119
+ setDataSourceConfig,
120
+ loadDataSource,
121
+ };
122
+
123
+ return <AppContext.Provider value={value}>{children}</AppContext.Provider>;
124
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./app_context";
2
+ export * from "./use_app_context";
@@ -0,0 +1,17 @@
1
+ import { useContext } from "react";
2
+ import type { AppContextType } from ".";
3
+ import { AppContext } from ".";
4
+
5
+ /**
6
+ * A custom React hook to access the application-wide state and methods provided by the ContextProvider using React Context.
7
+ * @returns {AppContextType} - An object containing application-wide state and methods.
8
+ * @throws {Error} - Throws an error if used outside the context of a ContextProvider.
9
+ * @description This hook allows components to access the application-wide state and methods provided by the ContextProvider using React Context. It retrieves the context value using the useContext hook and ensures that the context is available. If used outside the context of an ContextProvider, it throws an error instructing developers to use it within an ContextProvider.
10
+ */
11
+ export const useAppContext = (): AppContextType => {
12
+ const context = useContext(AppContext);
13
+ if (!context) {
14
+ throw new Error("useAppContext must be used within a ContextProvider");
15
+ }
16
+ return context;
17
+ };
@@ -0,0 +1,73 @@
1
+ import { LoadingIndicator } from "@canva/app-ui-kit";
2
+ import { useEffect } from "react";
3
+ import { useNavigate } from "react-router-dom";
4
+ import { useAppContext } from "./context";
5
+ import {
6
+ isDataRefEmpty,
7
+ isLaunchedWithError,
8
+ isOutdatedSource,
9
+ } from "./utils/data_params";
10
+ import { Paths } from "./routes";
11
+ import { DATA_SOURCES } from "./api/data_sources";
12
+ import type {
13
+ APIResponseItem,
14
+ DataSourceConfig,
15
+ DataSourceHandler,
16
+ } from "./api";
17
+
18
+ const parseDataSource = (source: string) => {
19
+ try {
20
+ return source ? JSON.parse(source) : undefined;
21
+ } catch {
22
+ return undefined;
23
+ }
24
+ };
25
+
26
+ export const Entrypoint = () => {
27
+ const navigate = useNavigate();
28
+ const context = useAppContext();
29
+ const { dataParams, setAppError, setDataSourceHandler } = context;
30
+
31
+ useEffect(() => {
32
+ // if the app was loaded with a populated data ref, we should reload the previous state.
33
+ // otherwise, if this is a first launch or there is an error, we should navigate to the first screen for a user - selecting a data source
34
+ let navigateTo: Paths | undefined;
35
+
36
+ if (isDataRefEmpty(dataParams)) {
37
+ // probably a first time launch - the user will need to select a data source
38
+ navigateTo = Paths.DATA_SOURCE_SELECTION;
39
+ } else if (
40
+ isOutdatedSource(dataParams) ||
41
+ isLaunchedWithError(dataParams)
42
+ ) {
43
+ // the configured source does not match the expected data source types
44
+ // so prompt the user to reconfigure the data source
45
+ setAppError("The data source configuration needs to be updated.");
46
+ navigateTo = Paths.DATA_SOURCE_SELECTION;
47
+ } else {
48
+ // there is a data ref, so we should parse it and navigate to the appropriate page
49
+ const dataRef = dataParams.invocationContext.dataSourceRef;
50
+ const parsedSource = parseDataSource(dataRef?.source ?? "");
51
+
52
+ if (parsedSource) {
53
+ const dataHandler = DATA_SOURCES.find((handler) =>
54
+ handler.matchSource(parsedSource),
55
+ );
56
+ if (dataHandler) {
57
+ setDataSourceHandler(
58
+ dataHandler as unknown as DataSourceHandler<
59
+ DataSourceConfig,
60
+ APIResponseItem
61
+ >,
62
+ );
63
+ dataHandler.sourceConfig = parsedSource;
64
+ navigateTo = Paths.DATA_SOURCE_CONFIG;
65
+ }
66
+ }
67
+ }
68
+
69
+ navigate(navigateTo || Paths.DATA_SOURCE_SELECTION);
70
+ }, [dataParams]);
71
+
72
+ return <LoadingIndicator />;
73
+ };
@@ -0,0 +1,21 @@
1
+ import { Outlet } from "react-router-dom";
2
+ import { Box, Rows } from "@canva/app-ui-kit";
3
+ import { AppError } from "./components";
4
+ import * as styles from "styles/components.css";
5
+
6
+ export const Home = () => (
7
+ <div className={styles.scrollContainer}>
8
+ <Box
9
+ justifyContent="center"
10
+ width="full"
11
+ alignItems="start"
12
+ display="flex"
13
+ height="full"
14
+ >
15
+ <Rows spacing="3u">
16
+ <AppError />
17
+ <Outlet />
18
+ </Rows>
19
+ </Box>
20
+ </div>
21
+ );
@@ -0,0 +1,69 @@
1
+ import type {
2
+ RenderSelectionUiParams,
3
+ FetchDataTableParams,
4
+ FetchDataTableResult,
5
+ } from "@canva/intents/data";
6
+ import { prepareDataConnector } from "@canva/intents/data";
7
+ import { createRoot } from "react-dom/client";
8
+ import { App } from "./app";
9
+ import { auth } from "@canva/user";
10
+ import { buildDataTableResult, scope } from "./api";
11
+
12
+ import "@canva/app-ui-kit/styles.css";
13
+ import { Alert, AppUiProvider } from "@canva/app-ui-kit";
14
+
15
+ const root = createRoot(document.getElementById("root") as Element);
16
+ prepareDataConnector({
17
+ /**
18
+ * Fetches structured data from an external source.
19
+ *
20
+ * This action is called in two scenarios:
21
+ *
22
+ * - During data selection to preview data before import (when {@link RenderSelectionUiParams.updateDataRef} is called).
23
+ * - When refreshing previously imported data (when the user requests an update).
24
+ *
25
+ * @param params - Parameters for the data fetching operation.
26
+ * @returns A promise resolving to either a successful result with data or an error.
27
+ */
28
+ fetchDataTable: async (
29
+ params: FetchDataTableParams,
30
+ ): Promise<FetchDataTableResult> => {
31
+ const oauth = auth.initOauth();
32
+ const token = await oauth.getAccessToken({ scope });
33
+ return buildDataTableResult(params, token?.token);
34
+ },
35
+
36
+ /**
37
+ * Renders a UI component for selecting and configuring data from external sources.
38
+ * This UI should allow users to browse data sources, apply filters, and select data.
39
+ * When selection is complete, the implementation must call the `updateDataRef`
40
+ * callback provided in the params to preview and confirm the data selection.
41
+ *
42
+ * @param params - parameters that provide context and configuration for the data selection UI.
43
+ * Contains invocation context, size limits, and the updateDataRef callback
44
+ */
45
+ renderSelectionUi: async (params: RenderSelectionUiParams) => {
46
+ function render() {
47
+ root.render(<App dataParams={params} />);
48
+ }
49
+
50
+ render();
51
+
52
+ if (module.hot) {
53
+ module.hot.accept("./app", render);
54
+ module.hot.accept("./api", render);
55
+ }
56
+ },
57
+ });
58
+
59
+ // TODO: Fallback message if you have not turned on the data connector intent.
60
+ // You can remove this once your app is correctly configured.
61
+ root.render(
62
+ <AppUiProvider>
63
+ {/* eslint-disable-next-line formatjs/no-literal-string-in-jsx */}
64
+ <Alert tone="critical">
65
+ If you're seeing this, you need to turn on the data connector intent in
66
+ the developer portal for this app.
67
+ </Alert>
68
+ </AppUiProvider>,
69
+ );
@@ -0,0 +1,9 @@
1
+ import { useAppContext } from "src/context";
2
+
3
+ export const DataSourceConfig = () => {
4
+ const { dataSourceHandler } = useAppContext();
5
+ if (!dataSourceHandler) {
6
+ return undefined; // should be impossible
7
+ }
8
+ return dataSourceHandler.configPage(dataSourceHandler.sourceConfig);
9
+ };
@@ -0,0 +1,37 @@
1
+ import { useNavigate } from "react-router-dom";
2
+ import { Button, Rows, Text } from "@canva/app-ui-kit";
3
+ import { Paths } from "src/routes";
4
+ import * as styles from "styles/components.css";
5
+ import { FormattedMessage, useIntl } from "react-intl";
6
+
7
+ /**
8
+ * Bare bones Error Page, please add relevant information and behavior that your app requires.
9
+ */
10
+ export const ErrorPage = () => {
11
+ const navigate = useNavigate();
12
+ const intl = useIntl();
13
+
14
+ const onClick = () => {
15
+ navigate(Paths.ENTRYPOINT);
16
+ };
17
+
18
+ return (
19
+ <div className={styles.scrollContainer}>
20
+ <Rows spacing="2u">
21
+ <Text>
22
+ <FormattedMessage
23
+ defaultMessage="Something went wrong."
24
+ description="A message to indicate that something went wrong, but no more information is available"
25
+ />
26
+ </Text>
27
+ <Button variant="primary" onClick={onClick} stretch={true}>
28
+ {intl.formatMessage({
29
+ defaultMessage: "Start over",
30
+ description:
31
+ "A button label to clear the error and the prompt and start again",
32
+ })}
33
+ </Button>
34
+ </Rows>
35
+ </div>
36
+ );
37
+ };
@@ -0,0 +1,4 @@
1
+ export * from "./error";
2
+ export * from "./login";
3
+ export * from "./select_source";
4
+ export * from "./data_source_config";
@@ -0,0 +1,145 @@
1
+ import {
2
+ Button,
3
+ LoadingIndicator,
4
+ Rows,
5
+ Title,
6
+ Text,
7
+ Box,
8
+ } from "@canva/app-ui-kit";
9
+ import { useState, useEffect, useCallback } from "react";
10
+ import * as styles from "styles/components.css";
11
+ import { useAppContext } from "../context";
12
+ import { useNavigate } from "react-router-dom";
13
+ import { Paths } from "../routes";
14
+ import { Header } from "src/components";
15
+ import { useIntl, FormattedMessage, defineMessages } from "react-intl";
16
+ import { scope } from "src/api";
17
+
18
+ export const Login = () => {
19
+ const intl = useIntl();
20
+ const navigate = useNavigate();
21
+ const { oauth, setAccessToken, isAuthenticated } = useAppContext();
22
+
23
+ const [error, setError] = useState<string | null>(null);
24
+ const [loading, setLoading] = useState(true);
25
+
26
+ useEffect(() => {
27
+ // Redirect if already authenticated
28
+ if (isAuthenticated) {
29
+ navigate(Paths.ENTRYPOINT);
30
+ return;
31
+ }
32
+
33
+ // check if the user is already authenticated
34
+ retrieveAndSetToken();
35
+ }, [isAuthenticated]);
36
+
37
+ const authorize = useCallback(async () => {
38
+ setLoading(true);
39
+ setError(null);
40
+ try {
41
+ await oauth.requestAuthorization({ scope });
42
+ await retrieveAndSetToken();
43
+ } catch (error) {
44
+ setError(error instanceof Error ? error.message : "Unknown error");
45
+ setLoading(false);
46
+ }
47
+ }, [oauth]);
48
+
49
+ // you MUST call getAccessToken every time you need a token, as the token may expire.
50
+ // Canva will handle caching and refreshing the token for you.
51
+ const retrieveAndSetToken = useCallback(
52
+ async (forceRefresh = false) => {
53
+ try {
54
+ const token = await oauth.getAccessToken({ forceRefresh, scope });
55
+ setAccessToken(token);
56
+ setLoading(false);
57
+ } catch (error) {
58
+ setError(error instanceof Error ? error.message : "Unknown error");
59
+ setLoading(false);
60
+ }
61
+ },
62
+ [oauth, setAccessToken],
63
+ );
64
+
65
+ return (
66
+ <div className={styles.scrollContainer}>
67
+ <Box
68
+ justifyContent="center"
69
+ width="full"
70
+ alignItems="center"
71
+ display="flex"
72
+ height="full"
73
+ >
74
+ {error && (
75
+ <Rows spacing="2u">
76
+ <Title>
77
+ <FormattedMessage {...loginMessages.authorizationError} />
78
+ </Title>
79
+ <Text>{error}</Text>
80
+ <Button variant="primary" onClick={authorize}>
81
+ {intl.formatMessage(loginMessages.tryAgain)}
82
+ </Button>
83
+ </Rows>
84
+ )}
85
+ {loading && <LoadingIndicator />}
86
+ {!loading && !error && (
87
+ <Rows spacing="2u">
88
+ <Header
89
+ title={intl.formatMessage(loginMessages.signInRequired)}
90
+ showBack={false}
91
+ />
92
+ <Text>
93
+ <FormattedMessage {...loginMessages.dataConnectorsOAuth} />
94
+ </Text>
95
+ <Text>
96
+ <FormattedMessage {...loginMessages.exampleDemonstration} />
97
+ </Text>
98
+ <Text>
99
+ <FormattedMessage {...loginMessages.setupInstructions} />
100
+ </Text>
101
+ <Button variant="primary" onClick={authorize}>
102
+ {intl.formatMessage(loginMessages.signIntoCanva)}
103
+ </Button>
104
+ </Rows>
105
+ )}
106
+ </Box>
107
+ </div>
108
+ );
109
+ };
110
+
111
+ const loginMessages = defineMessages({
112
+ authorizationError: {
113
+ defaultMessage: "Authorization error",
114
+ description:
115
+ "Title displayed when there is an error during OAuth authorization",
116
+ },
117
+ tryAgain: {
118
+ defaultMessage: "Try again",
119
+ description: "Button text to retry authorization after an error occurs",
120
+ },
121
+ signInRequired: {
122
+ defaultMessage: "Sign in required",
123
+ description:
124
+ "Header title for the login page indicating authentication is needed",
125
+ },
126
+ dataConnectorsOAuth: {
127
+ defaultMessage:
128
+ "Data connectors can use OAuth to authenticate with other platforms.",
129
+ description: "Body text shown when the user is prompted to sign in",
130
+ },
131
+ exampleDemonstration: {
132
+ defaultMessage:
133
+ "This example demonstrates how to do this with the Canva Connect API.",
134
+ description: "Body text shown when the user is prompted to sign in",
135
+ },
136
+ setupInstructions: {
137
+ defaultMessage:
138
+ "For set up instructions please see the README.md in the root folder.",
139
+ description: "Body text shown when the user is prompted to sign in",
140
+ },
141
+ signIntoCanva: {
142
+ defaultMessage: "Sign into Canva",
143
+ description: "Button text for initiating Canva authentication",
144
+ },
145
+ });
@@ -0,0 +1,24 @@
1
+ import { Box, Rows } from "@canva/app-ui-kit";
2
+ import { useIntl } from "react-intl";
3
+ import { DATA_SOURCES } from "src/api/data_sources";
4
+ import { Footer, Header } from "src/components";
5
+
6
+ export const SelectSource = () => {
7
+ const intl = useIntl();
8
+
9
+ return (
10
+ <Box paddingEnd="2u" paddingTop="2u">
11
+ <Rows spacing="1u" align="start">
12
+ <Header
13
+ title={intl.formatMessage({
14
+ defaultMessage: "What would you like to import? ",
15
+ description: "The header text for the data source selection view",
16
+ })}
17
+ showBack={false}
18
+ />
19
+ {DATA_SOURCES.map((handler) => handler.selectionPage())}
20
+ <Footer />
21
+ </Rows>
22
+ </Box>
23
+ );
24
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./routes";
2
+ export * from "./protected_route";
@@ -0,0 +1,25 @@
1
+ import { useEffect } from "react";
2
+ import { useNavigate } from "react-router-dom";
3
+ import { useAppContext } from "../context";
4
+ import { Paths } from "../routes";
5
+
6
+ interface ProtectedRouteProps {
7
+ children: React.ReactNode;
8
+ }
9
+
10
+ /**
11
+ * A component that protects routes from unauthorized access.
12
+ * If the user is not authenticated, they will be redirected to the login page.
13
+ */
14
+ export const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
15
+ const { isAuthenticated } = useAppContext();
16
+ const navigate = useNavigate();
17
+
18
+ useEffect(() => {
19
+ if (!isAuthenticated) {
20
+ navigate(Paths.LOGIN);
21
+ }
22
+ }, [isAuthenticated, navigate]);
23
+
24
+ return <>{children}</>;
25
+ };
@@ -0,0 +1,46 @@
1
+ import { Entrypoint } from "src/entrypoint";
2
+ import { Home } from "src/home";
3
+ import { ErrorPage, Login, DataSourceConfig, SelectSource } from "src/pages";
4
+ import { ProtectedRoute } from "./protected_route";
5
+
6
+ export enum Paths {
7
+ ENTRYPOINT = "/",
8
+ LOGIN = "/login",
9
+ DATA_SOURCE_SELECTION = "/data-source-selection",
10
+ DATA_SOURCE_CONFIG = "/data-source-config",
11
+ ERRORS = "/errors/:retry",
12
+ }
13
+
14
+ export const routes = [
15
+ {
16
+ path: Paths.ENTRYPOINT,
17
+ element: <Home />,
18
+ errorElement: <ErrorPage />,
19
+ children: [
20
+ {
21
+ index: true,
22
+ element: <Entrypoint />,
23
+ },
24
+ {
25
+ path: Paths.LOGIN,
26
+ element: <Login />,
27
+ },
28
+ {
29
+ path: Paths.DATA_SOURCE_SELECTION,
30
+ element: (
31
+ <ProtectedRoute>
32
+ <SelectSource />
33
+ </ProtectedRoute>
34
+ ),
35
+ },
36
+ {
37
+ path: Paths.DATA_SOURCE_CONFIG,
38
+ element: (
39
+ <ProtectedRoute>
40
+ <DataSourceConfig />
41
+ </ProtectedRoute>
42
+ ),
43
+ },
44
+ ],
45
+ },
46
+ ];
@@ -0,0 +1,17 @@
1
+ import type { RenderSelectionUiParams } from "@canva/intents/data";
2
+
3
+ export const isLaunchedWithError = (dataParams: RenderSelectionUiParams) => {
4
+ return dataParams.invocationContext.reason === "app_error";
5
+ };
6
+
7
+ export const isOutdatedSource = (dataParams: RenderSelectionUiParams) => {
8
+ return dataParams.invocationContext.reason === "outdated_source_ref";
9
+ };
10
+
11
+ export const isDataRefEmpty = (dataParams: RenderSelectionUiParams) => {
12
+ return (
13
+ !dataParams?.invocationContext ||
14
+ (!isLaunchedWithError(dataParams) &&
15
+ !dataParams.invocationContext.dataSourceRef?.source)
16
+ );
17
+ };