Repository navigation
fix: user information in the community edition portal does not display token data - #8200
Conversation
| </template> | ||
| </div> | ||
| <el-divider /> | ||
| <MkDropdownMenu> |
There was a problem hiding this comment.
Here is an analysis of the code diff, focusing on irregularities, potential issues, and optimization suggestions.
1. Functional Logic & Behavior
- Feature Gating (
!auth.isCE): The primary change wraps the token quota display (text + progress bar) in a<template v-if>that excludes Community Edition (CE) users. This prevents CE users from seeing quota limits they likely don't have access to or aren't enforced in the same way, which is logical if CE has unlimited or distinct billing logic. - Consistency: The wrapping ensures that both the textual representation of tokens and the visual progress bar are hidden together. This is consistent UX behavior.
2. Potential Issues
A. v-if Placement with <template>
While valid Vue syntax, using <template v-if> around multiple elements is fine. However, ensure auth and isCE are reactive and available in the component scope. If auth.isCE is undefined by default, it might evaluate as falsy (!undefined is true), accidentally showing quota info to CE users until auth initializes. Ensure auth.isCE defaults to false or handles initial state correctly.
B. Token Percentage Display (el-progress)
:percentage="tokenPercentage"- Rounding: If
tokenPercentageis a floating-point number (e.g.,15.7),el-progresstypically rounds it for display but uses the exact value for width. This is usually acceptable. - NaN/Infinity Risk: If
account?.token_quota?.used_tokensortoken_limitresult in division by zero (iftoken_limitis not unlimited but somehow 0 or undefined),tokenPercentagecould becomeInfinityorNaN, causing the progress bar to render incorrectly or break. EnsuretokenPercentagecomputed property guards against this (e.g.,Math.min(100, Math.max(0, ...))).
C. Translation Key Fallback
The code uses t('chatUser.quota.unlimited'). If the translation key is missing for certain locales, it may break rendering or show raw keys. Ensure this key exists in all locale files.
3. Code Quality & Readability
A. Redundant Optional Chaining
!account?.token_quota || account?.token_quota?.quota_type === QUOTA_TYPE.UNLIMITEDaccount?.token_quotais optional chained. Ifaccountis null/undefined, the first part!account?.token_quotabecomes!undefined→true, so the condition passes early.- If
account?.token_quotaexists,account?.token_quota?.quota_typeis safe. - Suggestion: This is logically correct due to short-circuiting, but slightly verbose. It’s acceptable.
B. Indentation & Formatting
The diff shows clean indentation inside the <template>. Vue SFC formatting tools (like Prettier with @vue/eslint-plugin-vue) should handle this automatically. Ensure your team’s linter agrees with the indentation style inside <template> tags.
4. Optimization Suggestions
A. Computed Property for Template Logic
The expression:
/:{{
!account?.token_quota || account?.token_quota?.quota_type === QUOTA_TYPE.UNLIMITED
? t('chatUser.quota.unlimited')
: formatTokenNumber(account?.token_quota?.token_limit)
}}is evaluated on every re-render. If account or t() are cheap, this is fine. For better performance and readability, move this logic to a computed property:
const displayedTokenLimit = computed(() => {
if (!account?.token_quota || account.token_quota.quota_type === QUOTA_TYPE.UNLIMITED) {
return t('chatUser.quota.unlimited');
}
return formatTokenNumber(account.token_quota.token_limit);
});Then use {{ displayedTokenLimit }} in the template.
B. Reuse for Progress Bar Visibility
The progress bar visibility condition duplicates part of the text logic:
v-if="account?.token_quota && account?.token_quota?.quota_type !== QUOTA_TYPE.UNLIMITED"You can define a computed property:
const showQuotaBar = computed(() =>
!!account?.token_quota && account.token_quota.quota_type !== QUOTA_TYPE.UNLIMITED
);Use v-if="showQuotaBar" for cleaner template code.
5. Accessibility
- The token count is displayed as plain text and a graphical progress bar. Consider adding
aria-labelto theel-progresscomponent for screen readers, e.g.:Element Plus may support this via props or wrapper divs.aria-label="Tokens usage: {{ used_tokens }} out of {{ token_limit }}"
Summary
✅ No critical bugs detected.
tokenPercentage.
💡 Optimize by moving complex expressions into computed properties for better readability and maintainability.
| "edition": edition, | ||
| "language": "zh-CN", | ||
| }, | ||
| **result, |
There was a problem hiding this comment.
Here's my review of the diff:
Irregularities / Potential Issues
-
Hard-coded
"language": "zh-CN"(pre-existing, but worth noting): Theeditionis being added dynamically while language remains hard-coded. If the edition varies (community vs. pro), you may want to consider whether language should also be configurable per deployment. -
Inconsistent insertion point: In the first
profilemethod,"edition"is nested inside a dict along with application/theme data, while in the twoprofile()variants it's placed at the top level blending withrsa_key,meta,logo. Verify all three responses share a consistent schema so frontend consumers don't have to branch on which profile responds. -
No fallback/default for
edition: Ifmaxkb.settings.editionis everNone, empty, or unrecognized, clients receive an unexpected value. Consider validating/normalizing it (e.g.,edition or "community") once in settings, rather than trusting the raw import. -
Duplication across three return sites: The same key (
edition) plus identical scaffolding (rsa_key,language,logo,meta) is repeated. Extract a shared helper (e.g.,_base_profile_context()) to avoid drift if one site changes later — this is the biggest maintainability concern.
Optimization Suggestions
-
Import placement: Adding
from maxkb.settings import editionis fine, but importing module-level constants fromsettingscan cause circular-import risks ifsettingsever imports app modules. Preferfrom django.conf import settings+settings.EDITIONif using Django conventions, or ensuremaxkb.settingshas no reverse imports. -
Consider grouping: Nest
editionalongside other deployment/version metadata (e.g., under a"meta"or"system"key) rather than as a flat sibling of user-facing fields likenameandlogo. This reduces future collisions. -
Tests: Add/verify tests asserting
editionappears in all three response shapes (authenticated app profile, anonymous default portal, named portal). The diff touches three code paths without visible test updates. -
Type/doc consistency: If there's an OpenAPI/serializer schema describing the profile response, update it to include
edition(not visible in the diff).
Let me know if you'd like a refactor showing the shared helper extraction.
| isCE: (state) => state.authProfile?.edition === 'CE', | ||
| }, | ||
|
|
||
| actions: { |
There was a problem hiding this comment.
Analysis:
-
Null Safety & Type Safety:
- The optional chaining (
?.) is correctly used forstate.authProfileand the computed properties below it. - If
authProfileoreditionis missing/undefined,isCEreturnsundefined(falsy), which is safe for boolean contexts. - Suggestion: If a strict boolean is required, use
Boolean(...). However, for a getter namedisCE, returningundefinedwhen not applicable is often idiomatic in Vue/Ts if the consumer handles falsy values correctly. To ensure strict typing asboolean, you can do:isCE: (state) => state.authProfile?.edition === 'CE'. Since===returns a boolean, this is already correct type-wise (unlesseditionis typed broadly).
- The optional chaining (
-
Consistency:
- Check if other edition checks (e.g., EE, Pro) exist. If so, consider creating a helper function or enum to avoid string literals (
'CE') scattered across the codebase. This improves maintainability if the string value changes.
- Check if other edition checks (e.g., EE, Pro) exist. If so, consider creating a helper function or enum to avoid string literals (
-
Performance:
- Pinia getters are computed properties and are cached. This is efficient. No issues here.
-
Naming:
isCEis clear and concise.
Optimization Suggestion:
If you want to enforce strict boolean return and improve readability/maintainability regarding edition checks, consider:
// Option 1: Ensure strict boolean (already true due to ===, but explicit is nice)
isCE: (state) => !!state.authProfile?.edition && state.authProfile.edition === 'CE', // Or simply keep current version if typescript infers boolean
// Option 2: Centralize magic strings (Recommended if more editions exist)
const EDITIONS = { CE: 'CE' } as const;
isCE: (state) => state.authProfile?.edition === EDITIONS.CE,Conclusion:
The code is correct and safe as written. No bugs. Minor optimization: replace 'CE' with a constant/enum if this edition identifier is used elsewhere to prevent typos and ease future refactoring.
fix: user information in the community edition portal does not display token data