Hacker News
This example shows how to show a RSS feed as a List.
The source code of the example can be found here. You can install it here.
Who doesn't like a good morning read on Hacker News with a warm coffee?! In this example, we create a simple list with the top stories on the frontpage.
Example: Read frontpage of Hacker News

Load top stories

First, let's get the latest top stories. For this we use a RSS feed:
1
import { List } from "@raycast/api";
2
import { useEffect, useState } from "react";
3
import Parser from "rss-parser";
4
5
const parser = new Parser();
6
7
interface State {
8
items?: Parser.Item[];
9
error?: Error;
10
}
11
12
export default function Command() {
13
const [state, setState] = useState<State>({});
14
15
useEffect(() => {
16
async function fetchStories() {
17
try {
18
const feed = await parser.parseURL("https://hnrss.org/frontpage?description=0&count=25");
19
setState({ items: feed.items });
20
} catch (error) {
21
setState({ error: error instanceof Error ? error : new Error("Something went wrong") });
22
}
23
}
24
25
fetchStories();
26
}, []);
27
28
console.log(state.items) // Prints stories
29
30
return <List isLoading={!state.items && !state.error} />;
31
}
32
Copied!
Breaking this down:
    We use a third-party dependency to parse the RSS feed and intially the parser.
    We define our command state as a TypeScript interface.
    We use React's useEffect hook to parse the RSS feed after the command did mount.
    We print the top stories to the console.
    We render a list and show the loading indicator as long as we load the stories.

Render stories

Now that we got the data from Hacker News, we want to render the stories. For this, we create a new React component and a few helper functions that render a story:
1
function StoryListItem(props: { item: Parser.Item; index: number }) {
2
const icon = getIcon(props.index + 1);
3
const points = getPoints(props.item);
4
const comments = getComments(props.item);
5
6
return (
7
<List.Item
8
icon={icon}
9
title={props.item.title ?? "No title"}
10
subtitle={props.item.creator}
11
accessoryTitle={`👍 ${points} 💬 ${comments}`}
12
/>
13
);
14
}
15
16
const iconToEmojiMap = new Map<number, string>([
17
[1, "1️⃣"],
18
[2, "2️⃣"],
19
[3, "3️⃣"],
20
[4, "4️⃣"],
21
[5, "5️⃣"],
22
[6, "6️⃣"],
23
[7, "7️⃣"],
24
[8, "8️⃣"],
25
[9, "9️⃣"],
26
[10, "🔟"],
27
]);
28
29
function getIcon(index: number) {
30
return iconToEmojiMap.get(index) ?? "⏺";
31
}
32
33
function getPoints(item: Parser.Item) {
34
const matches = item.contentSnippet?.match(/(?<=Points:\s*)(\d+)/g);
35
return matches?.[0];
36
}
37
38
function getComments(item: Parser.Item) {
39
const matches = item.contentSnippet?.match(/(?<=Comments:\s*)(\d+)/g);
40
return matches?.[0];
41
}
42
Copied!
To give the list item a nice look, we use a simple number emoji as icon, add the creator's name as subtitle and the points and comments as accessory title. Now we can render the <StoryListItem>:
1
export default function Command() {
2
const [state, setState] = useState<State>({});
3
4
// ...
5
6
return (
7
<List isLoading={!state.items && !state.error}>
8
{state.items?.map((item, index) => (
9
<StoryListItem key={item.guid} item={item} index={index} />
10
))}
11
</List>
12
);
13
}
Copied!

Add actions

When we select a story in the list, we want to be able to open it in the browser and also copy it's link so that we can share it in our watercooler Slack channel. For this, we create a new React Component:
1
function Actions(props: { item: Parser.Item }) {
2
return (
3
<ActionPanel title={props.item.title}>
4
<ActionPanel.Section>
5
{props.item.link && <OpenInBrowserAction url={props.item.link} />}
6
{props.item.guid && <OpenInBrowserAction url={props.item.guid} title="Open Comments in Browser" />}
7
</ActionPanel.Section>
8
<ActionPanel.Section>
9
{props.item.link && (
10
<CopyToClipboardAction
11
content={props.item.link}
12
title="Copy Link"
13
shortcut={{ modifiers: ["cmd"], key: "." }}
14
/>
15
)}
16
</ActionPanel.Section>
17
</ActionPanel>
18
);
19
}
Copied!
The component takes a story and renders an <ActionPanel> with our required actions. We add the actions to the <StoryListItem>:
1
function StoryListItem(props: { item: Parser.Item; index: number }) {
2
// ...
3
4
return (
5
<List.Item
6
icon={icon}
7
title={props.item.title ?? "No title"}
8
subtitle={props.item.creator}
9
accessoryTitle={`👍 ${points} 💬 ${comments}`}
10
// Wire up actions
11
actions={<Actions item={props.item} />}
12
/>
13
);
14
}
Copied!

Handle errors

Lastly, we want to be a good citizen and also handle errors appropriately to a gurantee a smooth experience. We'll show a toast whenever our network request fails:
1
export default function Command() {
2
const [state, setState] = useState<State>({});
3
4
// ...
5
6
if (state.error) {
7
showToast(ToastStyle.Failure, "Failed loading stories", state.error.message);
8
}
9
10
// ...
11
}
Copied!

Wrapping up

That's it, you have a working extension to read the fronpage of Hacker News. As next steps, you can add another command to show the jobs feed or add an action to copy a Markdown formatted link.
Last modified 7d ago