Skip to content

Commit

Permalink
feat(slice-machine-ui): update Slice Builder layout
Browse files Browse the repository at this point in the history
  • Loading branch information
bapmrl committed Oct 20, 2023
1 parent cf44e79 commit 9f1e26c
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 158 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Box } from "theme-ui";

import { ensureDnDDestination } from "@lib/utils";
import { transformKeyAccessor } from "@utils/str";

Expand All @@ -10,6 +8,7 @@ import * as Widgets from "@lib/models/common/widgets";
import sliceBuilderWidgetsArray from "@lib/models/common/widgets/sliceBuilderArray";

import { DropResult } from "react-beautiful-dnd";
import { List } from "@src/components/List";
import useSliceMachineActions from "@src/modules/useSliceMachineActions";
import { VariationSM, WidgetsArea } from "@lib/models/common/Slice";

Expand Down Expand Up @@ -118,7 +117,7 @@ const FieldZones: React.FunctionComponent<FieldZonesProps> = ({
};

return (
<>
<List>
<Zone
zoneType="slice"
tabId={undefined}
Expand All @@ -145,7 +144,6 @@ const FieldZones: React.FunctionComponent<FieldZonesProps> = ({
dataCy="slice-non-repeatable-zone"
isRepeatableCustomType={undefined}
/>
<Box mt={4} />
<Zone
zoneType="slice"
tabId={undefined}
Expand All @@ -171,7 +169,7 @@ const FieldZones: React.FunctionComponent<FieldZonesProps> = ({
dataCy="slice-repeatable-zone"
isRepeatableCustomType={undefined}
/>
</>
</List>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import VariationPopover from "./VariationsPopover";
import useSliceMachineActions from "@src/modules/useSliceMachineActions";
import { VariationSM } from "@lib/models/common/Slice";
import { ComponentUI } from "@lib/models/common/ComponentUI";
import { ModelStatus } from "@lib/models/common/ModelStatus";

const Header: React.FC<{
component: ComponentUI;
status: ModelStatus;
variation: VariationSM;
imageLoading?: boolean;
}> = ({ component, variation }) => {
const router = useRouter();
const [showVariationModal, setShowVariationModal] = useState(false);
Expand Down
45 changes: 6 additions & 39 deletions packages/slice-machine/lib/builders/SliceBuilder/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,28 @@ import {
AppLayoutHeader,
} from "@components/AppLayout";
import SimulatorButton from "@lib/builders/SliceBuilder/Header/SimulatorButton";
import { SliceSM, VariationSM } from "@lib/models/common/Slice";
import { VariationSM } from "@lib/models/common/Slice";
import { ComponentUI } from "@lib/models/common/ComponentUI";

import { FloatingBackButton } from "@src/features/slices/sliceBuilder/FloatingBackButton";
import { selectIsSimulatorAvailableForFramework } from "@src/modules/environment";
import { isSelectedSliceTouched } from "@src/modules/selectedSlice/selectors";
import { getRemoteSlice } from "@src/modules/slices";
import { useModelStatus } from "@src/hooks/useModelStatus";
import { ComponentWithSliceProps } from "@src/layouts/WithSlice";
import {
LocalAndRemoteSlice,
LocalOnlySlice,
} from "@lib/models/common/ModelData";

export type SliceBuilderState = {
imageLoading: boolean;
loading: boolean;
done: boolean;
error: null | string;
status: number | null;
};

export const initialState: SliceBuilderState = {
imageLoading: false,
loading: false,
done: false,
error: null,
status: null,
};

const SliceBuilder: ComponentWithSliceProps = ({ slice, variation }) => {
const { openToaster, updateSlice } = useSliceMachineActions();
const { isTouched, remoteSlice } = useSelector(
(store: SliceMachineStoreType) => ({
isTouched: isSelectedSliceTouched(store, slice.from, slice.model.id),
remoteSlice: getRemoteSlice(store, slice.model.id),
})
const isTouched = useSelector((store: SliceMachineStoreType) =>
isSelectedSliceTouched(store, slice.from, slice.model.id)
);

// We need to move this state to somewhere global to update the UI if any action from anywhere save or update to the filesystem I'd guess
Expand Down Expand Up @@ -88,7 +73,6 @@ const SliceBuilder: ComponentWithSliceProps = ({ slice, variation }) => {
updateSlice={updateSlice.bind(null, slice, setData)}
slice={slice}
variation={variation}
remoteSlice={remoteSlice}
isTouched={isTouched}
data={data}
/>
Expand All @@ -99,32 +83,20 @@ type SliceBuilderForVariationProps = {
updateSlice: () => void;
slice: ComponentUI;
variation: VariationSM;
remoteSlice: SliceSM | undefined;
isTouched: boolean;
data: SliceBuilderState;
};
const SliceBuilderForVariation: React.FC<SliceBuilderForVariationProps> = ({
updateSlice,
slice,
variation,
remoteSlice,
isTouched,
data,
}) => {
const { isSimulatorAvailableForFramework } = useSelector(
(state: SliceMachineStoreType) => ({
isSimulatorAvailableForFramework:
selectIsSimulatorAvailableForFramework(state),
})
const isSimulatorAvailableForFramework = useSelector(
selectIsSimulatorAvailableForFramework
);

const sliceModel: LocalAndRemoteSlice | LocalOnlySlice = {
local: slice.model,
localScreenshots: slice.screenshots,
...(remoteSlice ? { remote: remoteSlice } : {}),
};
const { modelsStatuses } = useModelStatus({ slices: [sliceModel] });

return (
<AppLayout>
<AppLayoutHeader>
Expand All @@ -147,12 +119,7 @@ const SliceBuilderForVariation: React.FC<SliceBuilderForVariationProps> = ({
</AppLayoutHeader>
<AppLayoutContent>
<BaseStyles>
<Header
component={slice}
status={modelsStatuses.slices[slice.model.id]}
variation={variation}
imageLoading={data.imageLoading}
/>
<Header component={slice} variation={variation} />
<Grid columns="1fr 320px" gap="16px" sx={{ pt: 4 }}>
<Box>
<FieldZones variation={variation} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const FieldZone = ({
isRepeatable,
dataCy,
isRepeatableCustomType,
sx,
}) => {
return (
<DragDropContext
Expand All @@ -41,8 +40,7 @@ const FieldZone = ({
{...provided.droppableProps}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
data-cy={dataCy}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
sx={sx}
sx={{ paddingInline: "16px !important" }}
>
{
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
Expand Down

This file was deleted.

100 changes: 32 additions & 68 deletions packages/slice-machine/lib/builders/common/Zone/index.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Button, ButtonGroup, Switch, Text } from "@prismicio/editor-ui";
import { Button, Switch, Text } from "@prismicio/editor-ui";
import { array, arrayOf, bool, func, object, shape, string } from "prop-types";
import { useState } from "react";
import { BaseStyles, Heading } from "theme-ui";
import { BaseStyles } from "theme-ui";

import { ListHeader } from "@src/components/List";

import SelectFieldTypeModal from "../SelectFieldTypeModal";
import NewField from "./Card/components/NewField";
import Card from "./Card";
import EmptyState from "./components/EmptyState";
import ZoneHeader from "./components/ZoneHeader";

const Zone = ({
zoneType /* type of the zone: customType or slice */,
Expand All @@ -30,8 +29,6 @@ const Zone = ({
dataCy,
isRepeatableCustomType,
}) => {
const isCustomType = zoneType === "customType";

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const widgetsArrayWithCondUid = (() => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-argument
Expand Down Expand Up @@ -74,68 +71,36 @@ const Zone = ({

return (
<>
{isCustomType ? (
<ListHeader
actions={
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
fields.length > 0 ? (
<>
<Text color="grey11" component="span">
Show code snippets?
</Text>
<Switch
checked={showHints}
onCheckedChange={setShowHints}
size="small"
/>
<Button
data-cy={`add-${
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
isRepeatable ? "Repeatable" : "Static"
}-field`}
onClick={enterSelectMode}
startIcon="add"
variant="secondary"
>
Add a new field
</Button>
</>
) : undefined
}
>
{title}
</ListHeader>
) : (
<BaseStyles>
<ZoneHeader
Heading={<Heading as="h6">{title}</Heading>}
Actions={
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
fields.length > 0 ? (
<ButtonGroup size="medium" variant="secondary">
<Button
variant="secondary"
onClick={() => setShowHints(!showHints)}
>
{showHints ? "Hide" : "Show"} code snippets
</Button>
<Button
variant="secondary"
onClick={() => enterSelectMode()}
data-cy={`add-${
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
isRepeatable ? "Repeatable" : "Static"
}-field`}
startIcon="add"
>
Add a new field
</Button>
</ButtonGroup>
) : null
}
/>
</BaseStyles>
)}
<ListHeader
actions={
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
fields.length > 0 ? (
<>
<Text color="grey11" component="span">
Show code snippets?
</Text>
<Switch
checked={showHints}
onCheckedChange={setShowHints}
size="small"
/>
<Button
data-cy={`add-${
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
isRepeatable ? "Repeatable" : "Static"
}-field`}
onClick={enterSelectMode}
startIcon="add"
variant="secondary"
>
Add a new field
</Button>
</>
) : undefined
}
>
{title}
</ListHeader>
{
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/strict-boolean-expressions
fields.length === 0 && !newFieldData ? (
Expand Down Expand Up @@ -191,7 +156,6 @@ const Zone = ({
/>
)
}
sx={isCustomType ? { paddingInline: "16px !important" } : {}}
/>
</BaseStyles>
) : undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const meta = {
disabled: { if: { arg: "interactive" } },
href: { if: { arg: "interactive" } },
onClick: { if: { arg: "interactive" } },
replace: { if: { arg: "interactive" } },
size: { control: "select", options: ["small", "medium"] },
variant: { control: "select", options: ["solid", "outlined"] },
},
Expand Down
9 changes: 7 additions & 2 deletions packages/slice-machine/src/components/Card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ type CardProps = PropsWithChildren<
variant?: keyof typeof styles.variant;
} & (
| { interactive?: false }
| ({ interactive: true; href: string } & {
component?: "a" | FC<LinkProps>;
| ({ interactive: true; href: string; component?: "a" } & {
replace?: undefined;
})
| ({ interactive: true; href: string; component: FC<LinkProps> } & {
replace?: boolean;
})
| ({ interactive: true; href?: undefined } & {
disabled?: boolean;
Expand Down Expand Up @@ -80,11 +83,13 @@ export const Card: FC<CardProps> = (props) => {
variant: _variant,
interactive: _interactive,
component = "a",
replace,
...otherProps
} = props;
return createElement(component, {
...otherProps,
...elementProps,
...(component === "a" ? {} : { replace }),
onClick: (event) => {
const target = event.target as HTMLElement;
if (findFocusableAncestor(target) !== event.currentTarget) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ type SharedSliceCardProps = {
variant: "outlined" | "solid";
variationId?: string;
} & (
| { mode: "navigation"; action: Action; selected?: boolean }
| {
mode: "navigation";
action: Action;
replace?: boolean;
selected?: boolean;
}
| {
mode: "selection";
action: Action | { type: "checkbox" };
Expand Down Expand Up @@ -85,6 +90,7 @@ export const SharedSliceCard: FC<SharedSliceCardProps> = (props) => {
variationId: variation.id,
}),
interactive: true,
replace: props.replace,
} as const)
: ({ interactive: false } as const);
const variantProps =
Expand Down
Loading

0 comments on commit 9f1e26c

Please sign in to comment.