add actions, reordering base interactions

This commit is contained in:
41666 2021-07-06 09:59:04 -05:00
parent b0c8b2378b
commit 2d589b988f
26 changed files with 383 additions and 427 deletions

View file

@ -1,12 +0,0 @@
import { guildEnum } from '@roleypoly/design-system/fixtures/storyData';
import { EditorCategoriesTab } from './EditorCategoriesTab';
export default {
title: 'Organisms/Editor/Categories Tab',
component: EditorCategoriesTab,
args: {
guild: guildEnum.guilds[0],
},
};
export const categoriesTab = (args) => <EditorCategoriesTab {...args} />;

View file

@ -1,8 +0,0 @@
import { palette } from '@roleypoly/design-system/atoms/colors';
import styled from 'styled-components';
export const CategoryContainer = styled.div`
background-color: ${palette.taupe100};
padding: 10px;
margin: 15px 0;
`;

View file

@ -1,39 +0,0 @@
import { TabDepth } from '@roleypoly/design-system/atoms/tab-view/TabView.styled';
import { EditorCategory } from '@roleypoly/design-system/molecules/editor-category';
import { EditorCategoryShort } from '@roleypoly/design-system/molecules/editor-category-short/EditorCategoryShort';
import { EditorShellProps } from '@roleypoly/design-system/organisms/editor-shell';
import { Category } from '@roleypoly/types';
import { sortBy } from 'lodash';
import * as React from 'react';
import { CategoryContainer } from './EditorCategoriesTab.styled';
export const EditorCategoriesTab = (props: EditorShellProps) => {
const [openStates, setOpenStates] = React.useState<Category['id'][]>([]);
const onCategoryOpen = (id: Category['id']) => () => {
setOpenStates([...new Set(openStates).add(id)]);
};
return (
<TabDepth>
{sortBy(props.guild.data.categories, ['position', 'id']).map((category, idx) =>
openStates.includes(category.id) ? (
<CategoryContainer key={idx}>
<EditorCategory
category={category}
uncategorizedRoles={[]}
guildRoles={props.guild.roles}
onChange={(category) => props.onCategoryChange?.(category)}
/>
</CategoryContainer>
) : (
<EditorCategoryShort
key={idx}
category={category}
onOpen={onCategoryOpen(category.id)}
/>
)
)}
</TabDepth>
);
};

View file

@ -1 +0,0 @@
export * from './EditorCategoriesTab';

View file

@ -1,20 +0,0 @@
import { Space } from '@roleypoly/design-system/atoms/space';
import { TabDepth } from '@roleypoly/design-system/atoms/tab-view/TabView.styled';
import { MultilineTextInput } from '@roleypoly/design-system/atoms/text-input';
import { Text } from '@roleypoly/design-system/atoms/typography';
import { EditorShellProps } from '@roleypoly/design-system/organisms/editor-shell';
import * as React from 'react';
export const EditorDetailsTab = (props: EditorShellProps) => {
return (
<TabDepth>
<Space />
<Text>Server Message</Text>
<MultilineTextInput
value={props.guild.data.message}
onChange={(eventData) => props.onMessageChange?.(eventData.target.value)}
/>
<Space />
</TabDepth>
);
};

View file

@ -1 +0,0 @@
export * from './EditorDetailsTab';

View file

@ -1,12 +1,20 @@
import { BreakpointsProvider } from '@roleypoly/design-system/atoms/breakpoints';
import { guildEnum } from '@roleypoly/design-system/fixtures/storyData';
import * as React from 'react';
import ReactTooltip from 'react-tooltip';
import { EditorShell } from './EditorShell';
export default {
title: 'Organisms/Editor',
component: EditorShell,
decorators: [(story) => <BreakpointsProvider>{story()}</BreakpointsProvider>],
decorators: [
(story) => (
<BreakpointsProvider>
{story()}
<ReactTooltip />
</BreakpointsProvider>
),
],
};
export const Shell = () => <EditorShell guild={guildEnum.guilds[0]} />;

View file

