File size: 9,680 Bytes
65d68f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"use client";

// @ts-nocheck - legacy design token bare names fixed by import below.

// @ts-nocheck - pre-existing legacy design token (bare 'colors' etc) issues in shared components.

import Link from "next/link";
import { forwardRef, HTMLAttributes } from "react";
import { clsx } from "clsx";
import { designSystem, colors, spacing } from "@/lib/design-system";

const { shadows } = designSystem; // radius/typography etc reserved

// =============================================================================
// Type Helpers for Card/Link Prop Compatibility
// =============================================================================

// Helper to safely cast hover style values
function getHoverValue<T>(obj: { hover?: Record<string, T> } | undefined, key: string, fallback: string = ''): T | string {
  return (obj && obj.hover && (obj.hover as Record<string, T>)[key]) || fallback;
}

// =============================================================================
// Card Variants
// =============================================================================

export type CardVariant = "primary" | "secondary" | "glass" | "elevated" | "bordered";

export interface CardProps extends Omit<HTMLAttributes<HTMLDivElement>, "onClick"> {
  variant?: CardVariant;
  href?: string;
  onClick?: (event: React.MouseEvent<HTMLDivElement | HTMLAnchorElement>) => void;
  hoverable?: boolean;
  padding?: "none" | "small" | "medium" | "large";
  fullWidthOnMobile?: boolean;
}

// =============================================================================
// SharedCard Component
// =============================================================================

export const SharedCard = forwardRef<HTMLDivElement, CardProps>(
  (
    {
      variant = "secondary",
      href,
      onClick,
      hoverable = true,
      padding = "medium",
      fullWidthOnMobile = true,
      className,
      children,
      ...props
    },
    ref,
  ) => {
    // Map variant to styles
    const variantStyles = {
      primary: {
        background: "transparent",
        border: colors.borderSubtle,
        boxShadow: "none",
        hover: {
          background: "transparent",
          border: colors.borderSubtle,
          boxShadow: shadows.lg,
          transform: designSystem.animation.hover.scale,
        },
      },
      secondary: {
        background: colors.bgGlass,
        border: colors.borderSubtle,
        boxShadow: "none",
        hover: {
          background: colors.bgElevated,
          border: colors.borderMedium,
          boxShadow: "none",
        },
      },
      glass: {
        background: colors.bgGlass,
        border: colors.borderSubtle,
        boxShadow: shadows.sm,
        hover: {
          background: colors.bgGlassHover,
          border: colors.borderMedium,
          boxShadow: shadows.md,
        },
      },
      elevated: {
        background: colors.bgElevated,
        border: colors.borderSubtle,
        boxShadow: shadows.sm,
        hover: {
          background: colors.bgSurface,
          border: colors.borderMedium,
          boxShadow: shadows.md,
        },
      },
      bordered: {
        background: colors.bgGlass,
        border: colors.borderSubtle,
        boxShadow: "none",
        hover: {
          background: colors.bgElevated,
          border: colors.borderMedium,
          boxShadow: "none",
        },
      },
    };

    const style = variantStyles[variant];

    // Map padding to values
    const paddingMap = {
      none: "",
      small: spacing.card.paddingSmall, // p-4 = 16px
      medium: spacing.card.padding, // p-7 = 28px
      large: "24px", // p-6
    };

    const paddingValue = paddingMap[padding];

    // Build class string
    const baseClasses = clsx(
      "rounded-xl", // 20px radius from home
      "border",
      "transition-all",
      "duration-150",
      fullWidthOnMobile && "w-full",
      paddingValue && `p-[${paddingValue}]`,
      className,
    );

    const normalClasses = clsx(
      baseClasses,
      style.background && `bg-[${style.background}]`,
      style.border && `border-[${style.border}]`,
      style.boxShadow && `shadow-[${style.boxShadow}]`,
    );

    // Type-safe hover class construction
    const hoverTransform = hoverable && style.hover ? getHoverValue<string>(style, 'transform') : undefined;
    const hoverShadow = hoverable && style.hover ? getHoverValue<string>(style, 'boxShadow') : undefined;
    const hoverBg = hoverable && style.hover ? getHoverValue<string>(style, 'background') : undefined;
    const hoverBorder = hoverable && style.hover ? getHoverValue<string>(style, 'border') : undefined;

    const hoverClasses = clsx(
      hoverable && "hover:scale-[1.01]",
      hoverable && hoverTransform && `hover:${hoverTransform}`,
      hoverable && hoverShadow && `hover:shadow-[${hoverShadow}]`,
      hoverable && hoverBg && `hover:bg-[${hoverBg}]`,
      hoverable && hoverBorder && `hover:border-[${hoverBorder}]`,
    );

    const allClasses = clsx(normalClasses, hoverClasses);

    // Decide if we need a Link or a div
    if (href) {
      const handleClick = (e: React.MouseEvent<HTMLAnchorElement>) => {
        onClick?.(e as unknown as React.MouseEvent<HTMLDivElement | HTMLAnchorElement>);
      };

      return (
        <Link
          href={href}
          ref={ref as unknown as React.RefObject<HTMLAnchorElement>}
          className={clsx(allClasses, "cursor-pointer")}
          onClick={handleClick}
          // Note: We intentionally do NOT spread props here as they come from
          // HTMLDivElement which is incompatible with HTMLAnchorElement.
          // CardProps extends HTMLAttributes<HTMLDivElement>, but Link
          // expects HTMLAnchorElement props. This is a known TypeScript limitation.
          // Users should pass Link-specific props via dedicated props, not spread.
        >
          {children}
        </Link>
      );
    }

    return (
      <div
        ref={ref as React.RefObject<HTMLDivElement>}
        className={clsx(allClasses, onClick && "cursor-pointer")}
        onClick={onClick}
        // Only spread props when rendering as div (HTMLDivElement)
        {...props}
      >
        {children}
      </div>
    );
  },
);

