From f60515cb8024dd04c1add5339fc37feca5d95d52 Mon Sep 17 00:00:00 2001 From: tipogi Date: Tue, 4 Feb 2025 10:54:21 +0100 Subject: [PATCH] add .env file and fix spinner behavior on error --- .env-sample | 2 ++ .gitignore | 3 +++ src/App.tsx | 2 +- src/css/index.css | 3 +++ src/state.ts | 52 ++++++++++++++++++++++++++++------------------- 5 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 .env-sample diff --git a/.env-sample b/.env-sample new file mode 100644 index 0000000..efa7f14 --- /dev/null +++ b/.env-sample @@ -0,0 +1,2 @@ +# Read from the local homeserver for testing +VITE_TESTNET=true \ No newline at end of file diff --git a/.gitignore b/.gitignore index a547bf3..3aa3a07 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,9 @@ yarn-error.log* pnpm-debug.log* lerna-debug.log* +# Environment file +.env + node_modules dist dist-ssr diff --git a/src/App.tsx b/src/App.tsx index 52dbd08..45d0dd4 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -3,7 +3,7 @@ import './css/App.css' import { Explorer } from './Explorer.tsx' import { Spinner } from './Spinner.tsx' import { Show, createSignal, } from "solid-js" -import { store, setStore, updateDir, resetStore, switchShallow, } from "./state.ts" +import { store, setStore, updateDir, switchShallow, } from "./state.ts" function App() { diff --git a/src/css/index.css b/src/css/index.css index df14592..24c8c13 100644 --- a/src/css/index.css +++ b/src/css/index.css @@ -91,4 +91,7 @@ input:focus-visible { button { background-color: #f9f9f9; } + input { + background-color: #f9f9f9; + } } diff --git a/src/state.ts b/src/state.ts index bf1e6d2..b6a9756 100644 --- a/src/state.ts +++ b/src/state.ts @@ -1,7 +1,9 @@ import { Client } from '@synonymdev/pubky' import { createStore } from 'solid-js/store'; -export const client = new Client(); +export const client = import.meta.env.VITE_TESTNET == "true" ? + Client.testnet() : + new Client(); export const [store, setStore] = createStore<{ explorer: Boolean, @@ -73,6 +75,14 @@ export function loadMore() { // @ts-ignore setStore('list', Array.from(map.values())) setStore('dir', path) + }) + .catch((e: String) => { + setStore('loading', false) + if (e === "error sending request") { + console.log(e, ": cannot reach homeserver or pk does not exist") + } else { + console.log("ERROR: ", e); + } }); } @@ -100,30 +110,30 @@ export function updateDir(path: string) { loadList() } -export function downloadFile(link: string) { +export async function downloadFile(link: string) { setStore("loading", true); - client.fetch(link).then(response => { - if (response.ok) { - // Convert the response to a Blob - response.blob().then(fileBlob => { - const element = document.createElement('a'); + try { + const response: Response = await client.fetch(link); - element.href = URL.createObjectURL(fileBlob); - let parts = link.split('/'); - element.download = parts[parts.length - 1]; - document.body.appendChild(element); // Required for this to work in Firefox - element.click(); - - element.remove(); - setStore("loading", false); - }); - } else { - console.error("Failed to fetch file:", response.statusText); - setStore("loading", false); + if (!response.ok) { + throw new Error(`Failed to fetch file: ${response.status} ${response.statusText}`); } - }).catch(err => { + + const fileBlob: Blob = await response.blob(); + + const element = document.createElement('a'); + element.href = URL.createObjectURL(fileBlob); + const parts = link.split('/'); + element.download = parts[parts.length - 1]; + + document.body.appendChild(element); + element.click(); + element.remove(); + } catch (err: unknown) { console.error("Error fetching file:", err); + } + finally { setStore("loading", false); - }); + } } \ No newline at end of file