Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prototype 1 #4

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"name": "graasp-app-math-input",
"version": "1.2.3",
"version": "0.0.0",
"license": "AGPL-3.0-only",
"author": "Graasp",
"contributors": [
"Basile Spaenlehauer",
"Jérémy La Scala"
{
"name": "Jérémy La Scala",
"email": "[email protected]",
"url": "https://github.com/swouf"
}
],
"homepage": ".",
"type": "module",
Expand All @@ -25,6 +28,7 @@
"@types/react": "18.2.79",
"@types/react-dom": "18.2.25",
"i18next": "^23.9.0",
"mathlive": "^0.98.6",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-i18next": "14.1.1",
Expand All @@ -34,6 +38,7 @@
"scripts": {
"dev": "yarn vite",
"start": "yarn dev",
"dev:mock": "VITE_ENABLE_MOCK_API=true && yarn vite",
"start:test": "yarn vite --mode test",
"build": "yarn vite build",
"build:test": "yarn vite build --mode test",
Expand Down
2 changes: 1 addition & 1 deletion src/config/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ i18n.use(initReactI18next).init({
debug: import.meta.env.DEV,
ns: [defaultNS],
defaultNS,
keySeparator: false,
keySeparator: '.',
interpolation: {
escapeValue: false,
formatSeparator: ',',
Expand Down
6 changes: 6 additions & 0 deletions src/config/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@ export const PLAYER_VIEW_CY = 'player-view';
export const BUILDER_VIEW_CY = 'builder-view';
export const ANALYTICS_VIEW_CY = 'analytics-view';

export const MATH_INPUT_VIEW_CY = 'math-input-view';
export const SETTINGS_VIEW_CY = 'settings_view';

export const MATH_INPUT_TAB_CY = 'math-input-tab';
export const SETTINGS_TAB_CY = 'settings-tab';

export const buildDataCy = (selector: string): string =>
`[data-cy=${selector}]`;
2 changes: 2 additions & 0 deletions src/langs/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"translations": {
"Welcome to the Graasp App Starter Kit": "Welcome to the Graasp App Starter Kit",
"MATH_INPUT_TAB": "Math input",
"SETTINGS_TAB": "Settings",
"ERROR_BOUNDARY": {
"FALLBACK": {
"MESSAGE_TITLE": "Sorry, something went wrong with this application",
Expand Down
14 changes: 14 additions & 0 deletions src/math-field.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MathfieldElement } from 'mathlive';

declare global {
namespace React.JSX {
interface IntrinsicElements {
'math-field': React.DetailedHTMLProps<
React.HTMLAttributes<MathfieldElement>,
MathfieldElement
>;
}
}
}

export {};
2 changes: 1 addition & 1 deletion src/mocks/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const mockItem: DiscriminatedItem = AppItemFactory({
export const defaultMockContext: LocalContext = {
apiHost: API_HOST,
permission: PermissionLevel.Admin,
context: 'builder',
context: 'player',
itemId: mockItem.id,
memberId: mockMembers[0].id,
};
Expand Down
50 changes: 50 additions & 0 deletions src/modules/common/MathField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { ChangeEvent, useEffect, useRef, useState } from 'react';

// eslint-disable-next-line import/no-duplicates
import 'mathlive';
// eslint-disable-next-line import/no-duplicates
import { MathfieldElement } from 'mathlive';

interface MathFieldProps {
value?: string;
onChange?: (newValue: string) => void;
}

const MathField = ({ value, onChange }: MathFieldProps): JSX.Element => {
const [internalValue, setInternalValue] = useState<string>('');
const mf = useRef<MathfieldElement>();

useEffect(() => {
if (mf.current) {
mf.current.mathVirtualKeyboardPolicy = 'manual';
}
});

useEffect(() => {
if (value) {
setInternalValue(value);
}
}, [value]);

const onInput = (evt: ChangeEvent<MathfieldElement>): void => {
const newValue = evt.target.value;
if (onChange) {
onChange(newValue);
}
setInternalValue(newValue);
};

return (
<math-field
ref={mf}
style={{
width: '100%',
}}
onInput={onInput}
>
{typeof value !== 'undefined' ? value : internalValue}
</math-field>
);
};

export default MathField;
30 changes: 30 additions & 0 deletions src/modules/common/TabPanel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';

interface TabPanelProps {
children?: React.ReactNode;
index: number;
value: number;
}

const TabPanel = (props: TabPanelProps): JSX.Element => {
const { children, value, index, ...other } = props;

return (
<div
role="tabpanel"
hidden={value !== index}
id={`simple-tabpanel-${index}`}
aria-labelledby={`simple-tab-${index}`}
{...other}
>
{value === index && (
<Box sx={{ p: 3 }}>
<Typography>{children}</Typography>
</Box>
)}
</div>
);
};

export default TabPanel;
162 changes: 72 additions & 90 deletions src/modules/main/BuilderView.tsx
Original file line number Diff line number Diff line change
@@ -1,104 +1,86 @@
import { Box, Button, Stack, Typography } from '@mui/material';
import { SyntheticEvent, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import Box from '@mui/material/Box';
import Paper from '@mui/material/Paper';
import Tab from '@mui/material/Tab';
import Tabs from '@mui/material/Tabs';

import { useLocalContext } from '@graasp/apps-query-client';
import { PermissionLevel } from '@graasp/sdk';

import { BUILDER_VIEW_CY } from '@/config/selectors';
import {
BUILDER_VIEW_CY,
MATH_INPUT_TAB_CY,
SETTINGS_TAB_CY,
} from '@/config/selectors';

import { hooks, mutations } from '../../config/queryClient';
import TabPanel from '../common/TabPanel';
import MathInputView from '../math-input-view/MathInputView';
import SettingsView from '../settings/SettingsView';

const AppSettingsDisplay = (): JSX.Element => {
const { data: appSettings } = hooks.useAppSettings();
return (
<Box p={2}>
<Typography>App Setting</Typography>
{appSettings ? (
<pre>{JSON.stringify(appSettings, null, 2)}</pre>
) : (
<Typography>Loading</Typography>
)}
</Box>
interface TabType {
tabLabel: string;
tabChild: JSX.Element;
tabSelector: string;
}

const BuilderView = (): JSX.Element => {
const [selectedTab, setSelectedTab] = useState<number>(0);
const { t } = useTranslation();
const handleChange = (event: SyntheticEvent, value: number): void => {
setSelectedTab(value);
};
const { permission } = useLocalContext();

const isAdmin = useMemo(
() => permission === PermissionLevel.Admin,
[permission],
);
};

const AppActionsDisplay = (): JSX.Element => {
const { data: appActions } = hooks.useAppActions();
return (
<Box p={2}>
<Typography>App Actions</Typography>
{appActions ? (
<pre>{JSON.stringify(appActions, null, 2)}</pre>
) : (
<Typography>Loading</Typography>
)}
</Box>
const mathInputTab = useMemo(
() => ({
tabLabel: t('MATH_INPUT_TAB'),
tabChild: <MathInputView />,
tabSelector: MATH_INPUT_TAB_CY,
}),
[t],
);
};

const BuilderView = (): JSX.Element => {
const { permission } = useLocalContext();
const { data: appDatas } = hooks.useAppData();
const { mutate: postAppData } = mutations.usePostAppData();
const { mutate: postAppAction } = mutations.usePostAppAction();
const { mutate: patchAppData } = mutations.usePatchAppData();
const { mutate: deleteAppData } = mutations.useDeleteAppData();
const { mutate: postAppSetting } = mutations.usePostAppSetting();
const settingsTab = useMemo(
() => ({
tabLabel: t('SETTINGS_TAB'),
tabChild: <SettingsView />,
tabSelector: SETTINGS_TAB_CY,
}),
[t],
);

const tabs: TabType[] = useMemo(
() => (isAdmin ? [mathInputTab, settingsTab] : [mathInputTab]),
[isAdmin, mathInputTab, settingsTab],
);

return (
<div data-cy={BUILDER_VIEW_CY}>
Builder as {permission}
<Stack direction="column" spacing={2}>
<Stack direction="row" justifyContent="center" spacing={1}>
<Button
variant="outlined"
onClick={() =>
postAppAction({ data: { content: 'hello' }, type: 'an-action' })
}
>
Post new App Action
</Button>
<Button
variant="outlined"
onClick={() =>
postAppData({ data: { content: 'hello' }, type: 'a-type' })
}
>
Post new App Data
</Button>
<Button
variant="outlined"
onClick={() =>
postAppSetting({ data: { content: 'hello' }, name: 'setting' })
}
>
Post new App Setting
</Button>
<Button
variant="outlined"
onClick={() => {
const data = appDatas?.at(-1);
patchAppData({
id: data?.id || '',
data: { content: `${data?.data.content}-` },
});
}}
>
Patch last App Data
</Button>
<Button
variant="outlined"
onClick={() => deleteAppData({ id: appDatas?.at(-1)?.id || '' })}
>
Delete last App Data
</Button>
</Stack>
<Box p={2}>
<Typography>App Data</Typography>
<pre>{JSON.stringify(appDatas, null, 2)}</pre>
</Box>
<AppSettingsDisplay />
<AppActionsDisplay />
</Stack>
</div>
<Paper data-cy={BUILDER_VIEW_CY} elevation={0} sx={{ width: '100%' }}>
<Box sx={{ borderBottom: 1, borderColor: 'divider' }}>
<Tabs
value={selectedTab}
onChange={handleChange}
aria-label="Tabs in the builder view."
>
{tabs.map((tab, index) => (
<Tab key={index} label={tab.tabLabel} data-cy={tab.tabSelector} />
))}
</Tabs>
</Box>
{tabs.map((tab, index) => (
<TabPanel key={index} value={selectedTab} index={index}>
{tab.tabChild}
</TabPanel>
))}
</Paper>
);
};

export default BuilderView;
25 changes: 7 additions & 18 deletions src/modules/main/PlayerView.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
import { Box, Typography } from '@mui/material';
import Container from '@mui/material/Container';

import { useLocalContext } from '@graasp/apps-query-client';

import { hooks } from '@/config/queryClient';
import { PLAYER_VIEW_CY } from '@/config/selectors';

const PlayerView = (): JSX.Element => {
const { permission } = useLocalContext();
const { data: appContext } = hooks.useAppContext();
const members = appContext?.members;
import MathInputView from '../math-input-view/MathInputView';

return (
<div data-cy={PLAYER_VIEW_CY}>
Player as {permission}
<Box p={2}>
<Typography>Members</Typography>
<pre>{JSON.stringify(members, null, 2)}</pre>
</Box>
</div>
);
};
const PlayerView = (): JSX.Element => (
<Container data-cy={PLAYER_VIEW_CY}>
<MathInputView />
</Container>
);
export default PlayerView;
11 changes: 11 additions & 0 deletions src/modules/math-input-view/MathInputView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Box from '@mui/material/Box';

import MathField from '../common/MathField';

const MathInputView = (): JSX.Element => (
<Box>
<MathField />
</Box>
);

export default MathInputView;
7 changes: 7 additions & 0 deletions src/modules/settings/SettingsView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Typography from '@mui/material/Typography';

const SettingsView = (): JSX.Element => (
<Typography variant="h1">Settings</Typography>
);

export default SettingsView;
Loading
Loading