Skip to content

Commit

Permalink
chore(web): refactor storypanel to remove any gql use (#697)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaWaite authored Sep 20, 2023
1 parent 6b5db31 commit 70d1dde
Show file tree
Hide file tree
Showing 24 changed files with 333 additions and 189 deletions.
78 changes: 74 additions & 4 deletions web/src/beta/features/Editor/Visualizer/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import { useMemo, useEffect, useCallback } from "react";
import type { Alignment, Location } from "@reearth/beta/lib/core/Crust";
import type { LatLng, Tag, ValueTypes, ComputedLayer } from "@reearth/beta/lib/core/mantle";
import type { Layer, LayerSelectionReason, Cluster } from "@reearth/beta/lib/core/Map";
import { useLayersFetcher, useSceneFetcher, useWidgetsFetcher } from "@reearth/services/api";
import type { ValueType } from "@reearth/beta/utils/value";
import {
useLayersFetcher,
useSceneFetcher,
useWidgetsFetcher,
useStorytellingFetcher,
usePropertyFetcher,
} from "@reearth/services/api";
import { config } from "@reearth/services/config";
import {
useSceneMode,
Expand All @@ -18,13 +25,28 @@ import {
} from "@reearth/services/state";

import { convertWidgets, processLayers } from "./convert";
import { BlockType } from "./type";
import type { BlockType } from "./type";

export default ({ sceneId, isBuilt }: { sceneId?: string; isBuilt?: boolean }) => {
export default ({
sceneId,
isBuilt,
storyId,
pageId,
}: {
sceneId?: string;
isBuilt?: boolean;
storyId?: string;
pageId?: string;
}) => {
const { useUpdateWidget, useUpdateWidgetAlignSystem } = useWidgetsFetcher();
const { useGetLayersQuery } = useLayersFetcher();
const { nlsLayers } = useGetLayersQuery({ sceneId });
const { useSceneQuery } = useSceneFetcher();
const { useInstalledStoryBlocksQuery, useCreateStoryBlock, useDeleteStoryBlock } =
useStorytellingFetcher();
const { useUpdatePropertyValue } = usePropertyFetcher();

const { nlsLayers } = useGetLayersQuery({ sceneId });

const { scene } = useSceneQuery({ sceneId });

const [sceneMode, setSceneMode] = useSceneMode();
Expand Down Expand Up @@ -173,6 +195,50 @@ export default ({ sceneId, isBuilt }: { sceneId?: string; isBuilt?: boolean }) =
[sceneId, useUpdateWidgetAlignSystem],
);

const { installedStoryBlocks } = useInstalledStoryBlocksQuery({
sceneId,
lang: undefined,
storyId,
pageId,
});

const handleStoryBlockCreate = useCallback(
(index?: number) => async (extensionId?: string, pluginId?: string) => {
if (!extensionId || !pluginId || !storyId || !pageId) return;
await useCreateStoryBlock({
pluginId,
extensionId,
storyId,
pageId,
index,
});
},
[storyId, pageId, useCreateStoryBlock],
);

const handleStoryBlockDelete = useCallback(
async (blockId?: string) => {
if (!blockId || !storyId || !pageId) return;
await useDeleteStoryBlock({ blockId, pageId, storyId });
},
[storyId, pageId, useDeleteStoryBlock],
);

const handlePropertyValueUpdate = useCallback(
async (
propertyId?: string,
schemaItemId?: string,
fieldId?: string,
itemId?: string,
vt?: ValueType,
v?: ValueTypes[ValueType],
) => {
if (!propertyId || !schemaItemId || !fieldId || !vt) return;
await useUpdatePropertyValue(propertyId, schemaItemId, itemId, fieldId, "en", v, vt);
},
[useUpdatePropertyValue],
);

const engineMeta = useMemo(
() => ({
cesiumIonAccessToken: config()?.cesiumIonAccessToken,
Expand Down Expand Up @@ -206,6 +272,10 @@ export default ({ sceneId, isBuilt }: { sceneId?: string; isBuilt?: boolean }) =
useExperimentalSandbox,
isVisualizerReady,
selectWidgetArea: selectedWidgetAreaVar,
installedStoryBlocks,
handleStoryBlockCreate,
handleStoryBlockDelete,
handlePropertyValueUpdate,
selectLayer,
selectBlock,
onBlockChange,
Expand Down
22 changes: 13 additions & 9 deletions web/src/beta/features/Editor/Visualizer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import { MutableRefObject, useCallback } from "react";

import ContentPicker from "@reearth/beta/components/ContentPicker";
import type { MapRef } from "@reearth/beta/lib/core/Map/ref";
import StoryPanel, {
type InstallableStoryBlock,
type GQLStory,
type GQLStoryPage,
} from "@reearth/beta/lib/core/StoryPanel";
import StoryPanel, { type InstallableStoryBlock } from "@reearth/beta/lib/core/StoryPanel";
import CoreVisualizer, { type Props as VisualizerProps } from "@reearth/beta/lib/core/Visualizer";
import type { Camera } from "@reearth/beta/utils/value";
import type { Page, Story } from "@reearth/services/api/storytellingApi/utils";
import { config } from "@reearth/services/config";
import { styled } from "@reearth/services/theme";

Expand All @@ -22,8 +19,8 @@ export type Props = {
currentCamera?: Camera;
// storytelling
showStoryPanel?: boolean;
selectedStory?: GQLStory;
currentPage?: GQLStoryPage;
selectedStory?: Story;
currentPage?: Page;
isAutoScrolling?: boolean;
installableBlocks?: InstallableStoryBlock[];
onAutoScrollingChange: (isScrolling: boolean) => void;
Expand Down Expand Up @@ -64,6 +61,10 @@ const Visualizer: React.FC<Props> = ({
layerSelectionReason,
useExperimentalSandbox,
isVisualizerReady: _isVisualizerReady,
installedStoryBlocks,
handleStoryBlockCreate,
handleStoryBlockDelete,
handlePropertyValueUpdate,
selectLayer,
selectBlock,
onBlockChange,
Expand All @@ -76,7 +77,7 @@ const Visualizer: React.FC<Props> = ({
handleDropLayer,
zoomToLayer,
handleMount,
} = useHooks({ sceneId, isBuilt });
} = useHooks({ sceneId, isBuilt, storyId: selectedStory?.id, pageId: currentPage?.id });

const renderInfoboxInsertionPopUp = useCallback<
NonNullable<VisualizerProps["renderInfoboxInsertionPopup"]>
Expand Down Expand Up @@ -132,12 +133,15 @@ const Visualizer: React.FC<Props> = ({
renderInfoboxInsertionPopup={renderInfoboxInsertionPopUp}>
{showStoryPanel && (
<StoryPanel
sceneId={sceneId}
selectedStory={selectedStory}
currentPage={currentPage}
isAutoScrolling={isAutoScrolling}
installableBlocks={installableBlocks}
installedBlocks={installedStoryBlocks}
isEditable={!!inEditor}
onBlockCreate={handleStoryBlockCreate}
onBlockDelete={handleStoryBlockDelete}
onPropertyUpdate={handlePropertyValueUpdate}
onAutoScrollingChange={onAutoScrollingChange}
onCurrentPageChange={onCurrentPageChange}
/>
Expand Down
6 changes: 2 additions & 4 deletions web/src/beta/features/Editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import useStorytelling from "@reearth/beta/features/Editor/useStorytelling";
import Visualizer from "@reearth/beta/features/Editor/Visualizer";
import Navbar, { type Tab } from "@reearth/beta/features/Navbar";
import { Provider as DndProvider } from "@reearth/beta/utils/use-dnd";
import { StoryFragmentFragment } from "@reearth/services/gql";
import { metrics, styled } from "@reearth/services/theme";

import DataSourceManager from "./DataSourceManager";
Expand All @@ -19,10 +18,9 @@ type Props = {
tab: Tab;
projectId?: string;
workspaceId?: string;
stories: StoryFragmentFragment[];
};

const Editor: React.FC<Props> = ({ sceneId, projectId, workspaceId, tab, stories }) => {
const Editor: React.FC<Props> = ({ sceneId, projectId, workspaceId, tab }) => {
const {
visualizerRef,
selectedSceneSetting,
Expand Down Expand Up @@ -55,7 +53,7 @@ const Editor: React.FC<Props> = ({ sceneId, projectId, workspaceId, tab, stories
handlePageMove,
} = useStorytelling({
sceneId,
stories,
onFlyTo: handleFlyTo,
});

const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ const dummyPages = [...Array(25)].map((_, i) => ({
title: `Page ${i}`,
swipeable: i % 2 === 0,
blocks: [],
propertyId: "1234",
property: {
id: "1234",
items: [],
},
}));

export const Default: Story = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import PopoverMenuContent from "@reearth/beta/components/PopoverMenuContent";
import Action from "@reearth/beta/features/Editor/tabs/story/LeftPanel/Action";
import PageItemWrapper from "@reearth/beta/features/Editor/tabs/story/LeftPanel/PageItemWrapper";
import { getFieldValue } from "@reearth/beta/lib/core/StoryPanel/utils";
import { convert } from "@reearth/services/api/propertyApi/utils";
import { StoryPageFragmentFragment } from "@reearth/services/gql";
import type { Page } from "@reearth/services/api/storytellingApi/utils";
import { useT } from "@reearth/services/i18n";
import { styled } from "@reearth/services/theme";

type Props = {
storyPages: StoryPageFragmentFragment[];
selectedPage?: StoryPageFragmentFragment;
storyPages: Page[];
selectedPage?: Page;
onPageSelect: (id: string) => void;
onPageAdd: (isSwipeable: boolean) => void;
onPageDuplicate: (id: string) => void;
Expand Down Expand Up @@ -59,7 +58,7 @@ const ContentPage: React.FC<Props> = ({
await onPageMove(item.id, index);
}}
renderItem={(storyPage, i) => {
const title = (getFieldValue(convert(storyPage.property) ?? [], "title", "title") ??
const title = (getFieldValue(storyPage.property.items ?? [], "title", "title") ??
t("Untitled")) as string;
return (
<PageItemWrapper
Expand Down
6 changes: 3 additions & 3 deletions web/src/beta/features/Editor/tabs/story/LeftPanel/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import SidePanelCommon from "@reearth/beta/features/Editor/SidePanel";
import ContentPage from "@reearth/beta/features/Editor/tabs/story/LeftPanel/ContentPage";
import { StoryFragmentFragment, StoryPageFragmentFragment } from "@reearth/services/gql";
import { Page, Story } from "@reearth/services/api/storytellingApi/utils";
import { useT } from "@reearth/services/i18n";

type Props = {
selectedStory?: StoryFragmentFragment;
selectedPage?: StoryPageFragmentFragment;
selectedStory?: Story;
selectedPage?: Page;
onPageSelect: (id: string) => void;
onPageDuplicate: (id: string) => void;
onPageDelete: (id: string) => void;
Expand Down
9 changes: 4 additions & 5 deletions web/src/beta/features/Editor/tabs/story/RightPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { useMemo } from "react";

import Settings from "@reearth/beta/features/Editor/Settings";
import SidePanelCommon from "@reearth/beta/features/Editor/SidePanel";
import type { GQLStoryPage } from "@reearth/beta/lib/core/StoryPanel/hooks";
import type { FlyTo } from "@reearth/beta/lib/core/types";
import type { Camera } from "@reearth/beta/utils/value";
import { convert } from "@reearth/services/api/propertyApi/utils";
import type { Page } from "@reearth/services/api/storytellingApi/utils";
import { useT } from "@reearth/services/i18n";

type Props = {
sceneId?: string;
selectedPage?: GQLStoryPage;
selectedPage?: Page;
currentCamera?: Camera;
onFlyTo?: FlyTo;
};
Expand All @@ -20,7 +19,7 @@ const StoryRightPanel: React.FC<Props> = ({ selectedPage, currentCamera, onFlyTo

const propertyItems = useMemo(
() =>
convert(selectedPage?.property)?.filter(
selectedPage?.property.items?.filter(
p => p.schemaGroup !== "panel" && p.schemaGroup !== "title",
),
[selectedPage?.property],
Expand All @@ -35,7 +34,7 @@ const StoryRightPanel: React.FC<Props> = ({ selectedPage, currentCamera, onFlyTo
title: t("Page Settings"),
children: selectedPage && (
<Settings
propertyId={selectedPage.propertyId}
propertyId={selectedPage.property.id}
propertyItems={propertyItems}
currentCamera={currentCamera}
onFlyTo={onFlyTo}
Expand Down
6 changes: 3 additions & 3 deletions web/src/beta/features/Editor/useLeftPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import MapSidePanel from "@reearth/beta/features/Editor/tabs/map/LeftPanel";
import StorySidePanel from "@reearth/beta/features/Editor/tabs/story/LeftPanel";
import { Tab } from "@reearth/beta/features/Navbar";
import type { NLSLayer } from "@reearth/services/api/layersApi/utils";
import { StoryFragmentFragment, StoryPageFragmentFragment } from "@reearth/services/gql";
import type { Page, Story } from "@reearth/services/api/storytellingApi/utils";

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

Expand All @@ -13,8 +13,8 @@ type Props = {
nlsLayers: NLSLayer[];

// storytelling
selectedStory?: StoryFragmentFragment;
currentPage?: StoryPageFragmentFragment;
selectedStory?: Story;
currentPage?: Page;
onCurrentPageChange: (id: string) => void;
onPageDuplicate: (id: string) => void;
onPageDelete: (id: string) => void;
Expand Down
4 changes: 2 additions & 2 deletions web/src/beta/features/Editor/useRightPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ReactNode, useMemo } from "react";

import type { Tab } from "@reearth/beta/features/Navbar";
import type { GQLStoryPage } from "@reearth/beta/lib/core/StoryPanel/hooks";
import type { FlyTo } from "@reearth/beta/lib/core/types";
import type { Camera } from "@reearth/beta/utils/value";
import type { Page } from "@reearth/services/api/storytellingApi/utils";

import MapSidePanel from "./tabs/map/RightPanel";
import StorySidePanel from "./tabs/story/RightPanel";
Expand All @@ -12,7 +12,7 @@ import WidgetSidePanel from "./tabs/widgets/RightPanel";
type Props = {
tab: Tab;
sceneId?: string;
currentPage?: GQLStoryPage;
currentPage?: Page;
showSceneSettings?: boolean;
currentCamera?: Camera;
onFlyTo?: FlyTo;
Expand Down
Loading

0 comments on commit 70d1dde

Please sign in to comment.