add .env file and fix spinner behavior on error

This commit is contained in:
tipogi
2025-02-04 10:54:21 +01:00
parent 321c0ec36d
commit f60515cb80
5 changed files with 40 additions and 22 deletions

2
.env-sample Normal file
View File

@@ -0,0 +1,2 @@
# Read from the local homeserver for testing
VITE_TESTNET=true

3
.gitignore vendored
View File

@@ -7,6 +7,9 @@ yarn-error.log*
pnpm-debug.log* pnpm-debug.log*
lerna-debug.log* lerna-debug.log*
# Environment file
.env
node_modules node_modules
dist dist
dist-ssr dist-ssr

View File

@@ -3,7 +3,7 @@ import './css/App.css'
import { Explorer } from './Explorer.tsx' import { Explorer } from './Explorer.tsx'
import { Spinner } from './Spinner.tsx' import { Spinner } from './Spinner.tsx'
import { Show, createSignal, } from "solid-js" 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() { function App() {

View File

@@ -91,4 +91,7 @@ input:focus-visible {
button { button {
background-color: #f9f9f9; background-color: #f9f9f9;
} }
input {
background-color: #f9f9f9;
}
} }

View File

@@ -1,7 +1,9 @@
import { Client } from '@synonymdev/pubky' import { Client } from '@synonymdev/pubky'
import { createStore } from 'solid-js/store'; 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<{ export const [store, setStore] = createStore<{
explorer: Boolean, explorer: Boolean,
@@ -73,6 +75,14 @@ export function loadMore() {
// @ts-ignore // @ts-ignore
setStore('list', Array.from(map.values())) setStore('list', Array.from(map.values()))
setStore('dir', path) 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() loadList()
} }
export function downloadFile(link: string) { export async function downloadFile(link: string) {
setStore("loading", true); setStore("loading", true);
client.fetch(link).then(response => { try {
if (response.ok) { const response: Response = await client.fetch(link);
// Convert the response to a Blob
response.blob().then(fileBlob => { if (!response.ok) {
throw new Error(`Failed to fetch file: ${response.status} ${response.statusText}`);
}
const fileBlob: Blob = await response.blob();
const element = document.createElement('a'); const element = document.createElement('a');
element.href = URL.createObjectURL(fileBlob); element.href = URL.createObjectURL(fileBlob);
let parts = link.split('/'); const parts = link.split('/');
element.download = parts[parts.length - 1]; element.download = parts[parts.length - 1];
document.body.appendChild(element); // Required for this to work in Firefox
element.click();
document.body.appendChild(element);
element.click();
element.remove(); element.remove();
setStore("loading", false); } catch (err: unknown) {
}); console.error("Error fetching file:", err);
} else { }
console.error("Failed to fetch file:", response.statusText); finally {
setStore("loading", false); setStore("loading", false);
} }
}).catch(err => {
console.error("Error fetching file:", err);
setStore("loading", false);
});
} }