Skip to content

Commit

Permalink
chore(web): refactor property and revert core (#788)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaWaite committed Nov 3, 2023
1 parent 38f26c8 commit e844ddd
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 163 deletions.
187 changes: 187 additions & 0 deletions web/src/beta/features/Editor/Visualizer/convert-story.ts
@@ -0,0 +1,187 @@
import { Story, StoryBlock, StoryPage } from "@reearth/beta/lib/core/StoryPanel/types";
import { valueFromGQL, valueTypeFromGQL } from "@reearth/beta/utils/value";
import { toUi } from "@reearth/services/api/propertyApi/utils";
import {
PropertyFieldFragmentFragment,
PropertyFragmentFragment,
PropertyGroupFragmentFragment,
PropertyItemFragmentFragment,
PropertySchemaFieldFragmentFragment,
PropertySchemaGroupFragmentFragment,
Story as GqlStory,
StoryPage as GqlStoryPage,
StoryBlock as GqlStoryBlock,
} from "@reearth/services/gql";

import { DatasetMap, P, datasetValue } from "./convert";

export const convertStory = (story?: GqlStory): Story | undefined => {
if (!story) return undefined;

const storyPages = (pages: GqlStoryPage[]): StoryPage[] =>
pages.map(p => ({
id: p.id,
title: p.title,
propertyId: p.propertyId,
property: processProperty(undefined, p.property),
blocks: storyBlocks(p.blocks),
}));

const storyBlocks = (blocks: GqlStoryBlock[]): StoryBlock[] =>
blocks.map(b => ({
id: b.id,
pluginId: b.pluginId,
extensionId: b.extensionId,
name: b.property?.schema?.groups.find(g => g.schemaGroupId === "default")?.title,
propertyId: b.property?.id,
property: processProperty(undefined, b.property),
}));

return {
id: story.id,
title: story.title,
position: story.panelPosition === "RIGHT" ? "right" : "left",
pages: storyPages(story.pages),
};
};

export const processProperty = (
parent: PropertyFragmentFragment | null | undefined,
orig?: PropertyFragmentFragment | null | undefined,
linkedDatasetId?: string | null | undefined,
datasets?: DatasetMap | null | undefined,
): P | undefined => {
const schema = orig?.schema || parent?.schema;
if (!schema) return;

const allItems: Record<
string,
{
schema: PropertySchemaGroupFragmentFragment;
orig?: PropertyItemFragmentFragment;
parent?: PropertyItemFragmentFragment;
}
> = schema.groups.reduce(
(a, b) => ({
...a,
[b.schemaGroupId]: {
schema: b,
orig: orig?.items.find(i => i.schemaGroupId === b.schemaGroupId),
parent: parent?.items.find(i => i.schemaGroupId === b.schemaGroupId),
},
}),
{},
);
const mergedProperty: P = Object.fromEntries(
Object.entries(allItems)
.map(([key, value]) => {
const { schema, orig, parent } = value;
if (!orig && !parent) {
if (schema.isList) {
return [key, undefined];
}
return [
key,
processPropertyGroups(schema, undefined, undefined, linkedDatasetId, datasets),
];
}

if (
(!orig || orig.__typename === "PropertyGroupList") &&
(!parent || parent.__typename === "PropertyGroupList")
) {
const used = orig || parent;
return [
key,
used?.groups.map(g => ({
...processPropertyGroups(schema, g, undefined, linkedDatasetId, datasets),
id: g.id,
})),
];
}

if (
(!orig || orig.__typename === "PropertyGroup") &&
(!parent || parent.__typename === "PropertyGroup")
) {
return [key, processPropertyGroups(schema, parent, orig, linkedDatasetId, datasets)];
}
return [key, null];
})
.filter(([, value]) => !!value),
);

return mergedProperty;
};

const processPropertyGroups = (
schema: PropertySchemaGroupFragmentFragment,
parent: PropertyGroupFragmentFragment | null | undefined,
original: PropertyGroupFragmentFragment | null | undefined,
linkedDatasetId: string | null | undefined,
datasets: DatasetMap | null | undefined,
): any => {

Check warning on line 123 in web/src/beta/features/Editor/Visualizer/convert-story.ts

View workflow job for this annotation

GitHub Actions / ci-web / ci

Unexpected any. Specify a different type
const allFields: Record<
string,
{
schema: PropertySchemaFieldFragmentFragment;
parent?: PropertyFieldFragmentFragment;
orig?: PropertyFieldFragmentFragment;
}
> = schema.fields.reduce(
(a, b) => ({
...a,
[b.fieldId]: {
schema: b,
parent: parent?.fields.find(i => i.fieldId === b.fieldId),
orig: original?.fields.find(i => i.fieldId === b.fieldId),
},
}),
{},
);

return Object.fromEntries(
Object.entries(allFields).map(([key, { schema, parent, orig }]) => {
const used = orig || parent;

const fieldMeta = {
type: valueTypeFromGQL(schema.type) || undefined,
ui: toUi(schema.ui) || undefined,
title: schema.translatedTitle || undefined,
description: schema.translatedDescription || undefined,
};

if (!used) {
return [
key,
{
...fieldMeta,
value: schema.defaultValue
? valueFromGQL(schema.defaultValue, schema.type)?.value
: undefined,
},
];
}

const datasetSchemaId = used?.links?.[0]?.datasetSchemaId;
const datasetFieldId = used?.links?.[0]?.datasetSchemaFieldId;
if (datasetSchemaId && linkedDatasetId && datasetFieldId) {
return [
key,
{
...fieldMeta,
value: datasetValue(datasets, datasetSchemaId, linkedDatasetId, datasetFieldId),
},
];
}

return [
key,
{
...fieldMeta,
value: valueFromGQL(used.value, used.type)?.value,
},
];
}),
);
};
93 changes: 15 additions & 78 deletions web/src/beta/features/Editor/Visualizer/convert.ts
Expand Up @@ -15,11 +15,9 @@ import { WidgetAreaPadding } from "@reearth/beta/lib/core/Crust/Widgets/WidgetAl
import { LayerAppearanceTypes } from "@reearth/beta/lib/core/mantle";
import type { Block, Tag } from "@reearth/beta/lib/core/mantle/compat/types";
import type { Layer } from "@reearth/beta/lib/core/Map";
import { Story, StoryBlock, StoryPage } from "@reearth/beta/lib/core/StoryPanel/types";
import { DEFAULT_LAYER_STYLE, valueTypeFromGQL } from "@reearth/beta/utils/value";
import { NLSLayer } from "@reearth/services/api/layersApi/utils";
import { LayerStyle } from "@reearth/services/api/layerStyleApi/utils";
import { toUi } from "@reearth/services/api/propertyApi/utils";
import {
type Maybe,
type WidgetZone as WidgetZoneType,
Expand All @@ -35,12 +33,9 @@ import {
PropertyFieldFragmentFragment,
ValueType as GQLValueType,
NlsLayerCommonFragment,
Story as GqlStory,
StoryPage as GqlStoryPage,
StoryBlock as GqlStoryBlock,
} from "@reearth/services/gql";

type P = { [key in string]: any };
export type P = { [key in string]: any };

Check warning on line 38 in web/src/beta/features/Editor/Visualizer/convert.ts

View workflow job for this annotation

GitHub Actions / ci-web / ci

Unexpected any. Specify a different type

export type DatasetMap = Record<string, Datasets>;

Expand Down Expand Up @@ -184,36 +179,6 @@ export const convertWidgets = (
};
};

export const convertStory = (story?: GqlStory): Story | undefined => {
if (!story) return undefined;

const storyPages = (pages: GqlStoryPage[]): StoryPage[] =>
pages.map(p => ({
id: p.id,
title: p.title,
propertyId: p.propertyId,
property: processProperty(undefined, p.property),
blocks: storyBlocks(p.blocks),
}));

const storyBlocks = (blocks: GqlStoryBlock[]): StoryBlock[] =>
blocks.map(b => ({
id: b.id,
pluginId: b.pluginId,
extensionId: b.extensionId,
name: b.property?.schema?.groups.find(g => g.schemaGroupId === "default")?.title,
propertyId: b.property?.id,
property: processProperty(undefined, b.property),
}));

return {
id: story.id,
title: story.title,
position: story.panelPosition === "RIGHT" ? "right" : "left",
pages: storyPages(story.pages),
};
};

export const processProperty = (
parent: PropertyFragmentFragment | null | undefined,
orig?: PropertyFragmentFragment | null | undefined,
Expand Down Expand Up @@ -310,52 +275,24 @@ const processPropertyGroups = (
);

return Object.fromEntries(
Object.entries(allFields).map(([key, { schema, parent, orig }]) => {
const used = orig || parent;

const fieldMeta = {
type: valueTypeFromGQL(schema.type) || undefined,
ui: toUi(schema.ui) || undefined,
title: schema.translatedTitle || undefined,
description: schema.translatedDescription || undefined,
};

if (!used) {
return [
key,
{
...fieldMeta,
value: schema.defaultValue
? valueFromGQL(schema.defaultValue, schema.type)?.value
: undefined,
},
];
}

const datasetSchemaId = used?.links?.[0]?.datasetSchemaId;
const datasetFieldId = used?.links?.[0]?.datasetSchemaFieldId;
if (datasetSchemaId && linkedDatasetId && datasetFieldId) {
return [
key,
{
...fieldMeta,
value: datasetValue(datasets, datasetSchemaId, linkedDatasetId, datasetFieldId),
},
];
}
Object.entries(allFields)
.map(([key, { parent, orig }]) => {
const used = orig || parent;
if (!used) return [key, null];

const datasetSchemaId = used?.links?.[0]?.datasetSchemaId;
const datasetFieldId = used?.links?.[0]?.datasetSchemaFieldId;
if (datasetSchemaId && linkedDatasetId && datasetFieldId) {
return [key, datasetValue(datasets, datasetSchemaId, linkedDatasetId, datasetFieldId)];
}

return [
key,
{
...fieldMeta,
value: valueFromGQL(used.value, used.type)?.value,
},
];
}),
return [key, valueFromGQL(used.value, used.type)?.value];
})
.filter(([, value]) => typeof value !== "undefined" && value !== null),
);
};

const datasetValue = (
export const datasetValue = (
datasets: DatasetMap | null | undefined,
datasetSchemaId: string,
datasetId: string,
Expand Down
3 changes: 2 additions & 1 deletion web/src/beta/features/Editor/Visualizer/hooks.ts
Expand Up @@ -26,7 +26,8 @@ import {
selectedLayerVar,
} from "@reearth/services/state";

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

export default ({
Expand Down

0 comments on commit e844ddd

Please sign in to comment.