aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-04-11 13:35:06 -0700
committerAdam Soltys <[email protected]>2024-04-11 13:35:06 -0700
commit9d55044d4321233f6ffdbada737bccb5894a0f2a (patch)
treeeadaa34e2c9e4e61184d4d0634cdbab3b4ac5d67 /src
parentc92dce530d00edad2447daba619514f50418f0ac (diff)
downloadcoinos-ui-9d55044d4321233f6ffdbada737bccb5894a0f2a.tar.gz
coinos-ui-9d55044d4321233f6ffdbada737bccb5894a0f2a.zip
service worker for pwa
Diffstat (limited to 'src')
-rw-r--r--src/app.html1
-rw-r--r--src/locales/en.json2
-rw-r--r--src/routes/(app)/[username]/(profile)/+layout.svelte6
-rw-r--r--src/routes/(app)/[username]/(profile)/+page.svelte28
-rw-r--r--src/routes/+layout.svelte17
-rw-r--r--src/service-worker.js83
6 files changed, 127 insertions, 10 deletions
diff --git a/src/app.html b/src/app.html
index 9d7e2fdb..728900ed 100644
--- a/src/app.html
+++ b/src/app.html
@@ -9,6 +9,7 @@
/>
<meta name="description" content="The easiest raysway to get started with bitcoin" />
<meta name="keywords" content="open source bitcoin lightning web wallet" />
+ <meta name="theme-color" content="#fff"/>
<link rel="manifest" href="/manifest.webmanifest" />
%sveltekit.head%
</head>
diff --git a/src/locales/en.json b/src/locales/en.json
index f29c8d29..93affb7f 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -155,7 +155,7 @@
"subheader": "Latest Updates"
},
"user": {
- "details": "Details",
+ "info": "Info",
"noMessages": "You have not sent or received any messages yet",
"profilePage": "Profile page",
"nostrPubkey": "Nostr pubkey",
diff --git a/src/routes/(app)/[username]/(profile)/+layout.svelte b/src/routes/(app)/[username]/(profile)/+layout.svelte
index 5752491b..11e33dbf 100644
--- a/src/routes/(app)/[username]/(profile)/+layout.svelte
+++ b/src/routes/(app)/[username]/(profile)/+layout.svelte
@@ -168,14 +168,16 @@
</a>
{/if}
+ {#if user.username !== subject.username}
<button
type="button"
on:click={toggleDetails}
class="rounded-2xl border py-5 px-6 font-bold hover:opacity-80 flex justify-center w-full"
>
- <Icon icon="edit" style="w-8 mr-2" />
- <div class="mt-1">{$t("user.details")}</div>
+ <Icon icon="profile" style="w-8 mr-2" />
+ <div class="mt-1">{$t("user.info")}</div>
</button>
+ {/if}
{#if user?.admin && user.username !== subject.username}
<form class="w-full flex" on:submit|preventDefault={reset}>
diff --git a/src/routes/(app)/[username]/(profile)/+page.svelte b/src/routes/(app)/[username]/(profile)/+page.svelte
index f0ab743a..9443c063 100644
--- a/src/routes/(app)/[username]/(profile)/+page.svelte
+++ b/src/routes/(app)/[username]/(profile)/+page.svelte
@@ -102,6 +102,21 @@
let reset = () =>
(dragDisabled =
user?.username !== subject?.username || window.innerWidth < 768);
+
+ let installPrompt;
+ onMount(() => {
+ if (!browser) return;
+ window.addEventListener("beforeinstallprompt", (event) => {
+ event.preventDefault();
+ installPrompt = event;
+ });
+ });
+
+ let install = async () => {
+ if (!installPrompt) return;
+ await installPrompt.prompt();
+ installPrompt = null;
+ };
</script>
<svelte:window on:resize={reset} />
@@ -148,18 +163,17 @@
</button>
</a>
- <a href={`/${user.username}/items/create`} class="contents">
+ {#if installPrompt}
<button
- class="rounded-2xl border py-5 px-6 font-bold hover:opacity-80 flex bg-primary grow basis-full"
+ class="rounded-2xl border py-5 px-6 font-bold hover:opacity-80 flex bg-black text-white grow basis-full fixed bottom-16"
+ on:click={install}
>
<div class="mx-auto flex gap-2">
- <Icon icon="plus" style="w-8 mx-auto" />
- <div class="my-auto text-xl whitespace-nowrap">
- {$t("items.add")}
- </div>
+ <Icon icon="save" style="w-8 mx-auto invert" />
+ <div class="my-auto text-xl whitespace-nowrap">Install app</div>
</div>
</button>
- </a>
+ {/if}
</div>
{/if}
diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte
index 545018ed..43e573ef 100644
--- a/src/routes/+layout.svelte
+++ b/src/routes/+layout.svelte
@@ -2,6 +2,23 @@
import { PUBLIC_DOMAIN } from "$env/static/public";
import "../app.css";
import { loading, t } from "$lib/translations";
+ import { onMount } from "svelte";
+ import { dev } from "$app/environment";
+
+ onMount(() => {
+ if ("serviceWorker" in navigator) {
+ navigator.serviceWorker
+ .register("/service-worker.js", {
+ type: dev ? "module" : "classic",
+ })
+ .then((registration) => {
+ console.log("SW registered: ", registration);
+ })
+ .catch((registrationError) => {
+ console.log("SW registration failed: ", registrationError);
+ });
+ }
+ });
export let data;
let { pathname } = data;
diff --git a/src/service-worker.js b/src/service-worker.js
new file mode 100644
index 00000000..a95ff268
--- /dev/null
+++ b/src/service-worker.js
@@ -0,0 +1,83 @@
+/// <reference types="@sveltejs/kit" />
+import { build, files, version } from "$service-worker";
+
+// Create a unique cache name for this deployment
+const CACHE = `cache-${version}`;
+
+const ASSETS = [
+ ...build, // the app itself
+ ...files, // everything in `static`
+];
+
+self.addEventListener("install", (event) => {
+ console.log("SW installed");
+
+ // Create a new cache and add all files to it
+ async function addFilesToCache() {
+ const cache = await caches.open(CACHE);
+ await cache.addAll(ASSETS);
+ }
+
+ event.waitUntil(addFilesToCache());
+});
+
+self.addEventListener("activate", (event) => {
+ // Remove previous cached data from disk
+ async function deleteOldCaches() {
+ for (const key of await caches.keys()) {
+ if (key !== CACHE) await caches.delete(key);
+ }
+ }
+
+ event.waitUntil(deleteOldCaches());
+});
+
+self.addEventListener("fetch", (event) => {
+ console.log("FETCH")
+ // ignore POST requests etc
+ if (event.request.method !== "GET") return;
+
+ async function respond() {
+ const url = new URL(event.request.url);
+ const cache = await caches.open(CACHE);
+
+ // `build`/`files` can always be served from the cache
+ if (ASSETS.includes(url.pathname)) {
+ const response = await cache.match(url.pathname);
+
+ if (response) {
+ return response;
+ }
+ }
+
+ // for everything else, try the network first, but
+ // fall back to the cache if we're offline
+ try {
+ const response = await fetch(event.request);
+
+ // if we're offline, fetch can return a value that is not a Response
+ // instead of throwing - and we can't pass this non-Response to respondWith
+ if (!(response instanceof Response)) {
+ throw new Error("invalid response from fetch");
+ }
+
+ if (response.status === 200) {
+ cache.put(event.request, response.clone());
+ }
+
+ return response;
+ } catch (err) {
+ const response = await cache.match(event.request);
+
+ if (response) {
+ return response;
+ }
+
+ // if there's no cache, then just error out
+ // as there is nothing we can do to respond to this request
+ throw err;
+ }
+ }
+
+ event.respondWith(respond());
+});
 








                                         



                           
 

                                                          
 
                                      
                                                                             


                                                   













                                                          
                                           
                                                     











                                                        
 
                                                                        








                                                                          
                                        







                                 
      
<script>
  import { run } from 'svelte/legacy';

  import { sats, f, s } from "$lib/utils";
  import { t } from "$lib/translations";
  import { enhance } from "$app/forms";
  import Numpad from "$comp/Numpad.svelte";
  import Spinner from "$comp/Spinner.svelte";
  import { pin } from "$lib/store";

  let { data, form } = $props();

  let { currency, username } = data.user;
  let {
    defaultDescription,
    minWithdrawable,
    maxWithdrawable,
    k1,
    callback,
    rates,
  } = data;
  let rate;
  run(() => {
    rate = rates[currency];
  });

  let amount = $state(Math.round(minWithdrawable / 1000)),
    loading = $state();

  let submit = () => (loading = true);
  let amountFiat = $derived(parseFloat(((amount * rate) / sats).toFixed(2)));
</script>

<div class="container px-4 mt-20 max-w-xl mx-auto">
  <div class="text-center mb-8">
    <div>
      <h1 class="text-3xl md:text-4xl font-semibold mb-2">
        {defaultDescription}
      </h1>
    </div>
  </div>

  {#if form?.error}
    <div class="text-red-600 text-center mb-5">
      {form.error}
    </div>
  {/if}

  {#if minWithdrawable === maxWithdrawable}
    <div class="text-center font-bold text-2xl mb-4">
      <div>
        {f(amountFiat, currency)}
      </div>
      <div>
        <span class="text-secondary font-normal text-xl"
          >⚡️{`${s(amount)}`}</span
        >
      </div>
    </div>
  {:else}
    <Numpad bind:amount {currency} bind:rate />
  {/if}

  <form action="?/withdraw" method="POST" use:enhance onsubmit={submit}>
    <input name="amount" value={amount} type="hidden" />
    <input name="username" value={username} type="hidden" />
    <input name="currency" value={currency} type="hidden" />
    <input name="minWithdrawable" value={minWithdrawable} type="hidden" />
    <input name="maxWithdrawable" value={maxWithdrawable} type="hidden" />
    <input name="callback" value={callback} type="hidden" />
    <input name="k1" value={k1} type="hidden" />

    <div class="flex w-full">
      <button type="submit" class="btn">
        {#if loading}
          <Spinner />
        {:else}
          {$t("payments.redeem")}
        {/if}
      </button>
    </div>
  </form>
</div>