@ -1,21 +1,12 @@
import { palette } from '@roleypoly/design-system/atoms/colors';
import { FaderOpacity } from '@roleypoly/design-system/atoms/fader';
import { Space } from '@roleypoly/design-system/atoms/space';
import { MultilineTextInput } from '@roleypoly/design-system/atoms/text-input';
import { AmbientLarge, LargeText } from '@roleypoly/design-system/atoms/typography';
import { PickerCategory } from '@roleypoly/design-system/molecules/picker-category';
import { EditableServerMessage } from '@roleypoly/design-system/molecules/editable-server-message';
import { ServerMasthead } from '@roleypoly/design-system/molecules/server-masthead';
import { SecondaryEditing } from '@roleypoly/design-system/organisms/masthead';
import {
CategoryContainer,
Container,
MessageBox,
} from '@roleypoly/design-system/organisms/role-picker/RolePicker.styled';
import { Category, CategoryType, PresentableGuild, Role } from '@roleypoly/types';
import { Container } from '@roleypoly/design-system/organisms/role-picker/RolePicker.styled';
import { ServerCategoryEditor } from '@roleypoly/design-system/organisms/server-category-editor';
import { Category, PresentableGuild } from '@roleypoly/types';
import deepEqual from 'deep-equal';
import { sortBy } from 'lodash';
import React from 'react';
import { GoEyeClosed } from 'react-icons/go';
export type EditorShellProps = {
guild: PresentableGuild;
@ -26,6 +17,7 @@ export type EditorShellProps = {
export const EditorShell = (props: EditorShellProps) => {
const [guild, setGuild] = React.useState<PresentableGuild>(props.guild);
const [reorderMode, setReorderMode] = React.useState<boolean>(false);
React.useEffect(() => {
setGuild(props.guild);
@ -35,12 +27,8 @@ export const EditorShell = (props: EditorShellProps) => {
setGuild(props.guild);
};
const onCategoryChange = (category: Category) => {
const replaceCategories = (categories: Category[]) => {
setGuild((currentGuild) => {
const categories = [
...currentGuild.data.categories.filter((x) => x.id !== category.id),
category,
];
return { ...currentGuild, data: { ...currentGuild.data, categories } };
});
};
@ -51,6 +39,10 @@ export const EditorShell = (props: EditorShellProps) => {
});
};
const doSubmit = () => {
props.onGuildChange?.(guild);
};
const hasChanges = React.useMemo(
() => !deepEqual(guild.data, props.guild.data),
[guild.data, props.guild.data]
@ -62,52 +54,18 @@ export const EditorShell = (props: EditorShellProps) => {
showReset={hasChanges}
guild={props.guild.guild}
onReset={reset}
onSubmit={() => props.onGuildChange?.(guild)}
onSubmit={doSubmit}
/>
<Container style={{ marginTop: 95 }}>
<ServerMasthead guild={props.guild.guild} editable={false} />
<Space />
<MessageBox>
<LargeText>Server Message</LargeText>
<MultilineTextInput
rows={2}
value={guild.data.message}
onChange={(event) => onMessageChange(event.target.value)}
placeholder={`Hey friend from ${guild.guild.name}! Pick your roles!`}
>
{guild.data.message}
</MultilineTextInput>
<AmbientLarge style={{ display: 'flex', color: palette.taupe600 }}>
Shows a message to your server members.
<FaderOpacity isVisible={guild.data.message.trim().length === 0}>
&nbsp;Since the message is empty, this won't show up.&nbsp;&nbsp;&nbsp;
<GoEyeClosed style={{ position: 'relative', top: 2 }} />
</FaderOpacity>
</AmbientLarge>
</MessageBox>
<EditableServerMessage
onChange={onMessageChange}
value={guild.data.message}
guild={guild.guild}
/>
<Space />
<div>
{sortBy(props.guild.data.categories, 'position').map((category, idx) => (
<CategoryContainer key={idx}>
<PickerCategory
key={idx}
category={category}
title={category.name}
selectedRoles={[]}
roles={
category.roles
.map((role) => props.guild.roles.find((r) => r.id === role))
.filter((r) => r !== undefined) as Role[]
}
onChange={() => () => {}}
wikiMode={false}
type={category.type === CategoryType.Single ? 'single' : 'multi'}
/>
</CategoryContainer>
))}
</div>
<ServerCategoryEditor guild={guild} onChange={replaceCategories} />
</Container>
</>
);

View file

@ -0,0 +1,42 @@
import { transitions } from '@roleypoly/design-system/atoms/timings';
import { CategoryContainer } from '@roleypoly/design-system/organisms/role-picker/RolePicker.styled';
import styled, { css } from 'styled-components';
export const CategoryActions = styled.div<{ right?: boolean }>`
display: flex;
flex-direction: row;
${(props) =>
props.right &&
css`
justify-content: flex-end;
`}
& > * {
${(props) => (props.right ? 'margin-left' : 'margin-right')}: 5px;
}
`;
export const ReorderButton = styled.div`
height: 40px;
width: 40px;
font-size: 20px;
margin-right: 5px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
cursor: pointer;
transition: background-color ${transitions.actionable}s ease-in-out;
&:hover {
background-color: rgba(0, 0, 0, 0.15);
}
`;
export const ReorderCategoryContainer = styled(CategoryContainer)`
display: flex;
align-items: center;
justify-content: flex-start;
user-select: none;
`;

View file

@ -0,0 +1,158 @@
import { BreakpointText } from '@roleypoly/design-system/atoms/breakpoints';
import { Button } from '@roleypoly/design-system/atoms/button';
import { FaderOpacity } from '@roleypoly/design-system/atoms/fader';
import { LargeText } from '@roleypoly/design-system/atoms/typography';
import { EditorCategory } from '@roleypoly/design-system/molecules/editor-category';
import { CategoryContainer } from '@roleypoly/design-system/organisms/role-picker/RolePicker.styled';
import { Category, CategoryType, PresentableGuild, Role } from '@roleypoly/types';
import KSUID from 'ksuid';
import { sortBy } from 'lodash';
import React from 'react';
import { CgReorder } from 'react-icons/cg';
import { GoArrowDown, GoArrowUp, GoCheck, GoGrabber, GoPlus } from 'react-icons/go';
import {
CategoryActions,
ReorderButton,
ReorderCategoryContainer,
} from './ServerCategoryEditor.styled';
type Props = {
guild: PresentableGuild;
onChange: (categories: PresentableGuild['data']['categories']) => void;
};
const resetOrder = (categories: Category[]) =>
sortBy(categories, ['position', 'id']).map((c, index) => ({ ...c, position: index }));
const forceOrder = (categories: Category[]) =>
categories.map((c, index) => ({ ...c, position: index }));
export const ServerCategoryEditor = (props: Props) => {
const [reorderMode, setReorderMode] = React.useState(false);
const updateSingleCategory = (category: Category) => {
const newCategories = props.guild.data.categories.map((c) => {
if (c.id === category.id) {
return category;
}
return c;
});
props.onChange(newCategories);
};
const createCategory = () => {
// Reset order now that we're creating a new category
const categories = resetOrder(props.guild.data.categories);
const newCategory: Category = {
id: KSUID.randomSync().toString(),
name: 'New Category',
type: CategoryType.Multi,
position: categories.length,
roles: [],
hidden: false,
};
props.onChange([...categories, newCategory]);
};
const onReorder = (categories: Category[]) => {
setReorderMode(false);
props.onChange(resetOrder(categories));
};
if (reorderMode) {
return <ReorderMode {...props} exitReorderMode={onReorder} />;
}
return (
<div>
<CategoryActions>
<Button color="muted" size="small" onClick={() => createCategory()}>
Create New <GoPlus />
</Button>
<Button color="muted" size="small" onClick={() => setReorderMode(true)}>
Change Order <CgReorder />
</Button>
</CategoryActions>
{sortBy(props.guild.data.categories, ['position', 'id']).map((category, idx) => (
<CategoryContainer key={idx}>
<EditorCategory
key={idx}
category={category}
title={category.name}
selectedRoles={[]}
roles={
category.roles
.map((role) => props.guild.roles.find((r) => r.id === role))
.filter((r) => r !== undefined) as Role[]
}
onChange={updateSingleCategory}
type={category.type === CategoryType.Single ? 'single' : 'multi'}
/>
</CategoryContainer>
))}
</div>
);
};
const ReorderMode = (props: Props & { exitReorderMode: (final: Category[]) => void }) => {
const [categories, setCategories] = React.useState(props.guild.data.categories);
React.useEffect(() => {
setCategories(props.guild.data.categories);
}, [props.guild.data.categories]);
const handleReorder = (category: Category, direction: 'up' | 'down') => () => {
const newCategories = [...categories];
const index = newCategories.findIndex((c) => c.id === category.id);
const newIndex = direction === 'up' ? index - 1 : index + 1;
if (newIndex < 0 || newIndex > newCategories.length - 1) {
return;
}
newCategories.splice(index, 1);
newCategories.splice(newIndex, 0, category);
setCategories(forceOrder(newCategories));
};
return (
<div>
<CategoryActions right>
<Button
color="muted"
size="small"
onClick={() => props.exitReorderMode(props.guild.data.categories)}
>
Reset
</Button>
<Button
color="primary"
size="small"
onClick={() => props.exitReorderMode(categories)}
>
<BreakpointText small="Save" large="Save Order" /> <GoCheck />
</Button>
</CategoryActions>
{sortBy(categories, ['position', 'id']).map((category, idx, array) => (
<ReorderCategoryContainer key={idx}>
<ReorderButton data-tip="Drag to reorder" style={{ cursor: 'grab' }}>
<GoGrabber />
</ReorderButton>
<FaderOpacity isVisible={idx !== 0}>
<ReorderButton onClick={handleReorder(category, 'up')} data-tip="Move up">
<GoArrowUp />
</ReorderButton>
</FaderOpacity>
<FaderOpacity isVisible={categories.length - 1 !== idx}>
<ReorderButton onClick={handleReorder(category, 'down')} data-tip="Move down">
<GoArrowDown />
</ReorderButton>
</FaderOpacity>
<LargeText>{category.name}</LargeText>
</ReorderCategoryContainer>
))}
</div>
);
};

View file

@ -0,0 +1 @@
export * from './ServerCategoryEditor';