Skip to content

fix: user information in the community edition portal does not display token data - #8200

Merged
peslyn merged 1 commit into
v3from
pr@v3@fix_user_information_in_the_community_edition_portal_does_not_display_token_data
Nov 27, 2026
Merged

peslyn merged 1 commit into
v3from
pr@v3@fix_user_information_in_the_community_edition_portal_does_not_display_token_data

Conversation

@shaohuzhang1

Copy link
Copy Markdown
Contributor

fix: user information in the community edition portal does not display token data

</template>
</div>
<el-divider />
<MkDropdownMenu>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 tokenPercentage is a floating-point number (e.g., 15.7), el-progress typically rounds it for display but uses the exact value for width. This is usually acceptable.
  • NaN/Infinity Risk: If account?.token_quota?.used_tokens or token_limit result in division by zero (if token_limit is not unlimited but somehow 0 or undefined), tokenPercentage could become Infinity or NaN, causing the progress bar to render incorrectly or break. Ensure tokenPercentage computed 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.UNLIMITED
  • account?.token_quota is optional chained. If account is null/undefined, the first part !account?.token_quota becomes !undefined → true, so the condition passes early.
  • If account?.token_quota exists, account?.token_quota?.quota_type is 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-label to the el-progress component for screen readers, e.g.:
    aria-label="Tokens usage: {{ used_tokens }} out of {{ token_limit }}"
    Element Plus may support this via props or wrapper divs.

Summary

✅ No critical bugs detected.
⚠️ Watch out for division-by-zero in tokenPercentage.
💡 Optimize by moving complex expressions into computed properties for better readability and maintainability.

@peslyn
peslyn merged commit 4591ba8 into v3 Nov 27, 2026
3 of 4 checks passed
@peslyn
peslyn deleted the pr@v3@fix_user_information_in_the_community_edition_portal_does_not_display_token_data branch November 27, 2026 06:34
"edition": edition,
"language": "zh-CN",
},
**result,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's my review of the diff:

Irregularities / Potential Issues

  1. Hard-coded "language": "zh-CN" (pre-existing, but worth noting): The edition is 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.

  2. Inconsistent insertion point: In the first profile method, "edition" is nested inside a dict along with application/theme data, while in the two profile() variants it's placed at the top level blending with rsa_key, meta, logo. Verify all three responses share a consistent schema so frontend consumers don't have to branch on which profile responds.

  3. No fallback/default for edition: If maxkb.settings.edition is ever None, 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.

  4. 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

  1. Import placement: Adding from maxkb.settings import edition is fine, but importing module-level constants from settings can cause circular-import risks if settings ever imports app modules. Prefer from django.conf import settings + settings.EDITION if using Django conventions, or ensure maxkb.settings has no reverse imports.

  2. Consider grouping: Nest edition alongside other deployment/version metadata (e.g., under a "meta" or "system" key) rather than as a flat sibling of user-facing fields like name and logo. This reduces future collisions.

  3. Tests: Add/verify tests asserting edition appears in all three response shapes (authenticated app profile, anonymous default portal, named portal). The diff touches three code paths without visible test updates.

  4. 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: {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Analysis:

  1. Null Safety & Type Safety:

    • The optional chaining (?.) is correctly used for state.authProfile and the computed properties below it.
    • If authProfile or edition is missing/undefined, isCE returns undefined (falsy), which is safe for boolean contexts.
    • Suggestion: If a strict boolean is required, use Boolean(...). However, for a getter named isCE, returning undefined when not applicable is often idiomatic in Vue/Ts if the consumer handles falsy values correctly. To ensure strict typing as boolean, you can do: isCE: (state) => state.authProfile?.edition === 'CE'. Since === returns a boolean, this is already correct type-wise (unless edition is typed broadly).
  2. 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.
  3. Performance:

    • Pinia getters are computed properties and are cached. This is efficient. No issues here.
  4. Naming:

    • isCE is 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants