aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/components/dashboard/preview/content-renderers/AmazonRenderer.tsx
blob: aaf33565741b1e23bf67024795bfc5d7864995bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { ShoppingCart } from "lucide-react";

import { BookmarkTypes, ZBookmark } from "@karakeep/shared/types/bookmarks";

import { ContentRenderer } from "./types";

function extractAmazonProductInfo(
  url: string,
): { asin: string; domain: string } | null {
  const patterns = [
    // Standard product URLs
    /amazon\.([a-z.]+)\/.*\/dp\/([A-Z0-9]{10})/,
    /amazon\.([a-z.]+)\/dp\/([A-Z0-9]{10})/,
    // Shortened URLs
    /amazon\.([a-z.]+)\/gp\/product\/([A-Z0-9]{10})/,
    // Mobile URLs
    /amazon\.([a-z.]+)\/.*\/product\/([A-Z0-9]{10})/,
    // International variations
    /amazon\.([a-z.]+)\/.*\/([A-Z0-9]{10})/,
  ];

  for (const pattern of patterns) {
    const match = url.match(pattern);
    if (match) {
      return {
        domain: match[1],
        asin: match[2],
      };
    }
  }
  return null;
}

function canRenderAmazon(bookmark: ZBookmark): boolean {
  if (bookmark.content.type !== BookmarkTypes.LINK) {
    return false;
  }

  const url = bookmark.content.url;
  return extractAmazonProductInfo(url) !== null;
}

function AmazonRendererComponent({ bookmark }: { bookmark: ZBookmark }) {
  if (bookmark.content.type !== BookmarkTypes.LINK) {
    return null;
  }

  const productInfo = extractAmazonProductInfo(bookmark.content.url);
  if (!productInfo) {
    return null;
  }

  const { title, description, imageUrl } = bookmark.content;

  return (
    <div className="relative h-full w-full overflow-auto">
      <div className="mx-auto flex max-w-2xl flex-col items-center p-6">
        {/* Product Image */}
        {imageUrl && (
          <div className="mb-6 w-full max-w-md">
            <img
              src={imageUrl}
              alt={title || "Amazon Product"}
              className="h-auto max-h-96 w-full rounded-lg object-contain shadow-lg"
            />
          </div>
        )}

        {/* Product Info Card */}
        <div className="w-full rounded-lg border bg-card p-6 shadow-sm">
          {/* Title */}
          {title && (
            <h2 className="mb-4 line-clamp-3 text-xl font-semibold">{title}</h2>
          )}

          {/* Description */}
          {description && (
            <p className="mb-6 line-clamp-4 text-muted-foreground">
              {description}
            </p>
          )}

          {/* Product Details */}
          <div className="mb-6 space-y-3">
            <div className="flex items-center gap-2 text-sm">
              <span className="font-medium">ASIN:</span>
              <span className="font-mono text-muted-foreground">
                {productInfo.asin}
              </span>
            </div>
            <div className="flex items-center gap-2 text-sm">
              <span className="font-medium">Domain:</span>
              <span className="text-muted-foreground">
                amazon.{productInfo.domain}
              </span>
            </div>
          </div>

          {/* Action Buttons */}
          <div className="flex gap-3">
            <a
              href={bookmark.content.url}
              target="_blank"
              rel="noopener noreferrer"
              className="flex flex-1 items-center justify-center gap-2 rounded-md bg-[#FF9900] px-4 py-2 text-center font-medium text-white transition-colors hover:bg-[#FF9900]/90"
            >
              <ShoppingCart size={16} />
              View on Amazon
            </a>
            <a
              href={`https://www.amazon.${productInfo.domain}/dp/${productInfo.asin}`}
              target="_blank"
              rel="noopener noreferrer"
              className="rounded-md border border-border px-4 py-2 text-sm transition-colors hover:bg-accent"
            >
              Direct Link
            </a>
          </div>

          {/* Amazon Disclaimer */}
          <p className="mt-4 text-center text-xs text-muted-foreground">
            Product information from Amazon. Prices and availability may vary.
          </p>
        </div>
      </div>
    </div>
  );
}

export const amazonRenderer: ContentRenderer = {
  id: "amazon",
  name: "Amazon Product",
  icon: ShoppingCart,
  canRender: canRenderAmazon,
  component: AmazonRendererComponent,
  priority: 10,
};