Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Migrate widgets that are still using format_structure (ALPHA-4191) #159

Merged
merged 8 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/frontend/src/api/types/agenda.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export type TAgendaItem = {
tags: string[];
id: string;
name: string;
category: string;
date: string;
description: string;
location?: string;
name?: string;
category?: string;
date?: string;
description?: string;
author?: string;
source_name?: string;
tags?: string[];
location?: string;
};
27 changes: 14 additions & 13 deletions packages/frontend/src/api/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
export * from "./agenda";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just reorder and added agenda

export * from "./blog";
export * from "./chat";
export * from "./custom";
export * from "./dao";
export * from "./discord";
export * from "./event";
export * from "./features";
export * from "./forum";
export * from "./gas";
export * from "./lens";
export * from "./market";
export * from "./news";
export * from "./notifications";
export * from "./podcast";
export * from "./portfolio";
export * from "./primitives";
export * from "./polls";
export * from "./qna";
export * from "./reddit";
export * from "./tvl";
export * from "./user";
export * from "./views";
export * from "./tweets";
export * from "./chat";
export * from "./tutorial";
export * from "./podcast";
export * from "./user";
export * from "./views";
export * from "./video";
export * from "./notifications";
export * from "./blog";
export * from "./forum";
export * from "./reddit";
export * from "./portfolio";
export * from "./lens";
export * from "./features";
export * from "./discord";
export * from "./qna";
11 changes: 9 additions & 2 deletions packages/frontend/src/api/utils/itemUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TItem } from "src/types";
import { TRemoteCustomData, TRemoteCustomDatum } from "../services";
import { Logger } from "./logging";

