Skip to content

Commit 93b2762

Browse files
authored
feat: diff view (#35)
1 parent 8d94650 commit 93b2762

File tree

17 files changed

+453
-87
lines changed

17 files changed

+453
-87
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Context, PermissionLevel } from '@graasp/sdk';
2+
3+
import {
4+
DIFF_VIEW_CONTAINER_CY,
5+
buildDataCy,
6+
} from '../../../src/config/selectors';
7+
import {
8+
CODE_EXPLAIN_MODE_SETTING,
9+
MOCK_DIFF_VIEW_SETTINGS,
10+
} from '../../fixtures/appSettings';
11+
12+
describe('Diff View', () => {
13+
beforeEach(() => {
14+
cy.setUpApi({
15+
database: {
16+
appSettings: [CODE_EXPLAIN_MODE_SETTING, MOCK_DIFF_VIEW_SETTINGS],
17+
},
18+
appContext: {
19+
context: Context.PLAYER,
20+
permission: PermissionLevel.Read,
21+
},
22+
});
23+
cy.visit('/');
24+
});
25+
26+
it('show diff view', () => {
27+
cy.get(buildDataCy(DIFF_VIEW_CONTAINER_CY)).should('be.visible');
28+
});
29+
});

cypress/fixtures/appSettings.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@ import {
66
APP_MODE_SETTINGS_NAME,
77
AppMode,
88
CODE_EXECUTION_SETTINGS_NAME,
9+
DIFF_VIEW_SETTINGS_NAME,
910
GENERAL_SETTINGS_NAME,
1011
INSTRUCTOR_CODE_VERSION_SETTINGS_NAME,
1112
} from '../../src/config/appSettingsTypes';
1213
import { PYTHON } from '../../src/config/constants';
1314
import {
1415
DEFAULT_APP_MODE_SETTINGS,
1516
DEFAULT_CODE_EXECUTION_SETTINGS,
17+
DEFAULT_DIFF_VIEW_SETTINGS,
1618
DEFAULT_GENERAL_SETTINGS,
1719
DEFAULT_INSTRUCTOR_CODE_VERSION_SETTINGS,
1820
} from '../../src/config/settings';
1921
import { CodeVersionType } from '../../src/interfaces/codeVersions';
2022
import {
2123
AppModeSettings,
2224
CodeExecutionSettings,
25+
DiffViewSettings,
2326
} from '../../src/interfaces/settings';
2427
import { MEMBERS } from './members';
2528
import { MOCK_SERVER_ITEM } from './mockItem';
@@ -44,6 +47,12 @@ const MOCK_COMMIT_MESSAGE = 'This is a mock commit message';
4447
const MOCK_COMMIT_DESCRIPTION =
4548
'This is a mock commit Description\nOn multiple lines';
4649

50+
export const MOCK_DIFF_OLD_CODE =
51+
'if not value is None:\n print("not none")';
52+
53+
export const MOCK_DIFF_NEW_CODE =
54+
'if value is not None:\n print("not none")';
55+
4756
export const EMPTY_SETTING: AppSetting = {
4857
id: v4(),
4958
name: '',
@@ -108,6 +117,15 @@ export const CODE_COLLABORATE_MODE_SETTING: AppSetting & {
108117
},
109118
};
110119

120+
export const CODE_EXPLAIN_MODE_SETTING: AppSetting & {
121+
data: AppModeSettings;
122+
} = {
123+
...MOCK_APP_MODE_SETTING,
124+
data: {
125+
mode: AppMode.Explain,
126+
},
127+
};
128+
111129
export const MOCK_CODE_SETTINGS: AppSetting & { data: CodeVersionType } = {
112130
...EMPTY_SETTING,
113131
id: v4(),
@@ -120,6 +138,18 @@ export const MOCK_CODE_SETTINGS: AppSetting & { data: CodeVersionType } = {
120138
},
121139
};
122140

