Files
Sybil-2/ios/Packages/Sybil/Sources/Sybil/SybilSidebarView.swift

312 lines
12 KiB
Swift
Raw Normal View History

2026-02-20 00:09:02 -08:00
import Observation
import SwiftUI
struct SybilSidebarView: View {
@Bindable var viewModel: SybilViewModel
private func isSelected(_ item: SidebarItem) -> Bool {
viewModel.draftKind == nil && viewModel.selectedItem == item.selection
}
var body: some View {
VStack(spacing: 0) {
2026-05-02 16:23:00 -07:00
VStack(alignment: .leading, spacing: 14) {
VStack(spacing: 10) {
sidebarActionButton(
title: "New chat",
systemImage: "plus",
isPrimary: true,
isActive: viewModel.draftKind == .chat
) {
viewModel.startNewChat()
}
sidebarActionButton(
title: "New search",
systemImage: "magnifyingglass",
isPrimary: false,
isActive: viewModel.draftKind == .search
) {
viewModel.startNewSearch()
}
2026-02-20 00:09:02 -08:00
}
}
.padding(.horizontal, 12)
2026-05-02 16:23:00 -07:00
.padding(.top, 18)
2026-02-20 00:09:02 -08:00
.padding(.bottom, 10)
Divider()
.overlay(SybilTheme.border)
if let errorMessage = viewModel.errorMessage {
Text(errorMessage)
2026-05-02 16:23:00 -07:00
.font(.sybil(.footnote))
2026-02-20 00:09:02 -08:00
.foregroundStyle(SybilTheme.danger)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.horizontal, 12)
.padding(.vertical, 10)
Divider()
.overlay(SybilTheme.border)
}
2026-05-04 20:19:58 -07:00
SybilSidebarItemList(
viewModel: viewModel,
isSelected: isSelected,
onSelect: { item in
viewModel.select(item.selection)
2026-02-20 00:09:02 -08:00
}
2026-05-04 20:19:58 -07:00
)
2026-02-20 00:09:02 -08:00
}
2026-05-02 16:23:00 -07:00
.background(SybilTheme.panelGradient)
2026-05-02 17:10:32 -07:00
.navigationTitle("")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .topBarLeading) {
SybilWordmark(size: 18)
}
2026-05-02 23:20:32 -07:00
ToolbarItem(placement: .topBarTrailing) {
Button {
viewModel.openSettings()
} label: {
Image(systemName: viewModel.selectedItem == .settings ? "gearshape.fill" : "gearshape")
.font(.system(size: 16, weight: .semibold))
.foregroundStyle(viewModel.selectedItem == .settings ? SybilTheme.primary : SybilTheme.textMuted)
}
.accessibilityLabel("Settings")
}
2026-05-02 17:10:32 -07:00
}
2026-05-02 16:23:00 -07:00
}
private func sidebarActionButton(
title: String,
systemImage: String,
isPrimary: Bool,
isActive: Bool,
action: @escaping () -> Void
) -> some View {
Button(action: action) {
Label(title, systemImage: systemImage)
.font(.sybil(.subheadline, weight: .semibold))
.foregroundStyle(isPrimary ? SybilTheme.text : SybilTheme.text.opacity(0.92))
.padding(.horizontal, 13)
.padding(.vertical, 11)
.frame(maxWidth: .infinity, alignment: .leading)
.background(
RoundedRectangle(cornerRadius: 11)
.fill(isPrimary ? SybilTheme.primaryGradient : LinearGradient(colors: [SybilTheme.surface.opacity(0.86), SybilTheme.surface.opacity(0.62)], startPoint: .topLeading, endPoint: .bottomTrailing))
)
.overlay(
RoundedRectangle(cornerRadius: 11)
.stroke(isActive ? SybilTheme.primary.opacity(0.70) : SybilTheme.border.opacity(isPrimary ? 0.28 : 0.72), lineWidth: 1)
)
}
.buttonStyle(.plain)
2026-02-20 00:09:02 -08:00
}
}
2026-05-04 20:14:16 -07:00
2026-05-04 20:19:58 -07:00
struct SybilSidebarItemList: View {
@Bindable var viewModel: SybilViewModel
var isSelected: (SidebarItem) -> Bool
var onSelect: (SidebarItem) -> Void
2026-05-28 22:22:55 -07:00
@State private var renameTarget: SidebarItem?
@State private var renameTitle = ""
private var isRenameAlertPresented: Binding<Bool> {
Binding {
renameTarget != nil
} set: { isPresented in
if !isPresented {
renameTarget = nil
renameTitle = ""
}
}
}
2026-05-04 20:19:58 -07:00
var body: some View {
2026-05-28 22:22:55 -07:00
Group {
if viewModel.isLoadingCollections && viewModel.sidebarItems.isEmpty {
VStack(alignment: .leading, spacing: 8) {
ProgressView()
.tint(SybilTheme.primary)
Text("Loading conversations…")
.font(.sybil(.footnote))
.foregroundStyle(SybilTheme.textMuted)
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
.padding(16)
} else if viewModel.sidebarItems.isEmpty {
VStack(spacing: 10) {
Image(systemName: "message.badge")
.font(.system(size: 20, weight: .medium))
.foregroundStyle(SybilTheme.textMuted)
Text("Start a chat or run your first search.")
.font(.sybil(.footnote))
.multilineTextAlignment(.center)
.foregroundStyle(SybilTheme.textMuted)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding(16)
} else {
ScrollView {
LazyVStack(alignment: .leading, spacing: 8) {
ForEach(viewModel.sidebarItems) { item in
Button {
onSelect(item)
2026-05-04 20:19:58 -07:00
} label: {
2026-05-28 22:22:55 -07:00
SybilSidebarRow(item: item, isSelected: isSelected(item))
}
.buttonStyle(.plain)
.contextMenu {
2026-05-28 22:47:45 -07:00
Button {
Task {
await viewModel.setItemStarred(item.selection, starred: !item.starred)
}
} label: {
Label(item.starred ? "Unstar" : "Star", systemImage: item.starred ? "star.slash" : "star")
}
2026-05-28 22:22:55 -07:00
if item.kind == .chat {
Button {
renameTarget = item
renameTitle = item.title
} label: {
Label("Rename", systemImage: "pencil")
}
}
Button(role: .destructive) {
Task {
await viewModel.deleteItem(item.selection)
}
} label: {
Label("Delete", systemImage: "trash")
}
2026-05-04 20:19:58 -07:00
}
}
}
2026-05-28 22:22:55 -07:00
.padding(10)
2026-05-04 20:19:58 -07:00
}
2026-05-28 22:22:55 -07:00
.refreshable {
await viewModel.refreshSidebarCollectionsFromPullToRefresh()
}
}
}
.alert("Rename Chat", isPresented: isRenameAlertPresented) {
TextField("Title", text: $renameTitle)
Button("Cancel", role: .cancel) {
renameTarget = nil
renameTitle = ""
2026-05-04 20:19:58 -07:00
}
2026-05-28 22:22:55 -07:00
Button("Save") {
let target = renameTarget
let title = renameTitle
renameTarget = nil
renameTitle = ""
if let target, case let .chat(chatID) = target.selection {
Task {
await viewModel.renameChat(chatID: chatID, title: title)
}
}
2026-05-04 20:19:58 -07:00
}
2026-05-28 22:22:55 -07:00
.disabled(renameTitle.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
2026-05-04 20:19:58 -07:00
}
}
}
struct SybilSidebarRow: View {
var item: SidebarItem
var isSelected: Bool
2026-05-04 21:07:55 -07:00
private var isHighlighted: Bool {
isSelected
}
2026-05-04 20:19:58 -07:00
private var iconName: String {
switch item.kind {
case .chat: return "message"
case .search: return "globe"
}
}
var body: some View {
VStack(alignment: .leading, spacing: 6) {
HStack(spacing: 8) {
Image(systemName: iconName)
.font(.system(size: 12, weight: .semibold))
2026-05-04 21:07:55 -07:00
.foregroundStyle(isHighlighted ? SybilTheme.accent : SybilTheme.textMuted)
2026-05-04 20:19:58 -07:00
.frame(width: 22, height: 22)
.background(
RoundedRectangle(cornerRadius: 7)
2026-05-04 21:07:55 -07:00
.fill(isHighlighted ? SybilTheme.accent.opacity(0.12) : SybilTheme.surface.opacity(0.72))
2026-05-04 20:19:58 -07:00
.overlay(
RoundedRectangle(cornerRadius: 7)
2026-05-04 21:07:55 -07:00
.stroke(isHighlighted ? SybilTheme.accent.opacity(0.36) : SybilTheme.border.opacity(0.72), lineWidth: 1)
2026-05-04 20:19:58 -07:00
)
)
Text(item.title)
.font(.sybil(.subheadline, weight: .semibold))
.lineLimit(1)
.layoutPriority(1)
2026-05-28 22:47:45 -07:00
if item.starred {
Image(systemName: "star.fill")
.font(.system(size: 10, weight: .semibold))
.foregroundStyle(.yellow)
}
2026-05-04 20:19:58 -07:00
Spacer(minLength: 8)
if item.isRunning {
SybilSidebarActivityIndicator()
}
}
HStack(spacing: 8) {
Text(item.updatedAt.sybilRelativeLabel)
.font(.sybil(.caption2))
.foregroundStyle(SybilTheme.textMuted)
if let initiated = item.initiatedLabel {
Spacer(minLength: 0)
Text(initiated)
.font(.sybil(.caption2))
.foregroundStyle(SybilTheme.textMuted.opacity(0.88))
.lineLimit(1)
.multilineTextAlignment(.trailing)
.frame(maxWidth: .infinity, alignment: .trailing)
}
}
}
.foregroundStyle(SybilTheme.text)
.padding(.horizontal, 12)
.padding(.vertical, 10)
.frame(maxWidth: .infinity, alignment: .leading)
.background(
RoundedRectangle(cornerRadius: 12)
2026-05-04 21:07:55 -07:00
.fill(isHighlighted ? SybilTheme.selectedRowGradient : LinearGradient(colors: [SybilTheme.surface.opacity(0.56), SybilTheme.surface.opacity(0.36)], startPoint: .topLeading, endPoint: .bottomTrailing))
2026-05-04 20:19:58 -07:00
)
.overlay(
RoundedRectangle(cornerRadius: 12)
2026-05-04 21:07:55 -07:00
.stroke(isHighlighted ? SybilTheme.primary.opacity(0.55) : SybilTheme.border.opacity(0.72), lineWidth: 1)
2026-05-04 20:19:58 -07:00
)
2026-05-04 21:07:55 -07:00
.contentShape(RoundedRectangle(cornerRadius: 12))
2026-05-04 20:19:58 -07:00
}
}
2026-05-04 20:14:16 -07:00
struct SybilSidebarActivityIndicator: View {
var body: some View {
ProgressView()
.progressViewStyle(.circular)
.controlSize(.small)
.tint(SybilTheme.accent)
.scaleEffect(0.82)
.frame(width: 16, height: 16)
.accessibilityLabel("Generating")
}
}