/**
* This type is used to define the root type of items which include news, daos, tweets etc
Expand Down Expand Up @@ -174,5 +175,11 @@ export const customDatumAsItem: (datum: TRemoteCustomDatum) => TItem = (d) => {
}, {} as TItem);
};

export const customDataAsItems: (data: TRemoteCustomData) => TItem[] = (d) =>
d.map(customDatumAsItem);
export const customDataAsItems: (data: TRemoteCustomData) => TItem[] = (d) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs a proper fix in the validation functions

try {
return d.map(customDatumAsItem);
} catch (error) {
Logger.error("customDataAsItems: unexpected error", error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's say this fails I think it will be helpful to know which container this error comes from.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right but I don't want to add code that will need to be cleaned up later on. This should never fail (that would mean we could not trust the expected type of the function input) so I'm actually removing the try-catch.

return [];
}
};
11 changes: 8 additions & 3 deletions packages/frontend/src/components/daos/DaoItemList.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { FormEvent, FC } from "react";
import { HRElement, ListItem, NoItems, ScrollBar } from "@alphaday/ui-kit";
import {
HRElement,
ListItem,
CenteredBlock,
ScrollBar,
} from "@alphaday/ui-kit";
import { TDaoItem } from "src/api/types";
import { shouldFetchMoreItems } from "src/api/utils/itemUtils";
import globalMessages from "src/globalMessages";
Expand All @@ -17,9 +22,9 @@ const DaoItemList: FC<IDaoItemList> = ({ items, handlePaginate }) => {
if (items) {
if (items.length === 0) {
return (
<NoItems>
<CenteredBlock>
<p>{globalMessages.queries.noMatchFound("items")}</p>
</NoItems>
</CenteredBlock>
);
}
return (
Expand Down
6 changes: 3 additions & 3 deletions packages/frontend/src/components/discord/DiscordModule.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FC, FormEvent } from "react";
import { ModuleLoader, NoItems, ScrollBar } from "@alphaday/ui-kit";
import { ModuleLoader, CenteredBlock, ScrollBar } from "@alphaday/ui-kit";
import { TDiscordItem } from "src/api/types";
import { shouldFetchMoreItems } from "src/api/utils/itemUtils";
import globalMessages from "src/globalMessages";
Expand Down Expand Up @@ -34,11 +34,11 @@ const DiscordModule: FC<IDiscord> = ({
return <DiscordFeedItem key={item.id} item={item} />;
})}
{!isLoadingItems && items.length === 0 && (
<NoItems>
<CenteredBlock>
<p>
{globalMessages.queries.noMatchFound("Discord Items")}
</p>
</NoItems>
</CenteredBlock>
)}
</ScrollBar>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { FC, FormEvent } from "react";
import { ModuleLoader, ScrollBar, ListItem, NoItems } from "@alphaday/ui-kit";
import {
ModuleLoader,
ScrollBar,
ListItem,
CenteredBlock,
} from "@alphaday/ui-kit";
import { shouldFetchMoreItems } from "src/api/utils/itemUtils";
import globalMessages from "src/globalMessages";
import { IReports } from "./types";
Expand All @@ -20,9 +25,9 @@ const ReportsModule: FC<IReports> = ({
}
if (items.length === 0) {
return (
<NoItems>
<CenteredBlock>
<p>{globalMessages.queries.noMatchFound("reports")}</p>
</NoItems>
</CenteredBlock>
);
}
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const AgendaItem: FC<Item> = ({ item, catColor, setItemsHeight }) => {
openAccordion={openAccordion}
author={author}
descHeightRef={descHeightRef}
description={description}
description={description ?? ""}
variant="agenda"
fullHeight={paddedDescHeight}
remarkPlugins={PLUGINS}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { FC } from "react";
import { ModuleLoader, ScrollBar } from "@alphaday/ui-kit";
import { CenteredBlock, ScrollBar } from "@alphaday/ui-kit";
import { useDynamicWidgetHeight } from "src/components/dynamic-modules/hooks/useDynamicWidgetHeight";
import CONFIG from "src/config/config";
import globalMessages from "src/globalMessages";
import { TItem } from "src/types";
import FaqItem from "./FaqItem";

Expand All @@ -25,15 +26,17 @@ const FaqModule: FC<IAgenda> = ({
});

return (
<span
<div
className="border-none block relative min-h-[110px]"
style={{
// height of the table should not be greater than max-content
height: `${Math.min(initialItemsHeight, widgetHeight)}px`,
}}
>
{!items || items?.length < 1 ? (
<ModuleLoader $height={`${widgetHeight}px`} />
<CenteredBlock>
<p>{globalMessages.queries.noResults}</p>
</CenteredBlock>
) : (
<ScrollBar containerRef={setScrollRef}>
<div
Expand All @@ -52,7 +55,7 @@ const FaqModule: FC<IAgenda> = ({
</div>
</ScrollBar>
)}
</span>
</div>
);
};

Expand Down
6 changes: 3 additions & 3 deletions packages/frontend/src/components/feeds/LensFeedModule.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FC } from "react";
import { ModuleLoader, NoItems, ScrollBar } from "@alphaday/ui-kit";
import { ModuleLoader, CenteredBlock, ScrollBar } from "@alphaday/ui-kit";
import { TLensPost } from "src/api/types";
import globalMessages from "src/globalMessages";
import LensFeedItem from "./LensFeedItem";
Expand All @@ -22,9 +22,9 @@ const LensFeedModule: FC<IPosts> = ({

if (posts.length === 0) {
return (
<NoItems>
<CenteredBlock>
<p>{globalMessages.queries.noMatchFound("lens posts")}</p>
</NoItems>
</CenteredBlock>
);
}
return (
Expand Down
11 changes: 8 additions & 3 deletions packages/frontend/src/components/news/NewsItemList.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { FormEvent, FC } from "react";
import { HRElement, ListItem, NoItems, ScrollBar } from "@alphaday/ui-kit";
import {
HRElement,
ListItem,
CenteredBlock,
ScrollBar,
} from "@alphaday/ui-kit";
import { TNewsItem } from "src/api/types";
import { shouldFetchMoreItems } from "src/api/utils/itemUtils";
import globalMessages from "src/globalMessages";
Expand All @@ -26,9 +31,9 @@ const NewsItemList: FC<INewsItemList> = ({
if (items) {
if (items.length === 0) {
return (
<NoItems>
<CenteredBlock>
<p>{globalMessages.queries.noMatchFound("news")}</p>
</NoItems>
</CenteredBlock>
);
}
return (
Expand Down
11 changes: 10 additions & 1 deletion packages/frontend/src/containers/calendar/CalendarContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useAppDispatch, useAppSelector } from "src/api/store/hooks";
import { TEvent } from "src/api/types";
import { getClosestEvent } from "src/api/utils/calendarUtils";
import { filteringListToStr } from "src/api/utils/filterUtils";
import { customDataAsItems } from "src/api/utils/itemUtils";
import { Logger } from "src/api/utils/logging";
import { buildViewPath } from "src/api/utils/viewUtils";
import CalendarModule from "src/components/calendar/CalendarModule";
Expand Down Expand Up @@ -78,7 +79,15 @@ const CalendarContainer: FC<IModuleContainer<TCategoryData[][]>> = ({

const viewPath = buildViewPath(selectedView?.data);

const widgetCats = moduleData.widget.format_structure.data?.[0];
const widgetCats = customDataAsItems(moduleData.widget.custom_data ?? []);

// TODO(v-almonacid): remove this block when format_structure is removed from db model
const legacyWidgetCats = moduleData.widget.format_structure?.data?.[0];
if (Array.isArray(legacyWidgetCats) && legacyWidgetCats.length > 0) {
Logger.warn(
`CalendarContainer: widget ${moduleData.widget.name} is using format_structure which has been deprecated`
);
}

const allowedCategories: TEventCategory[] =
widgetCats?.map((wCat) => {
Expand Down
35 changes: 33 additions & 2 deletions packages/frontend/src/containers/countdown/CountdownContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FC } from "react";
import { FC, useMemo } from "react";
import moment from "moment";
import { TCounterData } from "src/api/types";
import assert from "src/api/utils/assert";
import { Logger } from "src/api/utils/logging";
import CountdownModule from "src/components/countdown/CountdownModule";
import { IModuleContainer } from "src/types";
Expand All @@ -10,9 +11,39 @@ const DEFAULT_TARGET_DATE = moment().unix();
const CountdownContainer: FC<IModuleContainer<TCounterData[]>> = ({
moduleData,
}) => {
const [data] = moduleData.widget.format_structure.data || [];
// const [data] = moduleData.widget.format_structure.data || [];
const isBeaconWidget = moduleData.widget.slug === "merge_countdown_widget";

const data = useMemo(() => {
try {
const [rawData] = moduleData.widget.custom_data ?? [];
assert(
rawData.announcement !== undefined,
"CountdownContainer: data must contain announcement"
);
assert(
rawData.date !== undefined,
"CountdownContainer: data must contain date"
);
// @ts-expect-error this is handled above
return rawData as TCounterData;
} catch (error) {
return undefined;
}
}, [moduleData.widget.custom_data]);

if (data === undefined) {
return null;
}

// TODO(v-almonacid): remove this block when format_structure is removed from db model
const { data: legacyData } = moduleData.widget.format_structure;
if (Array.isArray(legacyData) && legacyData.length > 0) {
Logger.warn(
`CountdownContainer: widget ${moduleData.widget.name} contains data in format_structure which has been deprecated`
);
}

let targetDate: number;
try {
targetDate =
Expand Down
22 changes: 14 additions & 8 deletions packages/frontend/src/containers/dynamic/AgendaContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { FC, useMemo } from "react";
import { useWidgetHeight } from "src/api/hooks";
import { TAgendaItem } from "src/api/types/agenda";
import { customDataAsItems } from "src/api/utils/itemUtils";
import { Logger } from "src/api/utils/logging";
import AgendaModule from "src/components/dynamic-modules/agenda/AgendaModule";
import { v4 as uuidV4 } from "uuid";
import { IModuleContainer } from "src/types";

const AgendaContainer: FC<IModuleContainer<TAgendaItem[]>> = ({
Expand All @@ -11,20 +12,25 @@ const AgendaContainer: FC<IModuleContainer<TAgendaItem[]>> = ({
const widgetHeight = useWidgetHeight(moduleData);

const items = useMemo(
() => moduleData.widget.format_structure.data || [],
[moduleData.widget.format_structure.data]
() => customDataAsItems(moduleData.widget.custom_data ?? []),
[moduleData.widget.custom_data]
);

// Add unique id to each item if id is not present, generate a uuid
const uniqueItems = useMemo(
() => items.map((item) => ({ ...item, id: item.id || uuidV4() })),
[items]
// TODO(v-almonacid): remove this block when format_structure is removed from db model
const legacyItems = useMemo(
() => moduleData.widget.format_structure.data || [],
[moduleData.widget.format_structure.data]
);
if (Array.isArray(legacyItems) && legacyItems.length > 0) {
Logger.warn(
`AgendaContainer: widget ${moduleData.widget.name} contains data in format_structure which has been deprecated`
);
}

return (
<AgendaModule
widgetHeight={widgetHeight}
items={uniqueItems}
items={items as TAgendaItem[]}
isLoadingItems={false}
onAdjustWidgetHeight={() => {}}
/>
Expand Down
19 changes: 17 additions & 2 deletions packages/frontend/src/containers/dynamic/FaqContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import { FC } from "react";
import { FC, useMemo } from "react";
import { useWidgetHeight } from "src/api/hooks";
import { customDataAsItems } from "src/api/utils/itemUtils";
import { Logger } from "src/api/utils/logging";
import FaqModule from "src/components/dynamic-modules/faq/FaqModule";
import { IModuleContainer, TItem } from "src/types";

const FaqContainer: FC<IModuleContainer<TItem[][]>> = ({ moduleData }) => {
const widgetHeight = useWidgetHeight(moduleData);
const items = moduleData.widget.format_structure.data?.[0] || [];

const items = useMemo(
() => customDataAsItems(moduleData.widget.custom_data ?? []),
[moduleData.widget.custom_data]
);

// TODO(v-almonacid): remove this block when format_structure is removed from db model
const { data } = moduleData.widget.format_structure;
if (Array.isArray(data) && data.length > 0) {
Logger.warn(
`FaqContainer: widget ${moduleData.widget.name} contains data in format_structure which has been deprecated`
);
}

return (
<FaqModule
widgetHeight={widgetHeight}
Expand Down
Loading
Loading