Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds several features using the Github API. #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
100 changes: 91 additions & 9 deletions src/Gist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,65 @@ import { RouteComponentProps } from 'react-router-dom'

import useFetch from './useFetch'

interface GistReponseFile extends Object {
filename: string;
type: string;
language: string;
raw_url: string;
truncated: boolean,
content: string;
}

interface GistReponse extends Object {
id: string;
html_url: string;
files: {
[key: string]: GistReponseFile;
};
public: boolean;
created_at: string;
updated_at: string;
description: string;
comments: number;
comments_url: string;
owner: {
login: string;
avatar_url: string;
html_url: string;
};
truncated: boolean;
}

function useFetchGist(user:string, id: string) {
const url = `https://gist.githubusercontent.com/${user}/${id}/raw`
return useFetch(url)
}

const url = `https://api.github.com/gists/${id}`
const response = useFetch(url) as unknown as GistReponse

if (typeof response == 'object') {
// @TODO: Verify that response.owner.login.toLowerCase() === user.toLowerCase()
const files : Array<GistReponseFile> = Object.values(response!.files)
const subjects = Object.entries(files).filter(([key, value]) => {
// @TODO: Replace with lookup function to support more languages
return value.language == 'Markdown'
}
);

// @TODO: If nothing found, grab first file (as previously done)
const subject = Object.values(subjects)[0][1]
console.log(subject)

return {
comments: response.comments,
comments_url: response.comments_url,
created_at: response.created_at,
image: response.owner.avatar_url,
public: response.public,
raw: subject.content,
title: response.description,
updated_at: response.updated_at,
}
}
}

type GistRouteParams = {
user: string
Expand All @@ -21,21 +76,48 @@ interface GistProps extends RouteComponentProps<GistRouteParams> {}

const Gist = (props: GistProps) => {
const { user, id } = props.match.params
const raw = useFetchGist(user, id)
const content = raw
? <Markdown raw={raw}/>

const response = useFetchGist(user, id)

const content = response
? <Markdown raw={response.raw}/>
: <p>Loading…</p>

return (
<article className='mw7-ns center ph2 mv4'>
<Helmet>
<title>Gist.io • @{user}/{id}</title>
<title>Gist.io • @{user} • {response ? response.title : ''}</title>
<meta name="robots" content={response && response.public ? 'index' :'noindex'} />
</Helmet>
<header className='mv4 f6'>
<img
src={response ? `${response.image}` : 'https://avatars.githubusercontent.com/u/0'}
className="br-100 ba h3 w3 dib"
alt="avatar"
/>
<a href={`https://github.com/${user}/`} target='_blank'>@&thinsp;{user}</a>&thinsp;/&thinsp;<a href={`https://gist.github.com/${user}/${id}`} target='_blank'>{id}</a>
-
<span>
{response ? new Date(response.created_at).toISOString().slice(0, 10) : ''}
</span>
<span>
&nbsp;(updated {response ? new Date(response.updated_at).toISOString().slice(0, 10) : ''})
</span>
</header>
<div className='content f3-ns f4'>
{content}
{content}
</div>

{response ?
<aside>
<a href={response ? response.comments_url : ''}
className="ba bg-animate bg-white br-pill dib hover-bg-black hover-white mid-gray no-underline ph3 pv2 "
>
There are {response ? response.comments:'no'} comments
</a>
</aside>
: ''}

<footer>
<div className='pt3 mt5 bt b--light-gray gray i'>
<a href='/'>gist.io</a> &middot; writing for hackers &middot; zero setup &middot; publish in seconds
Expand All @@ -44,5 +126,5 @@ const Gist = (props: GistProps) => {
</article>
)
}

export default Gist

8 changes: 4 additions & 4 deletions src/useFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ import React, { useState, useEffect } from "react"

const useFetch = (url: string, defaultData?: string) => {
const [data, updateData] = useState(defaultData)

useEffect(() => {
let stale = false
async function fetchData() {
if (!url) {
return
}
const resp = await fetch(url)
const text = await resp.text()
const text = await resp.json()
if (!stale) {
updateData(text)
updateData(text)
}
}
fetchData()
return () => {
stale = true
}
}, [ url ])

return data
}

Expand Down