CommentWriterView.swift 20 KB

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