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

feat: add folder redesign amendments #1207

Merged
merged 11 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@emotion/styled": "11.11.5",
"@graasp/chatbox": "3.1.0",
"@graasp/map": "1.11.1",
"@graasp/query-client": "3.5.0",
"@graasp/query-client": "3.6.0",
"@graasp/sdk": "4.7.6",
"@graasp/translations": "1.27.0",
"@graasp/ui": "4.17.0",
Expand Down
25 changes: 18 additions & 7 deletions src/components/item/form/DescriptionForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Stack } from '@mui/material';
import { Box, FormLabel, Stack, Typography } from '@mui/material';

import {
DescriptionPlacementType,
Expand All @@ -7,6 +7,9 @@ import {
} from '@graasp/sdk';
import TextEditor from '@graasp/ui/text-editor';

import { useBuilderTranslation } from '@/config/i18n';
import { BUILDER } from '@/langs/constants';

import DescriptionPlacementForm from './DescriptionPlacementForm';

type DescriptionFormProps = {
Expand All @@ -22,6 +25,7 @@ const DescriptionForm = ({
item,
setChanges,
}: DescriptionFormProps): JSX.Element => {
const { t: translateBuilder } = useBuilderTranslation();
const onChange = (content: string): void => {
setChanges({
description: content,
Expand All @@ -38,12 +42,19 @@ const DescriptionForm = ({

return (
<Stack spacing={2}>
<TextEditor
id={id}
value={(updatedProperties?.description || item?.description) ?? ''}
onChange={onChange}
showActions={false}
/>
<Box>
<FormLabel>
<Typography variant="caption">
{translateBuilder(BUILDER.DESCRIPTION_LABEL)}
</Typography>
</FormLabel>
<TextEditor
id={id}
value={(updatedProperties?.description || item?.description) ?? ''}
onChange={onChange}
showActions={false}
/>
</Box>

{updatedProperties.type !== ItemType.FOLDER && (
<DescriptionPlacementForm
Expand Down
16 changes: 3 additions & 13 deletions src/components/item/form/DisplayNameForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ import { ChangeEvent } from 'react';

import ClearIcon from '@mui/icons-material/Clear';
import InfoIcon from '@mui/icons-material/Info';
import {
IconButton,
Stack,
TextField,
Tooltip,
useMediaQuery,
useTheme,
} from '@mui/material';
import { IconButton, Stack, TextField, Tooltip } from '@mui/material';

import { useBuilderTranslation } from '../../../config/i18n';
import { ITEM_FORM_DISPLAY_NAME_INPUT_ID } from '../../../config/selectors';
Expand All @@ -23,9 +16,6 @@ const DisplayNameForm = ({
setChanges,
}: DisplayNameFormProps): JSX.Element => {
const { t: translateBuilder } = useBuilderTranslation();
const theme = useTheme();
// when the screen is large, use only half of the width for the input.
const largeScreen = useMediaQuery(theme.breakpoints.up('sm'));

const handleDisplayNameInput = (event: ChangeEvent<{ value: string }>) => {
setChanges({ displayName: event.target.value });
Expand Down Expand Up @@ -71,8 +61,8 @@ const DisplayNameForm = ({
),
}}
// only take full width when on small screen size
fullWidth={!largeScreen}
sx={{ my: 1, width: largeScreen ? '50%' : undefined }}
fullWidth
sx={{ my: 1 }}
/>
);
};
Expand Down
47 changes: 47 additions & 0 deletions src/components/item/form/FolderForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Stack } from '@mui/material';

import { DiscriminatedItem } from '@graasp/sdk';

import { FOLDER_FORM_DESCRIPTION_ID } from '../../../config/selectors';
import DescriptionForm from './DescriptionForm';
import FolderThumbnail from './FolderThumbnail';
import NameForm from './NameForm';

export type FolderFormProps = {
item?: DiscriminatedItem;
setChanges: (
payload: Partial<DiscriminatedItem> & { thumbnail?: Blob },
) => void;
updatedProperties: Partial<DiscriminatedItem> & { thumbnail?: Blob };
};

const FolderForm = ({
item,
updatedProperties,
setChanges,
}: FolderFormProps): JSX.Element => (
<Stack direction="column" gap={2}>
<Stack
direction="row"
justifyContent="flex-start"
alignItems="flex-end"
gap={3}
>
<FolderThumbnail setChanges={setChanges} />
<NameForm
required
setChanges={setChanges}
item={item}
updatedProperties={updatedProperties}
/>
</Stack>
<DescriptionForm
id={FOLDER_FORM_DESCRIPTION_ID}
item={item}
updatedProperties={updatedProperties}
setChanges={setChanges}
/>
</Stack>
);

export default FolderForm;
141 changes: 141 additions & 0 deletions src/components/item/form/FolderThumbnail.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { FormEventHandler, useRef, useState } from 'react';

import { Dialog, Stack, styled, useTheme } from '@mui/material';

import { DiscriminatedItem } from '@graasp/sdk';

import { ImageUp as ImageUpIcon } from 'lucide-react';

import CropModal, {
MODAL_TITLE_ARIA_LABEL_ID,
} from '@/components/common/CropModal';

const THUMBNAIL_DIMENSION = 60;

const VisuallyHiddenInput = styled('input')({
clip: 'rect(0 0 0 0)',
clipPath: 'inset(50%)',
height: 1,
overflow: 'hidden',
position: 'absolute',
bottom: 0,
left: 0,
whiteSpace: 'nowrap',
width: 1,
});

export type FolderThumbnailProps = {
setChanges: (
payload: Partial<DiscriminatedItem> & { thumbnail?: Blob },
) => void;
};

const FolderThumbnail = ({ setChanges }: FolderThumbnailProps): JSX.Element => {
const inputRef = useRef<HTMLInputElement>(null);
const [showCropModal, setShowCropModal] = useState(false);
const [newAvatar, setNewAvatar] = useState<string>();
const [fileSource, setFileSource] = useState<string>();
const theme = useTheme();

const onSelectFile: FormEventHandler<HTMLInputElement> = (e) => {
const t = e.target as HTMLInputElement;
if (t.files && t.files?.length > 0) {
const reader = new FileReader();
reader.addEventListener('load', () =>
setFileSource(reader.result as string),
);
reader.readAsDataURL(t.files?.[0]);
setShowCropModal(true);
}
};

const onClose = () => {
setShowCropModal(false);
if (inputRef.current) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
inputRef.current.value = null;
}
};

const onConfirmCrop = (croppedImage: Blob | null) => {
onClose();

if (!croppedImage) {
return console.error('croppedImage is not defined');
}
// submit cropped image
try {
setChanges({ thumbnail: croppedImage });
// replace img src with croppedImage
const url = URL.createObjectURL(croppedImage);
setNewAvatar(url);
} catch (error) {
console.error(error);
}

return true;
};

const onEdit = () => {
inputRef.current?.click();
};

return (
<Stack justifyContent="flex-start" direction="column">
<Stack
onClick={onEdit}
onKeyDown={(event) => {
if (['Enter', ' '].includes(event.key)) {
onEdit();
}
}}
aria-label="change folder avatar"
role="button"
tabIndex={0}
height={THUMBNAIL_DIMENSION}
width={THUMBNAIL_DIMENSION}
borderRadius={2}
bgcolor="#E4DFFF"
alignItems="center"
justifyContent="center"
overflow="hidden"
position="relative"
sx={{ cursor: 'pointer' }}
>
{newAvatar ? (
<img
alt="folder thumbnail"
src={newAvatar}
height={THUMBNAIL_DIMENSION}
width={THUMBNAIL_DIMENSION}
/>
) : (
<ImageUpIcon color={theme.palette.primary.main} />
)}
</Stack>
<VisuallyHiddenInput
type="file"
accept="image/*"
// onInput={onSelectFile}
spaenleh marked this conversation as resolved.
Show resolved Hide resolved
onChange={onSelectFile}
ref={inputRef}
/>
{fileSource && (
<Dialog
open={showCropModal}
onClose={onClose}
aria-labelledby={MODAL_TITLE_ARIA_LABEL_ID}
>
<CropModal
onClose={onClose}
src={fileSource}
onConfirm={onConfirmCrop}
/>
</Dialog>
)}
</Stack>
);
};

export default FolderThumbnail;
32 changes: 6 additions & 26 deletions src/components/item/form/NameForm.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { ChangeEvent } from 'react';

import ClearIcon from '@mui/icons-material/Clear';
import InfoIcon from '@mui/icons-material/Info';
import {
IconButton,
TextField,
Tooltip,
useMediaQuery,
useTheme,
} from '@mui/material';
import { IconButton, TextField } from '@mui/material';

import { useBuilderTranslation } from '../../../config/i18n';
import { ITEM_FORM_NAME_INPUT_ID } from '../../../config/selectors';
Expand All @@ -28,9 +21,7 @@ const NameForm = ({
autoFocus = true,
}: NameFormProps): JSX.Element => {
const { t: translateBuilder } = useBuilderTranslation();
const theme = useTheme();
// when the screen is large, us only half of the width for the input.
const largeScreen = useMediaQuery(theme.breakpoints.up('sm'));

const handleNameInput = (event: ChangeEvent<{ value: string }>) => {
setChanges({ name: event.target.value, displayName: event.target.value });
};
Expand All @@ -44,19 +35,8 @@ const NameForm = ({
variant="standard"
autoFocus={autoFocus}
id={ITEM_FORM_NAME_INPUT_ID}
label={
<div style={{ display: 'flex', alignItems: 'center' }}>
{translateBuilder(BUILDER.CREATE_NEW_ITEM_NAME_LABEL)}
{required && <span>*</span>}
<Tooltip
title={translateBuilder(BUILDER.CREATE_NEW_ITEM_NAME_HELPER_TEXT)}
>
<IconButton size="small">
<InfoIcon fontSize="small" color="primary" />
</IconButton>
</Tooltip>
</div>
}
label={translateBuilder(BUILDER.CREATE_NEW_ITEM_NAME_LABEL)}
required={required}
value={updatedProperties?.name ?? item?.name ?? ''}
onChange={handleNameInput}
// always shrink because setting name from defined app does not shrink automatically
Expand All @@ -73,8 +53,8 @@ const NameForm = ({
),
}}
// only take full width when on small screen size
fullWidth={!largeScreen}
sx={{ my: 1, width: largeScreen ? '50%' : undefined }}
fullWidth
sx={{ my: 1 }}
/>
);
};
Expand Down
Loading
Loading