Files
helder/Sources/HelderUI/SchemaView/SchemaDetailPanel.swift
T

153 lines
5.9 KiB
Swift

import HelderCore
import SwiftUI
/// The 320pt panel for the selected table: its columns, what it belongs to and has many, and the footer actions.
struct SchemaDetailPanel: View {
let model: WorkspaceModel
let store: SchemaStore
let schema: SchemaModel
let table: SchemaTable
var body: some View {
let outgoing = schema.outgoing(from: table.name)
let incoming = schema.incoming(to: table.name)
VStack(spacing: 0) {
header
ScrollView {
VStack(alignment: .leading, spacing: 20) {
columns
if !outgoing.isEmpty {
relations("[ belongs to ]", arrow: "→", rows: outgoing.map { ($0.id, $0.to, $0.column) })
}
if !incoming.isEmpty {
relations("[ has many ]", arrow: "←", rows: incoming.map { ($0.id, $0.from, $0.column) })
}
if outgoing.isEmpty && incoming.isEmpty {
Text("— No foreign keys in or out.")
.typeRole(Typography.gitEmpty)
.foregroundStyle(Palette.textMuted)
.padding(.horizontal, 16)
}
}
.padding(.top, 14)
.padding(.bottom, 20)
.frame(maxWidth: .infinity, alignment: .leading)
}
HStack(spacing: 8) {
SchemaOutlineButton(label: "Pass on to Claude", fills: true, padding: 8) { model.perform(.passSchemaTable(table.name)) }
SchemaOutlineButton(label: "Copy name", fills: true, padding: 8) { model.perform(.copySchemaTableName(table.name)) }
}
.padding(.horizontal, 16)
.padding(.vertical, 12)
.hairline(.top)
}
.background(Palette.panel)
.hairline(.leading)
}
private var header: some View {
HStack(spacing: 10) {
Text(table.name)
.typeRole(Typography.schemaDetailName)
.foregroundStyle(Palette.text1)
.lineLimit(1)
.truncationMode(.tail)
.frame(maxWidth: .infinity, alignment: .leading)
if table.rows != nil {
Text(table.rowsLabel + " rows").typeRole(Typography.meta).foregroundStyle(Palette.textMuted).fixedSize()
}
SchemaIconButton(icon: .x, tip: "Close (Esc)") { store.clearSelection() }
}
.padding(.leading, 16)
.padding(.trailing, 10)
.frame(height: SchemaViewMetrics.detailHeader)
.hairline(.bottom)
}
private var columns: some View {
VStack(alignment: .leading, spacing: 0) {
sectionLabel("[ columns ] \(table.columns.count)")
ForEach(Array(table.columns.enumerated()), id: \.offset) { _, column in
VStack(alignment: .leading, spacing: 2) {
HStack(alignment: .firstTextBaseline, spacing: 10) {
Text(column.name)
.typeRole(Typography.schemaDetailColumn)
.foregroundStyle(Palette.text2)
.lineLimit(1)
.truncationMode(.tail)
.frame(maxWidth: .infinity, alignment: .leading)
Text(column.type).typeRole(Typography.schemaDetailMeta).foregroundStyle(Palette.textMuted).lineLimit(1).fixedSize()
}
Text(column.flags)
.typeRole(Typography.schemaDetailMeta)
.foregroundStyle(column.isPrimary || column.isForeign ? Palette.amberSoft : Palette.text4)
}
.padding(.horizontal, 16)
.padding(.vertical, 7)
.hairline(.top)
}
}
}
private func relations(_ label: String, arrow: String, rows: [(id: String, table: String, via: String)]) -> some View {
VStack(alignment: .leading, spacing: 0) {
sectionLabel(label)
ForEach(rows, id: \.id) { row in
SchemaRelationRow(arrow: arrow, table: row.table, via: row.via,
onHover: { inside in hover(row.table, inside) },
onClick: { store.centre(row.table) })
}
}
}
private func sectionLabel(_ text: String) -> some View {
Text(text)
.typeRole(Typography.panelLabel)
.foregroundStyle(Palette.text4)
.padding(.horizontal, 16)
.padding(.bottom, 8)
}
private func hover(_ name: String, _ inside: Bool) {
if inside {
store.hover(name)
} else if store.hovered == name {
store.hover(nil)
}
}
}
/// `→ users … via user_id`: hover traces the table on the canvas, a click selects and centres it.
private struct SchemaRelationRow: View {
let arrow: String
let table: String
let via: String
let onHover: (Bool) -> Void
let onClick: () -> Void
@State private var isHovered = false
var body: some View {
Button(action: onClick) {
HStack(spacing: 8) {
Text(arrow).foregroundStyle(Palette.amber)
Text(table).foregroundStyle(Palette.text1).lineLimit(1)
Spacer(minLength: 0)
Text("via " + via).foregroundStyle(Palette.textMuted).lineLimit(1)
}
.typeRole(Typography.path)
.padding(.horizontal, 16)
.padding(.vertical, 8)
.background(isHovered ? Palette.gitRowHover : .clear)
.hairline(.top)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.pointerHand()
.onHover { inside in
isHovered = inside
onHover(inside)
}
.animation(Metrics.colorTransition, value: isHovered)
}
}