app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

PublisherHide.svelte (2591B)


      1 <script lang="ts">
      2   import { getContext, onMount } from 'svelte';
      3   import Button from './primitives/Button.svelte';
      4   import {
      5     productViewPublisherProof,
      6     type ProductView
      7   } from '../catalog/product-view.ts';
      8   import {
      9     LOCAL_PUBLIC_HIDES_CONTEXT,
     10     PUBLIC_HIDE_DISCLOSURE,
     11     localPublicHidesSnapshot,
     12     subscribeLocalPublicHides,
     13     hideLocalPublicPublisher,
     14     type LocalPublicHides
     15   } from '../persistence/local-public-hides.ts';
     16   let { view }: { view: ProductView } = $props();
     17   const hides = getContext<LocalPublicHides | undefined>(
     18     LOCAL_PUBLIC_HIDES_CONTEXT
     19   );
     20   let snapshot = $state.raw(
     21     hides ? localPublicHidesSnapshot(hides) : { state: 'guest' as const }
     22   );
     23   let busy = $state(false),
     24     notice = $state<string | undefined>();
     25   let alive = false;
     26   onMount(() => {
     27     alive = true;
     28     const off = hides
     29       ? subscribeLocalPublicHides(hides, () => {
     30           snapshot = localPublicHidesSnapshot(hides);
     31         })
     32       : () => {};
     33     if (hides) snapshot = localPublicHidesSnapshot(hides);
     34     return () => {
     35       alive = false;
     36       off();
     37     };
     38   });
     39   async function hide() {
     40     const selected = view;
     41     const proof = productViewPublisherProof(selected);
     42     if (!hides || !proof || busy || snapshot.state !== 'ready') return;
     43     busy = true;
     44     const result = await hideLocalPublicPublisher(
     45       hides,
     46       proof,
     47       'reviewed_local_public_hide'
     48     );
     49     if (!alive || view !== selected) return;
     50     busy = false;
     51     const current = localPublicHidesSnapshot(hides);
     52     if (
     53       current.state !== 'ready' ||
     54       snapshot.state !== 'ready' ||
     55       current.owner !== snapshot.owner
     56     ) {
     57       notice = undefined;
     58       return;
     59     }
     60     notice =
     61       result.status === 'saved' || result.status === 'existing'
     62         ? 'Publisher hidden in this browser. Undo is available in Privacy.'
     63         : 'The hide was not confirmed. Existing local data has not been reset; review Privacy before trying again.';
     64   }
     65 </script>
     66 
     67 <div class="stack">
     68   <p>{PUBLIC_HIDE_DISCLOSURE}</p>
     69   {#if snapshot.state === 'ready'}
     70     <Button label="Hide publisher locally" onclick={hide} disabled={busy} />
     71   {:else if snapshot.state === 'guest'}
     72     <p>Connect to use account-local publisher controls.</p>
     73   {:else if snapshot.state === 'loading'}
     74     <p>Checking local publisher preferences…</p>
     75   {:else}
     76     <p class="notice">
     77       Local preferences could not be checked. You can browse public food;
     78       existing local data has not been reset.
     79     </p>
     80   {/if}
     81   {#if notice}<p role="status">{notice}</p>{/if}
     82 </div>