app

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

ProductEnquiry.svelte (8498B)


      1 <script module lang="ts">
      2   declare function setInterval(
      3     callback: () => void,
      4     milliseconds: number
      5   ): number;
      6   declare function clearInterval(timer: number): void;
      7 </script>
      8 
      9 <script lang="ts">
     10   import { getContext, onMount, untrack } from 'svelte';
     11   import CapabilityGate from './CapabilityGate.svelte';
     12   import InboxSetup from './InboxSetup.svelte';
     13   import MessageComposer from './MessageComposer.svelte';
     14   import {
     15     IDENTITY_VIEW_CONTEXT,
     16     identityViewSnapshot,
     17     identityViewSession,
     18     subscribeIdentityView,
     19     connectIdentityView,
     20     probeIdentityViewMessaging,
     21     invalidateIdentityView,
     22     type IdentityViewContext
     23   } from '../runtime/view-context.ts';
     24   import {
     25     PUBLIC_RUNTIME_CONTEXT,
     26     publicRuntimeReady,
     27     type PublicRuntimeContext,
     28     type PublicRuntime
     29   } from '../runtime/public-runtime.ts';
     30   import {
     31     createRuntimeInboxSetupView,
     32     closeInboxSetupView,
     33     stopInboxSetupView,
     34     type InboxSetupView
     35   } from '../messaging/inbox-setup-view.ts';
     36   import {
     37     createProductRequestController,
     38     configureProductRequest,
     39     closeProductRequestController,
     40     stopProductRequestWork,
     41     productRequestSnapshot,
     42     type ProductRequestController
     43   } from '../messaging/product-request-controller.ts';
     44   import {
     45     acknowledgeProductViewUncertainty,
     46     type ProductStatusAcknowledgement,
     47     type ProductView,
     48     type ProductViewSnapshot
     49   } from '../catalog/product-view.ts';
     50   import { enquiryReadinessSnapshot } from '../messaging/enquiry-readiness.ts';
     51   import { internalHref } from '../navigation-url.ts';
     52   import {
     53     captureProductEnquiry,
     54     productEnquirySnapshot,
     55     closeProductEnquiry,
     56     type ProductEnquiry
     57   } from '../messaging/product-enquiry.ts';
     58   import Button from './primitives/Button.svelte';
     59   let {
     60     view,
     61     observation,
     62     oncapture,
     63     setup,
     64     onrequest
     65   }: {
     66     view: ProductView;
     67     observation?: ProductViewSnapshot;
     68     oncapture?: (draft: ProductEnquiry) => void;
     69     setup?: InboxSetupView;
     70     onrequest?: (controller: ProductRequestController) => void;
     71   } = $props();
     72   let draft = $state.raw<ProductEnquiry | undefined>(),
     73     captured = $state<ReturnType<typeof productEnquirySnapshot>>(),
     74     workflow = $state<ReturnType<typeof productRequestSnapshot>>(),
     75     request = $state.raw<ProductRequestController | undefined>(),
     76     ownedSetup = $state.raw<InboxSetupView | undefined>();
     77   const identityContext = getContext<IdentityViewContext>(
     78     IDENTITY_VIEW_CONTEXT
     79   );
     80   const publicContext = getContext<PublicRuntimeContext>(
     81     PUBLIC_RUNTIME_CONTEXT
     82   );
     83   let identity = $state(
     84     identityContext
     85       ? identityViewSnapshot(identityContext).identity
     86       : { state: 'guest' as const, reason: 'unavailable' as const }
     87   );
     88   let runtime: PublicRuntime | undefined;
     89   let acknowledgement = $state.raw<ProductStatusAcknowledgement | undefined>();
     90   const readiness = $derived.by(() => {
     91     void observation;
     92     void identity;
     93     return enquiryReadinessSnapshot(
     94       view,
     95       identityContext && identityViewSession(identityContext),
     96       acknowledgement
     97     );
     98   });
     99   function acquire() {
    100     if (!identityContext) return;
    101     identity = identityViewSnapshot(identityContext).identity;
    102     if (!setup && runtime && identity.state === 'messaging_capable') {
    103       if (ownedSetup) closeInboxSetupView(ownedSetup);
    104       ownedSetup = createRuntimeInboxSetupView(identityContext, runtime);
    105     }
    106     configure();
    107   }
    108   function configure() {
    109     const actor = identityContext && identityViewSession(identityContext);
    110     const selected = setup ?? ownedSetup;
    111     if (request && actor && selected)
    112       configureProductRequest(
    113         request,
    114         actor,
    115         selected,
    116         'reviewed_product_request_setup'
    117       );
    118   }
    119   onMount(() => {
    120     let alive = true;
    121     const off = identityContext
    122       ? subscribeIdentityView(identityContext, acquire)
    123       : () => {};
    124     if (publicContext)
    125       void publicRuntimeReady(publicContext).then((value) => {
    126         if (alive) {
    127           runtime = value;
    128           acquire();
    129         }
    130       });
    131     const timer = setInterval(() => {
    132       if (draft) captured = productEnquirySnapshot(draft);
    133       workflow = request && productRequestSnapshot(request);
    134     }, 250);
    135     return () => {
    136       alive = false;
    137       clearInterval(timer);
    138       off();
    139       if (ownedSetup) closeInboxSetupView(ownedSetup);
    140     };
    141   });
    142   $effect(() => {
    143     void setup;
    144     configure();
    145   });
    146   $effect(() => {
    147     const original = view;
    148     return () =>
    149       untrack(() => {
    150         void original;
    151         if (draft) closeProductEnquiry(draft);
    152         if (request) closeProductRequestController(request);
    153         request = undefined;
    154         draft = undefined;
    155         captured = undefined;
    156       });
    157   });
    158   $effect(() => {
    159     void observation;
    160     if (draft) captured = productEnquirySnapshot(draft);
    161   });
    162   function open() {
    163     if (draft) closeProductEnquiry(draft);
    164     draft = captureProductEnquiry(
    165       view,
    166       'reviewed_product_enquiry',
    167       acknowledgement
    168     );
    169     captured = draft ? productEnquirySnapshot(draft) : undefined;
    170     if (draft) oncapture?.(draft);
    171     request = draft && createProductRequestController(draft, runtime);
    172     configure();
    173     if (request) onrequest?.(request);
    174   }
    175   function cancelSetup() {
    176     if (request)
    177       stopProductRequestWork(request, 'reviewed_product_request_cancel');
    178     if (identityContext) invalidateIdentityView(identityContext);
    179     stopInboxSetupView(setup ?? ownedSetup);
    180   }
    181   function discard() {
    182     cancelSetup();
    183     if (request) closeProductRequestController(request);
    184     if (draft) closeProductEnquiry(draft);
    185     request = undefined;
    186     draft = undefined;
    187     captured = undefined;
    188   }
    189 </script>
    190 
    191 {#if readiness.action === 'own'}
    192   <p>
    193     This is your listing. Manage it rather than sending yourself an enquiry.
    194   </p>
    195   <a href={internalHref(`/products/${readiness.naddr}/edit`)}>Manage listing</a>
    196 {:else if readiness.action === 'sold'}
    197   <p>
    198     The seller marked this listing sold. New requests are unavailable; existing
    199     conversations remain in Messages.
    200   </p>
    201 {:else if readiness.action === 'withdrawn'}
    202   <p>
    203     This listing was withdrawn. New requests are unavailable; existing
    204     conversations remain in Messages.
    205   </p>
    206 {/if}
    207 {#if captured}
    208   <section class="stack" aria-label="Product request">
    209     <h2>Message about {captured.title}</h2>
    210     <p>Publisher: <code>{captured.peer}</code></p>
    211     <p>Advertised price (seller assertion): {captured.advertisedPrice}</p>
    212     {#if request}<MessageComposer
    213         {request}
    214         oncancel={cancelSetup}
    215         ondiscard={discard}
    216       />{/if}
    217     <details>
    218       <summary>Included product references</summary>
    219       <p>Product: <code>nostr:{captured.naddr}</code></p>
    220       <p>Viewed listing: <code>nostr:{captured.nevent}</code></p>
    221     </details>
    222     {#if !captured.listingCurrent}<p role="status">
    223         Listing status needs review. Your unsent text is kept in memory.
    224       </p>{/if}
    225     <p>
    226       This is a message, not a confirmed order. It does not reserve the food.
    227     </p>
    228     <CapabilityGate
    229       {identity}
    230       required="messaging"
    231       compact={true}
    232       primary={true}
    233       onconnect={identityContext
    234         ? () => connectIdentityView(identityContext)
    235         : undefined}
    236       onprobe={identityContext
    237         ? () =>
    238             probeIdentityViewMessaging(identityContext, 'reviewed_self_copy')
    239         : undefined}
    240     />
    241     {#if identity.state === 'messaging_capable'}
    242       {#key setup ?? ownedSetup}<InboxSetup
    243           controller={setup ?? ownedSetup}
    244           inline={true}
    245         />{/key}
    246     {/if}
    247     {#if !workflow || workflow.state === 'idle'}<p>
    248         Connect and prepare your inbox to send a request. No message has been
    249         sent.
    250       </p>{/if}
    251   </section>
    252 {:else}
    253   {#if readiness.action === 'acknowledgement_required'}
    254     <p>
    255       The listing status check is incomplete. Availability and these last-known
    256       terms may have changed.
    257     </p>
    258     <Button
    259       label="I understand the listing status is uncertain"
    260       onclick={() => {
    261         acknowledgement = acknowledgeProductViewUncertainty(
    262           view,
    263           'reviewed_last_known_listing'
    264         );
    265       }}
    266     />
    267   {/if}
    268   {#if readiness.action === 'available' || readiness.action === 'acknowledgement_required'}<Button
    269       label="Message seller"
    270       onclick={open}
    271       disabled={readiness.action !== 'available'}
    272     />{/if}
    273 {/if}