CommentWriterView.swift 22 KB

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