app

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

listing-view.ts (2226B)


      1 import type { HeadResolution } from './resolve-head.ts';
      2 import type { FoodProjection } from '../contracts/food-availability-v1/read.ts';
      3 import { encodeProductReference } from '../nostr/references.ts';
      4 import { internalHref } from '../navigation-url.ts';
      5 import type { PublisherView } from './publishers.ts';
      6 export type ListingView = Readonly<{
      7   eventId: string;
      8   createdAt: number;
      9   identifier: string;
     10   href: string;
     11   title: string;
     12   summary: string;
     13   content: string;
     14   location: string;
     15   price: FoodProjection['price'];
     16   quantity: FoodProjection['quantity'];
     17   publisher: PublisherView;
     18   lastKnown: boolean;
     19   coverage: HeadResolution['coverage'];
     20 }>;
     21 // Only the genuine resolver supplies these rows. Ordering follows winner,
     22 // deletion, future and focused admission and local matching, before pagination.
     23 export function orderFoodRows(
     24   rows: readonly HeadResolution[]
     25 ): readonly HeadResolution[] {
     26   return rows.toSorted(
     27     (a, b) =>
     28       b.state.head.created_at - a.state.head.created_at ||
     29       (a.state.head.id < b.state.head.id
     30         ? -1
     31         : a.state.head.id > b.state.head.id
     32           ? 1
     33           : 0)
     34   );
     35 }
     36 export function listingView(
     37   row: HeadResolution,
     38   publisher?: PublisherView
     39 ): ListingView | undefined {
     40   const food = row.state.food,
     41     head = row.state.head;
     42   if (
     43     !food ||
     44     food.status !== 'active' ||
     45     row.deletion.outcome === 'suppressed'
     46   )
     47     return undefined;
     48   const naddr = encodeProductReference({
     49     kind: 30402,
     50     pubkey: head.pubkey,
     51     identifier: food.identifier
     52   });
     53   const href =
     54     naddr === undefined ? undefined : internalHref(`/products/${naddr}`);
     55   if (href === undefined) return undefined;
     56   return {
     57     eventId: head.id,
     58     createdAt: head.created_at,
     59     identifier: food.identifier,
     60     href,
     61     title: food.title,
     62     summary: food.summary,
     63     content: food.content,
     64     location: food.location,
     65     price: { ...food.price },
     66     quantity: food.quantity === null ? null : { ...food.quantity },
     67     publisher:
     68       publisher?.pubkey === head.pubkey
     69         ? { ...publisher }
     70         : { pubkey: head.pubkey, label: head.pubkey, assertedName: false },
     71     lastKnown: row.lastKnown,
     72     coverage: row.coverage
     73   };
     74 }