Repository navigation
Conversation
…hared knowledge bases' menus
| <FolderTree v-model="currentFolderId" :can-edit="false" :show-shared="(auth.isEE || auth.isPE) && isToolMenu" :source="source" @select="loadResources" /> | ||
| </aside> | ||
|
|
||
| <section v-loading="loading" class="flex-column min-w-0 flex-1"> |
There was a problem hiding this comment.
Here is an analysis of the provided code diff, focusing on irregularities, potential issues, and optimization suggestions.
1. Potential Issue: Reactivity Loss with Destructured Store
Code: const { auth } = useStore()
- Problem: In Vue's Pinia or similar stores, destructuring properties like this (
const { auth } = useStore()) breaks reactivity. Theauthvariable will hold a static snapshot of the state at the time of component setup. It will not update automatically when the store state changes (e.g., if the user logs in, upgrades their plan, or permissions change dynamically). - Impact: The condition
(auth.isEE || auth.isPE) && isToolMenumay fail to update the UI whenauth.isEEorauth.isPEchanges from false to true (or vice versa) after the component has mounted. - Recommendation: Use
storeToRefsto maintain reactivity.
import { storeToRefs } from 'pinia'; // Or equivalent for your store library
const { auth } = storeToRefs(useStore());
// Note: With storeToRefs, auth becomes a Ref<AuthState>, so you access properties via auth.value?.isEEAlternatively, if useStore returns reactive refs directly (Vue 3 Composition API style), ensure you are accessing them correctly. If it's a standard Pinia store instance, do not destructure actions/getters that depend on state without storeToRefs.
Corrected Approach (Pinia Example):
import { storeToRefs } from 'pinia'
import { useStore as useMainStore } from '@/stores' // Renamed to avoid conflict if needed
const mainStore = useMainStore()
const { auth } = storeToRefs(mainStore)
// Now use auth.value.isEE in template or computed property2. Optimization: Computed Property for Complex Condition
Code: :show-shared="(auth.isEE || auth.isPE) && isToolMenu"
- Issue: Complex conditional logic directly in the template can hurt readability and performance (re-evaluated on every render). It also combines business logic (permissions) with UI presentation.
- Suggestion: Move this logic into a
computedproperty in the<script setup>block. This improves testability, readability, and ensures the value is cached until dependencies change.
const canShowShared = computed(() => {
// Assuming auth is now reactive via storeToRefs
return (auth.value?.isEE || auth.value?.isPE) && isToolMenu
})Then in template:
<FolderTree ... :show-shared="canShowShared" ... />3. Naming Convention & Context Clarity
- Variable Name Confusion: The function
useStore()imported from@/storesis very generic. Since there is already auseWorkflowStorefrom@/workflow-canvas/store, using a generic name likeuseStoremight lead to confusion about which store is being accessed.- Suggestion: Rename the import to be more specific, e.g.,
import { useAuthStore } from '@/stores'orimport { useGlobalStore } from '@/stores', depending on whatauthrepresents.
- Suggestion: Rename the import to be more specific, e.g.,
4. Optional Chaining Safety
- Issue: If
authcan beundefinedornullduring initial load (before hydration or async initialization), accessingauth.isEEdirectly might throw an error. - Suggestion: Use optional chaining
auth?.isEEregardless of the store implementation, especially ifauthis derived asynchronously.
5. Summary of Recommended Changes
<script setup>
// ... existing imports
import { storeToRefs } from 'pinia' // Add import
import { useAuthStore } from '@/stores' // More descriptive name recommended
// ... other imports
const workflowStore = useWorkflowStore()
const authStore = useAuthStore()
const { auth } = storeToRefs(authStore) // Maintain reactivity
// ... existing code
// Add computed property
const showSharedFolder = computed({
get: () => {
// Handle potential null/undefined and use .value for ref
return (auth.value?.isEE || auth.value?.isPE) ? isToolMenu : false
}
})
defineOptions({ name: 'ResourceNodeMenu' })
// ... rest of script
</script>
<template>
<!-- Use the computed property -->
<aside v-if="!isSystemScope" class="flex w-sidebar-expanded shrink-0 border-r pt-3">
<FolderTree
v-model="currentFolderId"
:can-edit="false"
:show-shared="showSharedFolder"
:source="source"
@select="loadResources"
/>
</aside>
<!-- ... -->
</template>Key Takeaway
The most critical issue is the loss of reactivity by destructuring auth without storeToRefs. This must be fixed to ensure the UI updates correctly when user entitlements (isEE/isPE) change.
| :show-shared="auth.isEE || auth.isPE" | ||
| @loaded="selectFolder" | ||
| @select="selectFolder" | ||
| /> |
There was a problem hiding this comment.
Here are the findings, potential issues, and optimization suggestions for the code changes:
1. Naming Collision: useStore vs useAuth
- Issue: You are importing
useStorefrom@/storesand immediately destructuringauthfrom it:const { auth } = useStore(). - Critique: If
useStoreis a global singleton store (like Pinia or Vuex), this works. However, ifauthis its own separate store, callinguseStore()might imply a dependency on a monolithic store rather than a modular one. - Suggestion: If
authis a distinct module, prefer using its specific composable (e.g.,useAuth()) directly to reduce coupling and improve clarity. If it must be part of a larger store, consider naming itauthStoreorauthStateto distinguish from component-local refs.
2. Missing Validation/Type Checks for Auth Flags
- Issue: The line
:show-shared="auth.isEE || auth.isPE"assumesauth.isEEandauth.isPEare always boolean-like. - Risk: If
authis undefined during initial loading or ifisEE/isPEare not initialized correctly, this could throw runtime errors or behave unexpectedly. - Suggestion: Ensure
authis safely initialized in the store. Alternatively, add optional chaining if supported by your template compiler (though often discouraged for performance)::show-shared="!!(auth?.isEE || auth?.isPE)". A safer approach is ensuring the store initializes these flags as booleans by default.
3. Hardcoded Business Logic in Template
- Issue: The condition
auth.isEE || auth.isPErepresents business logic (Enterprise Edition or Professional Edition users can see shared resources). - Optimization: It is cleaner to compute this permission check in the script setup block rather than embedding it directly in the template.
- Suggestion:
Then use
const showShared = computed(() => auth.isEE || auth.isPE);
:show-shared="showShared". This improves readability, testability, and allows for easier modification of logic later.
4. Potential Race Condition / Initial Render
- Issue: If
useStore()returns an empty object initially while fetching auth state asynchronously,auth.isEEmight beundefinedduring the first render. - Recommendation: Verify that the store provides default values (
false) forisEEandisPEto prevent temporary UI glitches or errors before data loads.
5. Code Style & Consistency
- Observation: The added lines follow existing indentation and style well. No formatting issues detected.
✅ Recommended Improved Code Snippet
<script setup>
import { computed } from 'vue';
import FolderTree from '@/components/business/folder-tree/index.vue'
import { TOOLTIP_SHOW_DELAY } from '@/components/global/mk-tooltip/constants'
import KnowledgeCard from '@/views/knowledge/knowledge-card/KnowledgeCard.vue'
import { FOLDER_ENTRY_ID } from '@/constants/folder'
import { useStore } from '@/stores' // Consider renaming if dedicated auth hook exists
import { isSystemResource, isSystemSharedResource, isWorkspaceResource } from '@/utils/resource-context'
const { t } = useI18n({ useScope: 'global' })
const { auth } = useStore()
// Extract logic into a computed property for clarity and reusability
const showSharedFeature = computed(() => !!(auth.isEE || auth.isPE));
defineOptions({ name: 'SelectKnowledgeDialog' })
// ...
</script>
<template>
<!-- ... -->
<FolderTree
class="pt-4"
:can-edit="false"
:source="RESOURCE_TYPE.KNOWLEDGE"
:show-shared="showSharedFeature"
@loaded="selectFolder"
@select="selectFolder"
/>
<!-- ... -->
</template>This version decouples logic from the template, enhances maintainability, and ensures safer evaluation of the feature flag.
fix: the community edition does not display the 'shared tools' and 'shared knowledge bases' menus