File size: 13,812 Bytes
0c5252a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useMemo } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { 
  ChevronDown, 
  ChevronUp, 
  Plus, 
  Search, 
  TrendingUp, 
  TrendingDown, 
  Trash2,
  PieChart as PieIcon,
  BarChart3,
  Info
} from 'lucide-react';
import { AreaChart, Area, XAxis, YAxis, Tooltip, ResponsiveContainer, ReferenceLine } from 'recharts';
import InvestmentCommittee from './InvestmentCommittee';

export default function HoldingsList({ 
  allocation, 
  prices, 
  onAddClick, 
  onAssetClick,
  onRemoveAsset 
}) {
  const [expandedId, setExpandedId] = useState(null);
  const [searchTerm, setSearchTerm] = useState('');

  const filteredAllocation = useMemo(() => {
    return allocation.filter(asset => 
      asset.ticker.toLowerCase().includes(searchTerm.toLowerCase()) ||
      asset.name.toLowerCase().includes(searchTerm.toLowerCase())
    );
  }, [allocation, searchTerm]);

  const toggleExpand = (ticker) => {
    setExpandedId(expandedId === ticker ? null : ticker);
  };

  return (
    <div className="bg-white rounded-3xl shadow-xl border border-gray-100 overflow-hidden">
      {/* Header section matching the user's requested layout */}
      <div className="p-6 border-b border-gray-50 flex flex-col md:flex-row justify-between items-center gap-4 bg-gs-light/30">
        <div className="flex items-center gap-3">
          <div className="p-2 bg-gs-navy rounded-xl text-white shadow-lg shadow-gs-navy/20">
            <BarChart3 size={20} />
          </div>
          <h2 className="text-xl font-bold text-gs-navy tracking-tight">Portfolio Holdings</h2>
        </div>
        
        <div className="flex items-center gap-3 w-full md:w-auto">
          <div className="relative flex-1 md:flex-none">
            <Search className="absolute left-3 top-1/2 -translate-y-1/2 text-gs-slate/40" size={16} />
            <input 
              type="text" 
              placeholder="Search holdings..." 
              value={searchTerm}
              onChange={(e) => setSearchTerm(e.target.value)}
              className="pl-10 pr-4 py-2 bg-white border border-gray-200 rounded-xl text-sm focus:outline-none focus:ring-2 focus:ring-gs-gold/30 focus:border-gs-gold transition-all w-full md:w-64 shadow-sm"
            />
          </div>
          <button 
            onClick={onAddClick}
            className="flex items-center gap-2 px-4 py-2 bg-gs-navy text-white rounded-xl hover:bg-gs-gold hover:text-gs-navy transition-all shadow-md font-bold text-sm whitespace-nowrap group"
          >
            <Plus size={16} className="group-hover:rotate-90 transition-transform duration-300" />
            Add Holding
          </button>
        </div>
      </div>

      {/* Table Header */}
      <div className="overflow-x-auto">
        <table className="w-full text-left border-collapse">
          <thead>
            <tr className="text-[10px] font-bold text-gs-slate/50 uppercase tracking-[0.1em] border-b border-gray-50">
              <th className="px-6 py-4 font-bold">Asset</th>
              <th className="px-6 py-4 font-bold text-right">Price</th>
              <th className="px-6 py-4 font-bold text-right hidden sm:table-cell">Shares</th>
              <th className="px-6 py-4 font-bold text-right">Value</th>
              <th className="px-6 py-4 font-bold text-right hidden lg:table-cell">Avg Cost</th>
              <th className="px-6 py-4 font-bold text-right hidden md:table-cell">Gain/Loss</th>
              <th className="px-6 py-4 font-bold text-right">Return</th>
              <th className="px-6 py-4 font-bold text-right">Alloc</th>
              <th className="px-4 py-4 w-10"></th>
            </tr>
          </thead>
          <tbody>
            {filteredAllocation.length > 0 ? (
              filteredAllocation.map((asset) => (
                <HoldingRow 
                  key={asset.ticker} 
                  asset={asset} 
                  priceData={prices[asset.ticker]}
                  isExpanded={expandedId === asset.ticker}
                  onToggle={() => toggleExpand(asset.ticker)}
                  onRemove={() => onRemoveAsset(asset.ticker)}
                />
              ))
            ) : (
              <tr>
                <td colSpan="9" className="px-6 py-20 text-center">
                  <div className="flex flex-col items-center gap-3">
                    <div className="p-4 bg-gs-light rounded-full text-gs-slate/20">
                      <Search size={40} />
                    </div>
                    <p className="text-gs-slate font-light text-lg">No holdings found matching your search.</p>
                  </div>
                </td>
              </tr>
            )}
          </tbody>
        </table>
      </div>
    </div>
  );
}