141+
export const MOCK_DIFF_VIEW_SETTINGS: AppSetting & { data: DiffViewSettings } =
142+
{
143+
...EMPTY_SETTING,
144+
id: v4(),
145+
name: DIFF_VIEW_SETTINGS_NAME,
146+
data: {
147+
...DEFAULT_DIFF_VIEW_SETTINGS,
148+
oldCode: MOCK_DIFF_OLD_CODE,
149+
newCode: MOCK_DIFF_NEW_CODE,
150+
},
151+
};
152+
123153
export const MOCK_EXECUTABLE_PYTHON_CODE_SETTINGS: AppSetting & {
124154
data: CodeVersionType;
125155
} = {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"prism-react-renderer": "1.3.5",
5151
"qs": "6.11.0",
5252
"react": "18.2.0",
53+
"react-diff-viewer": "3.1.1",
5354
"react-dom": "18.2.0",
5455
"react-i18next": "12.0.0",
5556
"react-markdown": "8.0.3",

src/components/common/AppModeWrapper.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import { AppModeSettingsKeys } from '../../interfaces/settings';
1111
import CodeReview from '../codeReview/CodeReview';
1212
import { useCodeVersionContext } from '../context/CodeVersionContext';
1313
import { useSettings } from '../context/SettingsContext';
14+
import DiffView from '../diffView/DiffView';
1415
import CodeReviewContainer from '../layout/CodeReviewContainer';
1516
import Repl from '../repl/Repl';
1617
import CodeEditor from './CodeEditor';
18+
import Loader from './Loader';
1719

1820
// eslint-disable-next-line @typescript-eslint/ban-types
1921
type Props = {};
@@ -23,7 +25,7 @@ const AppModeWrapper: FC<Props> = () => {
2325
const {
2426
[APP_MODE_SETTINGS_NAME]: appModeSetting = DEFAULT_APP_MODE_SETTINGS,
2527
} = useSettings();
26-
const [view, setView] = useState<AppView>(AppView.CodeExecution);
28+
const [view, setView] = useState<AppView>(AppView.LoadingView);
2729

2830
useEffect(() => {
2931
switch (appModeSetting[AppModeSettingsKeys.Mode]) {
@@ -36,12 +38,17 @@ const AppModeWrapper: FC<Props> = () => {
3638
case AppMode.Review:
3739
setView(AppView.CodeReview);
3840
break;
41+
case AppMode.Explain:
42+
setView(AppView.DiffView);
43+
break;
3944
default:
4045
setView(DEFAULT_APP_VIEW);
4146
}
4247
}, [appModeSetting]);
4348

4449
switch (view) {
50+
case AppView.LoadingView:
51+
return <Loader />;
4552
case AppView.CodeEditor:
4653
return (
4754
<CodeReviewContainer data-cy={CODE_EDITOR_CONTAINER_CYPRESS}>
@@ -58,6 +65,8 @@ const AppModeWrapper: FC<Props> = () => {
5865
onClose={() => setView(AppView.CodeReview)}
5966
/>
6067
);
68+
case AppView.DiffView:
69+
return <DiffView />;
6170
case AppView.CodeReview:
6271
default:
6372
return <CodeReview setView={setView} />;

src/components/context/SettingsContext.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
CODE_EXECUTION_SETTINGS_NAME,
1010
DATA_FILE_LIST_SETTINGS_NAME,
1111
DATA_FILE_SETTINGS_NAME,
12+
DIFF_VIEW_SETTINGS_NAME,
1213
GENERAL_SETTINGS_NAME,
1314
INSTRUCTOR_CODE_VERSION_SETTINGS_NAME,
1415
} from '../../config/appSettingsTypes';
@@ -17,13 +18,15 @@ import {
1718
DEFAULT_APP_MODE_SETTINGS,
1819
DEFAULT_CODE_EXECUTION_SETTINGS,
1920
DEFAULT_DATA_FILE_LIST_SETTINGS,
21+
DEFAULT_DIFF_VIEW_SETTINGS,
2022
DEFAULT_GENERAL_SETTINGS,
2123
DEFAULT_INSTRUCTOR_CODE_VERSION_SETTINGS,
2224
} from '../../config/settings';
2325
import {
2426
AppModeSettings,
2527
CodeExecutionSettings,
2628
DataFileListSettings,
29+
DiffViewSettings,
2730
GeneralSettings,
2831
InstructorCodeVersionSettings,
2932
} from '../../interfaces/settings';
@@ -36,6 +39,7 @@ interface AllSettingsType {
3639
[INSTRUCTOR_CODE_VERSION_SETTINGS_NAME]?: InstructorCodeVersionSettings;
3740
[APP_MODE_SETTINGS_NAME]?: AppModeSettings;
3841
[DATA_FILE_LIST_SETTINGS_NAME]?: DataFileListSettings;
42+
[DIFF_VIEW_SETTINGS_NAME]?: DiffViewSettings;
3943
}
4044

4145
// default values for the data property of settings by name
@@ -46,6 +50,7 @@ const defaultSettingsValues: AllSettingsType = {
4650
DEFAULT_INSTRUCTOR_CODE_VERSION_SETTINGS,
4751
[APP_MODE_SETTINGS_NAME]: DEFAULT_APP_MODE_SETTINGS,
4852
[DATA_FILE_LIST_SETTINGS_NAME]: DEFAULT_DATA_FILE_LIST_SETTINGS,
53+
[DIFF_VIEW_SETTINGS_NAME]: DEFAULT_DIFF_VIEW_SETTINGS,
4954
};
5055

5156
// list of the settings names
@@ -55,6 +60,7 @@ const ALL_SETTING_NAMES = [
5560
INSTRUCTOR_CODE_VERSION_SETTINGS_NAME,
5661
APP_MODE_SETTINGS_NAME,
5762
DATA_FILE_LIST_SETTINGS_NAME,
63+
DIFF_VIEW_SETTINGS_NAME,
5864
] as const;
5965

6066
// automatically generated types
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { FC } from 'react';
2+
import ReactDiffViewer from 'react-diff-viewer';
3+
4+
import { Stack } from '@mui/material';
5+
6+
import { DIFF_VIEW_SETTINGS_NAME } from '../../config/appSettingsTypes';
7+
import { DIFF_VIEW_CONTAINER_CY } from '../../config/selectors';
8+
import { DEFAULT_DIFF_VIEW_SETTINGS } from '../../config/settings';
9+
import { DiffViewSettingsKeys } from '../../interfaces/settings';
10+
import { useSettings } from '../context/SettingsContext';
11+
12+
const DiffView: FC = () => {
13+
const {
14+
[DIFF_VIEW_SETTINGS_NAME]: diffViewSettings = DEFAULT_DIFF_VIEW_SETTINGS,
15+
} = useSettings();
16+
17+
return (
18+
<Stack data-cy={DIFF_VIEW_CONTAINER_CY} direction="column" m={2}>
19+
<ReactDiffViewer
20+
linesOffset={diffViewSettings[DiffViewSettingsKeys.LinesOffset]}
21+
oldValue={diffViewSettings[DiffViewSettingsKeys.OldCode]}
22+
newValue={diffViewSettings[DiffViewSettingsKeys.NewCode]}
23+
/>
24+
</Stack>
25+
);
26+
};
27+
export default DiffView;

src/components/views/admin/settings/AppModeSelect.tsx

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)