@canva/cli 0.0.1-beta.3 → 0.0.1-beta.30
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 +218 -104
- package/cli.js +624 -394
- package/lib/cjs/index.cjs +5 -0
- package/lib/esm/index.mjs +5 -0
- package/package.json +19 -4
- package/templates/base/eslint.config.mjs +2 -27
- 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/{webpack.config.cjs → webpack.config.ts} +46 -46
- package/templates/common/.cursor/mcp.json +8 -0
- 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/.vscode/mcp.json +9 -0
- package/templates/common/README.md +4 -7
- 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/backend/jwt_middleware/index.ts +1 -0
- package/templates/common/utils/backend/jwt_middleware/jwt_middleware.ts +229 -0
- package/templates/common/utils/backend/jwt_middleware/tests/jwt_middleware.tests.ts +630 -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 +23 -19
- package/templates/dam/eslint.config.mjs +2 -28
- package/templates/dam/package.json +43 -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 +24 -3
- package/templates/dam/src/config.ts +212 -87
- package/templates/{hello_world/webpack.config.cjs → dam/webpack.config.ts} +46 -46
- package/templates/data_connector/README.md +84 -0
- package/templates/data_connector/declarations/declarations.d.ts +29 -0
- package/templates/data_connector/eslint.config.mjs +14 -0
- package/templates/data_connector/package.json +91 -0
- package/templates/data_connector/scripts/copy_env.ts +10 -0
- package/templates/data_connector/scripts/ssl/ssl.ts +131 -0
- package/templates/data_connector/scripts/start/app_runner.ts +201 -0
- package/templates/data_connector/scripts/start/context.ts +171 -0
- package/templates/data_connector/scripts/start/start.ts +46 -0
- package/templates/data_connector/scripts/start/tests/start.tests.ts +61 -0
- package/templates/data_connector/src/api/connect_client.ts +6 -0
- package/templates/data_connector/src/api/data_source.ts +96 -0
- package/templates/data_connector/src/api/data_sources/designs.tsx +253 -0
- package/templates/data_connector/src/api/data_sources/index.ts +4 -0
- package/templates/data_connector/src/api/data_sources/templates.tsx +287 -0
- package/templates/data_connector/src/api/fetch_data_table.ts +51 -0
- package/templates/data_connector/src/api/index.ts +4 -0
- package/templates/data_connector/src/api/oauth.ts +8 -0
- package/templates/data_connector/src/api/tests/data_source.test.tsx +99 -0
- package/templates/data_connector/src/app.tsx +24 -0
- package/templates/data_connector/src/components/app_error.tsx +15 -0
- package/templates/data_connector/src/components/footer.tsx +26 -0
- package/templates/data_connector/src/components/header.tsx +40 -0
- package/templates/data_connector/src/components/index.ts +3 -0
- package/templates/data_connector/src/components/inputs/messages.tsx +80 -0
- package/templates/data_connector/src/components/inputs/select_field.tsx +26 -0
- package/templates/data_connector/src/context/app_context.tsx +124 -0
- package/templates/data_connector/src/context/index.ts +2 -0
- package/templates/data_connector/src/context/use_app_context.ts +17 -0
- package/templates/data_connector/src/entrypoint.tsx +73 -0
- package/templates/data_connector/src/home.tsx +21 -0
- package/templates/data_connector/src/index.tsx +69 -0
- package/templates/data_connector/src/pages/data_source_config.tsx +9 -0
- package/templates/data_connector/src/pages/error.tsx +37 -0
- package/templates/data_connector/src/pages/index.ts +4 -0
- package/templates/data_connector/src/pages/login.tsx +145 -0
- package/templates/data_connector/src/pages/select_source.tsx +24 -0
- package/templates/data_connector/src/routes/index.ts +2 -0
- package/templates/data_connector/src/routes/protected_route.tsx +25 -0
- package/templates/data_connector/src/routes/routes.tsx +46 -0
- package/templates/data_connector/src/utils/data_params.ts +17 -0
- package/templates/data_connector/src/utils/data_table.ts +100 -0
- package/templates/data_connector/src/utils/fetch_result.ts +36 -0
- package/templates/data_connector/src/utils/index.ts +2 -0
- package/templates/data_connector/src/utils/tests/data_table.test.ts +133 -0
- package/templates/data_connector/styles/components.css +38 -0
- package/templates/data_connector/tsconfig.json +54 -0
- package/templates/{gen_ai/webpack.config.cjs → data_connector/webpack.config.ts} +46 -46
- package/templates/gen_ai/README.md +1 -40
- package/templates/gen_ai/backend/routers/image.ts +11 -11
- package/templates/gen_ai/backend/server.ts +0 -7
- package/templates/gen_ai/eslint.config.mjs +2 -27
- package/templates/gen_ai/package.json +48 -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 +24 -79
- 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 +7 -25
- package/templates/gen_ai/src/components/index.ts +0 -1
- package/templates/gen_ai/src/components/loading_results.tsx +4 -8
- package/templates/gen_ai/src/components/tests/remaining_credit.tests.tsx +43 -0
- package/templates/gen_ai/src/context/app_context.tsx +5 -33
- 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/{dam/webpack.config.cjs → gen_ai/webpack.config.ts} +46 -46
- package/templates/hello_world/eslint.config.mjs +2 -27
- package/templates/hello_world/package.json +47 -34
- 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 +20 -0
- 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/webpack.config.ts +270 -0
- package/templates/common/conf/eslint-general.mjs +0 -277
- package/templates/common/conf/eslint-i18n.mjs +0 -23
- package/templates/gen_ai/backend/routers/oauth.ts +0 -393
- package/templates/gen_ai/src/components/logged_in_status.tsx +0 -44
- package/templates/gen_ai/src/services/auth.tsx +0 -26
- package/templates/gen_ai/src/services/index.ts +0 -1
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/* eslint-disable formatjs/no-literal-string-in-object */
|
|
2
|
+
/* eslint-disable formatjs/no-literal-string-in-jsx */
|
|
3
|
+
import type { DataSourceConfig, APIResponseItem } from "../data_source";
|
|
4
|
+
import { DataSourceHandler, DataAPIError } from "../data_source";
|
|
5
|
+
import { toDataTable } from "src/utils";
|
|
6
|
+
|
|
7
|
+
// Mock dependencies
|
|
8
|
+
jest.mock("src/utils", () => ({
|
|
9
|
+
toDataTable: jest.fn().mockReturnValue({ rows: [] }),
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
describe("DataSourceHandler", () => {
|
|
13
|
+
// Test interface
|
|
14
|
+
interface TestConfig extends DataSourceConfig {
|
|
15
|
+
testField: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface TestResponse extends APIResponseItem {
|
|
19
|
+
name: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Test data
|
|
23
|
+
const mockConfig: TestConfig = {
|
|
24
|
+
schema: "test/v1",
|
|
25
|
+
testField: "value",
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const mockColumns = [
|
|
29
|
+
{
|
|
30
|
+
label: "ID",
|
|
31
|
+
getValue: (item: TestResponse) => `ID ${item.id}`,
|
|
32
|
+
toCell: jest.fn().mockReturnValue({ type: "string", value: "id-123" }),
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
label: "Name",
|
|
36
|
+
getValue: "name" as const,
|
|
37
|
+
toCell: jest.fn().mockReturnValue({ type: "string", value: "Test Name" }),
|
|
38
|
+
},
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
const mockFetchData = jest.fn();
|
|
42
|
+
const mockSelectionPage = jest.fn().mockReturnValue(<div>Selection</div>);
|
|
43
|
+
const mockConfigPage = jest.fn().mockReturnValue(<div>Config</div>);
|
|
44
|
+
|
|
45
|
+
let handler: DataSourceHandler<TestConfig, TestResponse>;
|
|
46
|
+
|
|
47
|
+
beforeEach(() => {
|
|
48
|
+
jest.clearAllMocks();
|
|
49
|
+
handler = new DataSourceHandler<TestConfig, TestResponse>(
|
|
50
|
+
mockConfig,
|
|
51
|
+
mockColumns,
|
|
52
|
+
mockFetchData,
|
|
53
|
+
mockSelectionPage,
|
|
54
|
+
mockConfigPage,
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("creates handler with correct schema", () => {
|
|
59
|
+
expect(handler.schema).toBe("test/v1");
|
|
60
|
+
expect(handler.sourceConfig).toEqual(mockConfig);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("matchSource returns true for matching schema", () => {
|
|
64
|
+
const result = handler.matchSource({ schema: "test/v1" });
|
|
65
|
+
expect(result).toBe(true);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("matchSource returns false for non-matching schema", () => {
|
|
69
|
+
const result = handler.matchSource({ schema: "wrong/v1" });
|
|
70
|
+
expect(result).toBe(false);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("fetchAndBuildTable calls fetchData with correct parameters", async () => {
|
|
74
|
+
const mockData = [{ id: "id-123", name: "Test Item" }];
|
|
75
|
+
mockFetchData.mockResolvedValueOnce(mockData);
|
|
76
|
+
|
|
77
|
+
const authToken = "test-token";
|
|
78
|
+
const rowLimit = 10;
|
|
79
|
+
const signal = new AbortController().signal;
|
|
80
|
+
|
|
81
|
+
await handler.fetchAndBuildTable(mockConfig, authToken, rowLimit, signal);
|
|
82
|
+
|
|
83
|
+
expect(mockFetchData).toHaveBeenCalledWith(
|
|
84
|
+
mockConfig,
|
|
85
|
+
authToken,
|
|
86
|
+
rowLimit,
|
|
87
|
+
signal,
|
|
88
|
+
);
|
|
89
|
+
expect(toDataTable).toHaveBeenCalledWith(mockData, mockColumns, rowLimit);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("fetchAndBuildTable throws DataAPIError when fetchData fails", async () => {
|
|
93
|
+
mockFetchData.mockRejectedValueOnce(new Error("Network error"));
|
|
94
|
+
|
|
95
|
+
await expect(
|
|
96
|
+
handler.fetchAndBuildTable(mockConfig, "token", 10, undefined),
|
|
97
|
+
).rejects.toThrow(DataAPIError);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createHashRouter, RouterProvider } from "react-router-dom";
|
|
2
|
+
import { ContextProvider } from "./context";
|
|
3
|
+
import { routes } from "./routes";
|
|
4
|
+
import { AppUiProvider } from "@canva/app-ui-kit";
|
|
5
|
+
import { AppI18nProvider } from "@canva/app-i18n-kit";
|
|
6
|
+
import { ErrorBoundary } from "react-error-boundary";
|
|
7
|
+
import { ErrorPage } from "./pages";
|
|
8
|
+
import type { RenderSelectionUiParams } from "@canva/intents/data";
|
|
9
|
+
|
|
10
|
+
export const App = ({
|
|
11
|
+
dataParams,
|
|
12
|
+
}: {
|
|
13
|
+
dataParams: RenderSelectionUiParams;
|
|
14
|
+
}) => (
|
|
15
|
+
<AppI18nProvider>
|
|
16
|
+
<AppUiProvider>
|
|
17
|
+
<ErrorBoundary fallback={<ErrorPage />}>
|
|
18
|
+
<ContextProvider renderSelectionUiParams={dataParams}>
|
|
19
|
+
<RouterProvider router={createHashRouter(routes)} />
|
|
20
|
+
</ContextProvider>
|
|
21
|
+
</ErrorBoundary>
|
|
22
|
+
</AppUiProvider>
|
|
23
|
+
</AppI18nProvider>
|
|
24
|
+
);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Alert } from "@canva/app-ui-kit";
|
|
2
|
+
import { useAppContext } from "src/context";
|
|
3
|
+
|
|
4
|
+
export const AppError = () => {
|
|
5
|
+
const { appError, setAppError } = useAppContext();
|
|
6
|
+
if (!appError) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return (
|
|
11
|
+
<Alert tone="critical" onDismiss={() => setAppError("")}>
|
|
12
|
+
{appError}
|
|
13
|
+
</Alert>
|
|
14
|
+
);
|
|
15
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Rows, Button } from "@canva/app-ui-kit";
|
|
2
|
+
import { useIntl } from "react-intl";
|
|
3
|
+
import { useAppContext } from "src/context";
|
|
4
|
+
|
|
5
|
+
export const Footer = () => {
|
|
6
|
+
const { isAuthenticated, logout } = useAppContext();
|
|
7
|
+
const intl = useIntl();
|
|
8
|
+
|
|
9
|
+
return (
|
|
10
|
+
<Rows spacing="1u">
|
|
11
|
+
{isAuthenticated && (
|
|
12
|
+
<Button
|
|
13
|
+
variant="tertiary"
|
|
14
|
+
onClick={async () => {
|
|
15
|
+
logout();
|
|
16
|
+
}}
|
|
17
|
+
>
|
|
18
|
+
{intl.formatMessage({
|
|
19
|
+
defaultMessage: "Log Out",
|
|
20
|
+
description: "Button for logging out of the data source",
|
|
21
|
+
})}
|
|
22
|
+
</Button>
|
|
23
|
+
)}
|
|
24
|
+
</Rows>
|
|
25
|
+
);
|
|
26
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useNavigate } from "react-router-dom";
|
|
2
|
+
import {
|
|
3
|
+
ArrowLeftIcon,
|
|
4
|
+
Box,
|
|
5
|
+
Button,
|
|
6
|
+
Column,
|
|
7
|
+
Columns,
|
|
8
|
+
Title,
|
|
9
|
+
} from "@canva/app-ui-kit";
|
|
10
|
+
import { Paths } from "src/routes";
|
|
11
|
+
|
|
12
|
+
export const Header = ({
|
|
13
|
+
title,
|
|
14
|
+
showBack,
|
|
15
|
+
}: {
|
|
16
|
+
title: string;
|
|
17
|
+
showBack: boolean;
|
|
18
|
+
}) => {
|
|
19
|
+
const navigate = useNavigate();
|
|
20
|
+
return (
|
|
21
|
+
<Box paddingBottom="1u">
|
|
22
|
+
<Columns spacing="0" alignY="center" align="start">
|
|
23
|
+
{showBack && (
|
|
24
|
+
<Column width="content">
|
|
25
|
+
<Button
|
|
26
|
+
onClick={() => navigate(Paths.DATA_SOURCE_SELECTION)}
|
|
27
|
+
icon={ArrowLeftIcon}
|
|
28
|
+
variant="tertiary"
|
|
29
|
+
/>
|
|
30
|
+
</Column>
|
|
31
|
+
)}
|
|
32
|
+
<Column>
|
|
33
|
+
<Title size="small" lineClamp={1}>
|
|
34
|
+
{title}
|
|
35
|
+
</Title>
|
|
36
|
+
</Column>
|
|
37
|
+
</Columns>
|
|
38
|
+
</Box>
|
|
39
|
+
);
|
|
40
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { defineMessages } from "react-intl";
|
|
2
|
+
|
|
3
|
+
export const ownershipFilter = defineMessages({
|
|
4
|
+
label: {
|
|
5
|
+
defaultMessage: "Filter by Ownership",
|
|
6
|
+
description: "Label for a select control to filter designs by ownership",
|
|
7
|
+
},
|
|
8
|
+
description: {
|
|
9
|
+
defaultMessage:
|
|
10
|
+
"Filter the list of designs based on the user's ownership of the designs",
|
|
11
|
+
description: "Subtitle for a select control to filter designs by ownership",
|
|
12
|
+
},
|
|
13
|
+
any: {
|
|
14
|
+
defaultMessage: "Any (default)",
|
|
15
|
+
description: "Default option for ownership filter",
|
|
16
|
+
},
|
|
17
|
+
owned: {
|
|
18
|
+
defaultMessage: "Owned by me",
|
|
19
|
+
description: "Option for ownership filter",
|
|
20
|
+
},
|
|
21
|
+
shared: {
|
|
22
|
+
defaultMessage: "Shared with me",
|
|
23
|
+
description: "Option for ownership filter",
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export const datasetFilter = defineMessages({
|
|
28
|
+
label: {
|
|
29
|
+
defaultMessage: "Filter by Dataset",
|
|
30
|
+
description: "Label for a select control to filter designs by dataset",
|
|
31
|
+
},
|
|
32
|
+
description: {
|
|
33
|
+
defaultMessage:
|
|
34
|
+
"Filter the list of brand templates based on the dataset definitions",
|
|
35
|
+
description: "Subtitle for a select control to filter designs by dataset",
|
|
36
|
+
},
|
|
37
|
+
any: {
|
|
38
|
+
defaultMessage: "Any (default)",
|
|
39
|
+
description: "Default option for dataset filter",
|
|
40
|
+
},
|
|
41
|
+
nonEmpty: {
|
|
42
|
+
defaultMessage: "Templates with one or more data fields defined",
|
|
43
|
+
description: "Option for dataset filter",
|
|
44
|
+
},
|
|
45
|
+
empty: {
|
|
46
|
+
defaultMessage: "Templates with no data fields defined",
|
|
47
|
+
description: "Option for dataset filter",
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
export const sortOrderField = defineMessages({
|
|
52
|
+
label: {
|
|
53
|
+
defaultMessage: "Sort by",
|
|
54
|
+
description: "Label for a select control to sort designs",
|
|
55
|
+
},
|
|
56
|
+
description: {
|
|
57
|
+
defaultMessage: "Sort the list of designs",
|
|
58
|
+
description: "Subtitle for a select control to sort designs",
|
|
59
|
+
},
|
|
60
|
+
relevance: {
|
|
61
|
+
defaultMessage: "Relevance (default)",
|
|
62
|
+
description: "Default option for sort order",
|
|
63
|
+
},
|
|
64
|
+
modifiedDesc: {
|
|
65
|
+
defaultMessage: "Last modified - descending",
|
|
66
|
+
description: "Option for sort order",
|
|
67
|
+
},
|
|
68
|
+
modifiedAsc: {
|
|
69
|
+
defaultMessage: "Last modified - ascending",
|
|
70
|
+
description: "Option for sort order",
|
|
71
|
+
},
|
|
72
|
+
titleDesc: {
|
|
73
|
+
defaultMessage: "Title - descending",
|
|
74
|
+
description: "Option for sort order",
|
|
75
|
+
},
|
|
76
|
+
titleAsc: {
|
|
77
|
+
defaultMessage: "Title - ascending",
|
|
78
|
+
description: "Option for sort order",
|
|
79
|
+
},
|
|
80
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { SelectOption } from "@canva/app-ui-kit";
|
|
2
|
+
import { FormField, Select } from "@canva/app-ui-kit";
|
|
3
|
+
|
|
4
|
+
interface SelectFieldProps {
|
|
5
|
+
value: string;
|
|
6
|
+
onChange: (value: string) => void;
|
|
7
|
+
label: string;
|
|
8
|
+
options: SelectOption<string>[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const SelectField = ({
|
|
12
|
+
value,
|
|
13
|
+
onChange,
|
|
14
|
+
label,
|
|
15
|
+
options,
|
|
16
|
+
}: SelectFieldProps) => {
|
|
17
|
+
return (
|
|
18
|
+
<FormField
|
|
19
|
+
label={label}
|
|
20
|
+
value={value}
|
|
21
|
+
control={(props) => (
|
|
22
|
+
<Select {...props} options={options} onChange={onChange} />
|
|
23
|
+
)}
|
|
24
|
+
/>
|
|
25
|
+
);
|
|
26
|
+
};
|
|
@@ -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,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
|
+
};
|