Skip to content

Commit

Permalink
chore(web): hide and show layer (#735)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkumbobeaty authored Oct 18, 2023
1 parent ce69e7f commit 90e2b96
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 18 deletions.
8 changes: 7 additions & 1 deletion web/src/beta/components/ListItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const ListItem: FC<Props> = ({
children
)}
</Inner>

{actionContent && (
<Popover.Provider
open={isOpenAction}
Expand All @@ -66,11 +67,16 @@ const Wrapper = styled.div`
width: 100%;
`;

const Inner = styled.button<{ border?: boolean; isSelected?: boolean; clamp?: Clamp }>`
const Inner = styled.button<{
border?: boolean;
isSelected?: boolean;
clamp?: Clamp;
}>`
display: flex;
width: 100%;
min-height: 38px;
align-items: center;
color: ${({ theme }) => theme.content.main};
border: 1px solid ${({ border, theme }) => (border ? theme.outline.weakest : "transparent")};
border-radius: ${({ clamp }) =>
clamp === "left" ? "0 6px 6px 0" : clamp === "right" ? "6px 0 0 6px" : "6px"};
Expand Down
2 changes: 2 additions & 0 deletions web/src/beta/features/Editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const Editor: React.FC<Props> = ({ sceneId, projectId, workspaceId, tab }) => {
handleLayerDelete,
handleLayerSelect,
handleLayerNameUpdate,
handleLayerVisibilityUpdate,
} = useLayers({
sceneId,
});
Expand All @@ -83,6 +84,7 @@ const Editor: React.FC<Props> = ({ sceneId, projectId, workspaceId, tab }) => {
onLayerDelete: handleLayerDelete,
onLayerSelect: handleLayerSelect,
onLayerNameUpdate: handleLayerNameUpdate,
onLayerVisibilityUpate: handleLayerVisibilityUpdate,
onSceneSettingSelect: handleSceneSettingSelect,
onDataSourceManagerOpen: handleDataSourceManagerOpener,
onFlyTo: handleFlyTo,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// import ListItem from "@reearth/beta/components/ListItem";
import SidePanelSectionField from "@reearth/beta/components/SidePanelSectionField";
import type { LayerNameUpdateProps } from "@reearth/beta/features/Editor/useLayers";
import type {
LayerNameUpdateProps,
LayerVisibilityUpdateProps,
} from "@reearth/beta/features/Editor/useLayers";
import { FlyTo } from "@reearth/beta/lib/core/types";
import type { NLSLayer } from "@reearth/services/api/layersApi/utils";
import { useT } from "@reearth/services/i18n";
Expand All @@ -17,6 +20,7 @@ type GroupSectionFieldProps = {
onLayerSelect: (id: string) => void;
onSceneSettingSelect: () => void;
onDataSourceManagerOpen: () => void;
onLayerVisibilityUpate: (inp: LayerVisibilityUpdateProps) => void;
onFlyTo?: FlyTo;
};

Expand All @@ -29,6 +33,7 @@ const GroupSectionField: React.FC<GroupSectionFieldProps> = ({
onLayerSelect,
// onSceneSettingSelect,
onDataSourceManagerOpen,
onLayerVisibilityUpate,
onFlyTo,
}) => {
const t = useT();
Expand All @@ -53,6 +58,7 @@ const GroupSectionField: React.FC<GroupSectionFieldProps> = ({
onLayerNameUpdate={onLayerNameUpdate}
onLayerSelect={onLayerSelect}
onDataSourceManagerOpen={onDataSourceManagerOpen}
onLayerVisibilityUpate={onLayerVisibilityUpate}
onFlyTo={onFlyTo}
/>
</StyledSidePanelSectionField>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,38 @@ import { MouseEvent, useCallback, useState } from "react";
import TextInput from "@reearth/beta/components/fields/common/TextInput";
import ListItem from "@reearth/beta/components/ListItem";
import PopoverMenuContent from "@reearth/beta/components/PopoverMenuContent";
import type { LayerNameUpdateProps } from "@reearth/beta/features/Editor/useLayers";
import type {
LayerNameUpdateProps,
LayerVisibilityUpdateProps,
} from "@reearth/beta/features/Editor/useLayers";
import { styled } from "@reearth/services/theme";

type LayerItemProps = {
id: string;
layerTitle: string;
isSelected: boolean;
visible: boolean;
onDelete: () => void;
onSelect: () => void;
onLayerNameUpdate: (inp: LayerNameUpdateProps) => void;
onLayerVisibilityUpate: (inp: LayerVisibilityUpdateProps) => void;
};

const LayerItem = ({
id,
layerTitle,
isSelected,
visible,
onDelete,
onSelect,
onLayerNameUpdate,
onLayerVisibilityUpate,
}: LayerItemProps) => {
const [isActionOpen, setActionOpen] = useState(false);
const [isEditing, setIsEditing] = useState(false);
const [newValue, setNewValue] = useState(layerTitle);
const [isVisible, setIsVisible] = useState(visible);
const [value, setValue] = useState(isVisible ? "V" : "");

const handleActionMenuToggle = useCallback(() => setActionOpen(prev => !prev), []);

Expand Down Expand Up @@ -59,6 +69,13 @@ const LayerItem = ({
[layerTitle, newValue, handleTitleSubmit],
);

const handleUpdateVisibility = useCallback(() => {
const newVisibility = !isVisible;
onLayerVisibilityUpate({ layerId: id, visible: newVisibility });
setIsVisible(newVisibility);
setValue(isVisible ? "" : "V");
}, [id, isVisible, onLayerVisibilityUpate]);

return (
<ListItem
isSelected={isSelected}
Expand Down Expand Up @@ -91,8 +108,27 @@ const LayerItem = ({
) : (
layerTitle
)}
<HideLayer onClick={handleUpdateVisibility}>{value}</HideLayer>
</ListItem>
);
};

export default LayerItem;

const HideLayer = styled.div`
min-width: 10px;
min-height: 20px;
padding: 3px 6px 0;
cursor: pointer;
border-radius: 4px;
border: 1.5px solid ${({ theme }) => theme.bg[1]};
color: ${({ theme }) => theme.content.strong};
background: ${({ theme }) => theme.bg[2]};
position: absolute;
right: 30px;
top: 50%;
transform: translateY(-50%);
:hover {
background: ${({ theme }) => theme.bg[2]};
}
`;
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Meta, StoryObj } from "@storybook/react";

import type { LayerNameUpdateProps } from "@reearth/beta/features/Editor/useLayers";
import type {
LayerNameUpdateProps,
LayerVisibilityUpdateProps,
} from "@reearth/beta/features/Editor/useLayers";
import type { NLSLayer } from "@reearth/services/api/layersApi/utils";

import Layers from ".";
Expand All @@ -19,6 +22,7 @@ function LeftPanelLayers() {
onLayerNameUpdate={(_inp: LayerNameUpdateProps) => {}}
onLayerSelect={(_id: string) => {}}
onDataSourceManagerOpen={() => {}}
onLayerVisibilityUpate={(_inp: LayerVisibilityUpdateProps) => {}}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { useCallback, useState } from "react";
import Icon from "@reearth/beta/components/Icon";
import * as Popover from "@reearth/beta/components/Popover";
import PopoverMenuContent from "@reearth/beta/components/PopoverMenuContent";
import type { LayerNameUpdateProps } from "@reearth/beta/features/Editor/useLayers";
import type {
LayerNameUpdateProps,
LayerVisibilityUpdateProps,
} from "@reearth/beta/features/Editor/useLayers";
import { FlyTo } from "@reearth/beta/lib/core/types";
import type { NLSLayer } from "@reearth/services/api/layersApi/utils";
import { useT } from "@reearth/services/i18n";
Expand All @@ -18,6 +21,7 @@ type LayersProps = {
onLayerNameUpdate: (inp: LayerNameUpdateProps) => void;
onLayerSelect: (id: string) => void;
onDataSourceManagerOpen: () => void;
onLayerVisibilityUpate: (inp: LayerVisibilityUpdateProps) => void;
onFlyTo?: FlyTo;
};

Expand All @@ -28,6 +32,7 @@ const Layers: React.FC<LayersProps> = ({
onLayerNameUpdate,
onLayerSelect,
onDataSourceManagerOpen,
onLayerVisibilityUpate,
onFlyTo,
}) => {
const t = useT();
Expand Down Expand Up @@ -87,10 +92,12 @@ const Layers: React.FC<LayersProps> = ({
key={layer.id}
id={layer.id}
layerTitle={layer.title}
visible={layer.visible}
isSelected={layer.id === selectedLayerId}
onDelete={() => onLayerDelete(layer.id)}
onSelect={() => onLayerSelect(layer.id)}
onLayerNameUpdate={onLayerNameUpdate}
onLayerVisibilityUpate={onLayerVisibilityUpate}
/>
))}
</LayerContainer>
Expand Down
5 changes: 4 additions & 1 deletion web/src/beta/features/Editor/tabs/map/LeftPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FlyTo } from "@reearth/beta/lib/core/types";
import type { NLSLayer } from "@reearth/services/api/layersApi/utils";
import { useT } from "@reearth/services/i18n";

import type { LayerNameUpdateProps } from "../../../useLayers";
import type { LayerNameUpdateProps, LayerVisibilityUpdateProps } from "../../../useLayers";

type Props = {
layers: NLSLayer[];
Expand All @@ -15,6 +15,7 @@ type Props = {
onLayerSelect: (id: string) => void;
onSceneSettingSelect: () => void;
onDataSourceManagerOpen: () => void;
onLayerVisibilityUpate: (inp: LayerVisibilityUpdateProps) => void;
onFlyTo?: FlyTo;
};

Expand All @@ -27,6 +28,7 @@ const MapSidePanel: React.FC<Props> = ({
onLayerNameUpdate,
onSceneSettingSelect,
onDataSourceManagerOpen,
onLayerVisibilityUpate,
onFlyTo,
}) => {
const t = useT();
Expand All @@ -48,6 +50,7 @@ const MapSidePanel: React.FC<Props> = ({
onLayerSelect={onLayerSelect}
onSceneSettingSelect={onSceneSettingSelect}
onDataSourceManagerOpen={onDataSourceManagerOpen}
onLayerVisibilityUpate={onLayerVisibilityUpate}
onFlyTo={onFlyTo}
/>
),
Expand Down
16 changes: 16 additions & 0 deletions web/src/beta/features/Editor/useLayers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export type LayerNameUpdateProps = {
name: string;
};

export type LayerVisibilityUpdateProps = {
layerId: string;
visible: boolean;
};

export default function ({ sceneId }: useLayerProps) {
const t = useT();
const { useGetLayersQuery, useAddNLSLayerSimple, useRemoveNLSLayer, useUpdateNLSLayer } =
Expand Down Expand Up @@ -79,12 +84,23 @@ export default function ({ sceneId }: useLayerProps) {
[useUpdateNLSLayer],
);

const handleLayerVisibilityUpdate = useCallback(
async (inp: LayerVisibilityUpdateProps) => {
await useUpdateNLSLayer({
layerId: inp.layerId,
visible: inp.visible,
});
},
[useUpdateNLSLayer],
);

return {
nlsLayers,
selectedLayer,
handleLayerAdd,
handleLayerDelete,
handleLayerSelect,
handleLayerNameUpdate,
handleLayerVisibilityUpdate,
};
}
6 changes: 5 additions & 1 deletion web/src/beta/features/Editor/useLeftPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FlyTo } from "@reearth/beta/lib/core/types";
import type { NLSLayer } from "@reearth/services/api/layersApi/utils";
import type { Story } from "@reearth/services/api/storytellingApi/utils";

import type { LayerNameUpdateProps } from "./useLayers";
import type { LayerNameUpdateProps, LayerVisibilityUpdateProps } from "./useLayers";

type Props = {
tab: Tab;
Expand All @@ -30,6 +30,7 @@ type Props = {
selectedLayerId?: string;
onLayerDelete: (id: string) => void;
onLayerNameUpdate: (inp: LayerNameUpdateProps) => void;
onLayerVisibilityUpate: (inp: LayerVisibilityUpdateProps) => void;
onLayerSelect: (id: string) => void;
onDataSourceManagerOpen: () => void;
onFlyTo?: FlyTo;
Expand All @@ -52,6 +53,7 @@ export default ({
onLayerSelect,
onSceneSettingSelect,
onDataSourceManagerOpen,
onLayerVisibilityUpate,
onFlyTo,
}: Props) => {
const leftPanel = useMemo<ReactNode | undefined>(() => {
Expand All @@ -64,6 +66,7 @@ export default ({
selectedSceneSetting={selectedSceneSetting}
onLayerDelete={onLayerDelete}
onLayerNameUpdate={onLayerNameUpdate}
onLayerVisibilityUpate={onLayerVisibilityUpate}
onLayerSelect={onLayerSelect}
onSceneSettingSelect={onSceneSettingSelect}
onDataSourceManagerOpen={onDataSourceManagerOpen}
Expand Down Expand Up @@ -94,6 +97,7 @@ export default ({
selectedSceneSetting,
onLayerDelete,
onLayerNameUpdate,
onLayerVisibilityUpate,
onLayerSelect,
onSceneSettingSelect,
onDataSourceManagerOpen,
Expand Down
12 changes: 5 additions & 7 deletions web/src/beta/lib/core/Map/Layers/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ test("tree", () => {
id: "a",
type: "group",
children: [
{ id: "b", type: "simple" },
{ id: "b", type: "simple", visible: true },
{ id: "c", type: "group", children: [] },
],
},
Expand All @@ -85,7 +85,7 @@ test("tree", () => {
expect(Feature.mock.calls[0][0].layer).toEqual({
id: "b",
features: [],
layer: { id: "b", type: "simple" },
layer: { id: "b", type: "simple", visible: true },
status: "ready",
originalFeatures: [],
});
Expand All @@ -107,7 +107,7 @@ test("ref", async () => {
ref.current?.replace({ id: layerId.current, type: "simple", title: "A" });
ss(ref.current?.findById(layerId.current)?.title ?? "");
} else {
const newLayer = ref.current?.add({ type: "simple", title: "a" });
const newLayer = ref.current?.add({ type: "simple", title: "a", visible: true });
layerId.current = newLayer?.id ?? "";
ss(newLayer?.title ?? "");
}
Expand All @@ -121,7 +121,6 @@ test("ref", async () => {
);
}

// add should add layers and getLayer should return a lazy layer
const { rerender } = render(<Comp />);

await waitFor(() => expect(screen.getByTestId("layer")).toHaveTextContent("a"));
Expand All @@ -130,17 +129,15 @@ test("ref", async () => {
expect(Feature.mock.calls[0][0].layer).toEqual({
id: expect.any(String),
features: [],
layer: { id: expect.any(String), type: "simple", title: "a" },
layer: { id: expect.any(String), type: "simple", title: "a", visible: true },
status: "ready",
originalFeatures: [],
});

// update should update the layer
rerender(<Comp replace />);

await waitFor(() => expect(screen.getByTestId("layer")).toHaveTextContent("A"));

// deleteLayer should delete the layer and getLayer should return nothing
rerender(<Comp del />);

await waitFor(() => expect(screen.getByTestId("layer")).toBeEmptyDOMElement());
Expand All @@ -151,6 +148,7 @@ test("computed", async () => {
{
id: "x",
type: "simple",
visible: true,
data: {
type: "geojson",
value: {
Expand Down
1 change: 0 additions & 1 deletion web/src/beta/lib/core/Map/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export default function ({
reason?: LayerSelectionReason,
info?: SelectedFeatureInfo,
) => {
console.log("handleLayerSelect");
selectLayer({ layerId, featureId, layer: await layer?.(), reason, info });
},
[],
Expand Down
Loading

0 comments on commit 90e2b96

Please sign in to comment.