diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 9fd65ba1..84c111fd 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -18,6 +18,47 @@ type FlyAndScaleParams = { duration?: number; }; + +export const groupNews = (news, watchList) => { + return Object.entries( + news + ?.map(item => { + // Add 'type' based on watchList + const match = watchList?.find(w => w?.symbol === item?.symbol); + return match ? { ...item, type: match.type } : { ...item }; + }) + ?.reduce((acc, item) => { + const dateKey = new Intl.DateTimeFormat('en-US', { + day: '2-digit', + month: 'short', + year: 'numeric', + }).format(new Date(item?.publishedDate)); + + const titleKey = item.title; + + if (!acc[dateKey]) acc[dateKey] = {}; + if (!acc[dateKey][titleKey]) acc[dateKey][titleKey] = []; + + acc[dateKey][titleKey]?.push(item); + + return acc; + }, {}) + )?.map(([date, titleGroup]) => [ + date, + Object?.entries(titleGroup)?.map(([title, items]) => { + // Sort by time for each group + items?.sort((a, b) => new Date(b?.publishedDate) - new Date(a?.publishedDate)); + + // Get the unique symbols + const symbols = [...new Set(items?.map(item => item.symbol))]; + + // Return the group with symbols and items + return { title, items, symbols }; + }), + ]); + } + + export const calculateChange = (oldList?: any[], newList?: any[]) => { if (!oldList?.length || !newList?.length) return [...(oldList || [])]; diff --git a/src/routes/watchlist/stocks/+page.svelte b/src/routes/watchlist/stocks/+page.svelte index 8ae7679d..35695d88 100644 --- a/src/routes/watchlist/stocks/+page.svelte +++ b/src/routes/watchlist/stocks/+page.svelte @@ -1,6 +1,6 @@