@@ -22,6 +22,7 @@ import Dropzone from 'react-dropzone';
22
22
import { MdFileUpload , MdWarning } from 'react-icons/md' ;
23
23
import { BlobReader , TextWriter , ZipReader } from '@zip.js/zip.js' ;
24
24
import { Button } from '@/components/Button' ;
25
+ import { Platform } from '@/utils/statsfm' ;
25
26
26
27
type Props = { } ;
27
28
@@ -36,7 +37,7 @@ export const getServerSideProps: GetServerSideProps<SSRProps> = async (ctx) => {
36
37
} ;
37
38
38
39
interface ImportService {
39
- id : string ;
40
+ id : Platform ;
40
41
name : string ;
41
42
enabled : boolean ;
42
43
acceptFiles : Accept ;
@@ -58,7 +59,7 @@ const ImportPage: NextPage<Props> = () => {
58
59
59
60
const services : ImportService [ ] = [
60
61
{
61
- id : 'spotify' ,
62
+ id : Platform . SPOTIFY ,
62
63
name : 'Spotify' ,
63
64
enabled : true ,
64
65
acceptFiles : {
@@ -137,15 +138,32 @@ const ImportPage: NextPage<Props> = () => {
137
138
}
138
139
} ,
139
140
} ,
140
- // {
141
- // id: 'applemusic',
142
- // name: 'Apple Music',
143
- // enabled: false,
144
- // acceptFiles: {
145
- // 'text/csv': ['.csv'],
146
- // },
147
- // handleFileUpload: async () => {},
148
- // },
141
+ {
142
+ id : Platform . APPLEMUSIC ,
143
+ name : 'Apple Music' ,
144
+ enabled : true ,
145
+ acceptFiles : {
146
+ 'text/csv' : [ '.csv' ] ,
147
+ } ,
148
+ handleFileUpload : async ( files ) => {
149
+ toaster . message ( 'Processing files...' ) ;
150
+ // eslint-disable-next-line no-restricted-syntax
151
+ for await ( const file of files ) {
152
+ const content = await file . text ( ) ;
153
+ setUploadedFiles ( ( oldList ) => [
154
+ ...oldList ,
155
+ {
156
+ name : file . name ,
157
+ addedAt : new Date ( ) ,
158
+ contentType : file . type ,
159
+ status : UploadedFilesStatus . Ready ,
160
+ data : content ,
161
+ service : Platform . APPLEMUSIC ,
162
+ } ,
163
+ ] ) ;
164
+ }
165
+ } ,
166
+ } ,
149
167
] ;
150
168
151
169
const uploadFiles = async ( ) => {
@@ -167,16 +185,25 @@ const ImportPage: NextPage<Props> = () => {
167
185
uploadedFile . status = UploadedFilesStatus . Uploading ;
168
186
const oldUrl = api . options . http . apiUrl ;
169
187
try {
170
- api . options . http . apiUrl = 'https://import.stats.fm/api' ;
188
+ api . options . http . apiUrl =
189
+ process . env . NEXT_PUBLIC_API_URL_IMPORT ??
190
+ 'https://import.stats.fm/api' ;
171
191
// eslint-disable-next-line no-await-in-loop
172
- await api . me . import ( {
173
- name : uploadedFile . name ,
174
- contentType : 'application/json' ,
175
- data : JSON . stringify ( uploadedFile . data ) ,
176
- key : 'files' ,
177
- } ) ;
192
+ await api . me . import (
193
+ {
194
+ name : uploadedFile . name ,
195
+ contentType : uploadedFile . contentType ,
196
+ data :
197
+ uploadedFile . service === Platform . SPOTIFY
198
+ ? JSON . stringify ( uploadedFile . data )
199
+ : uploadedFile . data ,
200
+ key : 'files' ,
201
+ } ,
202
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
203
+ importService . id
204
+ ) ;
178
205
179
- event ( 'IMPORT_upload_file' ) ;
206
+ event ( `IMPORT_ ${ uploadedFile . service } _upload_file` ) ;
180
207
uploadedFile . status = UploadedFilesStatus . Uploaded ;
181
208
} catch ( e : any ) {
182
209
uploadedFile . status = UploadedFilesStatus . Failed ;
0 commit comments