PostGeneratorView.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. import AppKit
  2. import SwiftUI
  3. import UniformTypeIdentifiers
  4. struct PostGeneratorView: View {
  5. @Environment(\.requirePremiumAccess) private var requirePremiumAccess
  6. @Bindable var viewModel: PostGeneratorViewModel
  7. private enum Layout {
  8. static let horizontalPadding: CGFloat = 24
  9. static let columnWidth: CGFloat = 300
  10. static let columnSpacing: CGFloat = 20
  11. static let sectionSpacing: CGFloat = 12
  12. static let imageUploadHeight: CGFloat = 140
  13. }
  14. var body: some View {
  15. VStack(spacing: 0) {
  16. header
  17. messageBanners
  18. ScrollView {
  19. VStack(alignment: .leading, spacing: 12) {
  20. tabBar
  21. PostPageBoundary {
  22. switch viewModel.selectedTab {
  23. case .compose:
  24. composeContent
  25. case .preview:
  26. previewContent
  27. }
  28. }
  29. }
  30. .padding(.horizontal, Layout.horizontalPadding)
  31. .padding(.top, 12)
  32. .padding(.bottom, 24)
  33. }
  34. }
  35. .background(AppTheme.background)
  36. .fileImporter(
  37. isPresented: $viewModel.showImageImporter,
  38. allowedContentTypes: [.image],
  39. allowsMultipleSelection: false
  40. ) { result in
  41. switch result {
  42. case .success(let urls):
  43. if let url = urls.first {
  44. viewModel.setImage(from: url)
  45. }
  46. case .failure(let error):
  47. viewModel.handleImageImportFailure(error)
  48. }
  49. }
  50. .alert("Replace existing content?", isPresented: $viewModel.showOverwriteConfirmation) {
  51. Button("Cancel", role: .cancel) {
  52. viewModel.cancelOverwriteConfirmation()
  53. }
  54. Button("Replace", role: .destructive) {
  55. guard requirePremiumAccess() else { return }
  56. Task { await viewModel.confirmOverwriteAndGenerate() }
  57. }
  58. } message: {
  59. Text("Generating will replace your current title, body, and poll options.")
  60. }
  61. .onChange(of: viewModel.draft.topic) { _, _ in viewModel.notifyDraftEdited() }
  62. .onChange(of: viewModel.draft.subreddit) { _, _ in viewModel.notifyDraftEdited() }
  63. .onChange(of: viewModel.draft.title) { _, _ in viewModel.notifyDraftEdited() }
  64. .onChange(of: viewModel.draft.body) { _, _ in viewModel.notifyDraftEdited() }
  65. .onChange(of: viewModel.draft.linkURL) { _, _ in viewModel.notifyDraftEdited() }
  66. .onChange(of: viewModel.draft.videoURL) { _, _ in viewModel.notifyDraftEdited() }
  67. .onChange(of: viewModel.draft.flair) { _, _ in viewModel.notifyDraftEdited() }
  68. }
  69. // MARK: - Header
  70. private var header: some View {
  71. HStack(spacing: 14) {
  72. ModernSidebarIconView(kind: .postGenerator, size: 40)
  73. VStack(alignment: .leading, spacing: 2) {
  74. HStack(spacing: 6) {
  75. Text("Post Generator")
  76. .font(.system(size: 18, weight: .semibold))
  77. .foregroundStyle(AppTheme.textPrimary)
  78. if viewModel.hasDraftChanges {
  79. Circle()
  80. .fill(AppTheme.accentOrange)
  81. .frame(width: 6, height: 6)
  82. .help("Draft has unsaved edits")
  83. }
  84. }
  85. Text(viewModel.usesLiveAI ? "Create Reddit-ready posts with OpenAI" : "Create Reddit-ready posts with AI templates")
  86. .font(.system(size: 11))
  87. .foregroundStyle(AppTheme.textSecondary)
  88. }
  89. Spacer()
  90. headerActions
  91. }
  92. .padding(.horizontal, Layout.horizontalPadding)
  93. .padding(.vertical, 16)
  94. .overlay(alignment: .bottom) {
  95. Rectangle()
  96. .fill(AppTheme.border)
  97. .frame(height: 1)
  98. }
  99. }
  100. @ViewBuilder
  101. private var messageBanners: some View {
  102. if let error = viewModel.errorMessage {
  103. PostGeneratorMessageBanner(message: error, isError: true)
  104. .padding(.horizontal, Layout.horizontalPadding)
  105. .padding(.top, 8)
  106. } else if let success = viewModel.successMessage {
  107. PostGeneratorMessageBanner(message: success, isError: false)
  108. .padding(.horizontal, Layout.horizontalPadding)
  109. .padding(.top, 8)
  110. }
  111. }
  112. private var headerActions: some View {
  113. HStack(spacing: 8) {
  114. Button {
  115. viewModel.resetDraft()
  116. } label: {
  117. Label("Reset", systemImage: "arrow.counterclockwise")
  118. .font(.system(size: 11, weight: .medium))
  119. }
  120. .buttonStyle(AppSecondaryButtonStyle())
  121. Button {
  122. viewModel.copyToClipboard()
  123. } label: {
  124. Label("Copy", systemImage: "doc.on.doc")
  125. .font(.system(size: 11, weight: .medium))
  126. }
  127. .buttonStyle(AppSecondaryButtonStyle())
  128. .disabled(!viewModel.canExport)
  129. Button {
  130. guard requirePremiumAccess() else { return }
  131. Task { await viewModel.generatePost() }
  132. } label: {
  133. HStack(spacing: 6) {
  134. if viewModel.isGenerating {
  135. ProgressView()
  136. .controlSize(.small)
  137. .tint(.white)
  138. } else {
  139. Image(systemName: "sparkles")
  140. .font(.system(size: 11, weight: .semibold))
  141. }
  142. Text(viewModel.isGenerating ? "Generating…" : "Generate")
  143. .font(.system(size: 11, weight: .semibold))
  144. }
  145. .foregroundStyle(.white)
  146. .padding(.horizontal, 14)
  147. .padding(.vertical, 8)
  148. .background(viewModel.canGenerate ? AppTheme.accentPurple : AppTheme.accentPurple.opacity(0.4))
  149. .clipShape(RoundedRectangle(cornerRadius: 8))
  150. }
  151. .buttonStyle(AppPrimaryButtonStyle())
  152. .disabled(!viewModel.canGenerate)
  153. }
  154. }
  155. // MARK: - Tab Bar
  156. private var tabBar: some View {
  157. HStack(spacing: 0) {
  158. ForEach(PostGeneratorTab.allCases) { tab in
  159. Button {
  160. withAnimation(.easeInOut(duration: 0.15)) {
  161. viewModel.selectedTab = tab
  162. }
  163. } label: {
  164. Text(tab.title)
  165. .font(.system(size: 11, weight: .semibold))
  166. .foregroundStyle(viewModel.selectedTab == tab ? AppTheme.textPrimary : AppTheme.textSecondary)
  167. .frame(maxWidth: .infinity)
  168. .padding(.vertical, 7)
  169. .background(
  170. viewModel.selectedTab == tab
  171. ? AppTheme.cardBackground
  172. : Color.clear
  173. )
  174. .clipShape(RoundedRectangle(cornerRadius: 7))
  175. }
  176. .buttonStyle(AppPlainButtonStyle())
  177. .hoverOverlay(cornerRadius: 7, isEnabled: viewModel.selectedTab != tab)
  178. }
  179. }
  180. .padding(3)
  181. .frame(width: 220)
  182. .background(AppTheme.panelBackground)
  183. .clipShape(RoundedRectangle(cornerRadius: 9))
  184. .overlay(
  185. RoundedRectangle(cornerRadius: 9)
  186. .stroke(AppTheme.cardBorder, lineWidth: 1)
  187. )
  188. }
  189. // MARK: - Compose
  190. private var composeContent: some View {
  191. composeColumns
  192. }
  193. private var composeColumns: some View {
  194. HStack(alignment: .top, spacing: Layout.columnSpacing) {
  195. composeColumn(title: "Configuration", icon: "slider.horizontal.3") {
  196. configurationPanel
  197. }
  198. .frame(width: Layout.columnWidth)
  199. .layoutPriority(1)
  200. Rectangle()
  201. .fill(AppTheme.border)
  202. .frame(width: 1)
  203. .padding(.vertical, 2)
  204. composeColumn(title: "Post Content", icon: "doc.text") {
  205. editorPanel
  206. }
  207. .frame(minWidth: 0, maxWidth: .infinity)
  208. .layoutPriority(0)
  209. }
  210. }
  211. private func composeColumn<Content: View>(
  212. title: String,
  213. icon: String,
  214. @ViewBuilder content: () -> Content
  215. ) -> some View {
  216. VStack(alignment: .leading, spacing: Layout.sectionSpacing) {
  217. composeSectionHeader(title: title, icon: icon)
  218. content()
  219. }
  220. }
  221. private func composeSectionHeader(title: String, icon: String) -> some View {
  222. HStack(spacing: 6) {
  223. Image(systemName: icon)
  224. .font(.system(size: 10, weight: .semibold))
  225. .foregroundStyle(AppTheme.accentPurpleLight)
  226. Text(title)
  227. .font(.system(size: 11, weight: .semibold))
  228. .foregroundStyle(AppTheme.textSecondary)
  229. }
  230. .textCase(.uppercase)
  231. .tracking(0.4)
  232. }
  233. private var configurationPanel: some View {
  234. VStack(spacing: Layout.sectionSpacing) {
  235. PostFormCard(title: "Post Type", subtitle: "Choose how you want to post") {
  236. PostTypePicker(selectedType: Binding(
  237. get: { viewModel.draft.postType },
  238. set: { viewModel.selectPostType($0) }
  239. ))
  240. }
  241. PostFormCard(title: "Target Subreddit", subtitle: "Where will this be posted?") {
  242. VStack(alignment: .leading, spacing: 6) {
  243. PostFormField(
  244. label: "Subreddit",
  245. placeholder: "technology",
  246. text: $viewModel.draft.subreddit,
  247. prefix: "r/"
  248. )
  249. if !viewModel.draft.subreddit.isEmpty,
  250. !PostDraftValidator.isValidSubreddit(viewModel.draft.subreddit) {
  251. validationHint("Use 3–21 characters: letters, numbers, underscores only.")
  252. }
  253. }
  254. }
  255. PostFormCard(title: "AI Settings", subtitle: "Guide the tone and topic") {
  256. VStack(alignment: .leading, spacing: 12) {
  257. PostFormField(
  258. label: "Topic / Keywords",
  259. placeholder: "e.g. macOS productivity tips",
  260. text: $viewModel.draft.topic
  261. )
  262. VStack(alignment: .leading, spacing: 6) {
  263. Text("Tone")
  264. .font(.system(size: 10, weight: .medium))
  265. .foregroundStyle(AppTheme.textTertiary)
  266. TonePicker(selectedTone: $viewModel.draft.tone)
  267. }
  268. }
  269. }
  270. PostFormCard(title: "Labels", subtitle: "Tags and optional flair") {
  271. VStack(alignment: .leading, spacing: 12) {
  272. HStack(spacing: 8) {
  273. PostTagToggle(
  274. title: "NSFW",
  275. systemImage: "exclamationmark.triangle.fill",
  276. activeColor: Color(hex: 0xEF4444),
  277. isOn: $viewModel.draft.isNSFW
  278. )
  279. .frame(maxWidth: .infinity)
  280. PostTagToggle(
  281. title: "Spoiler",
  282. systemImage: "eye.slash.fill",
  283. activeColor: AppTheme.textSecondary,
  284. isOn: $viewModel.draft.isSpoiler
  285. )
  286. .frame(maxWidth: .infinity)
  287. PostTagToggle(
  288. title: "OC",
  289. systemImage: "star.fill",
  290. activeColor: AppTheme.accentGreen,
  291. isOn: $viewModel.draft.isOC
  292. )
  293. .frame(maxWidth: .infinity)
  294. }
  295. PostFormField(
  296. label: "Flair",
  297. placeholder: "Discussion, Question, Meme…",
  298. text: $viewModel.draft.flair
  299. )
  300. }
  301. }
  302. }
  303. }
  304. private var editorPanel: some View {
  305. VStack(spacing: Layout.sectionSpacing) {
  306. PostFormCard(title: "Title", subtitle: "Required for all post types") {
  307. PostFormField(
  308. label: "Post Title",
  309. placeholder: "An engaging title for your post",
  310. text: $viewModel.draft.title
  311. )
  312. }
  313. postTypeEditor
  314. .frame(minHeight: 260, alignment: .top)
  315. characterCountFooter
  316. }
  317. }
  318. @ViewBuilder
  319. private var postTypeEditor: some View {
  320. switch viewModel.draft.postType {
  321. case .text:
  322. textPostEditor
  323. case .image:
  324. imagePostEditor
  325. case .link:
  326. linkPostEditor
  327. case .video:
  328. videoPostEditor
  329. case .poll:
  330. pollPostEditor
  331. }
  332. }
  333. private var textPostEditor: some View {
  334. PostFormCard(title: "Body", subtitle: "Markdown supported — **bold**, *italic*, links") {
  335. PostFormTextEditor(
  336. label: "Post Body",
  337. placeholder: "Write your post content here…",
  338. text: $viewModel.draft.body,
  339. minHeight: 220
  340. )
  341. }
  342. }
  343. private var imagePostEditor: some View {
  344. PostFormCard(title: "Image", subtitle: "Upload an image for your post") {
  345. VStack(alignment: .leading, spacing: 12) {
  346. imageUploadArea
  347. PostFormTextEditor(
  348. label: "Caption (optional)",
  349. placeholder: "Add context for your image…",
  350. text: $viewModel.draft.body,
  351. minHeight: 80
  352. )
  353. }
  354. }
  355. }
  356. private var linkPostEditor: some View {
  357. PostFormCard(title: "Link", subtitle: "Share a URL with the community") {
  358. VStack(alignment: .leading, spacing: 12) {
  359. PostFormField(
  360. label: "URL",
  361. placeholder: "https://example.com/article",
  362. text: $viewModel.draft.linkURL
  363. )
  364. if !viewModel.draft.linkURL.isEmpty,
  365. !PostDraftValidator.isValidHTTPURL(viewModel.draft.linkURL) {
  366. validationHint("Enter a valid http or https URL.")
  367. }
  368. PostFormTextEditor(
  369. label: "Description (optional)",
  370. placeholder: "Why are you sharing this link?",
  371. text: $viewModel.draft.body,
  372. minHeight: 100
  373. )
  374. }
  375. }
  376. }
  377. private var videoPostEditor: some View {
  378. PostFormCard(title: "Video", subtitle: "Link to YouTube, Vimeo, or direct video URL") {
  379. VStack(alignment: .leading, spacing: 12) {
  380. PostFormField(
  381. label: "Video URL",
  382. placeholder: "https://youtube.com/watch?v=…",
  383. text: $viewModel.draft.videoURL
  384. )
  385. if !viewModel.draft.videoURL.isEmpty,
  386. !PostDraftValidator.isValidHTTPURL(viewModel.draft.videoURL) {
  387. validationHint("Enter a valid http or https URL.")
  388. }
  389. PostFormTextEditor(
  390. label: "Description (optional)",
  391. placeholder: "Add context for your video…",
  392. text: $viewModel.draft.body,
  393. minHeight: 100
  394. )
  395. }
  396. }
  397. }
  398. private var pollPostEditor: some View {
  399. PostFormCard(title: "Poll", subtitle: "2–6 options, community votes") {
  400. VStack(alignment: .leading, spacing: 12) {
  401. ForEach(Array(viewModel.draft.pollOptions.enumerated()), id: \.element.id) { index, option in
  402. HStack(spacing: 8) {
  403. PostFormField(
  404. label: "Option \(index + 1)",
  405. placeholder: "Enter poll option",
  406. text: Binding(
  407. get: { option.text },
  408. set: { viewModel.updatePollOption(id: option.id, text: $0) }
  409. )
  410. )
  411. if viewModel.canRemovePollOption {
  412. Button {
  413. viewModel.removePollOption(option)
  414. } label: {
  415. Image(systemName: "minus.circle.fill")
  416. .font(.system(size: 14))
  417. .foregroundStyle(Color(hex: 0xEF4444).opacity(0.8))
  418. }
  419. .buttonStyle(AppPlainButtonStyle())
  420. .hoverOverlay(cornerRadius: 7)
  421. .padding(.top, 18)
  422. }
  423. }
  424. }
  425. if viewModel.canAddPollOption {
  426. Button {
  427. viewModel.addPollOption()
  428. } label: {
  429. Label("Add Option", systemImage: "plus.circle.fill")
  430. .font(.system(size: 11, weight: .medium))
  431. .foregroundStyle(AppTheme.accentPurpleLight)
  432. }
  433. .buttonStyle(AppPlainButtonStyle())
  434. .hoverOverlay(cornerRadius: 6)
  435. }
  436. VStack(alignment: .leading, spacing: 6) {
  437. Text("Poll Duration")
  438. .font(.system(size: 10, weight: .medium))
  439. .foregroundStyle(AppTheme.textTertiary)
  440. HStack(spacing: 8) {
  441. ForEach(PollDuration.allCases) { duration in
  442. Button {
  443. viewModel.draft.pollDuration = duration
  444. viewModel.notifyDraftEdited()
  445. } label: {
  446. Text(duration.label)
  447. .font(.system(size: 10, weight: .semibold))
  448. .foregroundStyle(
  449. viewModel.draft.pollDuration == duration ? .white : AppTheme.textSecondary
  450. )
  451. .frame(maxWidth: .infinity)
  452. .padding(.vertical, 7)
  453. .background(
  454. RoundedRectangle(cornerRadius: 7)
  455. .fill(
  456. viewModel.draft.pollDuration == duration
  457. ? AppTheme.accentPurple
  458. : AppTheme.panelBackground
  459. )
  460. )
  461. .overlay(
  462. RoundedRectangle(cornerRadius: 7)
  463. .stroke(AppTheme.cardBorder, lineWidth: 1)
  464. )
  465. }
  466. .buttonStyle(AppPlainButtonStyle())
  467. .hoverOverlay(cornerRadius: 7)
  468. }
  469. }
  470. }
  471. PostFormTextEditor(
  472. label: "Context (optional)",
  473. placeholder: "Explain why you're running this poll…",
  474. text: $viewModel.draft.body,
  475. minHeight: 80
  476. )
  477. }
  478. }
  479. }
  480. @ViewBuilder
  481. private var imageUploadArea: some View {
  482. Group {
  483. if let url = viewModel.draft.imageFileURL, let nsImage = NSImage(contentsOf: url) {
  484. ZStack(alignment: .topTrailing) {
  485. Image(nsImage: nsImage)
  486. .resizable()
  487. .scaledToFit()
  488. .frame(maxWidth: .infinity, maxHeight: Layout.imageUploadHeight)
  489. .frame(maxWidth: .infinity, maxHeight: Layout.imageUploadHeight)
  490. .clipped()
  491. Button {
  492. viewModel.removeImage()
  493. } label: {
  494. Image(systemName: "xmark.circle.fill")
  495. .font(.system(size: 18))
  496. .foregroundStyle(.white)
  497. .shadow(radius: 2)
  498. }
  499. .buttonStyle(AppPlainButtonStyle())
  500. .hoverOverlay(cornerRadius: 8)
  501. .padding(8)
  502. }
  503. } else {
  504. Button {
  505. viewModel.showImageImporter = true
  506. } label: {
  507. VStack(spacing: 8) {
  508. Image(systemName: "photo.badge.plus")
  509. .font(.system(size: 24))
  510. .foregroundStyle(AppTheme.accentPurpleLight)
  511. Text("Click to upload image")
  512. .font(.system(size: 11, weight: .medium))
  513. .foregroundStyle(AppTheme.textSecondary)
  514. Text("PNG, JPG, GIF, WebP")
  515. .font(.system(size: 9))
  516. .foregroundStyle(AppTheme.textTertiary)
  517. }
  518. .frame(maxWidth: .infinity, maxHeight: .infinity)
  519. .background(AppTheme.panelBackground)
  520. .clipShape(RoundedRectangle(cornerRadius: 8))
  521. .overlay(
  522. RoundedRectangle(cornerRadius: 8)
  523. .strokeBorder(
  524. AppTheme.accentPurple.opacity(0.3),
  525. style: StrokeStyle(lineWidth: 1, dash: [6, 4])
  526. )
  527. )
  528. }
  529. .buttonStyle(AppPlainButtonStyle())
  530. .hoverCard(cornerRadius: 8)
  531. }
  532. }
  533. .frame(maxWidth: .infinity)
  534. .frame(height: Layout.imageUploadHeight)
  535. .clipShape(RoundedRectangle(cornerRadius: 8))
  536. }
  537. private var characterCountFooter: some View {
  538. HStack(spacing: 12) {
  539. Text(titleCharacterCount)
  540. .font(.system(size: 10, weight: .medium, design: .monospaced))
  541. .foregroundStyle(
  542. viewModel.draft.title.count > PostDraftValidator.maxTitleLength
  543. ? Color(hex: 0xF87171)
  544. : AppTheme.textTertiary
  545. )
  546. if !viewModel.draft.body.isEmpty {
  547. Text("·")
  548. .foregroundStyle(AppTheme.textTertiary)
  549. Text(bodyCharacterCount)
  550. .font(.system(size: 10, weight: .medium, design: .monospaced))
  551. .foregroundStyle(
  552. viewModel.draft.body.count > PostDraftValidator.maxBodyLength
  553. ? Color(hex: 0xF87171)
  554. : AppTheme.textTertiary
  555. )
  556. }
  557. Spacer()
  558. Text("Reddit limits: 300 title · 40,000 body")
  559. .font(.system(size: 10))
  560. .foregroundStyle(AppTheme.textTertiary)
  561. }
  562. .padding(.horizontal, 12)
  563. .padding(.vertical, 9)
  564. .background(
  565. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  566. .fill(AppTheme.cardBackground)
  567. .overlay(
  568. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  569. .stroke(AppTheme.cardBorder, lineWidth: 1)
  570. )
  571. )
  572. }
  573. private var titleCharacterCount: String {
  574. "\(viewModel.draft.title.count) / \(PostDraftValidator.maxTitleLength) title"
  575. }
  576. private var bodyCharacterCount: String {
  577. "\(viewModel.draft.body.count) / \(PostDraftValidator.maxBodyLength) body"
  578. }
  579. // MARK: - Preview
  580. private var previewContent: some View {
  581. VStack(alignment: .leading, spacing: Layout.columnSpacing) {
  582. composeSectionHeader(title: "Live Preview", icon: "eye")
  583. HStack {
  584. Spacer(minLength: 0)
  585. VStack(spacing: Layout.sectionSpacing) {
  586. RedditPostPreviewCard(
  587. draft: viewModel.draft,
  588. formattedSubreddit: viewModel.formattedSubreddit
  589. )
  590. previewMetadata
  591. }
  592. .frame(maxWidth: 560)
  593. Spacer(minLength: 0)
  594. }
  595. }
  596. }
  597. private var previewMetadata: some View {
  598. VStack(alignment: .leading, spacing: 0) {
  599. metadataRow(label: "Type", value: viewModel.draft.postType.title)
  600. metadataDivider
  601. metadataRow(label: "Subreddit", value: viewModel.formattedSubreddit)
  602. metadataDivider
  603. metadataRow(label: "Tone", value: viewModel.draft.tone.title)
  604. metadataDivider
  605. metadataRow(label: "Engine", value: viewModel.usesLiveAI ? "OpenAI" : "Local templates")
  606. if viewModel.draft.isNSFW || viewModel.draft.isSpoiler || viewModel.draft.isOC {
  607. metadataDivider
  608. metadataRow(
  609. label: "Tags",
  610. value: [
  611. viewModel.draft.isNSFW ? "NSFW" : nil,
  612. viewModel.draft.isSpoiler ? "Spoiler" : nil,
  613. viewModel.draft.isOC ? "OC" : nil,
  614. ]
  615. .compactMap { $0 }
  616. .joined(separator: ", ")
  617. )
  618. }
  619. }
  620. .padding(14)
  621. .frame(maxWidth: .infinity, alignment: .leading)
  622. .background(
  623. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  624. .fill(AppTheme.cardBackground)
  625. .overlay(
  626. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  627. .stroke(AppTheme.cardBorder, lineWidth: 1)
  628. )
  629. )
  630. }
  631. private var metadataDivider: some View {
  632. Rectangle()
  633. .fill(AppTheme.border)
  634. .frame(height: 1)
  635. .padding(.vertical, 6)
  636. }
  637. private func metadataRow(label: String, value: String) -> some View {
  638. HStack(alignment: .top, spacing: 12) {
  639. Text(label)
  640. .font(.system(size: 10, weight: .medium))
  641. .foregroundStyle(AppTheme.textTertiary)
  642. .frame(width: 72, alignment: .leading)
  643. Text(value)
  644. .font(.system(size: 10))
  645. .foregroundStyle(AppTheme.textSecondary)
  646. .fixedSize(horizontal: false, vertical: true)
  647. }
  648. }
  649. private func validationHint(_ message: String) -> some View {
  650. HStack(spacing: 4) {
  651. Image(systemName: "exclamationmark.circle.fill")
  652. .font(.system(size: 9))
  653. Text(message)
  654. .font(.system(size: 9))
  655. }
  656. .foregroundStyle(Color(hex: 0xF87171))
  657. }
  658. }
  659. private struct PostSecondaryButtonStyle: ButtonStyle {
  660. func makeBody(configuration: Configuration) -> some View {
  661. AppSecondaryButtonStyle().makeBody(configuration: configuration)
  662. }
  663. }
  664. #Preview {
  665. PostGeneratorView(viewModel: PostGeneratorViewModel())
  666. .frame(width: 880, height: 720)
  667. }