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
|
"use client"
import { useState, useRef } from "react"
import { useSubscriptions } from "@/lib/queries/use-subscriptions"
import { useSubscribeToFeed } from "@/lib/queries/use-subscribe-to-feed"
import { useUserProfile } from "@/lib/queries/use-user-profile"
import { downloadOpml, parseOpml } from "@/lib/opml"
import type { ParsedOpmlGroup } from "@/lib/opml"
import { notify } from "@/lib/notify"
export function ImportExportSettings() {
const { data: subscriptionsData } = useSubscriptions()
const { data: userProfile } = useUserProfile()
const subscribeToFeed = useSubscribeToFeed()
const [parsedGroups, setParsedGroups] = useState<ParsedOpmlGroup[] | null>(
null
)
const [isImporting, setIsImporting] = useState(false)
const [isExportingData, setIsExportingData] = useState(false)
const fileInputReference = useRef<HTMLInputElement>(null)
const tier = userProfile?.tier ?? "free"
function handleExport() {
if (!subscriptionsData) return
downloadOpml(subscriptionsData.subscriptions, subscriptionsData.folders)
notify("subscriptions exported")
}
function handleFileSelect(event: React.ChangeEvent<HTMLInputElement>) {
const file = event.target.files?.[0]
if (!file) return
const reader = new FileReader()
reader.onload = (loadEvent) => {
const xmlString = loadEvent.target?.result as string
try {
const groups = parseOpml(xmlString)
setParsedGroups(groups)
} catch {
notify("failed to parse opml file")
}
}
reader.readAsText(file)
if (fileInputReference.current) {
fileInputReference.current.value = ""
}
}
async function handleImport() {
if (!parsedGroups) return
setIsImporting(true)
let importedCount = 0
let failedCount = 0
for (const group of parsedGroups) {
for (const feed of group.feeds) {
try {
await new Promise<void>((resolve) => {
subscribeToFeed.mutate(
{
feedUrl: feed.url,
customTitle: feed.title || null,
},
{
onSuccess: () => {
importedCount++
resolve()
},
onError: () => {
failedCount++
resolve()
},
}
)
})
} catch {
failedCount++
}
}
}
setIsImporting(false)
setParsedGroups(null)
if (failedCount > 0) {
notify(`imported ${importedCount} feeds, ${failedCount} failed`)
} else {
notify(`imported ${importedCount} feeds`)
}
}
async function handleDataExport() {
setIsExportingData(true)
try {
const response = await fetch("/api/export")
if (!response.ok) throw new Error("export failed")
const blob = await response.blob()
const url = URL.createObjectURL(blob)
const anchor = document.createElement("a")
anchor.href = url
anchor.download = `asa-news-export-${new Date().toISOString().slice(0, 10)}.json`
anchor.click()
URL.revokeObjectURL(url)
notify("data exported")
} catch {
notify("failed to export data")
} finally {
setIsExportingData(false)
}
}
const totalFeedsInImport =
parsedGroups?.reduce((sum, group) => sum + group.feeds.length, 0) ?? 0
return (
<div className="px-4 py-3">
<div className="mb-6">
<h3 className="mb-2 text-text-primary">import</h3>
<p className="mb-3 text-text-dim">
import subscriptions from an opml file
</p>
{parsedGroups === null ? (
<div>
<input
ref={fileInputReference}
type="file"
accept=".opml,.xml"
onChange={handleFileSelect}
className="hidden"
/>
<button
onClick={() => fileInputReference.current?.click()}
className="border border-border bg-background-tertiary px-4 py-2 text-text-primary transition-colors hover:bg-border"
>
select opml file
</button>
</div>
) : (
<div>
<p className="mb-3 text-text-secondary">
found {totalFeedsInImport} feed
{totalFeedsInImport !== 1 && "s"} to import:
</p>
<div className="mb-3 max-h-60 overflow-y-auto border border-border">
{parsedGroups.map((group, groupIndex) => (
<div key={groupIndex}>
{group.folderName && (
<div className="bg-background-tertiary px-3 py-1 text-text-secondary">
{group.folderName}
</div>
)}
{group.feeds.map((feed, feedIndex) => (
<div
key={feedIndex}
className="border-b border-border px-3 py-2 last:border-b-0"
>
<p className="truncate text-text-primary">{feed.title}</p>
<p className="truncate text-text-dim">{feed.url}</p>
</div>
))}
</div>
))}
</div>
<div className="flex gap-2">
<button
onClick={() => setParsedGroups(null)}
className="border border-border px-4 py-2 text-text-secondary transition-colors hover:bg-background-tertiary hover:text-text-primary"
>
cancel
</button>
<button
onClick={handleImport}
disabled={isImporting}
className="border border-border bg-background-tertiary px-4 py-2 text-text-primary transition-colors hover:bg-border disabled:opacity-50"
>
{isImporting
? "importing..."
: `import ${totalFeedsInImport} feeds`}
</button>
</div>
</div>
)}
</div>
<div className="mb-6">
<h3 className="mb-2 text-text-primary">export opml</h3>
<p className="mb-3 text-text-dim">
download your subscriptions as an opml file
</p>
<button
onClick={handleExport}
disabled={!subscriptionsData}
className="border border-border bg-background-tertiary px-4 py-2 text-text-primary transition-colors hover:bg-border disabled:opacity-50"
>
export opml
</button>
</div>
<div>
<h3 className="mb-2 text-text-primary">export data</h3>
<p className="mb-3 text-text-dim">
{tier === "pro" || tier === "developer"
? "download all your data as json (subscriptions, folders, saved entries)"
: "download your saved entries as json (upgrade to pro for full export)"}
</p>
<button
onClick={handleDataExport}
disabled={isExportingData}
className="border border-border bg-background-tertiary px-4 py-2 text-text-primary transition-colors hover:bg-border disabled:opacity-50"
>
{isExportingData ? "exporting..." : "export data"}
</button>
</div>
</div>
)
}
|