mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-06-16 09:39:09 +00:00
Feat/editor as preview (#294)
* try editor as preview * add databinding for editor actions and message * add actions, reordering base interactions * add drag and drop ordering for categoriers * category skeleton * fix linting issues * add role list and add button, non-functional * bump packages * add role search prototype * yarn.lock sync * fix lint * remove cfw-emulator bin
This commit is contained in:
parent
7d681d69d6
commit
ab3f718e6d
43 changed files with 1157 additions and 741 deletions
|
@ -0,0 +1,49 @@
|
|||
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;
|
||||
|
||||
& > button {
|
||||
${(props) =>
|
||||
props.right
|
||||
? css`
|
||||
margin-left: 5px;
|
||||
`
|
||||
: css`
|
||||
margin-right: 5px;
|
||||
`};
|
||||
}
|
||||
|
||||
${(props) =>
|
||||
props.right &&
|
||||
css`
|
||||
justify-content: flex-end;
|
||||
`}
|
||||
`;
|
||||
|
||||
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;
|
||||
`;
|
|
@ -0,0 +1,203 @@
|
|||
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 { flatten, sortBy } from 'lodash';
|
||||
import React from 'react';
|
||||
import { DragDropContext, Draggable, Droppable, DropResult } from 'react-beautiful-dnd';
|
||||
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 unselectedRoles = React.useMemo(() => {
|
||||
const selectedRoles = flatten(props.guild.data.categories.map((c) => c.roles));
|
||||
return props.guild.roles.filter((r) => !selectedRoles.includes(r.id));
|
||||
}, [props.guild.data.categories, props.guild.roles]);
|
||||
|
||||
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().string,
|
||||
name: 'New Category',
|
||||
type: CategoryType.Multi,
|
||||
position: categories.length,
|
||||
roles: [],
|
||||
hidden: false,
|
||||
};
|
||||
|
||||
props.onChange([...categories, newCategory]);
|
||||
};
|
||||
|
||||
const onReorder = (categories: Category[] | null) => {
|
||||
setReorderMode(false);
|
||||
if (categories === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
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
|
||||
category={category}
|
||||
title={category.name}
|
||||
unselectedRoles={unselectedRoles}
|
||||
roles={
|
||||
category.roles
|
||||
.map((role) => props.guild.roles.find((r) => r.id === role))
|
||||
.filter((r) => r !== undefined) as Role[]
|
||||
}
|
||||
onChange={updateSingleCategory}
|
||||
/>
|
||||
</CategoryContainer>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const ReorderMode = (
|
||||
props: Props & { exitReorderMode: (final: Category[] | null) => 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));
|
||||
};
|
||||
|
||||
const handleDrop = (dropEvent: DropResult) => {
|
||||
const newCategories = [...categories];
|
||||
const { source, destination } = dropEvent;
|
||||
|
||||
if (!destination || source.index === destination.index) {
|
||||
return;
|
||||
}
|
||||
|
||||
newCategories.splice(source.index, 1);
|
||||
newCategories.splice(destination.index, 0, categories[source.index]);
|
||||
setCategories(forceOrder(newCategories));
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<CategoryActions right>
|
||||
<Button color="muted" size="small" onClick={() => props.exitReorderMode(null)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
color="primary"
|
||||
size="small"
|
||||
onClick={() => props.exitReorderMode(categories)}
|
||||
>
|
||||
<BreakpointText small="Save" large="Save Order" /> <GoCheck />
|
||||
</Button>
|
||||
</CategoryActions>
|
||||
<DragDropContext onDragEnd={handleDrop}>
|
||||
<Droppable droppableId="categories">
|
||||
{(provided, snapshot) => (
|
||||
<div ref={provided.innerRef} {...provided.droppableProps}>
|
||||
{sortBy(categories, ['position', 'id']).map((category, idx) => (
|
||||
<Draggable key={category.id} index={idx} draggableId={category.id}>
|
||||
{(provided, snapshot) => (
|
||||
<ReorderCategoryContainer
|
||||
ref={provided.innerRef}
|
||||
{...provided.draggableProps}
|
||||
>
|
||||
<ReorderButton
|
||||
data-tip="Drag to reorder"
|
||||
style={{ cursor: 'grab' }}
|
||||
{...provided.dragHandleProps}
|
||||
>
|
||||
<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>
|
||||
)}
|
||||
</Draggable>
|
||||
))}
|
||||
{provided.placeholder}
|
||||
</div>
|
||||
)}
|
||||
</Droppable>
|
||||
</DragDropContext>
|
||||
</div>
|
||||
);
|
||||
};
|
|
@ -0,0 +1 @@
|
|||
export * from './ServerCategoryEditor';
|
Loading…
Add table
Add a link
Reference in a new issue