// Theirs
import React from 'react'
import Link from 'next/link'
import Router from 'next/router'
import formatDistanceToNow from 'date-fns/formatDistanceToNow'
import { useAsyncCallback } from 'actionsack'
import Button from '../components/Button'
import LoginButton from '../components/LoginButton'
import ConfirmButton from '../components/ConfirmButton'
import { useAuth } from '../components/AuthContext'
import { useAPI } from '../components/ApiContext'
import { MetaLinks } from '../components/Meta'
import Carbon from '../components/Carbon'
import { COLORS, DEFAULT_SETTINGS } from '../lib/constants'
// Ours
import Page from '../components/Page'
function correctTimestamp(n) {
if (n < 9e12) {
return n * 1000
}
return n
}
function Snippet(props) {
const config = { ...DEFAULT_SETTINGS, ...props, fontSize: '2px', windowControls: false }
return (
{props.code}
{props.title || props.id}
Edited {formatDistanceToNow(correctTimestamp(props.updatedAt), { addSuffix: true })}
Delete
)
}
function ActionButton(props) {
return (
)
}
function useOnMount() {
const [mounted, mount] = React.useState(false)
React.useEffect(() => {
mount(true)
}, [])
return mounted
}
function SnippetsPage() {
const user = useAuth()
const api = useAPI()
const [snippets, setSnippets] = React.useState([])
const [page, setPage] = React.useState(0)
const mounted = useOnMount()
const [loadMore, { loading, data: previousRes }] = useAsyncCallback(api.snippet.list)
React.useEffect(() => {
if (user) {
loadMore(page).then(newSnippets => setSnippets(curr => curr.concat(newSnippets)))
}
}, [loadMore, page, user])
function deleteSnippet(id) {
return api.snippet.delete(id).then(() => setSnippets(curr => curr.filter(s => s.id !== id)))
}
if (!user) {
return
}
return (
{snippets.map(snippet => (
))}
{snippets.length && previousRes && previousRes.length < 10 ? null : (
{
if (snippets.length) return setPage(p => p + 1)
Router.push('/')
}}
>
{loading ? 'Loading...' : !snippets.length ? 'Create snippet +' : 'Load more +'}
)}
)
}
export default () => (
)