feat: skip save if no article

This commit is contained in:
Ayo 2023-06-04 16:05:26 +02:00
parent 6c3cff1b66
commit 3b6e6c36b1
2 changed files with 20 additions and 7 deletions

View file

@ -1,27 +1,30 @@
---
export interface Props {
postDivSelector: string
postDivSelector: string,
skipSave?: boolean
}
const {postDivSelector} = Astro.props;
const {postDivSelector, skipSave = false} = Astro.props;
---
<div id="library">
<ul id="post-list"></ul>
</div>
<input value={postDivSelector} name="postDivSelector" id="postDivSelector" hidden />
<input type="checkbox" id="skipSave" name="skipSave" checked={skipSave} hidden />
<script>
const cache = await caches.open('cozy-reader');
const url = new URL(window.location.href);
const response = await cache.match(url)
if (!response) {
const postDivSelector = document.getElementById('postDivSelector') as HTMLInputElement;
const skipSave = document.getElementById('skipSave') as HTMLInputElement;
const postDiv = document.querySelector(postDivSelector?.value);
if (!response && !skipSave.checked) {
await cache.add(url);
}
const postDivSelector = document.getElementById('postDivSelector') as HTMLInputElement;
const postDiv = document.querySelector(postDivSelector?.value);
const cachedResponses = await cache.keys();
const list = document.querySelector('#post-list');

View file

@ -8,6 +8,7 @@ import Library from "../components/Library.astro";
const params = Astro.url.searchParams;
const url = params.get('url') || '';
let article;
let skipSave;
try {
article = await extract(url);
@ -18,17 +19,26 @@ try {
};
}
if (!article) {
article = {
title: "Something is not right",
content: "<p>The article extractor did not get any result.</p>",
}
skipSave = true;
}
if (url === '') {
article = {
title: "Welcome to Cozy 🧸",
author: "Ayo Ayco",
content: "<p>Enter a URL in the address bar above to get started.</p>",
};
skipSave = true;
}
---
<Layout title={url !== "" ? article?.title : "Your modern-day reading assistant"}>
<AddressBar slot="address-bar" url={url} />
<Post slot="post" article={article} />
<Library slot="library" postDivSelector="#post" />
<Library skipSave={skipSave} slot="library" postDivSelector="#post" />
</Layout>