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

Add tabs in newsfeed and update colopicker #322

Merged
merged 9 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions ui/src/components/General/ListItems/News/News.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NewsType } from "./utils/types";
import { Card, Skeleton, Typography } from "antd";
import { Card, Image, Skeleton, Typography } from "antd";
import { ListItemProps } from "components/RenderProps/List/utils/types";
const { Title } = Typography;

Expand All @@ -8,11 +8,12 @@ const News: React.FC<ListItemProps<NewsType>> = ({
handleOnClick,
isLoading,
}) => {
const { title, content, url } = resource || {};
const { title, content, url, image } = resource || {};

return (
<Card onClick={() => handleOnClick(url)} hoverable>
<Skeleton loading={isLoading}>
<Image preview={false} src={image} alt="" />
<Title level={4}>{title}</Title>
{content}
</Skeleton>
Expand Down
1 change: 1 addition & 0 deletions ui/src/components/General/ListItems/News/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ interface NewsType {
title: string;
content: string;
url: string;
image?: string;
}

export type { NewsType };
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ClipboardButton from "components/General/ClipboardButton";
import { classNames, getTextColor, isTransparent } from "lib/utils/helper";
import { DisplayColorProps } from "pages/Colors/ColorPicker/utils/types";
import { useSearchParams } from "react-router-dom";
import CodeHighlightWithCopy from "components/General/CodeHighlightWithCopy";

const { Title } = Typography;

Expand All @@ -14,6 +15,7 @@ const DisplayColor: React.FC<DisplayColorProps> = ({
customValue,
value,
format,
title,
}) => {
const {
token: { colorBgContainer, colorText },
Expand All @@ -35,19 +37,27 @@ const DisplayColor: React.FC<DisplayColorProps> = ({
const containerStyle = {
backgroundColor: color ? backgroundColor : colorBgContainer,
border: color ? border : "none",
padding: "5px",
ashik-75 marked this conversation as resolved.
Show resolved Hide resolved
};

const titleStyle = {
color: color ? textColor : colorText,
};

return (
return title === "Colors" ? (
<div className={classes} style={containerStyle}>
<Title level={5} style={titleStyle}>
{customLabel}: {color ? customValue : ""}
</Title>
<Clipboard text={value} clipboardComponent={ClipboardButton} />
</div>
) : (
<div style={containerStyle}>
<CodeHighlightWithCopy
codeString={`${customLabel}: ${color ? customValue : ""}`}
language="css"
/>
</div>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const DisplayColors: React.FC<DisplayColorsProps> = ({
customValue={determineValue(value, displayType, colors)}
value={colors[value]}
format={format}
title={title}
/>
))}
</Space>
Expand Down
1 change: 1 addition & 0 deletions ui/src/pages/Colors/ColorPicker/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface DisplayColorProps {
customValue: string;
value: string;
format: string;
title: string;
}

export type { FormatType, DisplayColorsProps, DisplayColorProps, colors };
44 changes: 32 additions & 12 deletions ui/src/pages/Newsfeed/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
import News from "components/General/ListItems/News/News";
import React, { useState } from "react";
import { SITE_OPTIONS, TAB_ITEMS } from "./utils/constants";
import useFetchList from "lib/utils/hooks/useFetchList";
import { Tabs } from "antd";
import ListSearchResults from "components/RenderProps/ListSearchResults";

const URL = `https://raw.githubusercontent.com/lifeparticle/binarytree/main/api/news.json`;
import News from "components/General/ListItems/News/News";
import { parseXML } from "./utils/helper";
export const QUERY_KEY_NEWS = "news";

const Newsfeed = () => {
const { data, isLoading, isError } = useFetchList(QUERY_KEY_NEWS, URL);
const Newsfeed: React.FC = () => {
const corsProxyUrl = "https://cors-anywhere.herokuapp.com/";
const [url, setUrl] = useState(SITE_OPTIONS["frontend-focus"].value);
const isFeedUrl =
url === SITE_OPTIONS["frontend-focus"].value ||
url === SITE_OPTIONS["status-code"].value;
const { data, isLoading, isError } = useFetchList(
url,
isFeedUrl ? corsProxyUrl + url : url
);

return (
<ListSearchResults
items={data?.articles}
resourceName={QUERY_KEY_NEWS}
itemComponent={News}
isLoading={isLoading}
isError={isError}
/>
<>
<Tabs
defaultActiveKey={SITE_OPTIONS["frontend-focus"].value}
items={TAB_ITEMS}
onChange={(value) => {
setUrl(value);
}}
/>

<ListSearchResults
isError={isError}
isLoading={isLoading}
itemComponent={News}
resourceName={QUERY_KEY_NEWS}
items={isFeedUrl ? parseXML(data) : data?.articles}
/>
</>
);
};

Expand Down
33 changes: 33 additions & 0 deletions ui/src/pages/Newsfeed/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { TabsProps } from "antd";

const SITE_OPTIONS = {
"frontend-focus": {
label: "Frontend Focus",
value: "https://cprss.s3.amazonaws.com/frontendfoc.us.xml",
},
"status-code": {
label: "Status Code",
value: "https://cprss.s3.amazonaws.com/react.statuscode.com.xml",
},
news: {
label: "News",
value: "https://raw.githubusercontent.com/lifeparticle/binarytree/main/api/news.json",
},
};

const TAB_ITEMS: TabsProps["items"] = [
{
key: SITE_OPTIONS["frontend-focus"].value,
label: SITE_OPTIONS["frontend-focus"].label,
},
{
key: SITE_OPTIONS["status-code"].value,
label: SITE_OPTIONS["status-code"].label,
},
{
key: SITE_OPTIONS["news"].value,
label: SITE_OPTIONS["news"].label,
},
];

export { SITE_OPTIONS, TAB_ITEMS };
27 changes: 27 additions & 0 deletions ui/src/pages/Newsfeed/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function parseXML(value: string) {
const parser = new DOMParser();
const xmldoc = parser.parseFromString(value, "text/xml");

const items = xmldoc.getElementsByTagName("item");
const list = [];

for (let i = 0; i < items.length; i++) {
Copy link
Owner

Choose a reason for hiding this comment

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

??

const item = items[i];

// Extract data from the 'item' element
const title = item.getElementsByTagName("title")[0].textContent;
const description = parser.parseFromString(
item.getElementsByTagName("description")[0].textContent!,

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

DOM text
is reinterpreted as HTML without escaping meta-characters.
"text/html"
);
const image = description?.getElementsByTagName("img")?.[0]?.src;
const pubDate = item.getElementsByTagName("pubDate")[0].textContent;
const url = item.getElementsByTagName("link")[0].textContent;

list.push({ title, pubDate, url, image });
}

return list;
}

export { parseXML };