summaryrefslogtreecommitdiff
path: root/src/streamml/front/options_tab.py
blob: 9e67fcfd61dd194c41b19a8bdaebb5f414ba3289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from textual.app import ComposeResult
from textual.containers import Container, Horizontal
from textual.widgets import Button, Input, Label
from textual import on

from ..back.notification_service import notification_service

class OptionsTab(Container):
    def __init__(self, detector_manager, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.detector_manager = detector_manager

    def compose(self) -> ComposeResult:
        notify_section = Container(id="notify-section", classes="section-card")
        notify_section.border_title = "Notification config"
        with notify_section:  
            yield Label("Discord Webhook URL:")
            yield Input(placeholder="https://discord.com/api/webhooks/...", id="input-webhook-url")
            
            with Horizontal(classes="modal-footer"):
                yield Button("Save config", id="save-config", variant="success")
                yield Button("notification test", id="test-notif", variant="primary")

    def on_mount(self):
        self.query_one("#input-webhook-url", Input).value = notification_service.webhook_url

    @on(Button.Pressed, "#save-config")
    def save_configuration(self):
        url = self.query_one("#input-webhook-url", Input).value.strip()
        
        if notification_service.save_config(url):
            self.app.notify("Config saved", severity="information")
        else:
            self.app.notify("Error during saving", severity="error")

    @on(Button.Pressed, "#test-notif")
    def test_notification(self):
        success = notification_service.send_message("**Test streamml**\n")
        
        if success:
            self.app.notify("good", severity="information")
        else:
            self.app.notify("bad", severity="error")