feat: render cached post to page

This commit is contained in:
Ayo 2023-06-04 14:56:04 +02:00
parent 42e71df3c6
commit c8672fc4eb
4 changed files with 34 additions and 13 deletions

View file

@ -10,9 +10,9 @@ Cozy is your modern-day reading assistant.
## Roadmap ## Roadmap
| Feature | Status | | Feature | Status |
| --- | --- | | --- | --- |
| Remove distractions| Done | | Remove distractions| ✅ DONE |
| Save to a Reading Library | In-Progress | | Save to a Reading Library | ✅ DONE |
| PWA: Offline access to library | | | PWA: Offline access to library | 🛠️ In-progress |
| AI insights | | | AI insights | |
| Browser Extensions | | | Browser Extensions | |

View file

@ -1,20 +1,26 @@
---
export interface Props {
postDivSelector: string
}
const {postDivSelector} = Astro.props;
---
<div id="library"></div> <div id="library"></div>
<input value={postDivSelector} name="postDivSelector" id="postDivSelector" hidden />
<script> <script>
const cache = await caches.open('cozy-reader'); const cache = await caches.open('cozy-reader');
const url = new URL(window.location.href); const url = new URL(window.location.href);
console.log('client script loaded', url);
const response = await cache.match(url) const response = await cache.match(url)
if (response) { if (!response) {
console.log('cache hit', response);
} else {
console.log('cache miss');
await cache.add(url); await cache.add(url);
} }
const cachedResponses = await cache.keys(); const postDivSelector = document.getElementById('postDivSelector') as HTMLInputElement;
console.log(cachedResponses); const postDiv = document.querySelector(postDivSelector?.value);
const cachedResponses = await cache.keys();
const library = document.getElementById('library'); const library = document.getElementById('library');
const list = document.createElement('ul'); const list = document.createElement('ul');
cachedResponses.forEach(response => { cachedResponses.forEach(response => {
@ -22,6 +28,21 @@
const link = document.createElement('a'); const link = document.createElement('a');
link.innerText = url; link.innerText = url;
link.href = url; link.href = url;
link.onclick = async (e) => {
e.preventDefault();
const fullResponse = await cache.match(url)
fullResponse?.text().then(data => {
const html = document.createElement('html');
html.innerHTML = data;
const body = html.querySelector('body');
const newPost = body?.querySelector('#post');
console.log(body?.querySelector('#post'));
if (postDiv && newPost?.innerHTML) {
postDiv.innerHTML = (newPost.innerHTML)
}
});
}
const item = document.createElement('li'); const item = document.createElement('li');
item.appendChild(link); item.appendChild(link);
list?.appendChild(item); list?.appendChild(item);

View file

@ -11,7 +11,7 @@ const datePublished =
{ {
article && ( article && (
<div class="post"> <div id="post">
{article.source && <span class="source">{article.source}</span>} {article.source && <span class="source">{article.source}</span>}
{article.title && <h1 class="title">{article.title}</h1>} {article.title && <h1 class="title">{article.title}</h1>}
{(article.author || datePublished) && ( {(article.author || datePublished) && (
@ -26,7 +26,7 @@ const datePublished =
} }
<style is:inline> <style is:inline>
div.post { div#post {
max-width: 600px; max-width: 600px;
margin: 1em auto; margin: 1em auto;
padding: 1em; padding: 1em;

View file

@ -21,6 +21,6 @@ try {
--- ---
<Layout title={url !== "" ? article?.title : "Your modern-day reading assistant"}> <Layout title={url !== "" ? article?.title : "Your modern-day reading assistant"}>
<AddressBar url={url} /> <AddressBar url={url} />
<Library /> <Library postDivSelector="#post" />
{url && <Post article={article} />} {url && <Post article={article} />}
</Layout> </Layout>