@codecademy/codebytes 0.3.1-alpha.405be7.0 → 0.3.1-alpha.6d07c2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/package.json +6 -3
- package/src/editor.tsx +3 -3
- package/src/helpers/index.ts +60 -0
- package/src/index.tsx +29 -19
- package/src/libs/eventTracking.ts +16 -0
- package/src/types.ts +9 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
-
### [0.3.1-alpha.
|
|
6
|
+
### [0.3.1-alpha.6d07c2.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.3.0...@codecademy/codebytes@0.3.1-alpha.6d07c2.0) (2022-01-20)
|
|
7
7
|
|
|
8
8
|
**Note:** Version bump only for package @codecademy/codebytes
|
|
9
9
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codecademy/codebytes",
|
|
3
3
|
"description": "Codebytes Code Editor",
|
|
4
|
-
"version": "0.3.1-alpha.
|
|
4
|
+
"version": "0.3.1-alpha.6d07c2.0",
|
|
5
5
|
"author": "Codecademy Engineering <dev@codecademy.com>",
|
|
6
6
|
"sideEffects": [
|
|
7
7
|
"**/*.css",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"@emotion/react": "^11.4.0",
|
|
28
28
|
"@emotion/styled": "^11.3.0",
|
|
29
29
|
"@monaco-editor/react": "4.3.1",
|
|
30
|
+
"jsuri": "^1.3.1",
|
|
30
31
|
"monaco-editor": ">= 0.25.0 < 1",
|
|
31
32
|
"react-resize-observer": "1.1.1"
|
|
32
33
|
},
|
|
@@ -43,10 +44,12 @@
|
|
|
43
44
|
"@emotion/jest": "^11.3.0",
|
|
44
45
|
"@testing-library/dom": "^7.31.2",
|
|
45
46
|
"@testing-library/react": "^11.0.4",
|
|
46
|
-
"@types/
|
|
47
|
+
"@types/jsuri": "^1.3.30",
|
|
48
|
+
"@types/loadable__component": "^5.13.2",
|
|
49
|
+
"js-base64": "^3.6.0"
|
|
47
50
|
},
|
|
48
51
|
"publishConfig": {
|
|
49
52
|
"access": "public"
|
|
50
53
|
},
|
|
51
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "59865e361c398793d3928dfcd7cd97cacb913fc9"
|
|
52
55
|
}
|
package/src/editor.tsx
CHANGED
|
@@ -12,6 +12,7 @@ import React, { useState } from 'react';
|
|
|
12
12
|
import { postSnippet } from './api';
|
|
13
13
|
import type { languageOption } from './consts';
|
|
14
14
|
import { Drawers } from './drawers';
|
|
15
|
+
import { trackClick } from './helpers';
|
|
15
16
|
import { SimpleMonacoEditor } from './MonacoEditor';
|
|
16
17
|
|
|
17
18
|
const Output = styled.pre<{ hasError: boolean }>`
|
|
@@ -39,7 +40,6 @@ type EditorProps = {
|
|
|
39
40
|
language: languageOption;
|
|
40
41
|
text: string;
|
|
41
42
|
onChange: (text: string) => void;
|
|
42
|
-
onCopy?: (text: string, language: string) => void;
|
|
43
43
|
snippetsBaseUrl?: string;
|
|
44
44
|
};
|
|
45
45
|
|
|
@@ -47,7 +47,6 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
47
47
|
language,
|
|
48
48
|
text,
|
|
49
49
|
hideCopyButton,
|
|
50
|
-
onCopy,
|
|
51
50
|
onChange,
|
|
52
51
|
snippetsBaseUrl,
|
|
53
52
|
}) => {
|
|
@@ -60,7 +59,7 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
60
59
|
.writeText(text)
|
|
61
60
|
// eslint-disable-next-line no-console
|
|
62
61
|
.catch(() => console.error('Failed to copy'));
|
|
63
|
-
|
|
62
|
+
trackClick('copy');
|
|
64
63
|
setIsCodeByteCopied(true);
|
|
65
64
|
}
|
|
66
65
|
};
|
|
@@ -80,6 +79,7 @@ export const Editor: React.FC<EditorProps> = ({
|
|
|
80
79
|
};
|
|
81
80
|
setStatus('waiting');
|
|
82
81
|
setOutput('');
|
|
82
|
+
trackClick('run');
|
|
83
83
|
|
|
84
84
|
try {
|
|
85
85
|
const response = await postSnippet(data, snippetsBaseUrl);
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { decode } from 'js-base64';
|
|
2
|
+
import Uri from 'jsuri';
|
|
3
|
+
|
|
4
|
+
import { trackUserClick } from '../libs/eventTracking';
|
|
5
|
+
|
|
6
|
+
import { languageOption, validLanguages } from '../consts';
|
|
7
|
+
|
|
8
|
+
export type CopyButtonMode = 'hide' | undefined;
|
|
9
|
+
|
|
10
|
+
export type CodebyteOptions = {
|
|
11
|
+
language: languageOption;
|
|
12
|
+
text: string;
|
|
13
|
+
copyMode: CopyButtonMode;
|
|
14
|
+
clientName: string;
|
|
15
|
+
parentPage: string;
|
|
16
|
+
renderMode: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export enum CodebytesParams {
|
|
20
|
+
Language = 'lang',
|
|
21
|
+
Text = 'text',
|
|
22
|
+
CopyMode = 'copy-mode',
|
|
23
|
+
ClientName = 'client-name',
|
|
24
|
+
Page = 'page',
|
|
25
|
+
Mode = 'mode',
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const getOptions = (): CodebyteOptions => {
|
|
29
|
+
const currentUri = new Uri(window.location.href);
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
language:
|
|
33
|
+
validLanguages.find(
|
|
34
|
+
(lang) =>
|
|
35
|
+
currentUri.getQueryParamValue(CodebytesParams.Language) === lang
|
|
36
|
+
) ?? '',
|
|
37
|
+
text: decode(currentUri.getQueryParamValue(CodebytesParams.Text) ?? ''),
|
|
38
|
+
copyMode: currentUri
|
|
39
|
+
.getQueryParamValue(CodebytesParams.CopyMode)
|
|
40
|
+
?.toLowerCase() as CopyButtonMode,
|
|
41
|
+
clientName:
|
|
42
|
+
currentUri.getQueryParamValue(CodebytesParams.ClientName) || 'Unknown',
|
|
43
|
+
parentPage:
|
|
44
|
+
currentUri.getQueryParamValue(CodebytesParams.Page) || document.referrer,
|
|
45
|
+
renderMode: currentUri.getQueryParamValue(CodebytesParams.Mode) || '',
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const trackClick = (target: string) => {
|
|
50
|
+
const options = getOptions();
|
|
51
|
+
const page_name = options.renderMode
|
|
52
|
+
? `${options.clientName}_${options.renderMode}`
|
|
53
|
+
: options.clientName;
|
|
54
|
+
|
|
55
|
+
trackUserClick({
|
|
56
|
+
page_name,
|
|
57
|
+
context: options.parentPage,
|
|
58
|
+
target,
|
|
59
|
+
});
|
|
60
|
+
};
|
package/src/index.tsx
CHANGED
|
@@ -3,24 +3,14 @@ import { FaviconIcon } from '@codecademy/gamut-icons';
|
|
|
3
3
|
import { Background, states, system } from '@codecademy/gamut-styles';
|
|
4
4
|
import { StyleProps } from '@codecademy/variance';
|
|
5
5
|
import styled from '@emotion/styled';
|
|
6
|
-
import React, { useState } from 'react';
|
|
6
|
+
import React, { useEffect, useState } from 'react';
|
|
7
7
|
|
|
8
8
|
import { helloWorld, languageOption } from './consts';
|
|
9
9
|
import { Editor } from './editor';
|
|
10
|
+
import { getOptions, trackClick } from './helpers';
|
|
10
11
|
import { LanguageSelection } from './languageSelection';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export interface CodeByteEditorProps {
|
|
15
|
-
text: string;
|
|
16
|
-
language: languageOption;
|
|
17
|
-
hideCopyButton: boolean;
|
|
18
|
-
onCopy?: CodebytesChangeHandler;
|
|
19
|
-
isIFrame?: boolean;
|
|
20
|
-
snippetsBaseUrl?: string;
|
|
21
|
-
onEdit?: CodebytesChangeHandler;
|
|
22
|
-
onLanguageChange?: CodebytesChangeHandler;
|
|
23
|
-
}
|
|
12
|
+
import { trackUserImpression } from './libs/eventTracking';
|
|
13
|
+
import { CodeByteEditorProps } from './types';
|
|
24
14
|
|
|
25
15
|
const editorStates = states({
|
|
26
16
|
isIFrame: { height: '100vh' },
|
|
@@ -50,11 +40,24 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
50
40
|
hideCopyButton,
|
|
51
41
|
isIFrame = false,
|
|
52
42
|
snippetsBaseUrl = process.env.CONTAINER_API_BASE,
|
|
53
|
-
onEdit,
|
|
54
|
-
onLanguageChange,
|
|
55
43
|
}) => {
|
|
56
44
|
const [text, setText] = useState<string>(initialText);
|
|
57
45
|
const [language, setLanguage] = useState<languageOption>(initialLanguage);
|
|
46
|
+
const [hasBeenEdited, setHasBeenEdited] = useState(false);
|
|
47
|
+
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
const options = getOptions();
|
|
50
|
+
const page_name = options.renderMode
|
|
51
|
+
? `${options.clientName}_${options.renderMode}`
|
|
52
|
+
: options.clientName;
|
|
53
|
+
|
|
54
|
+
trackUserImpression({
|
|
55
|
+
page_name,
|
|
56
|
+
context: options.parentPage,
|
|
57
|
+
target: 'codebyte',
|
|
58
|
+
});
|
|
59
|
+
}, []);
|
|
60
|
+
|
|
58
61
|
return (
|
|
59
62
|
<EditorContainer bg="black" as="main" isIFrame={isIFrame}>
|
|
60
63
|
<Box borderBottom={1} borderColor="gray-900" py={4} pl={8}>
|
|
@@ -65,6 +68,7 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
65
68
|
target="_blank"
|
|
66
69
|
rel="noreferrer"
|
|
67
70
|
aria-label="visit codecademy.com"
|
|
71
|
+
onClick={() => trackClick('logo')}
|
|
68
72
|
/>
|
|
69
73
|
</Box>
|
|
70
74
|
{language ? (
|
|
@@ -74,17 +78,23 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
|
|
|
74
78
|
hideCopyButton={hideCopyButton}
|
|
75
79
|
onChange={(newText: string) => {
|
|
76
80
|
setText(newText);
|
|
77
|
-
|
|
81
|
+
|
|
82
|
+
const { renderMode } = getOptions();
|
|
83
|
+
if (!renderMode && hasBeenEdited === false) {
|
|
84
|
+
setHasBeenEdited(true);
|
|
85
|
+
trackClick('edit');
|
|
86
|
+
}
|
|
78
87
|
}}
|
|
79
88
|
snippetsBaseUrl={snippetsBaseUrl}
|
|
80
89
|
/>
|
|
81
90
|
) : (
|
|
82
91
|
<LanguageSelection
|
|
83
92
|
onChange={(newLanguage) => {
|
|
84
|
-
const newText: string =
|
|
93
|
+
const newText: string =
|
|
94
|
+
text || (newLanguage ? helloWorld[newLanguage] : '');
|
|
85
95
|
setLanguage(newLanguage);
|
|
86
96
|
setText(newText);
|
|
87
|
-
|
|
97
|
+
trackClick('lang_select');
|
|
88
98
|
}}
|
|
89
99
|
/>
|
|
90
100
|
)}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createTracker } from '@codecademy/tracking';
|
|
2
|
+
|
|
3
|
+
const IS_DEV = process.env.NODE_ENV === 'development';
|
|
4
|
+
const API_BASE_URL =
|
|
5
|
+
process.env.GATSBY_MONOLITH_API_BASE || 'https://www.codecademy.com';
|
|
6
|
+
|
|
7
|
+
const tracker = createTracker({
|
|
8
|
+
apiBaseUrl: API_BASE_URL,
|
|
9
|
+
verbose: IS_DEV,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export const {
|
|
13
|
+
click: trackUserClick,
|
|
14
|
+
visit: trackUserVisit,
|
|
15
|
+
impression: trackUserImpression,
|
|
16
|
+
} = tracker;
|