2023-03-29 12:51:29 -07:00
|
|
|
import clsx from 'clsx';
|
2023-03-29 17:54:35 -07:00
|
|
|
import React, { useEffect, useMemo, useState } from 'react';
|
2023-03-29 12:51:29 -07:00
|
|
|
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
|
|
|
|
|
|
|
|
import Icon from 'soapbox/components/icon';
|
2023-03-29 17:54:35 -07:00
|
|
|
import { Avatar, Button, Column, Form, FormActions, FormGroup, HStack, Input, Spinner, Text, Textarea } from 'soapbox/components/ui';
|
2023-03-29 17:18:20 -07:00
|
|
|
import { useAppSelector, useInstance } from 'soapbox/hooks';
|
|
|
|
import { useGroup, useUpdateGroup } from 'soapbox/hooks/api';
|
2023-03-29 12:51:29 -07:00
|
|
|
import { isDefaultAvatar, isDefaultHeader } from 'soapbox/utils/accounts';
|
|
|
|
import resizeImage from 'soapbox/utils/resize-image';
|
|
|
|
|
|
|
|
import type { List as ImmutableList } from 'immutable';
|
|
|
|
|
2023-03-29 17:18:20 -07:00
|
|
|
const nonDefaultAvatar = (url: string | undefined) => url && isDefaultAvatar(url) ? undefined : url;
|
|
|
|
const nonDefaultHeader = (url: string | undefined) => url && isDefaultHeader(url) ? undefined : url;
|
|
|
|
|
2023-03-29 12:51:29 -07:00
|
|
|
interface IMediaInput {
|
2023-03-29 17:18:20 -07:00
|
|
|
src: string | undefined
|
2023-03-29 12:51:29 -07:00
|
|
|
accept: string
|
|
|
|
onChange: React.ChangeEventHandler<HTMLInputElement>
|
|
|
|
disabled: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
heading: { id: 'navigation_bar.edit_group', defaultMessage: 'Edit Group' },
|
|
|
|
groupNamePlaceholder: { id: 'manage_group.fields.name_placeholder', defaultMessage: 'Group Name' },
|
|
|
|
groupDescriptionPlaceholder: { id: 'manage_group.fields.description_placeholder', defaultMessage: 'Description' },
|
|
|
|
});
|
|
|
|
|
2023-03-29 17:18:20 -07:00
|
|
|
const HeaderPicker = React.forwardRef<HTMLInputElement, IMediaInput>(({ src, onChange, accept, disabled }, ref) => {
|
2023-03-29 12:51:29 -07:00
|
|
|
return (
|
|
|
|
<label
|
|
|
|
className='dark:sm:shadow-inset relative h-24 w-full cursor-pointer overflow-hidden rounded-lg bg-primary-100 text-primary-500 dark:bg-gray-800 dark:text-accent-blue sm:h-36 sm:shadow'
|
|
|
|
>
|
|
|
|
{src && <img className='h-full w-full object-cover' src={src} alt='' />}
|
|
|
|
<HStack
|
|
|
|
className={clsx('absolute top-0 h-full w-full transition-opacity', {
|
|
|
|
'opacity-0 hover:opacity-90 bg-primary-100 dark:bg-gray-800': src,
|
|
|
|
})}
|
|
|
|
space={1.5}
|
|
|
|
alignItems='center'
|
|
|
|
justifyContent='center'
|
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
src={require('@tabler/icons/photo-plus.svg')}
|
|
|
|
className='h-4.5 w-4.5'
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Text size='md' theme='primary' weight='semibold'>
|
|
|
|
<FormattedMessage id='group.upload_banner' defaultMessage='Upload photo' />
|
|
|
|
</Text>
|
|
|
|
|
|
|
|
<input
|
2023-03-29 17:18:20 -07:00
|
|
|
ref={ref}
|
2023-03-29 12:51:29 -07:00
|
|
|
name='header'
|
|
|
|
type='file'
|
|
|
|
accept={accept}
|
|
|
|
onChange={onChange}
|
|
|
|
disabled={disabled}
|
|
|
|
className='hidden'
|
|
|
|
/>
|
|
|
|
</HStack>
|
|
|
|
</label>
|
|
|
|
);
|
2023-03-29 17:18:20 -07:00
|
|
|
});
|
2023-03-29 12:51:29 -07:00
|
|
|
|
2023-03-29 17:18:20 -07:00
|
|
|
const AvatarPicker = React.forwardRef<HTMLInputElement, IMediaInput>(({ src, onChange, accept, disabled }, ref) => {
|
2023-03-29 12:51:29 -07:00
|
|
|
return (
|
|
|
|
<label className='absolute left-1/2 bottom-0 h-20 w-20 -translate-x-1/2 translate-y-1/2 cursor-pointer rounded-full bg-primary-500 ring-2 ring-white dark:ring-primary-900'>
|
|
|
|
{src && <Avatar src={src} size={80} />}
|
|
|
|
<HStack
|
|
|
|
alignItems='center'
|
|
|
|
justifyContent='center'
|
|
|
|
|
|
|
|
className={clsx('absolute left-0 top-0 h-full w-full rounded-full transition-opacity', {
|
|
|
|
'opacity-0 hover:opacity-90 bg-primary-500': src,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
src={require('@tabler/icons/camera-plus.svg')}
|
|
|
|
className='h-5 w-5 text-white'
|
|
|
|
/>
|
|
|
|
</HStack>
|
|
|
|
<span className='sr-only'>Upload avatar</span>
|
|
|
|
<input
|
2023-03-29 17:18:20 -07:00
|
|
|
ref={ref}
|
2023-03-29 12:51:29 -07:00
|
|
|
name='avatar'
|
|
|
|
type='file'
|
|
|
|
accept={accept}
|
|
|
|
onChange={onChange}
|
|
|
|
disabled={disabled}
|
|
|
|
className='hidden'
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
);
|
2023-03-29 17:18:20 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
interface IEditGroup {
|
|
|
|
params: {
|
|
|
|
id: string
|
|
|
|
}
|
|
|
|
}
|
2023-03-29 12:51:29 -07:00
|
|
|
|
2023-03-29 17:18:20 -07:00
|
|
|
const EditGroup: React.FC<IEditGroup> = ({ params: { id: groupId } }) => {
|
2023-03-29 12:51:29 -07:00
|
|
|
const intl = useIntl();
|
|
|
|
const instance = useInstance();
|
|
|
|
|
2023-03-29 17:54:35 -07:00
|
|
|
const { group, isLoading } = useGroup(groupId);
|
2023-03-29 17:18:20 -07:00
|
|
|
const { updateGroup } = useUpdateGroup(groupId);
|
2023-03-29 12:51:29 -07:00
|
|
|
|
2023-03-29 17:54:35 -07:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
|
|
|
|
const avatarField = useImageField({ maxPixels: 400 * 400, preview: nonDefaultAvatar(group?.avatar) });
|
|
|
|
const headerField = useImageField({ maxPixels: 1920 * 1080, preview: nonDefaultHeader(group?.header) });
|
2023-03-29 17:18:20 -07:00
|
|
|
|
2023-03-29 17:54:35 -07:00
|
|
|
const displayNameField = useTextField(group?.display_name);
|
|
|
|
const noteField = useTextField(group?.note);
|
2023-03-29 17:18:20 -07:00
|
|
|
|
2023-03-29 17:54:35 -07:00
|
|
|
const maxName = Number(instance.configuration.getIn(['groups', 'max_characters_name']));
|
|
|
|
const maxNote = Number(instance.configuration.getIn(['groups', 'max_characters_description']));
|
2023-03-29 12:51:29 -07:00
|
|
|
|
|
|
|
const attachmentTypes = useAppSelector(
|
|
|
|
state => state.instance.configuration.getIn(['media_attachments', 'supported_mime_types']) as ImmutableList<string>,
|
|
|
|
)?.filter(type => type.startsWith('image/')).toArray().join(',');
|
|
|
|
|
2023-03-29 17:54:35 -07:00
|
|
|
async function handleSubmit() {
|
|
|
|
setIsSubmitting(true);
|
2023-03-29 12:51:29 -07:00
|
|
|
|
2023-03-29 17:18:20 -07:00
|
|
|
await updateGroup({
|
2023-03-29 17:54:35 -07:00
|
|
|
display_name: displayNameField.value,
|
|
|
|
note: noteField.value,
|
|
|
|
avatar: avatarField.file,
|
|
|
|
header: headerField.file,
|
2023-03-29 12:51:29 -07:00
|
|
|
});
|
2023-03-29 17:54:35 -07:00
|
|
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLoading) {
|
|
|
|
return <Spinner />;
|
2023-03-29 17:18:20 -07:00
|
|
|
}
|
2023-03-29 12:51:29 -07:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Column label={intl.formatMessage(messages.heading)}>
|
2023-03-29 17:54:35 -07:00
|
|
|
<Form onSubmit={handleSubmit}>
|
2023-03-29 12:51:29 -07:00
|
|
|
<div className='relative mb-12 flex'>
|
2023-03-29 17:54:35 -07:00
|
|
|
<HeaderPicker accept={attachmentTypes} disabled={isSubmitting} {...headerField} />
|
|
|
|
<AvatarPicker accept={attachmentTypes} disabled={isSubmitting} {...avatarField} />
|
2023-03-29 12:51:29 -07:00
|
|
|
</div>
|
|
|
|
<FormGroup
|
|
|
|
labelText={<FormattedMessage id='manage_group.fields.name_label' defaultMessage='Group name (required)' />}
|
|
|
|
>
|
|
|
|
<Input
|
|
|
|
type='text'
|
|
|
|
placeholder={intl.formatMessage(messages.groupNamePlaceholder)}
|
2023-03-29 17:54:35 -07:00
|
|
|
{...displayNameField}
|
|
|
|
maxLength={maxName}
|
2023-03-29 12:51:29 -07:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
labelText={<FormattedMessage id='manage_group.fields.description_label' defaultMessage='Description' />}
|
|
|
|
>
|
|
|
|
<Textarea
|
|
|
|
autoComplete='off'
|
|
|
|
placeholder={intl.formatMessage(messages.groupDescriptionPlaceholder)}
|
2023-03-29 17:54:35 -07:00
|
|
|
{...noteField}
|
|
|
|
maxLength={maxNote}
|
2023-03-29 12:51:29 -07:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
2023-03-29 17:18:20 -07:00
|
|
|
|
|
|
|
<FormActions>
|
|
|
|
<Button theme='primary' type='submit' disabled={isSubmitting} block>
|
|
|
|
<FormattedMessage id='edit_profile.save' defaultMessage='Save' />
|
|
|
|
</Button>
|
|
|
|
</FormActions>
|
2023-03-29 12:51:29 -07:00
|
|
|
</Form>
|
|
|
|
</Column>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-03-29 17:18:20 -07:00
|
|
|
function usePreview(file: File | null | undefined): string | undefined {
|
|
|
|
return useMemo(() => {
|
|
|
|
if (file) {
|
|
|
|
return URL.createObjectURL(file);
|
|
|
|
}
|
|
|
|
}, [file]);
|
|
|
|
}
|
|
|
|
|
2023-03-29 17:54:35 -07:00
|
|
|
function useTextField(initialValue: string | undefined) {
|
|
|
|
const [value, setValue] = useState(initialValue);
|
|
|
|
const hasInitialValue = typeof initialValue === 'string';
|
|
|
|
|
|
|
|
const onChange: React.ChangeEventHandler<HTMLInputElement | HTMLTextAreaElement> = (e) => {
|
|
|
|
setValue(e.target.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (hasInitialValue) {
|
|
|
|
setValue(initialValue);
|
|
|
|
}
|
|
|
|
}, [hasInitialValue]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
interface UseImageFieldOpts {
|
|
|
|
maxPixels?: number
|
|
|
|
preview?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
function useImageField(opts: UseImageFieldOpts = {}) {
|
|
|
|
const [file, setFile] = useState<File>();
|
|
|
|
const src = usePreview(file) || opts.preview;
|
|
|
|
|
|
|
|
const onChange: React.ChangeEventHandler<HTMLInputElement> = ({ target: { files } }) => {
|
|
|
|
const file = files?.item(0) || undefined;
|
|
|
|
if (file) {
|
|
|
|
if (typeof opts.maxPixels === 'number') {
|
|
|
|
resizeImage(file, opts.maxPixels)
|
|
|
|
.then((f) => setFile(f))
|
|
|
|
.catch(console.error);
|
|
|
|
} else {
|
|
|
|
setFile(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
src,
|
|
|
|
file,
|
|
|
|
onChange,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-29 12:51:29 -07:00
|
|
|
export default EditGroup;
|