<commit_before>misc  = require('smc-util/misc')
{defaults, required} = misc

{Actions, Store, Table, redux} = require('./smc-react')
{alert_message} = require('./alerts')

name = 'system_notifications'

class NotificationsActions extends Actions
    send_message: (opts) =>
        opts = defaults opts,
            id       : misc.uuid()
            time     : new Date()
            text     : required
            priority : 'high'
        table.set(opts)

    # set all recent messages to done
    mark_all_done: =>
        store.get('notifications')?.map (mesg, id) =>
            if not mesg.get('done')
                table.set(id:id, done:true)

actions = redux.createActions(name, NotificationsActions)
store   = redux.createStore(name, {loading:true})

class NotificationsTable extends Table
    query: ->
        return 'system_notifications'

    _change: (table, keys) =>
        actions.setState(loading:false, notifications:table.get())
        # TODO: below is to display a notification old-fashioned way -- will be replaced by react thing later
        s = {}
        if localStorage?
            try
                s = misc.from_json(localStorage.system_notifications)
            catch e
                # pass
        # show any message from the last hour that we haven't seen already
        recent = misc.minutes_ago(60)
        table.get().map (m, id) =>
            if not s[id]?
                mesg = m.toJS()
                if mesg.time >= recent and mesg.priority == 'high' and not mesg.done
                    s[id] = mesg.time
                    alert_message(type:'info', message:"SYSTEM MESSAGE (#{mesg.time.toLocaleString()}): #{mesg.text}", timeout:3600)
        # also delete older stuff from localStorage.system_notifications
        for id, x of s
            if x.time < recent
                delete s[id]
        localStorage.system_notifications = misc.to_json(s)

redux.createTable(name, NotificationsTable)<commit_msg>redux: bugfix -- system notifications didn't work<commit_after>misc  = require('smc-util/misc')
{defaults, required} = misc

{Actions, Store, Table, redux} = require('./smc-react')
{alert_message} = require('./alerts')

name = 'system_notifications'

class NotificationsActions extends Actions
    send_message: (opts) =>
        opts = defaults opts,
            id       : misc.uuid()
            time     : new Date()
            text     : required
            priority : 'high'
        table.set(opts)

    # set all recent messages to done
    mark_all_done: =>
        store.get('notifications')?.map (mesg, id) =>
            if not mesg.get('done')
                table.set(id:id, done:true)

actions = redux.createActions(name, NotificationsActions)
store   = redux.createStore(name, {loading:true})

class NotificationsTable extends Table
    query: ->
        return 'system_notifications'

    _change: (table, keys) =>
        actions.setState(loading:false, notifications:table.get())
        # TODO: below is to display a notification old-fashioned way -- will be replaced by react thing later
        s = {}
        if localStorage?
            try
                s = misc.from_json(localStorage.system_notifications)
            catch e
                # pass
        # show any message from the last hour that we haven't seen already
        recent = misc.minutes_ago(60)
        table.get().map (m, id) =>
            if not s[id]?
                mesg = m.toJS()
                if mesg.time >= recent and mesg.priority == 'high' and not mesg.done
                    s[id] = mesg.time
                    alert_message(type:'info', message:"SYSTEM MESSAGE (#{mesg.time.toLocaleString()}): #{mesg.text}", timeout:3600)
        # also delete older stuff from localStorage.system_notifications
        for id, x of s
            if x.time < recent
                delete s[id]
        localStorage.system_notifications = misc.to_json(s)

table = redux.createTable(name, NotificationsTable)
{'fraction_non_alphanumeric': 0.1073943661971831, 'fraction_numerical': 0.0030181086519114686, 'mean_word_length': 2.163882259347653, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1073071', 'n_tokens_mistral': 1153, 'n_tokens_neox': 1101, 'n_words': 305}