app

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

ConversationThread.svelte (7712B)


      1 <script lang="ts">
      2   import { getContext, onMount, tick } from 'svelte';
      3   import InboxStatus from './InboxStatus.svelte';
      4   import Button from './primitives/Button.svelte';
      5   import MessageItem from './MessageItem.svelte';
      6   import ConversationOptions from './ConversationOptions.svelte';
      7   import MessageComposer from './MessageComposer.svelte';
      8   import {
      9     createReplyController,
     10     createRuntimeReplyController,
     11     selectReplyParent,
     12     replySnapshot,
     13     closeReplyController,
     14     type ReplyController
     15   } from '../messaging/reply-controller.ts';
     16   import {
     17     IDENTITY_VIEW_CONTEXT,
     18     identityViewSession,
     19     subscribeIdentityView,
     20     type IdentityViewContext
     21   } from '../runtime/view-context.ts';
     22   import {
     23     PUBLIC_RUNTIME_CONTEXT,
     24     publicRuntimeReady,
     25     type PublicRuntimeContext,
     26     type PublicRuntime
     27   } from '../runtime/public-runtime.ts';
     28   import {
     29     createRuntimeInboxSetupView,
     30     closeInboxSetupView
     31   } from '../messaging/inbox-setup-view.ts';
     32   import {
     33     createInboxView,
     34     inboxViewSnapshot,
     35     inboxViewSetup,
     36     subscribeInboxView,
     37     closeInboxView,
     38     inboxThreadSnapshot,
     39     inboxThreadRevision,
     40     inboxConversationBlocked,
     41     markInboxConversationDisplayed,
     42     type InboxView
     43   } from '../messaging/inbox-view.ts';
     44   let {
     45     controller,
     46     conversationId
     47   }: { controller?: InboxView; conversationId: string } = $props();
     48   const identity = getContext<IdentityViewContext>(IDENTITY_VIEW_CONTEXT),
     49     publicContext = getContext<PublicRuntimeContext>(PUBLIC_RUNTIME_CONTEXT);
     50   let owned = $state.raw<InboxView | undefined>(),
     51     reply = $state.raw<ReplyController | undefined>(),
     52     model = $state(inboxThreadSnapshot(undefined, '')),
     53     announcement = $state(''),
     54     readProblem = $state(false),
     55     retryRevision = $state(0),
     56     displayedPending = $state(false);
     57   let runtime = $state.raw<PublicRuntime | undefined>();
     58   let off = () => {},
     59     initialized = false,
     60     previous = Array.from<string>([]),
     61     attempted = Array.from<string>([]),
     62     disposed = false;
     63   function captureReply(parent: string) {
     64     if (!owned) return;
     65     return runtime
     66       ? createRuntimeReplyController(owned, conversationId, parent, runtime)
     67       : createReplyController(owned, conversationId, parent);
     68   }
     69   function replyTo(parent: string) {
     70     if (reply && replySnapshot(reply)?.dirty) {
     71       selectReplyParent(reply, parent, 'reviewed_reply_parent');
     72       return;
     73     }
     74     if (reply) closeReplyController(reply);
     75     reply = captureReply(parent);
     76   }
     77   onMount(() => {
     78     let observedRevision = -2,
     79       observedMode = '';
     80     function acquire() {
     81       if (disposed) return;
     82       if (owned && inboxViewSnapshot(owned).owner) return;
     83       off();
     84       if (owned && !controller) closeInboxView(owned);
     85       owned = controller;
     86       if (!owned && runtime) {
     87         const session = identityViewSession(identity),
     88           setup = createRuntimeInboxSetupView(identity, runtime);
     89         if (session && setup) {
     90           owned = createInboxView({ identity: session, setup, conversationId });
     91           if (!owned) closeInboxSetupView(setup);
     92         }
     93       }
     94       observedRevision = -2;
     95       off = subscribeInboxView(owned, (panel) => {
     96         const revision = inboxThreadRevision(owned),
     97           mode = panel.state + ':' + panel.count + ':' + (panel.reason ?? '');
     98         if (revision === observedRevision && mode === observedMode) return;
     99         observedRevision = revision;
    100         observedMode = mode;
    101         const next = inboxThreadSnapshot(owned, conversationId);
    102         if (next.status !== 'ready') {
    103           if (reply) closeReplyController(reply);
    104           reply = undefined;
    105           model = next;
    106           announcement = '';
    107           readProblem = false;
    108           initialized = false;
    109           previous = [];
    110           attempted = [];
    111           return;
    112         }
    113         const ids = next.messages.map((row) => row.data.rumorId),
    114           added = ids.filter((id) => !previous.includes(id)).length;
    115         if (ids.length > 0 && initialized && added)
    116           announcement =
    117             added + (added === 1 ? ' message added.' : ' messages added.');
    118         if (ids.length > 0) initialized = true;
    119         previous = ids;
    120         model = next;
    121         if (!reply && ids.length) reply = captureReply(ids[ids.length - 1]);
    122       });
    123     }
    124     const identityOff = identity
    125       ? subscribeIdentityView(identity, acquire)
    126       : () => {};
    127     acquire();
    128     if (!controller && publicContext)
    129       void publicRuntimeReady(publicContext)
    130         .then((value) => {
    131           if (!disposed) {
    132             runtime = value;
    133             acquire();
    134           }
    135         })
    136         .catch(() => {});
    137     return () => {
    138       disposed = true;
    139       identityOff();
    140       off();
    141       if (reply) closeReplyController(reply);
    142       if (owned && !controller) {
    143         const setup = inboxViewSetup(owned);
    144         closeInboxView(owned);
    145         if (setup) closeInboxSetupView(setup);
    146       }
    147     };
    148   });
    149   $effect(() => {
    150     const original = owned,
    151       selected = conversationId,
    152       rows = model.messages;
    153     void retryRevision;
    154     if (!original || model.status !== 'ready' || displayedPending) return;
    155     const ids = rows
    156       .map((row) => row.data.rumorId)
    157       .filter((id) => !attempted.includes(id));
    158     if (!ids.length) return;
    159     attempted = attempted.concat(ids);
    160     displayedPending = true;
    161     void tick()
    162       .then(async () => {
    163         if (disposed || owned !== original || conversationId !== selected)
    164           return;
    165         const actual = inboxThreadSnapshot(original, selected);
    166         if (
    167           actual.status !== 'ready' ||
    168           ids.some(
    169             (id) => !actual.messages.some((row) => row.data.rumorId === id)
    170           )
    171         )
    172           return;
    173         const result = await markInboxConversationDisplayed(
    174           original,
    175           selected,
    176           ids,
    177           'reviewed_displayed_conversation'
    178         );
    179         if (!disposed && owned === original && conversationId === selected)
    180           readProblem = !result;
    181       })
    182       .finally(() => {
    183         displayedPending = false;
    184       });
    185   });
    186 </script>
    187 
    188 {#if owned}<InboxStatus
    189     controller={owned}
    190     showLists={false}
    191     manageController={false}
    192   />{:else}<InboxStatus showLists={false} manageController={false} />{/if}
    193 <div class="page page--reading stack">
    194   <h2>Conversation</h2>
    195   {#if owned && model.status === 'ready'}
    196     <ConversationOptions controller={owned} {conversationId} />
    197     {#each model.messages as message (message.data.rumorId)}<MessageItem
    198         controller={owned}
    199         {conversationId}
    200         {message}
    201         {runtime}
    202         onreply={replyTo}
    203       />{/each}
    204     {#if reply}{#key reply}<MessageComposer
    205           controller={reply}
    206           focusScope="conversation"
    207         />{/key}{/if}
    208   {:else if owned && inboxConversationBlocked(owned, conversationId)}
    209     <p>
    210       This person is blocked locally. Messages and previews are hidden;
    211       encrypted evidence and remote copies remain.
    212     </p>
    213     <a href="/privacy">Review local blocks and Undo</a>
    214   {:else}<p>
    215       This conversation is unavailable until it is unlocked in the matching
    216       browser and account.
    217     </p>{/if}
    218   {#if readProblem}<p class="notice">
    219       Local read status could not be confirmed. Encrypted copies are preserved.
    220     </p>
    221     <Button
    222       label="Retry local read status"
    223       disabled={displayedPending}
    224       onclick={() => {
    225         attempted = [];
    226         readProblem = false;
    227         retryRevision++;
    228       }}
    229     />{/if}
    230   <p role="status" aria-live="polite" data-thread-announcement>
    231     {announcement}
    232   </p>
    233   <a href="/messages">Back to messages</a>
    234 </div>