@codecademy/codebytes 0.3.1-alpha.cef935.0 → 0.4.1-alpha.e45de0.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 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.cef935.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.3.0...@codecademy/codebytes@0.3.1-alpha.cef935.0) (2022-01-13)
6
+ ### [0.4.1-alpha.e45de0.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.4.0...@codecademy/codebytes@0.4.1-alpha.e45de0.0) (2022-01-21)
7
7
 
8
8
  **Note:** Version bump only for package @codecademy/codebytes
9
9
 
@@ -11,6 +11,15 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
11
11
 
12
12
 
13
13
 
14
+ ## [0.4.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.3.0...@codecademy/codebytes@0.4.0) (2022-01-21)
15
+
16
+
17
+ ### Features
18
+
19
+ * **Codebytes:** move language selection component disc 354 ([#17](https://github.com/Codecademy/client-modules/issues/17)) ([040553d](https://github.com/Codecademy/client-modules/commit/040553dcc7867b6e331712365bcc19ea2df306d5))
20
+
21
+
22
+
14
23
  ## [0.3.0](https://github.com/Codecademy/client-modules/compare/@codecademy/codebytes@0.2.0...@codecademy/codebytes@0.3.0) (2022-01-12)
15
24
 
16
25
 
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.cef935.0",
4
+ "version": "0.4.1-alpha.e45de0.0",
5
5
  "author": "Codecademy Engineering <dev@codecademy.com>",
6
6
  "sideEffects": [
7
7
  "**/*.css",
@@ -23,10 +23,13 @@
23
23
  "@codecademy/gamut": "*",
24
24
  "@codecademy/gamut-icons": "*",
25
25
  "@codecademy/gamut-styles": "*",
26
+ "@codecademy/tracking": "0.18.0",
26
27
  "@codecademy/variance": "*",
27
28
  "@emotion/react": "^11.4.0",
28
29
  "@emotion/styled": "^11.3.0",
29
30
  "@monaco-editor/react": "4.3.1",
31
+ "js-base64": "^3.6.0",
32
+ "jsuri": "^1.3.1",
30
33
  "monaco-editor": ">= 0.25.0 < 1",
31
34
  "react-resize-observer": "1.1.1"
32
35
  },
@@ -43,10 +46,11 @@
43
46
  "@emotion/jest": "^11.3.0",
44
47
  "@testing-library/dom": "^7.31.2",
45
48
  "@testing-library/react": "^11.0.4",
49
+ "@types/jsuri": "^1.3.30",
46
50
  "@types/loadable__component": "^5.13.2"
47
51
  },
48
52
  "publishConfig": {
49
53
  "access": "public"
50
54
  },
51
- "gitHead": "0f05e194904faae9337cdbd57e84382acc355a92"
55
+ "gitHead": "ea213a90925e28700700dc0f3fed37494acd8614"
52
56
  }
package/src/api.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { languageOption } from './consts';
1
+ import type { LanguageOption } from './consts';
2
2
 
