Files
wekan/client/lib/minicardLabelText.js
T
Lauri Ojansivu 6d5f24ee27 Large board: read only the user fields each card and list needs.
Opening a card writes the user document (profile.cardLastViews, #3078). The
board view (Utils.storedBoardView and allowBoardView, which every list's card
loop reads) and the helpers every minicard runs read the WHOLE user document,
so that one write re-ran every list's card loop, and Blaze then re-evaluated
every minicard: 37,452 Tracker invalidations for one card open on a 40-card
board. Measured in the running app, it is now 390.

client/lib/currentUserWith.js reads the current user with only the named
fields; minimongo re-runs a field-limited query only when a projected field
changes. The board view, feature preview, unread comments, folds, drag
handles, label text, dependency layers, date format, week number, mobile mode
and the admin-only custom field check now use it. setCardLastViewed $sets one
entry instead of rewriting the whole map, which is also capped at 5,000.

Thanks to markusst1982 and xet7 !

Fixes #6745.
2026-10-07 03:08:48 +03:00

106 lines
5.0 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { ReactiveCache } from '/imports/reactiveCache';
import { currentUserWith } from '/client/lib/currentUserWith';
import {
resolveShowLabelText as resolveShowLabelTextValue,
nextShowLabelTextOverride,
} from '/models/lib/labelTextVisibility';
// "Show minicard label text" - the labels on a minicard as coloured WORDS,
// or as coloured bars with the words left out (#4256).
//
// This layers exactly the same way the global theme override does
// (client/components/main/globalThemeColor.js): a BOARD-level default
// (Boards.showLabelText, Board Settings) that every board can set for
// itself, and an optional PER-USER override (profile.showLabelTextOverride)
// that - when set - wins on every board regardless of that board's own
// setting. Unset (null/undefined) means "use the board's setting"; the
// historical global default (text shown) is what a board or a user that has
// never touched either setting still gets, so existing boards and users see
// no behaviour change.
//
// Logged-out readers of a public board have no profile to store an override
// on, so they keep the old, simpler localStorage-only personal toggle,
// falling back to the board's own setting.
export function resolveShowLabelText(board) {
const boardShowLabelText = board && typeof board.showLabelText === 'boolean'
? board.showLabelText
: null;
// #6745: once per minicard, so read the one field it needs.
const currentUser = currentUserWith(['profile.showLabelTextOverride']);
if (currentUser) {
const override = (currentUser.profile || {}).showLabelTextOverride;
return resolveShowLabelTextValue(
typeof override === 'boolean' ? override : null,
boardShowLabelText,
);
}
const stored = window.localStorage.getItem('hiddenMinicardLabelText');
const storedOverride = stored !== null ? stored !== 'true' : null;
return resolveShowLabelTextValue(storedOverride, boardShowLabelText);
}
// Blaze helper: called with `this` set to the current card by the minicard
// template, so the board it belongs to is read from there. Falls back to no
// board (global default) when there is no card in context, e.g. a stray call
// from a non-card template.
export function hiddenMinicardLabelText() {
const board = this && typeof this.board === 'function' ? this.board() : null;
return !resolveShowLabelText(board);
}
// Cycles the per-user override: no override -> force shown -> force hidden ->
// no override, mirroring the tri-state the global theme override uses
// (unset/'no override' vs. an explicit chosen value).
export function toggleMinicardLabelText() {
const currentUser = ReactiveCache.getCurrentUser();
if (currentUser) {
const override = (currentUser.profile || {}).showLabelTextOverride;
const next = nextShowLabelTextOverride(typeof override === 'boolean' ? override : null);
// The profile is reactive, so the board redraws by itself.
Meteor.call('setShowLabelTextOverride', next);
return;
}
// localStorage is not reactive, so the page has to be read again for the
// change to show. Only the logged-OUT half pays that.
if (window.localStorage.getItem('hiddenMinicardLabelText')) {
window.localStorage.removeItem('hiddenMinicardLabelText');
} else {
window.localStorage.setItem('hiddenMinicardLabelText', 'true');
}
location.reload();
}
// The same two layers for the labels of the OPENED card (Board Settings /
// Card, "Labels text" in Show on Card): the board's allowsLabelTextOnCard
// (shown unless turned off) and the user's profile.cardLabelTextOverride.
// A reader who is not logged in follows the board.
export function resolveShowCardLabelText(board) {
const boardValue = board && typeof board.allowsLabelTextOnCard === 'boolean' ? board.allowsLabelTextOnCard : null;
const currentUser = ReactiveCache.getCurrentUser();
const override = currentUser ? (currentUser.profile || {}).cardLabelTextOverride : null;
return resolveShowLabelTextValue(typeof override === 'boolean' ? override : null, boardValue);
}
export function hasCardLabelTextOverride() {
const currentUser = ReactiveCache.getCurrentUser();
return Boolean(currentUser) && typeof (currentUser.profile || {}).cardLabelTextOverride === 'boolean';
}
// Cycles the card override like the minicard's: none -> shown -> hidden -> none.
// The profile is the user's own and writable by them (models/users.js).
export function toggleCardLabelText() {
const currentUser = ReactiveCache.getCurrentUser();
if (!currentUser) return;
const override = (currentUser.profile || {}).cardLabelTextOverride;
const next = nextShowLabelTextOverride(typeof override === 'boolean' ? override : null);
Meteor.users.update(currentUser._id, next === null
? { $unset: { 'profile.cardLabelTextOverride': '' } }
: { $set: { 'profile.cardLabelTextOverride': next } });
}
export function resetCardLabelTextOverride() {
const currentUser = ReactiveCache.getCurrentUser();
if (currentUser) Meteor.users.update(currentUser._id, { $unset: { 'profile.cardLabelTextOverride': '' } });
}