cozy/src/components/Library.astro
2023-06-12 22:17:55 +02:00

157 lines
No EOL
4.2 KiB
Text

---
export interface Props {
postDivSelector: string,
skipSave?: boolean
}
const {postDivSelector, skipSave = false} = Astro.props;
---
<div id="library">
<span id="heading"></span>
<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>
import { getPostCard } from '../utils/library-utils'
const cache = await caches.open('cozy-reader');
const url = new URL(window.location.href);
const response = await cache.match(url)
const postDivSelector = document.getElementById('postDivSelector') as HTMLInputElement;
const skipSave = document.getElementById('skipSave') as HTMLInputElement;
const postDiv = document.querySelector(postDivSelector?.value);
if (!response) {
if (!skipSave?.checked) {
await cache.add(url);
}
}
const cachedRequests = await cache.keys();
const list = document.querySelector('#post-list');
if(cachedRequests?.length) {
const heading = document.querySelector('#library span#heading') as HTMLHeadingElement;
heading.innerHTML = 'History';
cachedRequests
// temporary delete all cached errors
.filter(request => {
const urlObj = new URL(request.url);
return urlObj.search !== '';
})
.reverse()
.forEach(async (request) => {
const {url} = request;
const link = document.createElement('a');
let responseText;
const fullResponse = await cache.match(url)
fullResponse?.text().then(data => {
responseText = data;
const html = document.createElement('html');
html.innerHTML = responseText;
const title = html.querySelector('meta[property="cozy:title"]')?.getAttribute('content');
if (title === 'Something is not right') {
cache.delete(url);
return; // temporary fix for deleting cached errors
}
const postCard = getPostCard(html);
link.innerHTML = postCard;
link.href = url;
link.onclick = async (e) => {
e.preventDefault();
scroll(0,0);
const html = document.createElement('html');
html.innerHTML = responseText;
const newPost = html.querySelector('body')?.querySelector('#post');
if (postDiv && newPost?.innerHTML) {
postDiv.innerHTML = newPost.innerHTML
}
}
const item = document.createElement('li');
item.appendChild(link);
list?.appendChild(item);
});
});
}
</script>
<style lang="scss">
#library {
:global(span#heading) {
color: #555;
font-size: small;
text-transform: uppercase;
}
}
#post-list {
:global(li) {
list-style: none;
width: calc(100% + 40px);
margin-left: -40px;
:global(a) {
text-decoration: none;
color: #000;
:global(h3) {
text-decoration: underline;
}
:global(.post-card) {
padding-top: 1rem;
:global(.post-card__image) {
float: left;
margin: 0.25rem 0.5rem 0.25rem 0;
:global(img, svg) {
width: 70px;
height: 70px;
object-fit: cover;
border-radius: 5px;
border: 1px solid #eee;
}
:global(svg) {
color: #ccc;
padding: 0.5rem;
}
}
}
:global(.post-card__content) {
display: flex;
flex-direction: column;
justify-content: center;
min-height: calc(70px + 0.5rem);
}
:global(.post-card__title, .post-card__description) {
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
:global(.post-card__meta, .post-card__description){
font-size: smaller;
color: #555;
}
:global(.post-card__meta) {
display: flex;
justify-content: space-between;
* {
flex: 1;
}
:global(.post-card__source) {
font-weight: bold;
}
}
}
}
}
</style>