Skip to content

Commit

Permalink
feat: Feed loader is enabled to load either provider_metadata.json or…
Browse files Browse the repository at this point in the history
… json feed directly
  • Loading branch information
Thomas Junk committed Oct 4, 2023
1 parent bac8bb1 commit 5c8a5fd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
10 changes: 7 additions & 3 deletions src/lib/feedview/Loader.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@

<script lang="ts">
import { appStore } from "$lib/store";
import { loadProviderMetaData } from "$lib/urlloader";
import { loadFeed, loadProviderMetaData } from "$lib/urlloader";
let url = "";
const load = () => {
loadProviderMetaData(url);
window.location.hash = `#/feed?q=${url}`;
if (/provider-metadata\.json/.test(url)) {
loadProviderMetaData(url);
window.location.hash = `#/feed?q=${url}`;
} else {
loadFeed(url);
}
};
const keydown = (e: KeyboardEvent) => {
Expand Down
28 changes: 1 addition & 27 deletions src/lib/feedview/feed/distributions/Distribution.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,8 @@
-->

<script lang="ts">
import { appStore } from "$lib/store";
import { tick } from "svelte";
import { loadFeed } from "$lib/urlloader";
export let distribution: any;
async function loadFeed(feedURL: string, e: Event) {
appStore.setSingleErrorMsg("");
try {
const response = await fetch(`${feedURL}`);
if (response.ok) {
const feedJSON = await response.json();
appStore.setCurrentFeed(null);
await tick();
appStore.setCurrentFeed(feedJSON);
appStore.setFeedSectionOpen();
appStore.unshiftHistory((e.target as Element).id);
setTimeout(() => {
const el = document.getElementById(`${feedURL}`);
el?.scrollIntoView({ block: "start", behavior: "smooth" });
}, 100);
}
if (response.status === 404) {
appStore.setSingleErrorMsg("The resource you requested was not found on the server.");
}
} catch (error) {
appStore.setSingleErrorMsg(
"Failed to load from URL. The server may be unreachable or the resource cannot be accessed due to CORS restrictions."
);
}
}
const openFeed = (e: Event) => {
loadFeed((e.target as Element).getAttribute("href")!, e);
e.preventDefault();
Expand Down
27 changes: 26 additions & 1 deletion src/lib/urlloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,29 @@ async function loadProviderMetaData(url: string) {
}
}

export { loadSingleCSAF, loadProviderMetaData };
async function loadFeed(feedURL: string, e?: Event) {
appStore.setSingleErrorMsg("");
try {
const response = await fetch(`${feedURL}`);
if (response.ok) {
const feedJSON = await response.json();
appStore.setCurrentFeed(null);
appStore.setCurrentFeed(feedJSON);
appStore.setFeedSectionOpen();
if (e) appStore.unshiftHistory((e.target as Element).id);
setTimeout(() => {
const el = document.getElementById(`${feedURL}`);
el?.scrollIntoView({ block: "start", behavior: "smooth" });
}, 100);
}
if (response.status === 404) {
appStore.setSingleErrorMsg("The resource you requested was not found on the server.");
}
} catch (error) {
appStore.setSingleErrorMsg(
"Failed to load from URL. The server may be unreachable or the resource cannot be accessed due to CORS restrictions."
);
}
}

export { loadSingleCSAF, loadProviderMetaData, loadFeed };

0 comments on commit 5c8a5fd

Please sign in to comment.