Skip to content

Commit

Permalink
Uplift of #21439 (squashed) to beta
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-builds committed Dec 21, 2023
1 parent 632ccd5 commit 65f50d3
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 67 deletions.
48 changes: 25 additions & 23 deletions components/brave_new_tab_ui/components/default/braveNews/FeedV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import * as React from 'react'
import styled from 'styled-components'
import Feed from '../../../../brave_news/browser/resources/Feed'
import FeedNavigation from '../../../../brave_news/browser/resources/FeedNavigation'
import NewsButton from '../../../../brave_news/browser/resources/NewsButton'
import Variables from '../../../../brave_news/browser/resources/Variables'
import { useBraveNews } from '../../../../brave_news/browser/resources/shared/Context'
import { isPublisherEnabled } from '../../../../brave_news/browser/resources/shared/api'
import { CLASSNAME_PAGE_STUCK } from '../page'

const Root = styled(Variables)`
Expand Down Expand Up @@ -56,19 +58,13 @@ const ButtonsContainer = styled.div`
background: var(--bn-glass-container);
`

const NewsButton = styled(Button)`
const SettingsButton = styled(Button)`
--leo-button-color: var(--bn-glass-50);
--leo-button-radius: ${radius.s};
--leo-button-padding: ${spacing.m};
`

const LoadNewContentButton = styled(Button)`
--leo-button-color: var(--bn-glass-10);
border-radius: 20px;
overflow: hidden;
backdrop-filter: brightness(0.8) blur(32px);
const LoadNewContentButton = styled(NewsButton)`
position: fixed;
z-index: 1;
top: ${spacing['3Xl']};
Expand All @@ -77,7 +73,22 @@ const LoadNewContentButton = styled(Button)`
`

export default function FeedV2() {
const { feedV2, setCustomizePage, refreshFeedV2, feedV2UpdatesAvailable } = useBraveNews()
const { feedV2, setCustomizePage, refreshFeedV2, feedV2UpdatesAvailable, publishers, channels } = useBraveNews()

// We don't want to decide whether we have subscriptions until the publishers
// and channels have loaded.
const loaded = React.useMemo(() => !!Object.values(publishers).length && !!Object.values(channels).length, [publishers, channels])

// This is a bit of an interesting |useMemo| - we only want it to be updated
// when the feed changes so as to not break the case where:
// 1. The user has no feeds (we show the NoFeeds card)
// 2. The user subscribes to a feed (we should still show the NoFeeds card,
// not the "Empty Feed")
// To achieve this, |hasSubscriptions| is only updated when the feed changes,
// or the opt-in status is changed.
const hasSubscriptions = React.useMemo(() => !loaded
|| Object.values(publishers).some(isPublisherEnabled)
|| Object.values(channels).some(c => c.subscribedLocales.length), [feedV2, loaded])

const ref = React.useRef<HTMLDivElement>()

Expand All @@ -92,15 +103,6 @@ export default function FeedV2() {
ref.current?.scrollIntoView()
}, [feedV2?.items])

// For some reason |createGlobalStyle| doesn't seem to work in Brave Core
// To get the background blur effect looking nice, we need to set the body
// background to black - unfortunately we can't do this in root HTML file
// because we want to avoid the background flash effect.
React.useEffect(() => {
// Note: This is always black because this doesn't support light mode.
document.body.style.backgroundColor = 'black';
}, [])

