app

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

+page.svelte (5535B)


      1 <script lang="ts">
      2   import { getContext, onMount } from 'svelte';
      3   import CapabilityGate from '../../lib/components/CapabilityGate.svelte';
      4   import SellingRow from '../../lib/components/SellingRow.svelte';
      5   import InboxSetup from '../../lib/components/InboxSetup.svelte';
      6   import Disclosure from '../../lib/components/primitives/Disclosure.svelte';
      7   import { internalHref } from '../../lib/navigation-url.ts';
      8   import {
      9     IDENTITY_VIEW_CONTEXT,
     10     identityViewSnapshot,
     11     identityViewSession,
     12     subscribeIdentityView,
     13     connectIdentityView,
     14     disconnectIdentityView,
     15     type IdentityViewContext
     16   } from '../../lib/runtime/view-context.ts';
     17   import {
     18     PUBLIC_RUNTIME_CONTEXT,
     19     publicRuntimeReady,
     20     type PublicRuntimeContext,
     21     type PublicRuntime
     22   } from '../../lib/runtime/public-runtime.ts';
     23   import {
     24     openSellingView,
     25     sellingViewSnapshot,
     26     subscribeSellingView,
     27     closeSellingView,
     28     type SellingView,
     29     type SellingSnapshot
     30   } from '../../lib/publishing/selling-view.ts';
     31   const identityContext = getContext<IdentityViewContext>(
     32     IDENTITY_VIEW_CONTEXT
     33   );
     34   const publicContext = getContext<PublicRuntimeContext>(
     35     PUBLIC_RUNTIME_CONTEXT
     36   );
     37   let identity = $state(identityViewSnapshot(identityContext));
     38   let result = $state.raw<SellingSnapshot | undefined>();
     39   let unavailable = $state(false);
     40   onMount(() => {
     41     let disposed = false,
     42       runtimeReady = false,
     43       runtime: PublicRuntime | undefined;
     44     let owner: string | undefined,
     45       generation = Symbol();
     46     let session: ReturnType<typeof identityViewSession>;
     47     let view: SellingView | undefined,
     48       off = () => {};
     49     function acquire() {
     50       const observed = identityViewSnapshot(identityContext);
     51       identity = observed;
     52       const selected =
     53         observed.mounted && 'publicKey' in observed.identity
     54           ? observed.identity.publicKey
     55           : undefined;
     56       const selectedSession = identityViewSession(identityContext);
     57       if (!runtimeReady || (selected === owner && selectedSession === session))
     58         return;
     59       owner = selected;
     60       session = selectedSession;
     61       generation = Symbol();
     62       const original = generation;
     63       off();
     64       if (view) closeSellingView(view);
     65       view = undefined;
     66       result = undefined;
     67       unavailable = false;
     68       if (!selected) return;
     69       void openSellingView(identityContext, runtime)
     70         .then((next) => {
     71           if (disposed || generation !== original) {
     72             if (next) closeSellingView(next);
     73             return;
     74           }
     75           if (!next) {
     76             unavailable = true;
     77             return;
     78           }
     79           view = next;
     80           result = sellingViewSnapshot(next);
     81           off = subscribeSellingView(next, () => {
     82             result = sellingViewSnapshot(next);
     83             unavailable = !result;
     84           });
     85         })
     86         .catch(() => {
     87           if (!disposed && generation === original) unavailable = true;
     88         });
     89     }
     90     const identityOff = subscribeIdentityView(identityContext, acquire);
     91     void publicRuntimeReady(publicContext)
     92       .then((next) => {
     93         if (disposed) return;
     94         runtime = next;
     95         runtimeReady = true;
     96         acquire();
     97       })
     98       .catch(() => {
     99         if (!disposed) {
    100           runtimeReady = true;
    101           acquire();
    102         }
    103       });
    104     return () => {
    105       disposed = true;
    106       generation = Symbol();
    107       identityOff();
    108       off();
    109       if (view) closeSellingView(view);
    110     };
    111   });
    112   function connect() {
    113     return connectIdentityView(identityContext);
    114   }
    115   function disconnect() {
    116     disconnectIdentityView(identityContext);
    117   }
    118   const empty = $derived(
    119     result?.local === 'ready' &&
    120       result.remote === 'bounded' &&
    121       !result.attention.length &&
    122       !result.drafts.length &&
    123       !result.listings.length
    124   );
    125 </script>
    126 
    127 <svelte:head
    128   ><title>HarvestCircle</title><meta
    129     name="robots"
    130     content="noindex"
    131   /></svelte:head
    132 >
    133 <div class="page page--reading stack">
    134   <div class="cluster">
    135     <h1>Selling</h1>
    136     <a href={internalHref('/sell')}>List food</a>
    137   </div>
    138   <CapabilityGate
    139     identity={identity.identity}
    140     required="connection"
    141     focusTarget="protected"
    142     onconnect={identity.mounted ? connect : undefined}
    143     ondisconnect={disconnect}
    144   />
    145   {#if unavailable}<p class="notice">
    146       Saved seller work is unavailable. Existing records have not been cleared.
    147     </p>{/if}
    148   {#if empty}<p>
    149       No listings yet. Create a listing so people can ask about your food.
    150     </p>{/if}
    151   {#if result}
    152     {#if result.attention.length}<section class="stack">
    153         <h2>Needs attention</h2>
    154         {#each result.attention as row (row.key)}<SellingRow {row} />{/each}
    155       </section>{/if}
    156     {#if result.drafts.length}<section class="stack">
    157         <h2>Drafts</h2>
    158         {#each result.drafts as row (row.key)}<SellingRow {row} />{/each}
    159       </section>{/if}
    160     {#if result.listings.length}<section class="stack">
    161         <h2>Listings</h2>
    162         {#each result.listings as row (row.key)}<SellingRow {row} />{/each}
    163       </section>{/if}
    164     <p>
    165       {result.remote === 'checking'
    166         ? 'Checking bounded remote listing sources.'
    167         : result.remote === 'bounded'
    168           ? 'Remote discovery is bounded. Other relay copies may exist.'
    169           : 'Remote listing lookup is incomplete. Local records remain separate from remote discovery.'}
    170     </p>
    171   {/if}
    172   <Disclosure summary="Inbox settings"><InboxSetup /></Disclosure>
    173 </div>