3
3
  interface Response {
4
4
  stderr: string;
@@ -7,7 +7,7 @@ interface Response {
7
7
  }
8
8
 
9
9
  interface PostSnippetData {
10
- language: languageOption;
10
+ language: LanguageOption;
11
11
  code: string;
12
12
  }
13
13
 
package/src/consts.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  // key = language param to send to snippets service
2
2
  // val = label in language selection drop down
3
- export const languageOptions = {
3
+ export const LanguageOptions = {
4
4
  '': 'Select a language',
5
5
  cpp: 'C++',
6
6
  csharp: 'C#',
@@ -12,11 +12,11 @@ export const languageOptions = {
12
12
  scheme: 'Scheme',
13
13
  };
14
14
 
15
- export type languageOption = keyof typeof languageOptions;
15
+ export type LanguageOption = keyof typeof LanguageOptions;
16
16
 
17
- export const validLanguages = Object.keys(languageOptions).filter(
17
+ export const validLanguages = Object.keys(LanguageOptions).filter(
18
18
  (option) => !!option
19
- ) as languageOption[];
19
+ ) as LanguageOption[];
20
20
 
21
21
  const cpp = `#include <iostream>
22
22
  int main() {
@@ -52,7 +52,7 @@ const scheme = `(begin
52
52
  (display "Hello world!")
53
53
  (newline))`;
54
54
 
55
- export const helloWorld: { [key in languageOption]?: string } = {
55
+ export const helloWorld = {
56
56
  cpp,
57
57
  csharp,
58
58
  golang,
@@ -61,4 +61,4 @@ export const helloWorld: { [key in languageOption]?: string } = {
61
61
  python,
62
62
  ruby,
63
63
  scheme,
64
- };
64
+ } as const;
package/src/editor.tsx CHANGED
@@ -10,10 +10,10 @@ import styled from '@emotion/styled';
10
10
  import React, { useState } from 'react';
11
11
 
12
12
  import { postSnippet } from './api';
13
- import type { languageOption } from './consts';
13
+ import type { LanguageOption } from './consts';
14
14
  import { Drawers } from './drawers';
15
+ import { trackClick } from './helpers';
15
16
  import { SimpleMonacoEditor } from './MonacoEditor';
16
- import { CodebytesChangeHandlerMap } from './types';
17
17
 
18
18
  const Output = styled.pre<{ hasError: boolean }>`
19
19
  width: 100%;
@@ -37,10 +37,9 @@ const DOCKER_SIGTERM = 143;
37
37
 
38
38
  type EditorProps = {
39
39
  hideCopyButton: boolean;
40
- language: languageOption;
40
+ language: LanguageOption;
41
41
  text: string;
42
42
  onChange: (text: string) => void;
43
- on?: CodebytesChangeHandlerMap;
44
43
  snippetsBaseUrl?: string;
45
44
  };
46
45
 
@@ -48,7 +47,6 @@ export const Editor: React.FC<EditorProps> = ({
48
47
  language,
49
48
  text,
50
49
  hideCopyButton,
51
- on = {},
52
50
  onChange,
53
51
  snippetsBaseUrl,
54
52
  }) => {
@@ -61,8 +59,8 @@ export const Editor: React.FC<EditorProps> = ({
61
59
  .writeText(text)
62
60
  // eslint-disable-next-line no-console
63
61
  .catch(() => console.error('Failed to copy'));
64
- on.copy?.(text, language);
65
62
  setIsCodeByteCopied(true);
63
+ trackClick('copy');
66
64
  }
67
65
  };
68
66
 
@@ -81,7 +79,7 @@ export const Editor: React.FC<EditorProps> = ({
81
79
  };
82
80
  setStatus('waiting');
83
81
  setOutput('');
84
- on.run?.();
82
+ trackClick('run');
85
83
 
86
84
  try {
87
85
  const response = await postSnippet(data, snippetsBaseUrl);
@@ -0,0 +1,43 @@
1
+ import Uri from 'jsuri';
2
+
3
+ import { trackUserClick } from '../libs/eventTracking';
4
+
5
+ export type CodebyteOptions = {
6
+ clientName: string;
7
+ parentPage: string;
8
+ renderMode: string;
9
+ };
10
+
11
+ export enum CodebytesParams {
12
+ Language = 'lang',
13
+ Text = 'text',
14
+ CopyMode = 'copy-mode',
15
+ ClientName = 'client-name',
16
+ Page = 'page',
17
+ Mode = 'mode',
18
+ }
19
+
20
+ export const getOptions = (): CodebyteOptions => {
21
+ const currentUri = new Uri(window.location.href);
22
+
23
+ return {
24
+ clientName:
25
+ currentUri.getQueryParamValue(CodebytesParams.ClientName) || 'Unknown',
26
+ parentPage:
27
+ currentUri.getQueryParamValue(CodebytesParams.Page) || document.referrer,
28
+ renderMode: currentUri.getQueryParamValue(CodebytesParams.Mode) || '',
29
+ };
30
+ };
31
+
32
+ export const trackClick = (target: string) => {
33
+ const options = getOptions();
34
+ const page_name = options.renderMode
35
+ ? `${options.clientName}_${options.renderMode}`
36
+ : options.clientName;
37
+
38
+ trackUserClick({
39
+ page_name,
40
+ context: options.parentPage,
41
+ target,
42
+ });
43
+ };
package/src/index.tsx CHANGED
@@ -3,11 +3,13 @@ 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
- import { helloWorld, languageOption } from './consts';
8
+ import { helloWorld, LanguageOption } from './consts';
9
9
  import { Editor } from './editor';
10
+ import { getOptions, trackClick } from './helpers';
10
11
  import { LanguageSelection } from './languageSelection';
12
+ import { trackUserImpression } from './libs/eventTracking';
11
13
  import { CodeByteEditorProps } from './types';
12
14
 
13
15
  const editorStates = states({
@@ -37,11 +39,27 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
37
39
  language: initialLanguage,
38
40
  hideCopyButton,
39
41
  isIFrame = false,
40
- on = {},
41
42
  snippetsBaseUrl = process.env.CONTAINER_API_BASE,
43
+ onEdit,
44
+ onLanguageChange,
42
45
  }) => {
43
46
  const [text, setText] = useState<string>(initialText);
44
- const [language, setLanguage] = useState<languageOption>(initialLanguage);
47
+ const [language, setLanguage] = useState<LanguageOption>(initialLanguage);
48
+ const [hasBeenEdited, setHasBeenEdited] = useState(false);
49
+
50
+ useEffect(() => {
51
+ const options = getOptions();
52
+ const page_name = options.renderMode
53
+ ? `${options.clientName}_${options.renderMode}`
54
+ : options.clientName;
55
+
56
+ trackUserImpression({
57
+ page_name,
58
+ context: options.parentPage,
59
+ target: 'codebyte',
60
+ });
61
+ }, []);
62
+
45
63
  return (
46
64
  <EditorContainer bg="black" as="main" isIFrame={isIFrame}>
47
65
  <Box borderBottom={1} borderColor="gray-900" py={4} pl={8}>
@@ -52,7 +70,7 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
52
70
  target="_blank"
53
71
  rel="noreferrer"
54
72
  aria-label="visit codecademy.com"
55
- onClick={() => on.logoClick?.()}
73
+ onClick={() => trackClick('logo')}
56
74
  />
57
75
  </Box>
58
76
  {language ? (
@@ -62,18 +80,25 @@ export const CodeByteEditor: React.FC<CodeByteEditorProps> = ({
62
80
  hideCopyButton={hideCopyButton}
63
81
  onChange={(newText: string) => {
64
82
  setText(newText);
65
- on.edit?.(newText, language);
83
+ onEdit?.(newText, language);
84
+
85
+ const { renderMode } = getOptions();
86
+ if (!renderMode && hasBeenEdited === false) {
87
+ setHasBeenEdited(true);
88
+ trackClick('edit');
89
+ }
66
90
  }}
67
- on={on}
68
91
  snippetsBaseUrl={snippetsBaseUrl}
69
92
  />
70
93
  ) : (
71
94
  <LanguageSelection
72
95
  onChange={(newLanguage) => {
73
- const newText: string = text || helloWorld[newLanguage] || '';
96
+ const newText: string =
97
+ text || (newLanguage ? helloWorld[newLanguage] : '');
74
98
  setLanguage(newLanguage);
75
99
  setText(newText);
76
- on.languageChange?.(newText, newLanguage);
100
+ trackClick('lang_select');
101
+ onLanguageChange?.(newText, newLanguage);
77
102
  }}
78
103
  />
79
104
  )}
@@ -1,45 +1,26 @@
1
- import { Select } from '@codecademy/gamut';
2
- import { Background } from '@codecademy/gamut-styles';
3
- import styled from '@emotion/styled';
1
+ import { Select, Text } from '@codecademy/gamut';
2
+ import { ColorMode } from '@codecademy/gamut-styles';
4
3
  import React from 'react';
5
4
 
6
- import type { languageOption } from './consts';
7
- import { languageOptions } from './consts';
8
-
9
- const StyledSelect = styled(Select)`
10
- margin-top: 1rem;
11
- color: ${({ theme }) => theme.colors.text};
12
- select {
13
- color: ${({ theme }) => theme.colors.text};
14
- background-color: ${({ theme }) => theme.colors.black};
15
-
16
- &:hover,
17
- &:active,
18
- &:focus {
19
- border-color: ${({ theme }) => theme.colors.primary};
20
- }
21
- &:focus {
22
- box-shadow: inset 0 0 0 1px ${({ theme }) => theme.colors.primary};
23
- }
24
- }
25
- `;
5
+ import type { LanguageOption } from './consts';
6
+ import { LanguageOptions } from './consts';
26
7
 
27
8
  export type LanguageSelectionProps = {
28
- onChange: (newLanguage: languageOption) => void;
9
+ onChange: (newLanguage: LanguageOption) => void;
29
10
  };
30
11
 
31
12
  export const LanguageSelection: React.FC<LanguageSelectionProps> = ({
32
13
  onChange,
33
14
  }) => {
34
15
  return (
35
- <Background bg="black" flex={1} px={16} pt={48}>
36
- Which language do you want to code in?
37
- <StyledSelect
16
+ <ColorMode mode="dark" flex={1} px={16} pt={48}>
17
+ <Text mb={16}>Which language do you want to code in?</Text>
18
+ <Select
38
19
  id="language-select"
39
20
  aria-label="Select a language"
40
- options={languageOptions}
41
- onChange={(e) => onChange(e.target.value as languageOption)}
21
+ options={LanguageOptions}
22
+ onChange={(e) => onChange(e.target.value as LanguageOption)}
42
23
  />
43
- </Background>
24
+ </ColorMode>
44
25
  );
45
26
  };
@@ -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;
package/src/types.ts CHANGED
@@ -1,20 +1,13 @@
1
- import { languageOption } from './consts';
2
-
3
- type CodebytesChangeHandler = (text: string, language: languageOption) => void;
4
-
5
- export type CodebytesChangeHandlerMap = {
6
- logoClick?: () => void;
7
- edit?: CodebytesChangeHandler;
8
- languageChange?: CodebytesChangeHandler;
9
- copy?: CodebytesChangeHandler;
10
- run?: () => void;
11
- };
1
+ import { LanguageOption } from './consts';
12
2
 
3
+ type CodebytesChangeHandler = (text: string, language: LanguageOption) => void;
13
4
  export interface CodeByteEditorProps {
14
5
  text: string;
15
- language: languageOption;
6
+ language: LanguageOption;
16
7
  hideCopyButton: boolean;
8
+ onCopy?: CodebytesChangeHandler;
17
9
  isIFrame?: boolean;
18
- snippetsBaseUrl?: string /* TODO in DISC-353: Determine best way to host and include snippets endpoint for both staging and production in both the monolith and next.js repo. */;
19
- on?: CodebytesChangeHandlerMap;
10
+ snippetsBaseUrl?: string;
11
+ onEdit?: CodebytesChangeHandler;
12
+ onLanguageChange?: CodebytesChangeHandler;
20
13
  }