app

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

MessageItem.svelte (5095B)


      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 { onMount, untrack } from 'svelte';
     11   import type { PublicRuntime } from '../runtime/public-runtime.ts';
     12   import {
     13     captureCitedProduct,
     14     citedProductSnapshot,
     15     closeCitedProduct,
     16     type CitedProduct
     17   } from '../messaging/cited-product.ts';
     18   import {
     19     inboxMessageSnapshot,
     20     inboxThreadRevision,
     21     subscribeInboxView,
     22     type InboxView
     23   } from '../messaging/inbox-view.ts';
     24   import type { ThreadMessage } from '../messaging/conversation-thread.ts';
     25   import { internalHref } from '../navigation-url.ts';
     26   let {
     27     controller,
     28     conversationId,
     29     message,
     30     onreply,
     31     runtime
     32   }: {
     33     controller: InboxView;
     34     conversationId: string;
     35     message: ThreadMessage;
     36     onreply?: (parent: string) => void;
     37     runtime?: PublicRuntime;
     38   } = $props();
     39   // Copied rows select only a hash. Render fresh current original room bytes.
     40   let observation = $state(0);
     41   const current = $derived.by(() => {
     42     void observation;
     43     return inboxMessageSnapshot(
     44       controller,
     45       conversationId,
     46       message.data.rumorId
     47     );
     48   });
     49   $effect(() => {
     50     const view = controller;
     51     let previous = -2,
     52       changes = 0;
     53     return untrack(() =>
     54       subscribeInboxView(view, () => {
     55         const next = inboxThreadRevision(view);
     56         if (next === previous) return;
     57         previous = next;
     58         changes += 1;
     59         observation = changes;
     60       })
     61     );
     62   });
     63   const time = $derived(
     64     current ? new Date(current.data.createdAt * 1000) : undefined
     65   );
     66   let citation = $state.raw<CitedProduct | undefined>(),
     67     context = $state<ReturnType<typeof citedProductSnapshot>>();
     68   function verify(value: string) {
     69     if (citation) closeCitedProduct(citation);
     70     citation =
     71       current && runtime
     72         ? captureCitedProduct(
     73             controller,
     74             conversationId,
     75             current.data.rumorId,
     76             value,
     77             runtime,
     78             'reviewed_product_citation'
     79           )
     80         : undefined;
     81     context = citation ? citedProductSnapshot(citation) : undefined;
     82   }
     83   onMount(() => {
     84     const timer = setInterval(() => {
     85       if (citation) context = citedProductSnapshot(citation);
     86     }, 250);
     87     return () => {
     88       clearInterval(timer);
     89       if (citation) closeCitedProduct(citation);
     90     };
     91   });
     92 </script>
     93 
     94 {#if current}
     95   <article class="stack" data-message-id={current.data.rumorId}>
     96     <div class="cluster">
     97       <span
     98         >{current.data.role === 'self_archive'
     99           ? 'You'
    100           : 'Peer ' + current.data.peer.slice(0, 8) + '…'}</span
    101       >
    102       {#if time && !Number.isNaN(time.getTime())}<time
    103           datetime={time.toISOString()}>{time.toLocaleString()}</time
    104         >{/if}
    105     </div>
    106     {#each current.data.content.split('\n') as line, index (index)}<p>
    107         {line}
    108       </p>{/each}
    109     {#if current.parent === 'missing'}<p class="text-small">
    110         Earlier message unavailable in this browser.
    111       </p>{/if}
    112     {#each current.citations as citation (citation.naddr)}
    113       <p>
    114         <a href={internalHref('/products/' + citation.naddr)}
    115           >Product citation: {citation.identifier || 'listing'} (unverified)</a
    116         >
    117       </p>
    118       {#if runtime}<button type="button" onclick={() => verify(citation.naddr)}
    119           >Verify product citation</button
    120         >{/if}
    121     {/each}
    122     {#if context}
    123       {#if context.outcome === 'checking'}<p>Checking listing citation…</p>
    124       {:else if context.outcome === 'withdrawn'}<p>Listing is withdrawn</p>
    125       {:else if context.food}<p>{context.food.title}</p>
    126         <p>
    127           {context.outcome === 'sold'
    128             ? 'Listing is sold'
    129             : 'Signed listing observed'}
    130         </p>
    131       {:else}<p>Listing citation remains unresolved</p>{/if}
    132       {#if context.lastKnownFood}<p>{context.lastKnownFood.title}</p>
    133         <p>
    134           Last-known signed listing; current head and withdrawal checks are
    135           incomplete.
    136         </p>{/if}
    137       {#if context.includedVersion === 'matched'}<p>
    138           Included version verified
    139         </p>{:else if context.includedVersion === 'different'}<p>
    140           Included version differs from the observed listing
    141         </p>{/if}
    142     {/if}
    143     {#if current.citations.length || /quoted price/i.test(current.data.content)}<p
    144         class="text-small"
    145       >
    146         A quoted price is not a current or locked quote.
    147       </p>{/if}
    148     {#each current.delivery ?? [] as label (label)}<p class="text-small">
    149         {label}
    150       </p>{/each}
    151     {#if onreply}<button
    152         type="button"
    153         class="button button--secondary"
    154         onclick={() => {
    155           const actual = inboxMessageSnapshot(
    156             controller,
    157             conversationId,
    158             message.data.rumorId
    159           );
    160           if (actual) onreply?.(actual.data.rumorId);
    161         }}>Reply to this message</button
    162       >{/if}
    163   </article>
    164 {/if}