CommentWriterView.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. import SwiftUI
  2. struct CommentWriterView: View {
  3. @Environment(\.requirePremiumAccess) private var requirePremiumAccess
  4. @Bindable var viewModel: CommentWriterViewModel
  5. var body: some View {
  6. VStack(spacing: 0) {
  7. header
  8. messageBanners
  9. tabBar
  10. ScrollView {
  11. PostPageBoundary {
  12. switch viewModel.selectedTab {
  13. case .compose:
  14. composeContent
  15. case .preview:
  16. previewContent
  17. case .variants:
  18. variantsContent
  19. }
  20. }
  21. .padding(.horizontal, 24)
  22. .padding(.top, 8)
  23. .padding(.bottom, 24)
  24. }
  25. }
  26. .background(AppTheme.background)
  27. .alert("Replace existing comment?", isPresented: $viewModel.showOverwriteConfirmation) {
  28. Button("Cancel", role: .cancel) {
  29. viewModel.cancelOverwriteConfirmation()
  30. }
  31. Button("Replace", role: .destructive) {
  32. guard requirePremiumAccess() else { return }
  33. Task { await viewModel.confirmOverwriteAndGenerate() }
  34. }
  35. } message: {
  36. Text("Generating will replace your current comment text.")
  37. }
  38. .onChange(of: viewModel.draft.subreddit) { _, _ in viewModel.notifyDraftEdited() }
  39. .onChange(of: viewModel.draft.postTitle) { _, _ in viewModel.notifyDraftEdited() }
  40. .onChange(of: viewModel.draft.postBody) { _, _ in viewModel.notifyDraftEdited() }
  41. .onChange(of: viewModel.draft.parentComment) { _, _ in viewModel.notifyDraftEdited() }
  42. .onChange(of: viewModel.draft.body) { _, _ in viewModel.notifyDraftEdited() }
  43. }
  44. // MARK: - Header
  45. private var header: some View {
  46. HStack(spacing: 14) {
  47. ModernSidebarIconView(kind: .commentWriter, size: 40)
  48. VStack(alignment: .leading, spacing: 2) {
  49. HStack(spacing: 6) {
  50. Text("Comment Writer")
  51. .font(.system(size: 18, weight: .semibold))
  52. .foregroundStyle(AppTheme.textPrimary)
  53. if viewModel.hasDraftChanges {
  54. Circle()
  55. .fill(AppTheme.accentGreen)
  56. .frame(width: 6, height: 6)
  57. .help("Draft has unsaved edits")
  58. }
  59. }
  60. Text(viewModel.usesLiveAI ? "Write Reddit-ready comments with OpenAI" : "Write Reddit-ready comments with AI templates")
  61. .font(.system(size: 11))
  62. .foregroundStyle(AppTheme.textSecondary)
  63. }
  64. Spacer()
  65. headerActions
  66. }
  67. .padding(.horizontal, 24)
  68. .padding(.vertical, 16)
  69. .overlay(alignment: .bottom) {
  70. Rectangle()
  71. .fill(AppTheme.border)
  72. .frame(height: 1)
  73. }
  74. }
  75. @ViewBuilder
  76. private var messageBanners: some View {
  77. if let error = viewModel.errorMessage {
  78. PostGeneratorMessageBanner(message: error, isError: true)
  79. .padding(.horizontal, 24)
  80. .padding(.top, 8)
  81. } else if let success = viewModel.successMessage {
  82. PostGeneratorMessageBanner(message: success, isError: false)
  83. .padding(.horizontal, 24)
  84. .padding(.top, 8)
  85. }
  86. }
  87. private var headerActions: some View {
  88. HStack(spacing: 8) {
  89. Button {
  90. viewModel.resetDraft()
  91. } label: {
  92. Label("Reset", systemImage: "arrow.counterclockwise")
  93. .font(.system(size: 11, weight: .medium))
  94. }
  95. .buttonStyle(CommentSecondaryButtonStyle())
  96. Button {
  97. viewModel.copyToClipboard()
  98. } label: {
  99. Label("Copy", systemImage: "doc.on.doc")
  100. .font(.system(size: 11, weight: .medium))
  101. }
  102. .buttonStyle(CommentSecondaryButtonStyle())
  103. .disabled(!viewModel.canExport)
  104. Button {
  105. guard requirePremiumAccess() else { return }
  106. Task { await viewModel.generateComment() }
  107. } label: {
  108. HStack(spacing: 6) {
  109. if viewModel.isGenerating {
  110. ProgressView()
  111. .controlSize(.small)
  112. .tint(.white)
  113. } else {
  114. Image(systemName: "sparkles")
  115. .font(.system(size: 11, weight: .semibold))
  116. }
  117. Text(viewModel.isGenerating ? "Generating…" : "Generate")
  118. .font(.system(size: 11, weight: .semibold))
  119. }
  120. .foregroundStyle(.white)
  121. .padding(.horizontal, 14)
  122. .padding(.vertical, 8)
  123. .background(viewModel.canGenerate ? AppTheme.accentGreen : AppTheme.accentGreen.opacity(0.4))
  124. .clipShape(RoundedRectangle(cornerRadius: 8))
  125. }
  126. .buttonStyle(.plain)
  127. .disabled(!viewModel.canGenerate)
  128. }
  129. }
  130. // MARK: - Tab Bar
  131. private var tabBar: some View {
  132. HStack(spacing: 4) {
  133. ForEach(CommentWriterTab.allCases) { tab in
  134. Button {
  135. viewModel.selectedTab = tab
  136. } label: {
  137. HStack(spacing: 4) {
  138. Text(tab.title)
  139. .font(.system(size: 11, weight: .semibold))
  140. if tab == .variants, !viewModel.variants.isEmpty {
  141. Text("\(viewModel.variants.count)")
  142. .font(.system(size: 9, weight: .bold))
  143. .foregroundStyle(.white)
  144. .padding(.horizontal, 5)
  145. .padding(.vertical, 2)
  146. .background(AppTheme.accentGreen)
  147. .clipShape(Capsule())
  148. }
  149. }
  150. .foregroundStyle(viewModel.selectedTab == tab ? AppTheme.textPrimary : AppTheme.textSecondary)
  151. .padding(.horizontal, 14)
  152. .padding(.vertical, 8)
  153. .background(
  154. viewModel.selectedTab == tab
  155. ? AppTheme.cardBackground
  156. : Color.clear
  157. )
  158. .clipShape(RoundedRectangle(cornerRadius: 8))
  159. }
  160. .buttonStyle(.plain)
  161. }
  162. Spacer()
  163. }
  164. .padding(.horizontal, 24)
  165. .padding(.top, 12)
  166. .padding(.bottom, 4)
  167. }
  168. // MARK: - Compose
  169. private var composeContent: some View {
  170. ViewThatFits(in: .horizontal) {
  171. HStack(alignment: .top, spacing: 16) {
  172. configurationPanel
  173. editorPanel
  174. }
  175. VStack(alignment: .leading, spacing: 16) {
  176. configurationPanel
  177. editorPanel
  178. }
  179. }
  180. }
  181. private var configurationPanel: some View {
  182. VStack(spacing: 12) {
  183. PostFormCard(title: "Comment Type", subtitle: "Top-level or reply to a comment") {
  184. CommentTypePicker(selectedType: Binding(
  185. get: { viewModel.draft.commentType },
  186. set: { viewModel.selectCommentType($0) }
  187. ))
  188. }
  189. PostFormCard(title: "Thread Context", subtitle: "What are you commenting on?") {
  190. VStack(alignment: .leading, spacing: 12) {
  191. PostFormField(
  192. label: "Subreddit",
  193. placeholder: "technology",
  194. text: $viewModel.draft.subreddit,
  195. prefix: "r/"
  196. )
  197. if !viewModel.draft.subreddit.isEmpty,
  198. !PostDraftValidator.isValidSubreddit(viewModel.draft.subreddit) {
  199. Text("Use 3–21 characters: letters, numbers, underscores only.")
  200. .font(.system(size: 9))
  201. .foregroundStyle(Color(hex: 0xF87171))
  202. }
  203. PostFormField(
  204. label: "Post Title",
  205. placeholder: "Paste or type the post title",
  206. text: $viewModel.draft.postTitle
  207. )
  208. PostFormTextEditor(
  209. label: "Post Body (optional)",
  210. placeholder: "Excerpt from the post for better context…",
  211. text: $viewModel.draft.postBody,
  212. minHeight: 70
  213. )
  214. if viewModel.draft.commentType == .reply {
  215. PostFormTextEditor(
  216. label: "Parent Comment",
  217. placeholder: "Paste the comment you're replying to…",
  218. text: $viewModel.draft.parentComment,
  219. minHeight: 90
  220. )
  221. }
  222. PostFormField(
  223. label: "Post URL (optional)",
  224. placeholder: "https://reddit.com/r/…/comments/…",
  225. text: $viewModel.draft.postURL
  226. )
  227. if !viewModel.draft.postURL.isEmpty,
  228. !CommentDraftValidator.isValidRedditURL(viewModel.draft.postURL) {
  229. Text("Enter a valid reddit.com URL to open the thread.")
  230. .font(.system(size: 9))
  231. .foregroundStyle(Color(hex: 0xF87171))
  232. }
  233. }
  234. }
  235. PostFormCard(title: "Comment Intent", subtitle: "What should this comment achieve?") {
  236. CommentIntentPicker(selectedIntent: $viewModel.draft.intent)
  237. }
  238. PostFormCard(title: "AI Settings", subtitle: "Tone, length, and style") {
  239. VStack(alignment: .leading, spacing: 12) {
  240. PostFormField(
  241. label: "Focus / Angle (optional)",
  242. placeholder: "e.g. share personal experience",
  243. text: $viewModel.draft.topic
  244. )
  245. VStack(alignment: .leading, spacing: 6) {
  246. Text("Tone")
  247. .font(.system(size: 10, weight: .medium))
  248. .foregroundStyle(AppTheme.textTertiary)
  249. CommentTonePicker(selectedTone: $viewModel.draft.tone)
  250. }
  251. VStack(alignment: .leading, spacing: 6) {
  252. Text("Length")
  253. .font(.system(size: 10, weight: .medium))
  254. .foregroundStyle(AppTheme.textTertiary)
  255. CommentLengthPicker(selectedLength: $viewModel.draft.length)
  256. }
  257. CommentToggleRow(
  258. title: "Use Markdown",
  259. subtitle: "Bold, italic, quotes, links",
  260. isOn: $viewModel.draft.useMarkdown
  261. )
  262. if viewModel.draft.commentType == .reply {
  263. CommentToggleRow(
  264. title: "Quote Parent",
  265. subtitle: "Include > blockquote of parent comment",
  266. isOn: $viewModel.draft.includeQuote
  267. )
  268. }
  269. VStack(alignment: .leading, spacing: 6) {
  270. Text("Variants")
  271. .font(.system(size: 10, weight: .medium))
  272. .foregroundStyle(AppTheme.textTertiary)
  273. CommentVariantCountPicker(selectedCount: $viewModel.draft.variantCount)
  274. }
  275. }
  276. }
  277. RedditCommentEtiquetteCard()
  278. }
  279. .frame(minWidth: 280, idealWidth: 300, maxWidth: 320)
  280. }
  281. private var editorPanel: some View {
  282. VStack(spacing: 12) {
  283. PostFormCard(
  284. title: "Your Comment",
  285. subtitle: viewModel.draft.useMarkdown
  286. ? "Markdown supported — edit after generating"
  287. : "Plain text — edit after generating"
  288. ) {
  289. PostFormTextEditor(
  290. label: "Comment Body",
  291. placeholder: "Generate a comment or write your own…",
  292. text: $viewModel.draft.body,
  293. minHeight: 220
  294. )
  295. }
  296. characterCountFooter
  297. RedditThreadContextCard(
  298. postTitle: viewModel.draft.postTitle,
  299. postBody: viewModel.draft.postBody,
  300. parentComment: viewModel.draft.parentComment,
  301. commentType: viewModel.draft.commentType,
  302. formattedSubreddit: viewModel.formattedSubreddit
  303. )
  304. }
  305. .frame(maxWidth: .infinity)
  306. }
  307. private var characterCountFooter: some View {
  308. HStack {
  309. Text("\(viewModel.draft.body.count) / \(CommentDraftValidator.maxCommentLength)")
  310. .font(.system(size: 10))
  311. .foregroundStyle(
  312. viewModel.draft.body.count > CommentDraftValidator.maxCommentLength
  313. ? Color(hex: 0xF87171)
  314. : AppTheme.textTertiary
  315. )
  316. Spacer()
  317. Text("Reddit limit: 10,000 characters")
  318. .font(.system(size: 10))
  319. .foregroundStyle(AppTheme.textTertiary)
  320. }
  321. }
  322. // MARK: - Preview
  323. private var previewContent: some View {
  324. HStack {
  325. Spacer(minLength: 0)
  326. VStack(spacing: 16) {
  327. Text("Live Preview")
  328. .font(.system(size: 12, weight: .semibold))
  329. .foregroundStyle(AppTheme.textSecondary)
  330. .frame(maxWidth: .infinity, alignment: .leading)
  331. RedditCommentPreviewCard(
  332. commentBody: viewModel.activeCommentBody,
  333. formattedSubreddit: viewModel.formattedSubreddit,
  334. commentType: viewModel.draft.commentType,
  335. parentComment: viewModel.draft.parentComment
  336. )
  337. previewMetadata
  338. }
  339. .frame(maxWidth: 560)
  340. Spacer(minLength: 0)
  341. }
  342. }
  343. private var previewMetadata: some View {
  344. VStack(alignment: .leading, spacing: 8) {
  345. metadataRow(label: "Type", value: viewModel.draft.commentType.title)
  346. metadataRow(label: "Subreddit", value: viewModel.formattedSubreddit)
  347. metadataRow(label: "Intent", value: viewModel.draft.intent.title)
  348. metadataRow(label: "Tone", value: viewModel.draft.tone.title)
  349. metadataRow(label: "Length", value: viewModel.draft.length.title)
  350. metadataRow(label: "Engine", value: viewModel.usesLiveAI ? "OpenAI" : "Local templates")
  351. if let best = viewModel.bestVariant {
  352. metadataRow(label: "Best Score", value: "\(best.score)/100")
  353. }
  354. }
  355. .padding(14)
  356. .frame(maxWidth: .infinity, alignment: .leading)
  357. .background(
  358. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  359. .fill(AppTheme.cardBackground)
  360. .overlay(
  361. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  362. .stroke(AppTheme.cardBorder, lineWidth: 1)
  363. )
  364. )
  365. }
  366. // MARK: - Variants
  367. private var variantsContent: some View {
  368. VStack(spacing: 16) {
  369. if viewModel.variants.isEmpty {
  370. CommentWriterEmptyState(
  371. icon: "bubble.left.and.bubble.right",
  372. title: "No variants yet",
  373. subtitle: "Fill in thread context on the Compose tab,\nthen tap Generate to get comment variants."
  374. )
  375. } else {
  376. variantsHeader
  377. VStack(spacing: 8) {
  378. ForEach(viewModel.variants) { variant in
  379. CommentVariantRow(
  380. variant: variant,
  381. isSelected: viewModel.selectedVariantID == variant.id,
  382. onSelect: { viewModel.selectVariant(variant) },
  383. onApply: { viewModel.applyVariant(variant) }
  384. )
  385. }
  386. }
  387. if !viewModel.activeCommentBody.isEmpty {
  388. PostFormCard(title: "Selected Preview", subtitle: "How this variant looks in a thread") {
  389. RedditCommentPreviewCard(
  390. commentBody: viewModel.activeCommentBody,
  391. formattedSubreddit: viewModel.formattedSubreddit,
  392. commentType: viewModel.draft.commentType,
  393. parentComment: viewModel.draft.parentComment
  394. )
  395. }
  396. }
  397. }
  398. }
  399. .frame(maxWidth: 640)
  400. .frame(maxWidth: .infinity)
  401. }
  402. private var variantsHeader: some View {
  403. HStack {
  404. VStack(alignment: .leading, spacing: 2) {
  405. Text("Comment Variants")
  406. .font(.system(size: 12, weight: .semibold))
  407. .foregroundStyle(AppTheme.textPrimary)
  408. Text("Tap a variant to preview, or Apply to use it")
  409. .font(.system(size: 10))
  410. .foregroundStyle(AppTheme.textSecondary)
  411. }
  412. Spacer()
  413. if let best = viewModel.bestVariant {
  414. HStack(spacing: 6) {
  415. Image(systemName: "trophy.fill")
  416. .font(.system(size: 10))
  417. .foregroundStyle(AppTheme.accentYellow)
  418. Text("Best: \(best.score)/100")
  419. .font(.system(size: 10, weight: .semibold))
  420. .foregroundStyle(AppTheme.textSecondary)
  421. }
  422. }
  423. }
  424. }
  425. private func metadataRow(label: String, value: String) -> some View {
  426. HStack(alignment: .top) {
  427. Text(label)
  428. .font(.system(size: 10, weight: .medium))
  429. .foregroundStyle(AppTheme.textTertiary)
  430. .frame(width: 70, alignment: .leading)
  431. Text(value)
  432. .font(.system(size: 10))
  433. .foregroundStyle(AppTheme.textSecondary)
  434. .fixedSize(horizontal: false, vertical: true)
  435. }
  436. }
  437. }
  438. #Preview {
  439. CommentWriterView(viewModel: CommentWriterViewModel())
  440. .frame(width: 880, height: 720)
  441. }