function HoldingRow({ asset, priceData, isExpanded, onToggle, onRemove }) {
  const currentPrice = priceData?.price || 0;
  const marketValue = asset.shares * currentPrice;
  const totalCost = asset.shares * (asset.avgCost || 0);
  const gainLoss = marketValue - totalCost;
  const returnPct = (totalCost > 0 && !isNaN(gainLoss)) ? (gainLoss / totalCost) * 100 : 0;
  
  const isPositive = gainLoss >= 0;

  return (
    <>
      <tr 
        onClick={onToggle}
        className={`group cursor-pointer border-b border-gray-50 transition-all ${isExpanded ? 'bg-gs-gold/5' : 'hover:bg-gs-light/50'}`}
      >
        {/* Asset Column */}
        <td className="px-6 py-4">
          <div className="flex items-center gap-4">
            <div 
              className="w-10 h-10 rounded-xl flex items-center justify-center text-white font-bold text-xs shadow-inner"
              style={{ backgroundColor: asset.color }}
            >
              {asset.ticker?.substring(0, 2) || '??'}
            </div>
            <div>
              <div className="flex items-center gap-2">
                <span className="font-bold text-gs-navy uppercase tracking-wide">{asset.ticker}</span>
                <span className="text-[10px] px-1.5 py-0.5 bg-gs-light text-gs-slate font-bold rounded uppercase">
                  {asset.type === 'mf' ? 'ETF' : 'STOCK'}
                </span>
              </div>
              <p className="text-xs text-gs-slate/60 font-medium truncate max-w-[120px]">{asset.name}</p>
              {priceData?.percent !== undefined && !isNaN(priceData.percent) && (
                <div className={`flex items-center gap-1 text-[10px] font-bold mt-0.5 ${priceData.percent >= 0 ? 'text-green-600' : 'text-red-500'}`}>
                  {priceData.percent >= 0 ? '▲' : '▼'} {Math.abs(priceData.percent).toFixed(2)}% today
                </div>
              )}
            </div>
          </div>
        </td>

        {/* Price Column */}
        <td className="px-6 py-4 text-right">
          <p className="font-bold text-gs-navy">${currentPrice.toFixed(2)}</p>
        </td>

        {/* Shares Column */}
        <td className="px-6 py-4 text-right hidden sm:table-cell">
          <p className="text-gs-slate font-medium">{asset.shares}</p>
        </td>

        {/* Value Column */}
        <td className="px-6 py-4 text-right">
          <p className="font-bold text-gs-navy">${marketValue.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}</p>
        </td>

        {/* Avg Cost Column */}
        <td className="px-6 py-4 text-right hidden lg:table-cell">
          <p className="text-gs-slate/60 font-medium">${(asset.avgCost || 0).toFixed(2)}</p>
        </td>

        {/* Gain/Loss Column */}
        <td className="px-6 py-4 text-right hidden md:table-cell">
          <p className={`font-bold ${isPositive ? 'text-green-600' : 'text-red-500'}`}>
            {isPositive ? '+' : ''}${Math.abs(gainLoss).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}
          </p>
        </td>

        {/* Return Column */}
        <td className="px-6 py-4 text-right">
          <div className={`inline-flex items-center px-2 py-1 rounded-lg text-xs font-bold ${isPositive ? 'bg-green-50 text-green-600' : 'bg-red-50 text-red-500'}`}>
            {isPositive ? '+' : ''}{returnPct.toFixed(2)}%
          </div>
        </td>

        {/* Alloc Column */}
        <td className="px-6 py-4 text-right">
          <div className="flex flex-col items-end gap-1.5">
            <span className="font-bold text-gs-navy text-xs">{asset.value.toFixed(1)}%</span>
            <div className="w-16 h-1.5 bg-gs-light rounded-full overflow-hidden shadow-inner">
              <div 
                className="h-full rounded-full transition-all duration-1000"
                style={{ width: `${asset.value}%`, backgroundColor: asset.color }}
              ></div>
            </div>
          </div>
        </td>

        {/* Actions/Expand Column */}
        <td className="px-4 py-4" onClick={(e) => e.stopPropagation()}>
           <div className="flex items-center gap-2">
            <button 
              onClick={() => onRemove()}
              className="p-1.5 text-gs-slate/20 hover:text-red-500 hover:bg-red-50 rounded-lg transition-all opacity-0 group-hover:opacity-100"
              title="Remove Holding"
            >
              <Trash2 size={14} />
            </button>
            <div className={`text-gs-slate/30 group-hover:text-gs-gold transition-colors ${isExpanded ? 'rotate-180' : ''}`}>
              <ChevronDown size={18} />
            </div>
           </div>
        </td>
      </tr>

      {/* Expanded Section */}
      <AnimatePresence>
        {isExpanded && (
          <motion.tr
            initial={{ opacity: 0, height: 0 }}
            animate={{ opacity: 1, height: 'auto' }}
            exit={{ opacity: 0, height: 0 }}
            className="bg-gs-light/30 border-b border-gray-50 overflow-hidden"
          >
            <td colSpan="9" className="p-0">
              <div className="p-6 grid grid-cols-1 xl:grid-cols-3 gap-8">
                {/* Visual Analysis */}
                <div className="xl:col-span-2 space-y-4">
                  <div className="flex justify-between items-end">
                    <div>
                      <h4 className="text-sm font-bold text-gs-navy uppercase tracking-wider mb-1">Price Performance</h4>
                      <p className="text-xs text-gs-slate/60">Real-time market tracking for {asset.ticker}</p>
                    </div>
                    <div className="flex gap-2">
                      <span className="text-[10px] font-bold px-2 py-1 bg-white border border-gray-100 rounded-lg shadow-sm">1 Month</span>
                    </div>
                  </div>
                  
                  <div className="h-48 bg-white p-4 rounded-2xl border border-gray-100 shadow-sm">
                    <ResponsiveContainer width="100%" height="100%">
                      <AreaChart data={priceData?.history || []}>
                        <defs>
                          <linearGradient id={`color-${asset.ticker}`} x1="0" y1="0" x2="0" y2="1">
                            <stop offset="5%" stopColor={asset.color} stopOpacity={0.1}/>
                            <stop offset="95%" stopColor={asset.color} stopOpacity={0}/>
                          </linearGradient>
                        </defs>
                        <Tooltip 
                          contentStyle={{ borderRadius: '12px', border: 'none', boxShadow: '0 10px 15px -3px rgba(0, 0, 0, 0.1)' }}
                        />
                        <Area 
                          type="monotone" 
                          dataKey="price" 
                          stroke={asset.color} 
                          strokeWidth={2} 
                          fill={`url(#color-${asset.ticker})`} 
                        />
                      </AreaChart>
                    </ResponsiveContainer>
                  </div>

                  <div className="grid grid-cols-3 gap-4">
                    <div className="bg-white p-3 rounded-xl border border-gray-100 shadow-sm">
                      <p className="text-[10px] font-bold text-gs-slate/40 uppercase mb-1">Volatility</p>
                      <p className="text-sm font-bold text-gs-navy">Moderate</p>
                    </div>
                    <div className="bg-white p-3 rounded-xl border border-gray-100 shadow-sm">
                      <p className="text-[10px] font-bold text-gs-slate/40 uppercase mb-1">Buy Price</p>
                      <p className="text-sm font-bold text-gs-navy">${(asset.avgCost || 0).toFixed(2)}</p>
                    </div>
                    <div className="bg-white p-3 rounded-xl border border-gray-100 shadow-sm">
                      <p className="text-[10px] font-bold text-gs-slate/40 uppercase mb-1">Hold Since</p>
                      <p className="text-sm font-bold text-gs-navy">{new Date(asset.dateBought).toLocaleDateString()}</p>
                    </div>
                  </div>
                </div>

                {/* AI Analysis Sidebar */}
                <div className="space-y-4">
                  <div className="flex items-center gap-2 mb-2">
                    <div className="w-6 h-6 rounded-lg bg-gs-gold/20 flex items-center justify-center text-gs-gold">
                      <Info size={14} />
                    </div>
                    <h4 className="text-sm font-bold text-gs-navy uppercase tracking-wider">AI Investment Thesis</h4>
                  </div>
                  <div className="bg-white rounded-2xl shadow-sm border border-gray-100 overflow-hidden">
                    <InvestmentCommittee ticker={asset.ticker} inline={true} />
                  </div>
                </div>
              </div>
            </td>
          </motion.tr>
        )}
      </AnimatePresence>
    </>
  );
}