app

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

product-enquiry.ts (4398B)


      1 import {
      2   productViewListingOwnership,
      3   type ProductView,
      4   type ProductStatusAcknowledgement
      5 } from '../catalog/product-view.ts';
      6 import {
      7   captureEnquiryContext,
      8   enquiryContextSnapshot,
      9   buildEnquiryMessage,
     10   type EnquiryContextSnapshot,
     11   type EnquiryContext
     12 } from './enquiry-context.ts';
     13 import { openInboxConversation, type InboxView } from './inbox-view.ts';
     14 import type { MessageWireParts } from '../contracts/message-v1/index.ts';
     15 
     16 declare const enquiryBrand: unique symbol;
     17 export type ProductEnquiry = Readonly<{ readonly [enquiryBrand]: true }>;
     18 type Snapshot = EnquiryContextSnapshot &
     19   Readonly<{
     20     text: string;
     21     revision: number;
     22     memoryOnly: true;
     23     saved: false;
     24     listingCurrent: boolean;
     25   }>;
     26 type Owner = {
     27   ownership():
     28     Readonly<{ context: EnquiryContext; current(): boolean }> | undefined;
     29   snapshot(): Snapshot | undefined;
     30   message(): MessageWireParts | undefined;
     31   update(text: unknown, review: unknown): boolean;
     32   reuse(view: InboxView, review: unknown): Promise<string | undefined>;
     33   close(): void;
     34 };
     35 const owners = new WeakMap<ProductEnquiry, Owner>();
     36 
     37 // A guest may capture and edit a memory-only draft. This is neither identity,
     38 // inbox readiness nor Send authority, and performs no network/native/SDK call.
     39 export function captureProductEnquiry(
     40   view: ProductView,
     41   review: unknown,
     42   acknowledgement?: ProductStatusAcknowledgement
     43 ): ProductEnquiry | undefined {
     44   if (typeof window === 'undefined' || review !== 'reviewed_product_enquiry')
     45     return;
     46   const listing = productViewListingOwnership(view, acknowledgement);
     47   if (!listing?.current()) return;
     48   const context = captureEnquiryContext(listing.proof);
     49   const observed = context && enquiryContextSnapshot(context);
     50   if (!context || !observed || observed.status !== 'active') return;
     51   let active = true,
     52     revision = 0,
     53     text = `Hello, I am interested in ${JSON.stringify(observed.title)}. Is it available?`;
     54   if (!buildEnquiryMessage(context, text)) return;
     55   function close() {
     56     active = false;
     57     text = '';
     58   }
     59   function current() {
     60     if (!active || !listing!.available()) {
     61       close();
     62       return false;
     63     }
     64     return true;
     65   }
     66   const token = Object.freeze({}) as ProductEnquiry;
     67   owners.set(token, {
     68     ownership: () =>
     69       current()
     70         ? { context, current: () => current() && listing.current() }
     71         : undefined,
     72     snapshot() {
     73       return current()
     74         ? {
     75             ...observed,
     76             text,
     77             revision,
     78             memoryOnly: true,
     79             saved: false,
     80             listingCurrent: listing.current()
     81           }
     82         : undefined;
     83     },
     84     message() {
     85       return current() && listing.current()
     86         ? buildEnquiryMessage(context, text)
     87         : undefined;
     88     },
     89     update(value, permission) {
     90       if (
     91         permission !== 'reviewed_product_enquiry_edit' ||
     92         !current() ||
     93         typeof value !== 'string' ||
     94         !buildEnquiryMessage(context, value)
     95       )
     96         return false;
     97       text = value;
     98       revision++;
     99       return true;
    100     },
    101     async reuse(inbox, permission) {
    102       if (
    103         permission !== 'reviewed_product_enquiry_conversation' ||
    104         !current() ||
    105         !listing.current()
    106       )
    107         return;
    108       const path = await openInboxConversation(
    109         inbox,
    110         observed.peer,
    111         'reviewed_admitted_conversation_navigation'
    112       );
    113       return current() && listing.current() ? path : undefined;
    114     },
    115     close
    116   });
    117   return token;
    118 }
    119 export function productEnquirySnapshot(
    120   token: ProductEnquiry
    121 ): Snapshot | undefined {
    122   return owners.get(token)?.snapshot();
    123 }
    124 export function productEnquiryMessage(
    125   token: ProductEnquiry
    126 ): MessageWireParts | undefined {
    127   return owners.get(token)?.message();
    128 }
    129 export function updateProductEnquiry(
    130   token: ProductEnquiry,
    131   text: unknown,
    132   review: unknown
    133 ): boolean {
    134   return owners.get(token)?.update(text, review) ?? false;
    135 }
    136 export function reuseProductEnquiryConversation(
    137   token: ProductEnquiry,
    138   view: InboxView,
    139   review: unknown
    140 ): Promise<string | undefined> {
    141   return owners.get(token)?.reuse(view, review) ?? Promise.resolve(undefined);
    142 }
    143 export function closeProductEnquiry(token: ProductEnquiry): void {
    144   owners.get(token)?.close();
    145 }
    146 export function productEnquirySendOwnership(token: ProductEnquiry) {
    147   return owners.get(token)?.ownership();
    148 }