10.5 KiB · text History 905ae26
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls as QQC2
import org.kde.plasma.plasmoid
import org.kde.plasma.components as PlasmaComponents3
import org.kde.plasma.extras as PlasmaExtras
import org.kde.kirigami as Kirigami

import "Vigil.js" as Vigil

// The popup: the org and project pickers, the monitors, and the
// unresolved issues as an accordion.
ColumnLayout {
    id: full

    required property var store

    spacing: Kirigami.Units.smallSpacing

    readonly property bool ready: store.hasValidConfig && store.authError === "" && !!store.org
    readonly property var filterProjects: store.watchedProjects
    readonly property var orgNames: store.orgs.map(function(o) { return o.name || o.slug })
    readonly property var projectNames: [i18n("All projects")].concat(filterProjects.map(function(p) { return p.name || p.slug }))

    // The widest entry, plus room for the arrow, capped at `maxUnits`.
    function pickerWidth(names, maxUnits) {
        var w = 0
        for (var i = 0; i < names.length; i++) w = Math.max(w, pickerMetrics.advanceWidth(names[i]))
        return Math.min(Kirigami.Units.gridUnit * maxUnits, Math.ceil(w) + Kirigami.Units.gridUnit * 2.5)
    }

    FontMetrics { id: pickerMetrics }

    // ── header ──────────────────────────────────────────────────────────

    RowLayout {
        Layout.fillWidth: true
        spacing: Kirigami.Units.smallSpacing

        Kirigami.Icon {
            source: full.store.logoSource
            isMask: true
            color: Kirigami.Theme.textColor
            Layout.preferredWidth: Kirigami.Units.iconSizes.smallMedium
            Layout.preferredHeight: Kirigami.Units.iconSizes.smallMedium
        }

        // Skipped when the token sees only one organization.
        PlasmaComponents3.ComboBox {
            id: orgPicker
            visible: full.store.orgs.length > 1
            Layout.preferredWidth: full.pickerWidth(full.orgNames, 12)
            model: full.orgNames
            currentIndex: {
                var os = full.store.orgs
                for (var i = 0; i < os.length; i++) if (os[i].slug === full.store.orgSlug) return i
                return -1
            }
            onActivated: function(index) { full.store.selectOrg(full.store.orgs[index].slug) }
        }

        Kirigami.Heading {
            visible: !orgPicker.visible
            level: 3
            elide: Text.ElideRight
            Layout.maximumWidth: Kirigami.Units.gridUnit * 10
            text: full.store.org ? (full.store.org.name || full.store.org.slug) : i18n("Vigil")
        }

        PlasmaComponents3.ComboBox {
            id: projectPicker
            visible: full.ready && full.filterProjects.length > 1
            Layout.preferredWidth: full.pickerWidth(full.projectNames, 11)
            model: full.projectNames
            currentIndex: {
                var f = Plasmoid.configuration.projectFilter
                if (f === "") return 0
                for (var i = 0; i < full.filterProjects.length; i++) if (full.filterProjects[i].slug === f) return i + 1
                return 0
            }
            onActivated: function(index) {
                Plasmoid.configuration.projectFilter = index === 0 ? "" : full.filterProjects[index - 1].slug
            }
        }

        Item { Layout.fillWidth: true }

        PlasmaComponents3.BusyIndicator {
            visible: full.store.loading
            Layout.preferredWidth: Kirigami.Units.iconSizes.smallMedium
            Layout.preferredHeight: Kirigami.Units.iconSizes.smallMedium
        }

        PlasmaComponents3.ToolButton {
            icon.name: "view-refresh"
            enabled: full.store.hasValidConfig && !full.store.loading
            onClicked: {
                full.store.authError = ""
                full.store.refresh()
            }
            PlasmaComponents3.ToolTip { text: i18n("Refresh") }
        }

        PlasmaComponents3.ToolButton {
            icon.name: "internet-web-browser"
            enabled: full.store.hasValidConfig
            onClicked: full.store.openWeb()
            PlasmaComponents3.ToolTip { text: i18n("Open in Browser") }
        }

        PlasmaComponents3.ToolButton {
            icon.name: "configure"
            onClicked: Plasmoid.internalAction("configure").trigger()
            PlasmaComponents3.ToolTip { text: i18n("Configure") }
        }

        PlasmaComponents3.ToolButton {
            checkable: true
            checked: Plasmoid.configuration.pin
            onToggled: Plasmoid.configuration.pin = checked
            icon.name: "window-pin"
            text: i18n("Keep Open")
            display: QQC2.AbstractButton.IconOnly
            PlasmaComponents3.ToolTip { text: i18n("Keep Open") }
        }
    }

    // ── what's wrong, or what just happened ────────────────────────────

    RowLayout {
        visible: full.store.flash !== ""
        Layout.fillWidth: true

        PlasmaComponents3.Label {
            Layout.fillWidth: true
            wrapMode: Text.WordWrap
            font: Kirigami.Theme.smallFont
            color: full.store.flashIsError ? Kirigami.Theme.negativeTextColor : Kirigami.Theme.textColor
            text: full.store.flash
        }

        PlasmaComponents3.ToolButton {
            visible: !!full.store.lastAction
            text: i18n("Undo")
            icon.name: "edit-undo"
            onClicked: full.store.undo()
        }
    }

    PlasmaComponents3.Label {
        readonly property string message: full.store.statusLine()
        visible: message !== "" && full.store.hasValidConfig
        Layout.fillWidth: true
        wrapMode: Text.WordWrap
        font: Kirigami.Theme.smallFont
        color: full.store.offline ? Kirigami.Theme.neutralTextColor : Kirigami.Theme.negativeTextColor
        text: message
    }

    Kirigami.Separator { Layout.fillWidth: true }

    // ── setup prompts ───────────────────────────────────────────────────

    PlasmaExtras.PlaceholderMessage {
        visible: !full.store.hasValidConfig || full.store.authError !== ""
        Layout.fillWidth: true
        Layout.fillHeight: true
        Layout.margins: Kirigami.Units.gridUnit
        iconName: "view-visible"
        text: !full.store.hasValidConfig ? i18n("Connect to Vigil") : i18n("The token was refused")
        explanation: i18n("Make a token under Settings → API tokens in Vigil (tick Mobile app for triage), then paste it into the widget's settings.")
        helpfulAction: QQC2.Action {
            icon.name: "configure"
            text: i18n("Configure…")
            onTriggered: Plasmoid.internalAction("configure").trigger()
        }
    }

    PlasmaComponents3.BusyIndicator {
        visible: full.store.hasValidConfig && full.store.authError === "" && !full.store.issuesLoaded
                 && full.store.statusLine() === ""
        Layout.alignment: Qt.AlignCenter
        Layout.fillHeight: true
    }

    // ── monitors, then issues ───────────────────────────────────────────

    PlasmaComponents3.ScrollView {
        visible: full.ready && full.store.issuesLoaded
        Layout.fillWidth: true
        Layout.fillHeight: true
        contentWidth: availableWidth

        ListView {
            id: issueList
            clip: true
            model: full.store.issuesModelRef
            reuseItems: false
            spacing: 0

            header: ColumnLayout {
                width: issueList.width
                spacing: 0

                SectionHeading {
                    visible: full.store.monitorRows.length > 0 || full.store.monitorsError !== ""
                    Layout.fillWidth: true
                    text: i18n("Monitors")
                    count: full.store.failingMonitorCount > 0
                           ? i18n("%1 failing · %2", full.store.failingMonitorCount, full.store.monitorRows.length)
                           : String(full.store.monitorRows.length)
                    countColor: full.store.failingMonitorCount > 0 ? Vigil.colors.error : Kirigami.Theme.disabledTextColor
                    collapsible: true
                    open: Plasmoid.configuration.monitorsOpen
                    onClicked: Plasmoid.configuration.monitorsOpen = !Plasmoid.configuration.monitorsOpen
                }

                PlasmaComponents3.Label {
                    visible: full.store.monitorsError !== "" && Plasmoid.configuration.monitorsOpen
                    Layout.fillWidth: true
                    Layout.leftMargin: Kirigami.Units.smallSpacing
                    wrapMode: Text.WordWrap
                    font: Kirigami.Theme.smallFont
                    opacity: 0.7
                    text: i18n("Monitors: %1", full.store.monitorsError)
                }

                Repeater {
                    // Closed, the failing ones still show.
                    model: Plasmoid.configuration.monitorsOpen
                           ? full.store.monitorRows
                           : full.store.monitorRows.filter(function(r) { return r.state.failing })
                    MonitorItem {
                        required property var modelData
                        Layout.fillWidth: true
                        row: modelData
                        store: full.store
                    }
                }

                SectionHeading {
                    Layout.fillWidth: true
                    Layout.topMargin: Kirigami.Units.smallSpacing
                    text: i18n("Unresolved")
                    count: full.store.moreIssues && Plasmoid.configuration.projectFilter === ""
                           ? issueList.count + "+" : String(issueList.count)
                    countColor: Kirigami.Theme.disabledTextColor
                }

                PlasmaComponents3.Label {
                    visible: issueList.count === 0
                    Layout.fillWidth: true
                    Layout.margins: Kirigami.Units.largeSpacing
                    horizontalAlignment: Text.AlignHCenter
                    opacity: 0.7
                    text: i18n("Nothing unresolved. All quiet.")
                }
            }

            delegate: IssueItem {
                required property string issueJson
                width: issueList.width
                store: full.store
                issue: JSON.parse(issueJson)
            }
        }
    }
}