|
| 1 | +import MarkdownUI |
| 2 | +import SwiftUI |
| 3 | + |
| 4 | +struct ChatPanel: View { |
| 5 | + @ObservedObject var viewModel: SuggestionPanelViewModel |
| 6 | + var chat: SuggestionPanelViewModel.Chat |
| 7 | + |
| 8 | + var body: some View { |
| 9 | + ZStack(alignment: .topTrailing) { |
| 10 | + ScrollView { |
| 11 | + LazyVStack { |
| 12 | + ForEach(chat.history.reversed(), id: \.id) { message in |
| 13 | + Markdown(message.text) |
| 14 | + .markdownTheme(.gitHub.text { |
| 15 | + BackgroundColor(Color.clear) |
| 16 | + }) |
| 17 | + .frame(maxWidth: .infinity, alignment: .leading) |
| 18 | + .padding() |
| 19 | + .background( |
| 20 | + RoundedRectangle(cornerRadius: 9, style: .continuous) |
| 21 | + .fill( |
| 22 | + message.isUser |
| 23 | + ? Color.userChatContentBackground |
| 24 | + : Color.contentBackground |
| 25 | + ) |
| 26 | + ) |
| 27 | + .rotationEffect(Angle(degrees: 180)) |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + .rotationEffect(Angle(degrees: 180)) |
| 32 | + |
| 33 | + // close button |
| 34 | + Button(action: { |
| 35 | + viewModel.isPanelDisplayed = false |
| 36 | + viewModel.content = .empty |
| 37 | + }) { |
| 38 | + Image(systemName: "xmark") |
| 39 | + .padding([.leading, .bottom], 16) |
| 40 | + .padding([.top, .trailing], 8) |
| 41 | + .foregroundColor(.white) |
| 42 | + } |
| 43 | + .buttonStyle(.plain) |
| 44 | + } |
| 45 | + .colorScheme(viewModel.colorScheme) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +struct ChatPanel_Preview: PreviewProvider { |
| 50 | + static var previews: some View { |
| 51 | + ChatPanel(viewModel: .init( |
| 52 | + content: .empty, |
| 53 | + isPanelDisplayed: true |
| 54 | + ), chat: .init( |
| 55 | + history: [ |
| 56 | + .init( |
| 57 | + id: "1", |
| 58 | + isUser: true, |
| 59 | + text: "**Hello**" |
| 60 | + ), |
| 61 | + .init(id: "2", isUser: false, text: "**Hey**! What can I do for you?"), |
| 62 | + .init( |
| 63 | + id: "3", |
| 64 | + isUser: true, |
| 65 | + text: #""" |
| 66 | + Please buy me a coffee! |
| 67 | + | Coffee | Milk | |
| 68 | + |--------|------| |
| 69 | + | Espresso | No | |
| 70 | + | Latte | Yes | |
| 71 | +
|
| 72 | + ```swift |
| 73 | + func foo() {} |
| 74 | + ``` |
| 75 | + """# |
| 76 | + ), |
| 77 | + ], |
| 78 | + isReceivingMessage: false |
| 79 | + )) |
| 80 | + .frame(width: 450, height: 500) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +struct ChatPanel_Light_Preview: PreviewProvider { |
| 85 | + static var previews: some View { |
| 86 | + ChatPanel(viewModel: .init( |
| 87 | + content: .empty, |
| 88 | + isPanelDisplayed: true, |
| 89 | + colorScheme: .light |
| 90 | + ), chat: .init( |
| 91 | + history: [ |
| 92 | + .init( |
| 93 | + id: "1", |
| 94 | + isUser: true, |
| 95 | + text: "**Hello**" |
| 96 | + ), |
| 97 | + .init(id: "2", isUser: false, text: "**Hey**! What can I do for you?"), |
| 98 | + .init( |
| 99 | + id: "3", |
| 100 | + isUser: true, |
| 101 | + text: #""" |
| 102 | + Please buy me a coffee! |
| 103 | + | Coffee | Milk | |
| 104 | + |--------|------| |
| 105 | + | Espresso | No | |
| 106 | + | Latte | Yes | |
| 107 | +
|
| 108 | + ```swift |
| 109 | + func foo() {} |
| 110 | + ``` |
| 111 | + """# |
| 112 | + ), |
| 113 | + ], |
| 114 | + isReceivingMessage: false |
| 115 | + )) |
| 116 | + .frame(width: 450, height: 500) |
| 117 | + } |
| 118 | +} |
0 commit comments