-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from project-slippi/rewrite/homepage
Populate homepage
- Loading branch information
Showing
26 changed files
with
2,270 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { fetch } from "cross-fetch"; | ||
import log from "electron-log"; | ||
|
||
const SECOND = 1000; | ||
const MINUTE = 60 * SECOND; | ||
|
||
const EXPIRES_IN = 10 * MINUTE; | ||
|
||
// Let's cache our Github responses so we don't exceed the 60 requests/hour rate limit | ||
interface ResponseCacheEntry { | ||
time: number; | ||
response: any; | ||
} | ||
|
||
const githubResponseCache: Record<string, ResponseCacheEntry> = {}; | ||
|
||
export async function getLatestRelease(owner: string, repo: string): Promise<any> { | ||
// We can re-use api calls by returning the first item in all releases | ||
// since it already returns the newest release first. | ||
const data = await getAllReleases(owner, repo); | ||
if (data.length > 0) { | ||
return data[0]; | ||
} | ||
throw new Error(`No releases found for ${owner}/${repo}`); | ||
} | ||
|
||
export async function getAllReleases(owner: string, repo: string): Promise<any> { | ||
const url = `https://api.github.com/repos/${owner}/${repo}/releases`; | ||
const data = await cachedFetch(url); | ||
return data; | ||
} | ||
|
||
async function cachedFetch(url: string): Promise<any> { | ||
log.debug(`Checking cache for: ${url}`); | ||
const cachedResponse = githubResponseCache[url]; | ||
if (cachedResponse) { | ||
// Check if the cache has expired | ||
const elapsedMs = Date.now() - cachedResponse.time; | ||
if (elapsedMs <= EXPIRES_IN) { | ||
log.debug(`Cache hit. Returning cached response.`); | ||
return cachedResponse.response; | ||
} else { | ||
log.debug(`Cache expired. Refetching data...`); | ||
} | ||
} | ||
|
||
log.debug(`Fetching: ${url}`); | ||
// Fetch the data | ||
const res = await fetch(url); | ||
const data = await res.json(); | ||
|
||
// Cache the data | ||
githubResponseCache[url] = { | ||
response: data, | ||
time: Date.now(), | ||
}; | ||
|
||
return data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { NewsItem } from "common/types"; | ||
import mediumJSONFeed from "medium-json-feed"; | ||
|
||
import { getAllReleases } from "./github"; | ||
|
||
export async function fetchNewsFeed(): Promise<NewsItem[]> { | ||
const mediumNews = fetchMediumNews(); | ||
const githubNews = fetchGithubReleaseNews(["Ishiiruka", "slippi-desktop-app"]); | ||
const allNews = (await Promise.all([mediumNews, githubNews])).flat(); | ||
return allNews.sort((a, b) => { | ||
// Sort all news item by reverse chronological order | ||
const aDate = new Date(a.publishedAt).getTime(); | ||
const bDate = new Date(b.publishedAt).getTime(); | ||
return bDate - aDate; | ||
}); | ||
} | ||
|
||
async function fetchMediumNews(): Promise<NewsItem[]> { | ||
const response = await mediumJSONFeed("project-slippi"); | ||
if (!response || response.status !== 200) { | ||
throw new Error("Error fetching Medium feed"); | ||
} | ||
|
||
const result = response.response; | ||
return result.map((post: any) => { | ||
const publishedAt = new Date(post.firstPublishedAt).toISOString(); | ||
return { | ||
id: `medium-${post.id}`, | ||
imageUrl: `https://cdn-images-1.medium.com/${post.virtuals.previewImage.imageId}`, | ||
title: post.title, | ||
subtitle: post.virtuals.subtitle, | ||
publishedAt, | ||
permalink: `https://medium.com/project-slippi/${post.uniqueSlug}`, | ||
} as NewsItem; | ||
}); | ||
} | ||
|
||
async function fetchGithubReleaseNews(repos: string[]): Promise<NewsItem[]> { | ||
const allReleases = await Promise.all( | ||
repos.map(async (repo) => { | ||
const releases = await getAllReleases("project-slippi", repo); | ||
return releases.map((release: any) => { | ||
return { | ||
id: `gh-${repo}-${release.id}`, | ||
title: `[${repo}] ${release.name}`, | ||
body: release.body, | ||
publishedAt: release.published_at, | ||
permalink: release.html_url, | ||
} as NewsItem; | ||
}); | ||
}), | ||
); | ||
|
||
return allReleases.flat(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/*** | ||
* This component automatically attaches target="_blank" and rel="noopener noreferrer" to links | ||
*/ | ||
|
||
import React from "react"; | ||
|
||
export const ExternalLink: React.FC<React.HTMLProps<HTMLAnchorElement>> = (props) => { | ||
return ( | ||
<a {...props} target="_blank" rel="noopener noreferrer"> | ||
{props.children} | ||
</a> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.