Skip to content

Commit

Permalink
Show export button only for visible chat messages (#9)
Browse files Browse the repository at this point in the history
# Show export button only for visible chat messages

## ♻️ Current situation & Problem
As of now, we show the export button within the `ChatView` toolbar if
the export is configured by the user AND the chat is not empty. However,
this is a bit short-sighted as for e.g., `SpeziLLM` the export button
should not be showcased if there are only (invisible) system message or
function calling messages within the chat.


## ⚙️ Release Notes 
- Show export button only in case there are visible chat messages
(assistant and user messages)


## 📚 Documentation
Proper documentation


## ✅ Testing
--


## 📝 Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md):
- [x] I agree to follow the [Code of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md).
  • Loading branch information
philippzagar authored Jan 21, 2024
1 parent 382fd87 commit 9d45c10
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
4 changes: 3 additions & 1 deletion Sources/SpeziChat/ChatView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ public struct ChatView: View {
}

private var exportEnabled: Bool {
exportFormat != nil && !chat.isEmpty
exportFormat != nil && chat.contains(where: {
$0.role == .assistant || $0.role == .user // Only show export toolbar item if there are visible messages
})
}


Expand Down
29 changes: 29 additions & 0 deletions Sources/SpeziChat/Models/ChatEntity+Alignment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// This source file is part of the Stanford Spezi open source project
//
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

import Foundation


extension ChatEntity {
/// Indicates if a ``ChatEntity`` is displayed in a leading or trailing position within a SwiftUI `View`.
enum Alignment {
case leading
case trailing
}


/// Dependent on the ``ChatEntity/Role``, display a ``ChatEntity`` in a leading or trailing position.
var alignment: Alignment {
switch self.role {
case .user:
return .trailing
default:
return .leading
}
}
}
17 changes: 0 additions & 17 deletions Sources/SpeziChat/Models/ChatEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ public struct ChatEntity: Codable, Equatable, Hashable {
}
}

/// Indicates if a ``ChatEntity`` is displayed in a leading or trailing position within a SwiftUI `View`.
enum Alignment {
case leading
case trailing
}


/// ``ChatEntity/Role`` associated with the ``ChatEntity``.
public let role: Role
Expand All @@ -45,17 +39,6 @@ public struct ChatEntity: Codable, Equatable, Hashable {
public let date: Date


/// Dependent on the ``ChatEntity/Role``, display a ``ChatEntity`` in a leading or trailing position.
var alignment: Alignment {
switch self.role {
case .user:
return .trailing
default:
return .leading
}
}


/// Creates a ``ChatEntity`` which is the building block of a Spezi ``Chat``.
/// - Parameters:
/// - role: ``ChatEntity/Role`` associated with the ``ChatEntity``.
Expand Down

0 comments on commit 9d45c10

Please sign in to comment.