Files
wekan/client/components/rules/ruleSuggestions.js
T
Lauri Ojansivu 2211b24119 Rule suggestions from what people on a board keep doing by hand.
Board menu -> Rules shows Suggested rules above the rule list: when the
same person does one thing to a card and, within two minutes, another, that
pair is counted once per card, and a pair seen on three cards in the last 30
days is offered as a rule (moved to a list -> label, member or archive;
added to a list -> label or member; label added -> member). A pair a rule
already does is not offered. Create goes through rules.createRule with the
same trigger and action the wizard builds; Dismiss hides one suggestion for
that admin on that board (profile.dismissedRuleSuggestions).

Only board admins get suggestions, and Admin Panel / Settings / Visibility /
Features has a Rule suggestions tick that turns them off, on the server too.
Translated in every language.

Thanks to xet7 !
2026-10-17 14:42:59 +03:00

69 lines
2.5 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import { Session } from 'meteor/session';
import { TAPi18n } from '/imports/i18n';
import { allowFeature } from '/client/lib/instanceFeatures';
import './ruleSuggestions.jade';
// docs/Features/Rules/Rule-Suggestions.md. The server finds them
// (server/methods/ruleSuggestions.js); Create goes through rules.createRule
// with the same trigger and action documents the wizard builds, and the
// sentences become their descriptions, so the rule list reads the same.
function whenText(s) {
if (s.triggerKind === 'moved') return TAPi18n.__('rule-suggestion-when-moved', { list: s.triggerName });
if (s.triggerKind === 'created') return TAPi18n.__('rule-suggestion-when-created', { list: s.triggerName });
return TAPi18n.__('rule-suggestion-when-label', { label: s.triggerName });
}
function thenText(s) {
if (s.actionKind === 'addLabel') return TAPi18n.__('rule-suggestion-then-label', { label: s.actionName });
if (s.actionKind === 'addMember') return TAPi18n.__('rule-suggestion-then-member', { member: s.actionName });
return TAPi18n.__('rule-suggestion-then-archive');
}
Template.ruleSuggestions.onCreated(function () {
this.suggestions = new ReactiveVar([]);
this.load = () => {
const boardId = Session.get('currentBoard');
if (!boardId || !allowFeature('rule-suggestions')) {
this.suggestions.set([]);
return;
}
Meteor.call('rules.suggestions', boardId, (err, rows) => {
if (!this.view.isDestroyed) this.suggestions.set(err ? [] : rows || []);
});
};
this.autorun(() => {
Session.get('currentBoard');
allowFeature('rule-suggestions');
this.load();
});
});
Template.ruleSuggestions.helpers({
suggestions() {
return Template.instance().suggestions.get();
},
sentence() {
return `${whenText(this)}, ${thenText(this)}.`;
},
});
Template.ruleSuggestions.events({
'click .js-create-suggested-rule'(event, tpl) {
event.preventDefault();
const s = this;
const boardId = Session.get('currentBoard');
Meteor.call('rules.createRule', boardId, '',
{ ...s.trigger, desc: whenText(s) }, { ...s.action, desc: thenText(s) },
err => { if (!err) tpl.load(); else console.error('rules.createRule', err); });
},
'click .js-dismiss-suggested-rule'(event, tpl) {
event.preventDefault();
Meteor.call('rules.dismissSuggestion', Session.get('currentBoard'), this.key,
err => { if (!err) tpl.load(); });
},
});