import ics from 'ics'; export const albumReleasesCalendar = (collection) => { const collectionData = collection.getAll()[0]; const { data } = collectionData; const { albumReleases: { all }, globals: { url } } = data; if (!all || all.length === 0) return ''; const events = all .map((album) => { const date = new Date(album.release_date); if (isNaN(date.getTime())) return null; const albumUrl = album.url?.includes('http') ? album.url : `${url}${album.url}`; const artistUrl = album.artist.url?.includes('http') ? album.artust.url : `${url}${album.artist.url}`; return { start: [date.getFullYear(), date.getMonth() + 1, date.getDate()], startInputType: 'local', startOutputType: 'local', title: `Release: ${album.artist.name} - ${album.title}`, description: `Check out this new album release: ${albumUrl}. Read more about ${album.artist.name} at ${artistUrl}`, url: albumUrl, uid: `${album.release_timestamp}-${album.artist.name}-${album.title}` }; }) .filter((event) => event !== null); const { error, value } = ics.createEvents(events, { calName: 'Album releases calendar • coryd.dev' }); if (error) { console.error('Error creating events: ', error); return ''; } return value; };