This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 174
/
hackernews.tsx
70 lines (58 loc) · 2.06 KB
/
hackernews.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// ReactQL Hacker News GraphQL example
// ----------------------------------------------------------------------------
// IMPORTS
/* NPM */
import React from "react";
// Emotion styled component
import styled from "@emotion/styled";
/* Local */
// Query to get top stories from HackerNews
import { GetHackerNewsTopStoriesComponent } from "@/graphql";
// ----------------------------------------------------------------------------
// Unstyled Emotion parent block, to avoid repeating <style> tags
// on child elements -- see https://github.com/emotion-js/emotion/issues/1061
const List = styled.ul``;
// Style the list item so it overrides the default font
const Story = styled("li")`
font-size: 16px;
a:hover {
/* shows an example of how we can use themes */
color: orange;
}
`;
// Execute the GraphQL query. With SSR, the server will await the returned
// result before rendering out the initial HTML. On the browser, it will re-use
// whatever the server has sent it - or, if it's a client-navigated route that
// doesn't already have data from the server -- it'll display a loading message
// while the data is being retrieved
export const HackerNews: React.FunctionComponent = () => (
<GetHackerNewsTopStoriesComponent>
{({ data, loading, error }) => {
// Any errors? Say so!
if (error) {
return <h1>Error retrieving news stories! — {error}</h1>;
}
// If the data is still loading, return with a basic
// message to alert the user
if (loading) {
return <h1>Loading Hacker News stories...</h1>;
}
// Otherwise, we have data to work with... map over it with a
// bullet-point list
return (
<>
<h3>Top stories from Hacker News</h3>
<List>
{data!.hn!.topStories!.map(story => (
<Story key={story!.id!}>
<a href={story!.url!} target="_blank">
{story!.title}
</a>
</Story>
))}
</List>
</>
);
}}
</GetHackerNewsTopStoriesComponent>
);