Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
fix: support tree-structured layers and tags in published pages (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Feb 1, 2022
1 parent 81f5cfa commit 17d9682
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
54 changes: 32 additions & 22 deletions src/components/organisms/Published/hooks.ts
Expand Up @@ -11,7 +11,7 @@ import type {
} from "@reearth/components/molecules/Visualizer";
import { LayerStore } from "@reearth/components/molecules/Visualizer";

import { PublishedData, WidgetZone, WidgetSection, WidgetArea } from "./types";
import { PublishedData, WidgetZone, WidgetSection, WidgetArea, Layer as RawLayer } from "./types";

export default (alias?: string) => {
const [data, setData] = useState<PublishedData>();
Expand All @@ -32,30 +32,14 @@ export default (alias?: string) => {
() =>
new LayerStore({
id: "",
children: data?.layers?.map<Layer>(l => ({
id: l.id,
title: l.name || "",
pluginId: l.pluginId,
extensionId: l.extensionId,
isVisible: true,
property: processProperty(l.property),
infobox: l.infobox
? {
property: processProperty(l.infobox.property),
blocks: l.infobox.fields.map<Block>(f => ({
id: f.id,
pluginId: f.pluginId,
extensionId: f.extensionId,
property: processProperty(f.property),
})),
}
: undefined,
})),
children: data?.layers?.map(processLayer),
}),
[data],
);

const widgetSystem = useMemo<
const tags = data?.tags; // Currently no need to convert tags

const widgets = useMemo<
{ floatingWidgets: Widget[]; alignSystem: WidgetAlignSystem | undefined } | undefined
>(() => {
if (!data || !data.widgets) return undefined;
Expand Down Expand Up @@ -190,13 +174,39 @@ export default (alias?: string) => {
sceneProperty,
pluginProperty,
layers,
tags,
clusterProperty,
widgets: widgetSystem,
widgets,
ready,
error,
};
};

function processLayer(l: RawLayer): Layer {
return {
id: l.id,
title: l.name || "",
pluginId: l.pluginId,
extensionId: l.extensionId,
isVisible: l.isVisible ?? true,
propertyId: l.propertyId,
property: processProperty(l.property),
infobox: l.infobox
? {
property: processProperty(l.infobox.property),
blocks: l.infobox.fields.map<Block>(f => ({
id: f.id,
pluginId: f.pluginId,
extensionId: f.extensionId,
property: processProperty(f.property),
})),
}
: undefined,
tags: l.tags, // Currently no need to convert tags
children: l.children?.map(processLayer),
};
}

function processProperty(p: any): any {
if (typeof p !== "object") return p;
return mapValues(p, g =>
Expand Down
3 changes: 2 additions & 1 deletion src/components/organisms/Published/index.tsx
Expand Up @@ -11,7 +11,7 @@ export type Props = {
};

export default function Published({ className, alias }: Props) {
const { sceneProperty, pluginProperty, layers, widgets, ready, error, clusterProperty } =
const { sceneProperty, pluginProperty, layers, widgets, tags, ready, error, clusterProperty } =
useHooks(alias);

return error ? (
Expand All @@ -22,6 +22,7 @@ export default function Published({ className, alias }: Props) {
engine="cesium"
layers={layers}
widgets={widgets}
tags={tags}
sceneProperty={sceneProperty}
pluginProperty={pluginProperty}
clusterProperty={clusterProperty}
Expand Down
12 changes: 12 additions & 0 deletions src/components/organisms/Published/types.ts
Expand Up @@ -10,6 +10,7 @@ export type PublishedData = {
widgets?: Widget[];
widgetAlignSystem?: WidgetAlignSystem;
clusters: Cluster[];
tags?: Tag[];
};

export type Plugin = {
Expand All @@ -22,11 +23,22 @@ export type Layer = {
name?: string;
pluginId: string;
extensionId: string;
propertyId?: string;
/** If undefined, it should be treated as true. */
isVisible?: boolean;
property: any;
infobox?: {
fields: Block[];
property: any;
} | null;
tags?: Tag[];
children?: Layer[];
};

export type Tag = {
id: string;
label: string;
tags?: Tag[];
};

export type Block = {
Expand Down

0 comments on commit 17d9682

Please sign in to comment.