return <Root ref={ref as any} data-theme="dark">
<SidebarContainer>
<FeedNavigation />
Expand All @@ -109,16 +111,16 @@ export default function FeedV2() {
{feedV2UpdatesAvailable && <LoadNewContentButton onClick={refreshFeedV2}>
{getLocale('braveNewsNewContentAvailable')}
</LoadNewContentButton>}
<Feed feed={feedV2} />
<Feed feed={feedV2} hasSubscriptions={hasSubscriptions} />
</Flex>

<ButtonsContainer>
<NewsButton fab kind='outline' onClick={() => setCustomizePage('news')} title={getLocale('braveNewsCustomizeFeed')}>
<SettingsButton fab kind='outline' onClick={() => setCustomizePage('news')} title={getLocale('braveNewsCustomizeFeed')}>
<Icon name="settings" />
</NewsButton>
<NewsButton fab isLoading={!feedV2} kind='outline' title={getLocale('braveNewsRefreshFeed')} onClick={() => {
</SettingsButton>
<SettingsButton fab isLoading={!feedV2} kind='outline' title={getLocale('braveNewsRefreshFeed')} onClick={() => {
refreshFeedV2()
}}><Icon name="refresh" /></NewsButton>
}}><Icon name="refresh" /></SettingsButton>
</ButtonsContainer>
</Root>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getLocale, getLocaleWithTag } from '../../../../../common/locale'
import * as Card from '../cardIntro'
import BraveNewsLogoUrl from '../braveNewsLogo.svg'
import { CardButton, TertiaryButton } from '../default'
import { NEWS_FEED_CLASS } from '../../../../../brave_news/browser/resources/Feed'