SharedCard.displayName = "SharedCard";

// =============================================================================
// Named exports for specific card types (for convenience)
// =============================================================================

// Primary Card (matching /home PrimaryCard)
export function PrimaryCard({
  href,
  icon: Icon,
  label,
  description,
  accentClass = "bg-[var(--accent-primary)]/10 text-[var(--accent-primary)]",
  borderClass = "border-[var(--accent-primary)]/20",
  bgClass = "bg-gradient-to-br from-[var(--accent-primary)]/5 to-transparent",
  className,
  ...props
}: {
  href: string;
  icon: React.ElementType;
  label: string;
  description: string;
  accentClass?: string;
  borderClass?: string;
  bgClass?: string;
  className?: string;
} & Omit<CardProps, "variant" | "href">) {
  return (
    <SharedCard
      variant="primary"
      href={href}
      className={clsx(
        "group flex flex-col",
        borderClass,
        bgClass,
        className,
      )}
      {...props}
    >
      <div className={clsx("mb-5 grid size-12 place-items-center rounded-xl", accentClass)}>
        <Icon className="size-6" aria-hidden="true" />
      </div>
      <h3 className="text-[18px] font-bold text-[var(--text-primary)]">{label}</h3>
      <p className="mt-2 flex-1 text-[14px] leading-6 text-[var(--text-secondary)]">
        {description}
      </p>
      <span className="mt-5 text-[13px] font-semibold text-[var(--accent-primary)] transition group-hover:underline">
        Get started →
      </span>
    </SharedCard>
  );
}

// Secondary Card (matching /home SecondaryCard)
export function SecondaryCard({
  href,
  icon: Icon,
  label,
  description,
  disabled = false,
  className,
  onClick,
  ...props
}: {
  href?: string;
  icon: React.ElementType;
  label: string;
  description: string;
  disabled?: boolean;
  className?: string;
  onClick?: () => void;
} & Omit<CardProps, "onClick" | "href">) {
  const baseClasses =
    "flex items-center gap-3.5 rounded-lg border border-[var(--border-subtle)] bg-[var(--bg-glass)] p-4 transition hover:border-[var(--border-medium)] hover:bg-[var(--bg-elevated)]";

  const inner = (
    <>
      <div className="grid size-9 shrink-0 place-items-center rounded-md bg-[var(--bg-elevated)]">
        <Icon className="size-4 text-[var(--text-muted)]" aria-hidden="true" />
      </div>
      <div className="min-w-0">
        <p className="text-[13px] font-semibold text-[var(--text-primary)]">
          {label}
          {disabled ? (
            <span className="ml-1.5 text-[10px] font-bold uppercase text-[var(--text-muted)]">
              Unavailable
            </span>
          ) : null}
        </p>
        <p className="truncate text-[12px] text-[var(--text-muted)]">{description}</p>
      </div>
    </>
  );

  if (disabled) {
    return (
      <div
        className="flex cursor-default items-center gap-3.5 rounded-lg border border-[var(--border-subtle)] bg-[var(--bg-glass)] p-4 opacity-50"
        onClick={onClick}
        {...props}
      >
        {inner}
      </div>
    );
  }

  if (!href) {
    return (
      <div
        className={clsx(baseClasses, className)}
        onClick={onClick}
        {...props}
      >
        {inner}
      </div>
    );
  }

  return (
    <Link href={href} className={clsx(baseClasses, className)}>
      {inner}
    </Link>
  );
}