-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathlocal-page.jsx
98 lines (87 loc) · 2.82 KB
/
local-page.jsx
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import React from 'react'
import PropTypes from 'prop-types'
import ReactDOM from 'react-dom'
import { Icon, Image } from 'semantic-ui-react'
import { getPage, getPageHtml, deletePage } from 'src/local-storage'
import { SaveAsButton, DeleteButton } from 'src/common-components'
import shortUrl from 'src/util/short-url'
import niceTime from 'src/util/nice-time'
import { isStoredInternally } from '.'
import ContentFrame from './ContentFrame'
const SnapshotChrome = ({ children }) => (
<div id='bar'>
<Image
title='WebMemex'
src='assets/webmemex-32.png'
as='a'
href='/overview.html'
wrapped
/>
{children}
</div>
)
SnapshotChrome.propTypes = {
children: PropTypes.node,
}
async function showPage(pageId) {
const containerElement = document.getElementById('app')
let page
try {
page = await getPage({ pageId })
} catch (err) {
let message
if (err.status === 404 && err.reason === 'deleted') {
message = 'This snapshot has been deleted'
} else if (err.status === 404) {
message = 'Snapshot not found'
} else {
message = 'Unknown error'
}
ReactDOM.render(
<div id='rootContainer'>
<SnapshotChrome>
<div id='errorMessage'>
{message}
</div>
</SnapshotChrome>
<div id='page' className='placeholder' />
</div>,
containerElement,
)
throw err
}
const html = await getPageHtml({ pageId })
const timestamp = page.timestamp
document.title = `${page.title}`
ReactDOM.render(
<div id='rootContainer'>
<SnapshotChrome>
<span id='description'>
<Icon name='camera' />
Snapshot of
<a href={page.url} style={{ margin: '0 4px' }}>
{shortUrl(page.url)}
</a>
<Icon name='clock' />
<time dateTime={new Date(timestamp)}>
{niceTime(timestamp)}
</time>
</span>
{isStoredInternally(page) && <SaveAsButton page={page} label />}
<DeleteButton
page={page}
onClick={async () => {
await deletePage({ page })
window.location.replace(window.location)
}}
label
/>
</SnapshotChrome>
<ContentFrame html={html} />
</div>,
containerElement,
)
}
// Read pageId from location: ?page=pageId
const pageId = new URL(window.location).searchParams.get('page')
showPage(pageId)