CommentWriterView.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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(AppSecondaryButtonStyle())
  104. Button {
  105. viewModel.copyToClipboard()
  106. } label: {
  107. Label("Copy", systemImage: "doc.on.doc")
  108. .font(.system(size: 11, weight: .medium))
  109. }
  110. .buttonStyle(AppSecondaryButtonStyle())
  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(AppPrimaryButtonStyle())
  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(AppPlainButtonStyle())
  171. .hoverOverlay(cornerRadius: 7, isEnabled: viewModel.selectedTab != tab)
  172. }
  173. }
  174. .padding(3)
  175. .frame(width: 300)
  176. .background(AppTheme.panelBackground)
  177. .clipShape(RoundedRectangle(cornerRadius: 9))
  178. .overlay(
  179. RoundedRectangle(cornerRadius: 9)
  180. .stroke(AppTheme.cardBorder, lineWidth: 1)
  181. )
  182. }
  183. // MARK: - Compose
  184. private var composeContent: some View {
  185. ViewThatFits(in: .horizontal) {
  186. composeColumns
  187. VStack(alignment: .leading, spacing: Layout.columnSpacing) {
  188. composeColumn(title: "Configuration", icon: "slider.horizontal.3") {
  189. configurationPanel
  190. }
  191. composeColumn(title: "Your Comment", icon: "bubble.left") {
  192. editorPanel
  193. }
  194. }
  195. }
  196. }
  197. private var composeColumns: some View {
  198. HStack(alignment: .top, spacing: Layout.columnSpacing) {
  199. composeColumn(title: "Configuration", icon: "slider.horizontal.3") {
  200. configurationPanel
  201. }
  202. .frame(width: Layout.columnWidth)
  203. Rectangle()
  204. .fill(AppTheme.border)
  205. .frame(width: 1)
  206. .padding(.vertical, 2)
  207. composeColumn(title: "Your Comment", icon: "bubble.left") {
  208. editorPanel
  209. }
  210. .frame(maxWidth: .infinity)
  211. }
  212. }
  213. private func composeColumn<Content: View>(
  214. title: String,
  215. icon: String,
  216. @ViewBuilder content: () -> Content
  217. ) -> some View {
  218. VStack(alignment: .leading, spacing: Layout.sectionSpacing) {
  219. sectionHeader(title: title, icon: icon)
  220. content()
  221. }
  222. }
  223. private func sectionHeader(title: String, icon: String) -> some View {
  224. HStack(spacing: 6) {
  225. Image(systemName: icon)
  226. .font(.system(size: 10, weight: .semibold))
  227. .foregroundStyle(AppTheme.accentGreen)
  228. Text(title)
  229. .font(.system(size: 11, weight: .semibold))
  230. .foregroundStyle(AppTheme.textSecondary)
  231. }
  232. .textCase(.uppercase)
  233. .tracking(0.4)
  234. }
  235. private var configurationPanel: some View {
  236. VStack(spacing: Layout.sectionSpacing) {
  237. PostFormCard(title: "Comment Type", subtitle: "Top-level or reply to a comment") {
  238. CommentTypePicker(selectedType: Binding(
  239. get: { viewModel.draft.commentType },
  240. set: { viewModel.selectCommentType($0) }
  241. ))
  242. }
  243. PostFormCard(title: "Thread Context", subtitle: "What are you commenting on?") {
  244. VStack(alignment: .leading, spacing: 12) {
  245. PostFormField(
  246. label: "Subreddit",
  247. placeholder: "technology",
  248. text: $viewModel.draft.subreddit,
  249. prefix: "r/"
  250. )
  251. if !viewModel.draft.subreddit.isEmpty,
  252. !PostDraftValidator.isValidSubreddit(viewModel.draft.subreddit) {
  253. validationHint("Use 3–21 characters: letters, numbers, underscores only.")
  254. }
  255. PostFormField(
  256. label: "Post Title",
  257. placeholder: "Paste or type the post title",
  258. text: $viewModel.draft.postTitle
  259. )
  260. PostFormTextEditor(
  261. label: "Post Body (optional)",
  262. placeholder: "Excerpt from the post for better context…",
  263. text: $viewModel.draft.postBody,
  264. minHeight: 70
  265. )
  266. if viewModel.draft.commentType == .reply {
  267. PostFormTextEditor(
  268. label: "Parent Comment",
  269. placeholder: "Paste the comment you're replying to…",
  270. text: $viewModel.draft.parentComment,
  271. minHeight: 90
  272. )
  273. }
  274. PostFormField(
  275. label: "Post URL (optional)",
  276. placeholder: "https://reddit.com/r/…/comments/…",
  277. text: $viewModel.draft.postURL
  278. )
  279. if !viewModel.draft.postURL.isEmpty,
  280. !CommentDraftValidator.isValidRedditURL(viewModel.draft.postURL) {
  281. validationHint("Enter a valid reddit.com URL to open the thread.")
  282. }
  283. }
  284. }
  285. PostFormCard(title: "Comment Intent", subtitle: "What should this comment achieve?") {
  286. CommentIntentPicker(selectedIntent: $viewModel.draft.intent)
  287. }
  288. PostFormCard(title: "AI Settings", subtitle: "Tone, length, and style") {
  289. VStack(alignment: .leading, spacing: 12) {
  290. PostFormField(
  291. label: "Focus / Angle (optional)",
  292. placeholder: "e.g. share personal experience",
  293. text: $viewModel.draft.topic
  294. )
  295. VStack(alignment: .leading, spacing: 6) {
  296. Text("Tone")
  297. .font(.system(size: 10, weight: .medium))
  298. .foregroundStyle(AppTheme.textTertiary)
  299. CommentTonePicker(selectedTone: $viewModel.draft.tone)
  300. }
  301. VStack(alignment: .leading, spacing: 6) {
  302. Text("Length")
  303. .font(.system(size: 10, weight: .medium))
  304. .foregroundStyle(AppTheme.textTertiary)
  305. CommentLengthPicker(selectedLength: $viewModel.draft.length)
  306. }
  307. CommentToggleRow(
  308. title: "Use Markdown",
  309. subtitle: "Bold, italic, quotes, links",
  310. isOn: $viewModel.draft.useMarkdown
  311. )
  312. if viewModel.draft.commentType == .reply {
  313. CommentToggleRow(
  314. title: "Quote Parent",
  315. subtitle: "Include > blockquote of parent comment",
  316. isOn: $viewModel.draft.includeQuote
  317. )
  318. }
  319. VStack(alignment: .leading, spacing: 6) {
  320. Text("Variants")
  321. .font(.system(size: 10, weight: .medium))
  322. .foregroundStyle(AppTheme.textTertiary)
  323. CommentVariantCountPicker(selectedCount: $viewModel.draft.variantCount)
  324. }
  325. }
  326. }
  327. RedditCommentEtiquetteCard()
  328. }
  329. }
  330. private var editorPanel: some View {
  331. VStack(spacing: Layout.sectionSpacing) {
  332. PostFormCard(
  333. title: "Your Comment",
  334. subtitle: viewModel.draft.useMarkdown
  335. ? "Markdown supported — edit after generating"
  336. : "Plain text — edit after generating"
  337. ) {
  338. PostFormTextEditor(
  339. label: "Comment Body",
  340. placeholder: "Generate a comment or write your own…",
  341. text: $viewModel.draft.body,
  342. minHeight: 190
  343. )
  344. }
  345. characterCountFooter
  346. RedditThreadContextCard(
  347. postTitle: viewModel.draft.postTitle,
  348. postBody: viewModel.draft.postBody,
  349. parentComment: viewModel.draft.parentComment,
  350. commentType: viewModel.draft.commentType,
  351. formattedSubreddit: viewModel.formattedSubreddit
  352. )
  353. }
  354. }
  355. private var characterCountFooter: some View {
  356. HStack(spacing: 12) {
  357. Text("\(viewModel.draft.body.count) / \(CommentDraftValidator.maxCommentLength)")
  358. .font(.system(size: 10, weight: .medium, design: .monospaced))
  359. .foregroundStyle(
  360. viewModel.draft.body.count > CommentDraftValidator.maxCommentLength
  361. ? Color(hex: 0xF87171)
  362. : AppTheme.textTertiary
  363. )
  364. Spacer()
  365. Text("Reddit limit: 10,000 characters")
  366. .font(.system(size: 10))
  367. .foregroundStyle(AppTheme.textTertiary)
  368. }
  369. .padding(.horizontal, 12)
  370. .padding(.vertical, 9)
  371. .background(
  372. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  373. .fill(AppTheme.cardBackground)
  374. .overlay(
  375. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  376. .stroke(AppTheme.cardBorder, lineWidth: 1)
  377. )
  378. )
  379. }
  380. // MARK: - Preview
  381. private var previewContent: some View {
  382. VStack(alignment: .leading, spacing: Layout.columnSpacing) {
  383. sectionHeader(title: "Live Preview", icon: "eye")
  384. HStack {
  385. Spacer(minLength: 0)
  386. VStack(spacing: Layout.sectionSpacing) {
  387. RedditCommentPreviewCard(
  388. commentBody: viewModel.activeCommentBody,
  389. formattedSubreddit: viewModel.formattedSubreddit,
  390. commentType: viewModel.draft.commentType,
  391. parentComment: viewModel.draft.parentComment
  392. )
  393. previewMetadata
  394. }
  395. .frame(maxWidth: 560)
  396. Spacer(minLength: 0)
  397. }
  398. }
  399. }
  400. private var previewMetadata: some View {
  401. VStack(alignment: .leading, spacing: 0) {
  402. metadataRow(label: "Type", value: viewModel.draft.commentType.title)
  403. metadataDivider
  404. metadataRow(label: "Subreddit", value: viewModel.formattedSubreddit)
  405. metadataDivider
  406. metadataRow(label: "Intent", value: viewModel.draft.intent.title)
  407. metadataDivider
  408. metadataRow(label: "Tone", value: viewModel.draft.tone.title)
  409. metadataDivider
  410. metadataRow(label: "Length", value: viewModel.draft.length.title)
  411. metadataDivider
  412. metadataRow(label: "Engine", value: viewModel.usesLiveAI ? "OpenAI" : "Local templates")
  413. if let best = viewModel.bestVariant {
  414. metadataDivider
  415. metadataRow(label: "Best Score", value: "\(best.score)/100")
  416. }
  417. }
  418. .padding(14)
  419. .frame(maxWidth: .infinity, alignment: .leading)
  420. .background(
  421. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  422. .fill(AppTheme.cardBackground)
  423. .overlay(
  424. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  425. .stroke(AppTheme.cardBorder, lineWidth: 1)
  426. )
  427. )
  428. }
  429. // MARK: - Variants
  430. private var variantsContent: some View {
  431. VStack(alignment: .leading, spacing: Layout.columnSpacing) {
  432. variantsHeader
  433. if viewModel.variants.isEmpty {
  434. CommentWriterEmptyState(
  435. icon: "bubble.left.and.bubble.right",
  436. title: "No variants yet",
  437. subtitle: "Fill in thread context on the Compose tab,\nthen tap Generate to get comment variants."
  438. )
  439. .frame(maxWidth: .infinity)
  440. } else {
  441. variantsBody
  442. }
  443. }
  444. }
  445. private var variantsBody: some View {
  446. VStack(spacing: Layout.sectionSpacing) {
  447. VStack(spacing: 8) {
  448. ForEach(viewModel.variants) { variant in
  449. CommentVariantRow(
  450. variant: variant,
  451. isSelected: viewModel.selectedVariantID == variant.id,
  452. onSelect: { viewModel.selectVariant(variant) },
  453. onApply: { viewModel.applyVariant(variant) }
  454. )
  455. }
  456. }
  457. if !viewModel.activeCommentBody.isEmpty {
  458. PostFormCard(title: "Selected Preview", subtitle: "How this variant looks in a thread") {
  459. RedditCommentPreviewCard(
  460. commentBody: viewModel.activeCommentBody,
  461. formattedSubreddit: viewModel.formattedSubreddit,
  462. commentType: viewModel.draft.commentType,
  463. parentComment: viewModel.draft.parentComment
  464. )
  465. }
  466. }
  467. }
  468. .frame(maxWidth: 640)
  469. .frame(maxWidth: .infinity)
  470. }
  471. private var variantsHeader: some View {
  472. HStack(alignment: .top) {
  473. sectionHeader(title: "Comment Variants", icon: "bubble.left.and.bubble.right")
  474. Spacer()
  475. if let best = viewModel.bestVariant {
  476. HStack(spacing: 6) {
  477. Image(systemName: "trophy.fill")
  478. .font(.system(size: 10))
  479. .foregroundStyle(AppTheme.accentYellow)
  480. Text("Best: \(best.score)/100")
  481. .font(.system(size: 10, weight: .semibold))
  482. .foregroundStyle(AppTheme.textSecondary)
  483. }
  484. .padding(.top, 1)
  485. }
  486. }
  487. }
  488. private var metadataDivider: some View {
  489. Rectangle()
  490. .fill(AppTheme.border)
  491. .frame(height: 1)
  492. .padding(.vertical, 6)
  493. }
  494. private func metadataRow(label: String, value: String) -> some View {
  495. HStack(alignment: .top, spacing: 12) {
  496. Text(label)
  497. .font(.system(size: 10, weight: .medium))
  498. .foregroundStyle(AppTheme.textTertiary)
  499. .frame(width: 72, alignment: .leading)
  500. Text(value)
  501. .font(.system(size: 10))
  502. .foregroundStyle(AppTheme.textSecondary)
  503. .fixedSize(horizontal: false, vertical: true)
  504. }
  505. }
  506. private func validationHint(_ message: String) -> some View {
  507. HStack(spacing: 4) {
  508. Image(systemName: "exclamationmark.circle.fill")
  509. .font(.system(size: 9))
  510. Text(message)
  511. .font(.system(size: 9))
  512. }
  513. .foregroundStyle(Color(hex: 0xF87171))
  514. }
  515. }
  516. #Preview {
  517. CommentWriterView(viewModel: CommentWriterViewModel())
  518. .frame(width: 880, height: 720)
  519. }