Skip to content

Commit

Permalink
fix(web): selection event for published page (#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
airslice authored Jul 5, 2023
1 parent 822d031 commit b4a111a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
6 changes: 6 additions & 0 deletions web/published.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
<body>
<div id="root"></div>
<script type="module" src="/src/published.tsx"></script>
<script>
// https://github.com/aws-amplify/amplify-js/issues/678
if (global === undefined) {
var global = window;
}
</script>
</body>

</html>
43 changes: 39 additions & 4 deletions web/src/classic/components/organisms/Published/core/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mapValues } from "lodash-es";
import { useState, useMemo, useEffect } from "react";
import { useState, useMemo, useEffect, useCallback } from "react";

import type { Block, ClusterProperty } from "@reearth/classic/components/molecules/Visualizer";
import {
Expand All @@ -15,7 +15,10 @@ import {
type LegacyLayer,
convertLegacyCluster,
} from "@reearth/classic/core/mantle";
import type { ComputedLayer } from "@reearth/classic/core/mantle/types";
import type { LayerSelectionReason } from "@reearth/classic/core/Map/Layers/hooks";
import { config } from "@reearth/services/config";
import { useSelected } from "@reearth/services/state";

import type {
PublishedData,
Expand All @@ -31,12 +34,18 @@ export default (alias?: string) => {
const [data, setData] = useState<PublishedData>();
const [ready, setReady] = useState(false);
const [error, setError] = useState(false);
const [selected, select] = useSelected();

const sceneProperty = processProperty(data?.property);
const pluginProperty = Object.keys(data?.plugins ?? {}).reduce<{ [key: string]: any }>(
(a, b) => ({ ...a, [b]: processProperty(data?.plugins?.[b]?.property) }),
{},
const pluginProperty = useMemo(
() =>
Object.keys(data?.plugins ?? {}).reduce<{ [key: string]: any }>(
(a, b) => ({ ...a, [b]: processProperty(data?.plugins?.[b]?.property) }),
{},
),
[data?.plugins],
);

const legacyClusters = useMemo<ClusterProperty[]>(
() => data?.clusters?.map(a => ({ ...processProperty(a.property), id: a.id })) ?? [],
[data],
Expand Down Expand Up @@ -203,6 +212,29 @@ export default (alias?: string) => {
[],
);

const selectedLayerId = useMemo(
() =>
selected?.type === "layer"
? { layerId: selected.layerId, featureId: selected.featureId }
: undefined,
[selected],
);

const layerSelectionReason = useMemo(
() => (selected?.type === "layer" ? selected.layerSelectionReason : undefined),
[selected],
);

const selectLayer = useCallback(
(
id?: string,
featureId?: string,
_layer?: () => Promise<ComputedLayer | undefined>,
layerSelectionReason?: LayerSelectionReason,
) => select(id ? { layerId: id, featureId, layerSelectionReason, type: "layer" } : undefined),
[select],
);

// GA
useGA(sceneProperty);

Expand All @@ -217,6 +249,9 @@ export default (alias?: string) => {
ready,
error,
engineMeta,
selectedLayerId,
layerSelectionReason,
selectLayer,
};
};

Expand Down
6 changes: 6 additions & 0 deletions web/src/classic/components/organisms/Published/core/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export default function Published({ alias }: Props) {
error,
clusters,
engineMeta,
selectedLayerId,
layerSelectionReason,
selectLayer,
} = useHooks(alias);

return error ? (
Expand All @@ -38,6 +41,9 @@ export default function Published({ alias }: Props) {
isBuilt
pluginBaseUrl={config()?.plugins}
meta={engineMeta}
selectedLayerId={selectedLayerId}
layerSelectionReason={layerSelectionReason}
onLayerSelect={selectLayer}
/>
);
}

0 comments on commit b4a111a

Please sign in to comment.