type Props = {
onOptIn: () => unknown
Expand All @@ -19,7 +20,7 @@ const descriptionTwoTextParts = getLocaleWithTag('braveNewsIntroDescriptionTwo')
export default function IntroCard (props: Props) {
const introElementRef = React.useRef(null)
return (
<Card.Intro ref={introElementRef}>
<Card.Intro ref={introElementRef} className={NEWS_FEED_CLASS}>
<Card.Image src={BraveNewsLogoUrl} />
<Card.Title>{getLocale('braveNewsIntroTitle')}</Card.Title>
<div>
Expand Down
6 changes: 6 additions & 0 deletions components/brave_news/browser/brave_news_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ void BraveNewsController::SetChannelSubscribed(
SetChannelSubscribedCallback callback) {
auto result =
channels_controller_.SetChannelSubscribed(locale, channel_id, subscribed);

// When channels are changed, see if it affects the feed.
if (MaybeInitFeedV2()) {
feed_v2_builder_->RecheckFeedHash();
}

std::move(callback).Run(std::move(result));
}

Expand Down
5 changes: 5 additions & 0 deletions components/brave_news/browser/channels_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ bool ChannelsController::GetChannelSubscribed(const std::string& locale,
}

void ChannelsController::OnPublishersUpdated(PublishersController* controller) {
// Don't notify updates until the pref is initialized.
if (!prefs_->HasPrefPath(prefs::kBraveNewsChannels)) {
return;
}

GetAllChannels(base::BindOnce(
[](ChannelsController* controller, Channels channels) {
auto event = mojom::ChannelsEvent::New();
Expand Down
17 changes: 14 additions & 3 deletions components/brave_news/browser/feed_v2_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,16 @@ std::string GetFeedHash(const Channels& channels,
}

for (const auto& [id, publisher] : publishers) {
if (publisher->user_enabled_status == mojom::UserEnabled::ENABLED) {
if (publisher->user_enabled_status == mojom::UserEnabled::ENABLED ||
publisher->type == mojom::PublisherType::DIRECT_SOURCE) {
hash_items.push_back(id);
}

// Disabling a publisher should also change the hash, as it will affect what
// articles can be shown.
if (publisher->user_enabled_status == mojom::UserEnabled::DISABLED) {
hash_items.push_back(id + "_disabled");
}
}

for (const auto& [region, etag] : etags) {
Expand Down Expand Up @@ -918,8 +925,7 @@ void FeedV2Builder::GetSignals(GetSignalsCallback callback) {
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void FeedV2Builder::OnPublishersUpdated(
PublishersController* publishers_controller) {
void FeedV2Builder::RecheckFeedHash() {
const auto& publishers = publishers_controller_->GetLastPublishers();
auto channels =
channels_controller_->GetChannelsFromPublishers(publishers, &*prefs_);
Expand All @@ -929,6 +935,11 @@ void FeedV2Builder::OnPublishersUpdated(
}
}

void FeedV2Builder::OnPublishersUpdated(
PublishersController* publishers_controller) {
RecheckFeedHash();
}

void FeedV2Builder::UpdateData(UpdateSettings settings,
base::OnceCallback<void()> callback) {
if (current_update_) {
Expand Down
2 changes: 2 additions & 0 deletions components/brave_news/browser/feed_v2_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class FeedV2Builder : public PublishersController::Observer {

void GetSignals(GetSignalsCallback callback);

void RecheckFeedHash();

// PublishersController::Observer:
void OnPublishersUpdated(PublishersController* controller) override;

Expand Down
29 changes: 17 additions & 12 deletions components/brave_news/browser/resources/Feed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

import { spacing } from "@brave/leo/tokens/css";
import { FeedItemV2, FeedV2 } from "gen/brave/components/brave_news/common/brave_news.mojom.m";
import * as React from 'react';
import styled from "styled-components";
import Advert from "./feed/Ad";
import Article from "./feed/Article";
import CaughtUp from "./feed/CaughtUp";
import Cluster from "./feed/Cluster";
import Discover from "./feed/Discover";
import HeroArticle from "./feed/Hero";
import { getHistoryValue, setHistoryState } from "./shared/history";
import NoArticles from "./feed/NoArticles";
import LoadingCard from "./feed/LoadingCard";
import { spacing } from "@brave/leo/tokens/css";
import CaughtUp from "./feed/CaughtUp";
import NoArticles from "./feed/NoArticles";
import NoFeeds from "./feed/NoFeeds";
import { getHistoryValue, setHistoryState } from "./shared/history";

// Restoring scroll position is complicated - we have two available strategies:
// 1. Scroll to the same position - as long as the window hasn't been resized,
Expand Down Expand Up @@ -44,6 +45,7 @@ const FeedContainer = styled.div`

interface Props {
feed: FeedV2 | undefined;
hasSubscriptions: boolean;
}

const getKey = (feedItem: FeedItemV2, index: number): React.Key => {
Expand Down Expand Up @@ -74,7 +76,7 @@ const saveScrollPos = (itemId: React.Key) => () => {
})
}

export default function Component({ feed }: Props) {
export default function Component({ feed, hasSubscriptions }: Props) {
const [cardCount, setCardCount] = React.useState(getHistoryValue(HISTORY_CARD_COUNT, PAGE_SIZE));

// Store the number of cards we've loaded in history - otherwise when we
Expand Down Expand Up @@ -144,12 +146,15 @@ export default function Component({ feed }: Props) {
}, [cardCount, feed?.items])

return <FeedContainer className={NEWS_FEED_CLASS}>
{feed
? !feed.items.length ? <NoArticles />
: <>
{cards}
<CaughtUp />
</>
: <LoadingCard />}
{!hasSubscriptions
? <NoFeeds />
: feed
? !feed.items.length
? <NoArticles />
: <>
{cards}
<CaughtUp />
</>
: <LoadingCard />}
</FeedContainer>
}
2 changes: 1 addition & 1 deletion components/brave_news/browser/resources/FeedPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ export default function FeedPage() {
const { feedV2 } = useBraveNews()
return <Container>
<h2>The Feed ({feedV2?.items.length} items. Truncated at {truncate})</h2>
<Feed feed={feedV2} />
<Feed feed={feedV2} hasSubscriptions />
</Container>
}
14 changes: 14 additions & 0 deletions components/brave_news/browser/resources/NewsButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
import Button from '@brave/leo/react/button'
import styled from 'styled-components'

export default styled(Button)`
--leo-button-color: var(--bn-glass-10);
border-radius: 20px;
overflow: hidden;
backdrop-filter: brightness(0.8) blur(32px);
`
43 changes: 26 additions & 17 deletions components/brave_news/browser/resources/Peek.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,35 @@ const scrollToNews = () => {
}

export default function Peek() {
const { feedV2 } = useBraveNews()
const { feedV2, isShowOnNTPPrefEnabled, isOptInPrefEnabled } = useBraveNews()
const top = feedV2?.items?.find(a => a.article || a.hero)
const data = (top?.hero ?? top?.article)?.data
const imageUrl = useUnpaddedImageUrl(data?.image.paddedImageUrl?.url ?? data?.image.imageUrl?.url, undefined, true)

if (!data) return null

return <Container>
<NewsButton onClick={scrollToNews}>
<Icon name='news-default' />
{getLocale('braveNewsNewsPeek')}
<Icon name='carat-down' />
</NewsButton>
<PeekingCard onClick={scrollToNews}>
<div>
<MetaInfo article={data} />
<Title>{data.title}</Title>
</div>
<SmallImage src={imageUrl} />
</PeekingCard>
</Container>
// For some reason |createGlobalStyle| doesn't seem to work in Brave Core
// To get the background blur effect looking nice, we need to set the body
// background to black - unfortunately we can't do this in root HTML file
// because we want to avoid the background flash effect.
React.useEffect(() => {
// Note: This is always black because this doesn't support light mode.
document.body.style.backgroundColor = 'black';
}, [])

return isShowOnNTPPrefEnabled
? <Container>
{(!isOptInPrefEnabled || data) && <NewsButton onClick={scrollToNews}>
<Icon name='news-default' />
{getLocale('braveNewsNewsPeek')}
<Icon name='carat-down' />
</NewsButton>}
{data && <PeekingCard onClick={scrollToNews}>
<div>
<MetaInfo article={data} />
<Title>{data.title}</Title>
</div>
<SmallImage src={imageUrl} />
</PeekingCard>}
</Container>
: null
}

20 changes: 10 additions & 10 deletions components/brave_news/browser/resources/feed/NoArticles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,24 @@ export default function NoArticles() {
<rect x="9.5" y="75" width="64" height="4" rx="2" fill="url(#paint4_linear_5051_10730)" />
<defs>
<linearGradient id="paint0_linear_5051_10730" x1="49.5" y1="1" x2="49.5" y2="78.5" gradientUnits="userSpaceOnUse">
<stop stop-color="white" stop-opacity="0.15" />
<stop offset="1" stop-color="white" stop-opacity="0" />
<stop stopColor="white" stopOpacity="0.15" />
<stop offset="1" stopColor="white" stopOpacity="0" />
</linearGradient>
<linearGradient id="paint1_linear_5051_10730" x1="9.5" y1="33" x2="89.5" y2="33" gradientUnits="userSpaceOnUse">
<stop stop-color="white" stop-opacity="0.03" />
<stop offset="1" stop-color="white" stop-opacity="0.2" />
<stop stopColor="white" stopOpacity="0.03" />
<stop offset="1" stopColor="white" stopOpacity="0.2" />
</linearGradient>
<linearGradient id="paint2_linear_5051_10730" x1="9.5" y1="71.0426" x2="89.5" y2="71.0425" gradientUnits="userSpaceOnUse">
<stop stop-color="white" stop-opacity="0.03" />
<stop offset="1" stop-color="white" stop-opacity="0.2" />
<stop stopColor="white" stopOpacity="0.03" />
<stop offset="1" stopColor="white" stopOpacity="0.2" />
</linearGradient>
<linearGradient id="paint3_linear_5051_10730" x1="9.5" y1="60.0213" x2="57.5" y2="60.0213" gradientUnits="userSpaceOnUse">
<stop stop-color="white" stop-opacity="0.03" />
<stop offset="1" stop-color="white" stop-opacity="0.2" />
<stop stopColor="white" stopOpacity="0.03" />
<stop offset="1" stopColor="white" stopOpacity="0.2" />
</linearGradient>
<linearGradient id="paint4_linear_5051_10730" x1="9.5" y1="77.0426" x2="73.5" y2="77.0425" gradientUnits="userSpaceOnUse">
<stop stop-color="white" stop-opacity="0.03" />
<stop offset="1" stop-color="white" stop-opacity="0.2" />
<stop stopColor="white" stopOpacity="0.03" />
<stop offset="1" stopColor="white" stopOpacity="0.2" />
</linearGradient>
</defs>
</svg>
Expand Down
Loading

0 comments on commit 65f50d3

Please sign in to comment.