app

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

InboxStatus.svelte (10869B)


      1 <script lang="ts">
      2   import { getContext, onMount } from 'svelte';
      3   import { goto } from '$app/navigation';
      4   import { internalHref } from '../navigation-url.ts';
      5   import ConversationRow from './ConversationRow.svelte';
      6   import CapabilityGate from './CapabilityGate.svelte';
      7   import InboxSetup from './InboxSetup.svelte';
      8   import Button from './primitives/Button.svelte';
      9   import {
     10     IDENTITY_VIEW_CONTEXT,
     11     identityViewSnapshot,
     12     identityViewSession,
     13     subscribeIdentityView,
     14     connectIdentityView,
     15     probeIdentityViewMessaging,
     16     disconnectIdentityView,
     17     type IdentityViewContext
     18   } from '../runtime/view-context.ts';
     19   import {
     20     PUBLIC_RUNTIME_CONTEXT,
     21     publicRuntimeReady,
     22     type PublicRuntimeContext,
     23     type PublicRuntime
     24   } from '../runtime/public-runtime.ts';
     25   import {
     26     createRuntimeInboxSetupView,
     27     closeInboxSetupView
     28   } from '../messaging/inbox-setup-view.ts';
     29   import {
     30     createInboxView,
     31     inboxViewSnapshot,
     32     inboxListSnapshot,
     33     inboxListRevision,
     34     openInboxConversation,
     35     loadOlderInboxView,
     36     inboxViewOlderAvailable,
     37     inboxViewSetup,
     38     subscribeInboxView,
     39     unlockInboxView,
     40     checkInboxView,
     41     readNewInboxView,
     42     stopInboxView,
     43     closeInboxView,
     44     type InboxView
     45   } from '../messaging/inbox-view.ts';
     46   let {
     47     controller,
     48     showLists = true,
     49     manageController = true
     50   }: {
     51     controller?: InboxView;
     52     showLists?: boolean;
     53     manageController?: boolean;
     54   } = $props();
     55   const identityContext = getContext<IdentityViewContext>(
     56       IDENTITY_VIEW_CONTEXT
     57     ),
     58     publicContext = getContext<PublicRuntimeContext>(PUBLIC_RUNTIME_CONTEXT);
     59   let identity = $state(identityViewSnapshot(identityContext)),
     60     panel = $state(inboxViewSnapshot(undefined)),
     61     list = $state(inboxListSnapshot(undefined)),
     62     olderAvailable = $state(false),
     63     owned = $state.raw<InboxView | undefined>();
     64   let runtime: PublicRuntime | undefined,
     65     off = () => {};
     66   const setup = $derived(inboxViewSetup(owned));
     67   onMount(() => {
     68     let observedRevision = -2,
     69       observedMode = '';
     70     function updateList() {
     71       const revision = inboxListRevision(owned),
     72         mode = panel.state + ':' + panel.count + ':' + (panel.reason ?? '');
     73       if (revision !== observedRevision || mode !== observedMode) {
     74         list = inboxListSnapshot(owned);
     75         observedRevision = revision;
     76         observedMode = mode;
     77       }
     78     }
     79     let disposed = false;
     80     function acquire() {
     81       if (disposed) return;
     82       identity = identityViewSnapshot(identityContext);
     83       if (owned && inboxViewSnapshot(owned).owner) return;
     84       off();
     85       if (owned && !controller) {
     86         const oldSetup = inboxViewSetup(owned);
     87         closeInboxView(owned);
     88         if (oldSetup) closeInboxSetupView(oldSetup);
     89       }
     90       owned = controller;
     91       if (
     92         !owned &&
     93         runtime &&
     94         identity.identity.state === 'messaging_capable'
     95       ) {
     96         const session = identityViewSession(identityContext),
     97           originalSetup = createRuntimeInboxSetupView(identityContext, runtime);
     98         if (session && originalSetup) {
     99           owned = createInboxView({ identity: session, setup: originalSetup });
    100           if (!owned) closeInboxSetupView(originalSetup);
    101         }
    102       }
    103       panel = inboxViewSnapshot(owned);
    104       observedRevision = -2;
    105       updateList();
    106       olderAvailable = inboxViewOlderAvailable(owned);
    107       off = subscribeInboxView(owned, (next) => {
    108         panel = next;
    109         updateList();
    110         olderAvailable = inboxViewOlderAvailable(owned);
    111       });
    112     }
    113     const identityOff = identityContext
    114       ? subscribeIdentityView(identityContext, acquire)
    115       : () => {};
    116     acquire();
    117     if (manageController && !controller && publicContext)
    118       void publicRuntimeReady(publicContext)
    119         .then((value) => {
    120           if (disposed) return;
    121           runtime = value;
    122           acquire();
    123         })
    124         .catch(() => {
    125           /* Keep genuine unavailable state. */
    126         });
    127     return () => {
    128       disposed = true;
    129       identityOff();
    130       off();
    131       if (owned && !controller) {
    132         const originalSetup = inboxViewSetup(owned);
    133         closeInboxView(owned);
    134         if (originalSetup) closeInboxSetupView(originalSetup);
    135       }
    136     };
    137   });
    138   async function openConversation(peer: string) {
    139     const original = owned;
    140     if (!original) return;
    141     const result = await openInboxConversation(
    142       original,
    143       peer,
    144       'reviewed_admitted_conversation_navigation'
    145     );
    146     const href = internalHref(result);
    147     if (owned !== original || inboxListSnapshot(original).status !== 'ready')
    148       return;
    149     if (href) void goto(href);
    150   }
    151   const effectiveIdentity = $derived(
    152     panel.owner ? panel.identity : identity.identity
    153   );
    154   const reasons: Record<string, string> = {
    155     local_preferences_unavailable:
    156       'Local private block preferences could not be checked. Private messages and decryption continuation are unavailable; encrypted evidence is preserved.',
    157     local_block_changed:
    158       'Local block preferences changed during this batch. Further decryption needs another explicit reviewed action.',
    159     access_unavailable:
    160       'Inbox access has not been qualified and exercised. Unlocked local copies do not prove a successful relay check.',
    161     storage_unavailable:
    162       'Browser storage is unavailable. Your inbox has not been checked.',
    163     unavailable:
    164       'The message check could not complete. Existing encrypted copies are preserved.',
    165     elapsed:
    166       'The bounded inbox check reached its active work limit. Existing encrypted copies are preserved.',
    167     budget:
    168       'The bounded inbox check reached its item or size limit. Continue only after reviewing the limit.',
    169     declined:
    170       'Your extension did not approve decryption. Choose Read new messages to try another explicit batch.',
    171     refused:
    172       'Your extension did not approve decryption. Choose Read new messages to try another explicit batch.',
    173     stopped: 'Messages are locked. Existing encrypted copies are preserved.',
    174     wait_expired:
    175       'Decryption is paused. Wait for the original extension request to settle before another explicit batch.'
    176   };
    177   const checked = $derived(
    178     panel.lastCheckedAt === null
    179       ? null
    180       : new Date(panel.lastCheckedAt).toLocaleTimeString()
    181   );
    182 </script>
    183 
    184 <svelte:head
    185   ><title>HarvestCircle</title><meta
    186     name="robots"
    187     content="noindex"
    188   /></svelte:head
    189 >
    190 <div class="page page--reading stack">
    191   <h1>
    192     {effectiveIdentity.state === 'guest' ||
    193     effectiveIdentity.state === 'pending'
    194       ? 'Connect or unlock'
    195       : 'Messages'}
    196   </h1>
    197   {#if panel.cleanupRequired}<p class="notice">
    198       Private resource cleanup is incomplete. Wait before reconnecting or
    199       starting another message action.
    200     </p>{/if}
    201   <CapabilityGate
    202     identity={effectiveIdentity}
    203     required="messaging"
    204     focusTarget="protected"
    205     onconnect={identity.mounted
    206       ? () => connectIdentityView(identityContext)
    207       : undefined}
    208     onprobe={identity.mounted
    209       ? () => probeIdentityViewMessaging(identityContext, 'reviewed_self_copy')
    210       : undefined}
    211     ondisconnect={() => disconnectIdentityView(identityContext)}
    212   />
    213   {#if panel.state === 'locked' || panel.state === 'unlocking'}
    214     <h2>Unlock your messages</h2>
    215     <p>
    216       Your extension will be asked to decrypt your messages. Each explicit batch
    217       checks up to 20 encrypted envelopes and may require two decryption
    218       approvals for each envelope. Further batches need another explicit action.
    219     </p>
    220     <Button
    221       label="Unlock messages"
    222       disabled={!owned || panel.busy}
    223       onclick={() => {
    224         if (owned) void unlockInboxView(owned, 'reviewed_messages_unlock');
    225       }}
    226     />
    227   {/if}
    228   {#if panel.state === 'partial' || panel.state === 'ready' || panel.state === 'empty' || panel.state === 'unlocking'}
    229     <p role="status" aria-live="polite">
    230       {panel.state === 'unlocking'
    231         ? 'Waiting for your explicit extension approvals.'
    232         : panel.state === 'empty'
    233           ? 'No messages found in the checked inbox history.'
    234           : panel.state === 'partial'
    235             ? 'Your unlocked local messages are available with limitations.'
    236             : 'Messages are unlocked. Check here for replies.'}
    237     </p>
    238     {#if panel.reason}<p class="notice">
    239         {reasons[panel.reason] ??
    240           'The check is partial. Existing encrypted copies are preserved; review the limitation before continuing.'}
    241       </p>{/if}
    242     <Button
    243       label="Check for messages"
    244       disabled={!owned || panel.busy}
    245       onclick={() => {
    246         if (owned) void checkInboxView(owned, 'reviewed_foreground_inbox');
    247       }}
    248     />
    249     {#if checked}<p>
    250         Last checked at {checked}. This was a bounded foreground check, not
    251         complete inbox history.
    252       </p>{/if}
    253     {#if panel.queued > 0}
    254       <p>
    255         {panel.queued} encrypted envelopes await an explicit decryption batch. Your
    256         extension will be asked to decrypt them; up to 20 envelopes are attempted
    257         per batch.
    258       </p>
    259       <Button
    260         label={'Read new messages (' + panel.queued + ')'}
    261         disabled={!owned || panel.busy}
    262         onclick={() => {
    263           if (owned) void readNewInboxView(owned, 'reviewed_decrypt_batch');
    264         }}
    265       />
    266     {/if}
    267     {#if showLists && owned && list.status === 'ready'}
    268       {#if list.requests.length > 0}<section
    269           class="stack"
    270           aria-label="Message requests"
    271         >
    272           <h2>Message requests</h2>
    273           {#each list.requests as row (row.peer)}<ConversationRow
    274               controller={owned}
    275               {row}
    276               onopen={openConversation}
    277             />{/each}
    278         </section>{/if}
    279       {#if list.conversations.length > 0}<section
    280           class="stack"
    281           aria-label="Conversations"
    282         >
    283           <h2>Conversations</h2>
    284           {#each list.conversations as row (row.peer)}<ConversationRow
    285               controller={owned}
    286               {row}
    287               onopen={openConversation}
    288             />{/each}
    289         </section>{/if}
    290       <p class="text-small">
    291         New means locally unread in this browser. No remote read receipt or
    292         background message awareness is implied.
    293       </p>
    294     {/if}
    295     <Button
    296       label="Load older messages"
    297       disabled={!owned || panel.busy || !olderAvailable}
    298       onclick={() => {
    299         if (owned) void loadOlderInboxView(owned, 'reviewed_load_older');
    300       }}
    301     />
    302     <Button
    303       label="Lock messages"
    304       disabled={!owned}
    305       onclick={() => {
    306         if (owned) stopInboxView(owned);
    307       }}
    308     />
    309   {/if}
    310   {#key setup}<InboxSetup controller={setup} />{/key}
    311   <p>
    312     Check here for replies. This prototype does not send push or email
    313     notifications.
    314   </p>
    315 </div>