Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
68 changes: 68 additions & 0 deletions react-app/src/components/feeds/Item.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
@import "../../styles/media";
@import "../../styles/theme_variables";

p {
margin: 2px 0;

@media #{$mobile-only} {
margin-bottom: 5px;
margin-top: 0;
}
}

a {
cursor: pointer;
text-decoration: none;
}

.title {
font-size: 16px;
font-family: Verdana, Geneva, sans-serif;
}

.subtext-laptop {
font-size: 12px;
font-weight: bold;
letter-spacing: 0.5px;

a {
&:hover {
text-decoration: underline;
};
}
@media #{$mobile-only} {
display: none;
}
}

.subtext-palm {
font-size: 13px;
font-weight: bold;
letter-spacing: 0.5px;

a {
&:hover {
text-decoration: underline;
};
}

.details {
margin-top: 5px;

.right {
float: right;
}
}
@media #{$laptop-only} {
display: none;
}
}

.domain {
color: #696969;
letter-spacing: 0.5px;
}

.item-details {
padding: 10px;
}
64 changes: 64 additions & 0 deletions react-app/src/components/feeds/Item.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { describe, expect, it } from 'vitest';

import { SettingsProvider } from '../../context/SettingsContext';
import type { Story } from '../../models/story';
import Item, { hasUrl } from './Item';

const story: Story = {
id: 1,
title: 'A linked story',
points: 42,
user: 'pg',
time: 0,
time_ago: 0,
type: 'story',
url: 'https://example.com/post',
domain: 'example.com',
comments_count: 3,
};

function renderItem(item: Story) {
return render(
<MemoryRouter>
<SettingsProvider>
<Item item={item} />
</SettingsProvider>
</MemoryRouter>
);
}

describe('Item', () => {
it('detects external urls', () => {
expect(hasUrl(story)).toBe(true);
expect(hasUrl({ ...story, url: 'item?id=1' })).toBe(false);
expect(hasUrl({ ...story, url: undefined })).toBe(false);
});

it('links external stories to their url and shows the domain', () => {
renderItem(story);

const link = screen.getByRole('link', { name: 'A linked story' });
expect(link).toHaveAttribute('href', 'https://example.com/post');
expect(link).not.toHaveAttribute('target');
expect(screen.getByText('(example.com)')).toBeInTheDocument();
expect(screen.getAllByText(/3 comments/).length).toBe(2);
});

it('links self posts to the item page', () => {
renderItem({ ...story, url: undefined, domain: undefined });

expect(screen.getByRole('link', { name: 'A linked story' })).toHaveAttribute(
'href',
'/item/1'
);
});

it('hides points and comments for jobs', () => {
renderItem({ ...story, type: 'job' });

expect(screen.queryByText('pg')).not.toBeInTheDocument();
expect(screen.queryByText(/comments/)).not.toBeInTheDocument();
});
});
94 changes: 94 additions & 0 deletions react-app/src/components/feeds/Item.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Link, NavLink } from 'react-router-dom';

import { useSettings } from '../../context/SettingsContext';
import type { Story } from '../../models/story';
import { formatCommentCount } from '../../utils/format-comment-count';

import './Item.scss';

export interface ItemProps {
item: Story;
}

export function hasUrl(item: Story): boolean {
return item.url?.startsWith('http') ?? false;
}

function activeClass({ isActive }: { isActive: boolean }) {
return isActive ? 'active' : '';
}

export default function Item({ item }: ItemProps) {
const { settings } = useSettings();
const titleStyle = { fontSize: `${settings.titleFontSize}px` };
const newTabProps = settings.openLinkInNewTab
? { target: '_blank', rel: 'noopener' }
: {};

return (
<div style={{ marginBottom: `${settings.listSpacing}px` }}>
{hasUrl(item) ? (
<p>
<a className="title" style={titleStyle} href={item.url} {...newTabProps}>
{item.title}
</a>
{item.domain && <span className="domain">({item.domain})</span>}
</p>
) : (
<p>
<Link className="title" style={titleStyle} to={`/item/${item.id}`}>
{item.title}
</Link>
</p>
)}
<div className="subtext-palm">
{item.type !== 'job' && (
<div className="details">
<span>
<NavLink to={`/user/${item.user}`} className={activeClass}>
{item.user}
</NavLink>
</span>
<span className="right">{item.points} ★</span>
</div>
)}
<div className="details">
{item.time_ago}
{item.type !== 'job' && (
<NavLink
to={`/item/${item.id}`}
className={({ isActive }) =>
isActive ? 'comment-number active' : 'comment-number'
}
>
{' '}
• {formatCommentCount(item.comments_count)}
</NavLink>
)}
</div>
</div>
<div className="subtext-laptop">
{item.type !== 'job' && (
<span>
{item.points} points by{' '}
<NavLink to={`/user/${item.user}`} className={activeClass}>
{item.user}
</NavLink>
</span>
)}
<span className={item.type !== 'job' ? 'item-details' : undefined}>
{item.time_ago}
{item.type !== 'job' && (
<span>
{' '}
|{' '}
<NavLink to={`/item/${item.id}`} className={activeClass}>
{formatCommentCount(item.comments_count)}
</NavLink>
</span>
)}
</span>
</div>
</div>
);
}
108 changes: 108 additions & 0 deletions react-app/src/pages/Feed.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
@import "../styles/media";
@import "../styles/theme_variables";

a {
text-decoration: none;
font-weight: bold;

&:hover {
text-decoration: underline;
};
}

ol {
Comment on lines +4 to +13

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔍 Ported styles are now global

Angular previously scoped these selectors per component. Audit a, p, ol, .nav, .title, and .details before the app shell mounts feeds.

Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Noted. Global SCSS is the established convention in react-app/ (_themes.scss targets global class names like .wrapper/.nav, so CSS Modules would break theming), and these rules are a verbatim port of feed.component.scss. The one real collision found so far (.name vs the header rule) is fixed in 3967066; a broader audit of the bare a/p/ol selectors is best done once the router mounts all views together.

padding: 0 40px;
margin: 0;

@media #{$mobile-only} {
box-sizing: border-box;
list-style: none;
padding: 0 10px;
}

li {
position: relative;
-webkit-transition: background-color .2s ease;
transition: background-color .2s ease;
}
}

.list-margin {
@media #{$mobile-only} {
margin-top: 55px;
}
}

.main-content {
position: relative;
width: 100%;
min-height: 100vh;
-webkit-transition: opacity .2s ease;
transition: opacity .2s ease;
box-sizing: border-box;
padding: 8px 0;
z-index: 0;
}

.post {
padding: 10px 0 10px 5px;
transition: background-color 0.2s ease;
border-bottom: 1px solid #CECECB;

.itemNum {
color: #696969;
position: absolute;
width: 30px;
text-align: right;
left: 0;
top: 4px;
}
}

.item-block {
display: block;
}


.nav {
padding: 10px 40px;
margin-top: 10px;
font-size: 17px;

a {
@media #{$mobile-only} {
text-decoration: none;
}
}

@media #{$mobile-only} {
margin: 20px 0;
text-align: center;
padding: 10px 80px;
height: 20px;
}

.prev {
padding-right: 20px;

@media #{$mobile-only} {
float: left;
padding-right: 0;
}
}

.more {
@media #{$mobile-only} {
float: right;
}
}
}

.job-header {
font-size: 15px;
padding: 0 40px 10px;

@media #{$mobile-only} {
padding: 60px 15px 25px 15px;
}
}
Loading