Skip to content

perf: Optimize file - #7969

Merged
wangdan-fit2cloud merged 1 commit into
v3from
pr@v3@feat_optimize_file
Nov 17, 2026
Merged

wangdan-fit2cloud merged 1 commit into
v3from
pr@v3@feat_optimize_file

Conversation

@shaohuzhang1

Copy link
Copy Markdown
Contributor

perf: Optimize file

@wangdan-fit2cloud
wangdan-fit2cloud merged commit 9acab1a into v3 Nov 17, 2026
3 of 4 checks passed
@wangdan-fit2cloud
wangdan-fit2cloud deleted the pr@v3@feat_optimize_file branch November 17, 2026 07:38
<span>{{ folderEntries.all.name }}</span>
</MkListItem>
</div>

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.

Overall

The refactor is clean and the as const satisfies Record<FolderSource, …> exhaustiveness check is a nice improvement. I found no outright bugs, but there are a few maintainability and reactivity subtleties worth addressing.

Medium-priority concerns

  1. Duplicated source of truth for the i18n keys.
    getFolderEntry (in @/constants/folder) already knew the mapping source → id + label key. The keys application.categories.all, etc. now live in two places. If a locale key or a RESOURCE_TYPE changes, the two can silently drift.
    Recommendation: move folderEntryKeys into @/constants/folder and re-implement getFolderEntry on top of it (or export a typed getFolderEntryName(source, kind, t)), then import it here. That keeps one authoritative map and still lets you use FOLDER_ENTRY_ID.* directly.

  2. Verify the IDs are identical to what getFolderEntry returned.
    The code now assumes getFolderEntry(source, 'all').id === FOLDER_ENTRY_ID.ALL and the same for shared. If getFolderEntry ever returned a different/prefixed id (or extra fields like parentId, isRoot, children), resolveCurrentFolder consumers would break. The previous code narrowed to { id, name } so the shape is unchanged, but the id equality is worth a one-line confirmation/test.

  3. t() and props.rootLabel are now read outside the computed's tracking scope.
    They are only touched inside the get name() accessors, so folderEntries will not re-evaluate when the locale or rootLabel changes — only when props.source changes. In the template this still works (the render effect tracks t and rootLabel when the getter is invoked), but:

    • watch(() => folderEntries.value.all.name, …) will never fire on locale change.
    • Reading .name in a non-reactive context (event handler, exported snapshot, state serialization) captures a stale/untracked value.
    • Any consumer that memoizes on folderEntries identity will miss locale changes.
      Recommendation: if the entries are only used in the template, this is fine — add a short comment. If not, compute the names eagerly inside the computed body instead of using getters.
  4. Potential undefined on folderEntryKeys[props.source].
    satisfies protects you at compile time, but if props.source originates from a route/query param cast to FolderSource, an unexpected value yields undefined and keys.all throws. A small guard or a ?? fallback would make this defensive.

Low-priority / optimizations

  1. Hoist folderEntryKeys out of the setup body. In <script setup> this object literal is allocated once per component instance. Since it is static, move it to module scope (or, preferably, to the constants module per point 1).

  2. as const is redundant with satisfies here. The values are already literal strings, so the satisfies clause gives you the key/type checking you want. as const only adds readonly, which is harmless — but you can drop it for brevity.

  3. Template duplication. The two MkListItem blocks differ only by id/name/icon/active state. A computed list such as
    [{ id, name, icon, activeIcon, active }, …] rendered with v-for would remove the repetition and make future entries (e.g. a "favorites" pseudo-folder) trivial to add.

  4. Naming. folderEntries.all is slightly ambiguous for a pseudo-root; root/allRoot might read better. Also consider aligning the name with the existing getFolderEntry vocabulary so future readers connect the two.

  5. Do not spread these entries ({ ...folderEntries.value.all }) anywhere — spreading would eagerly invoke the getters and freeze the translated name.

  6. Clean up getFolderEntry if now unused. If no other module imports it after this change, either delete it or mark it deprecated so there is exactly one implementation of the mapping.

@loaded="handleFolderSelect"
@select="handleFolderSelect"
/>
</template>

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.

Reviewed the diff. No obvious critical bug, but there are several points worth checking.

Potential issues

  1. Fallback object may not satisfy FolderItem

    const currentFolder = computed<FolderItem>(() =>
      selectedFolder.value ?? {
        id: FOLDER_ENTRY_ID.ALL,
        name: t('application.categories.all'),
      }
    )

    If FolderItem has required fields beyond id and name, this will either fail type-checking or create an incomplete object at runtime. This is the main risk of removing getFolderEntry().

    Safer options:

    • Keep using getFolderEntry() to build the default folder.
    • Or create a dedicated helper that returns a valid FolderItem.
    • Avoid as FolderItem unless the missing fields are deliberately irrelevant.
  2. currentFolderId duplicates currentFolder.id
    currentFolderId is still mutable state:

    const currentFolderId = ref<string>(FOLDER_ENTRY_ID.ALL)

    but it is always derived from currentFolder.value.id inside handleFolderSelect. This can become inconsistent if selectedFolder is changed elsewhere.

    Prefer:

    const currentFolderId = computed(() => currentFolder.value.id)

    Then remove the assignment in handleFolderSelect.

  3. @loaded="handleFolderSelect" relies on the event payload contract
    The old code had:

    function handleFolderLoaded(folder?: FolderItem) {
      handleFolderSelect(folder ?? allApplicationsFolder.value)
    }

    The new code directly binds:

    @loaded="handleFolderSelect"

    This is fine if loaded emits undefined when there is no folder and you intentionally want the computed fallback. But if loaded can emit null, an array, or fire after a user selection, the behavior may be surprising.

    If the contract is not guaranteed, keep a small adapter:

    function handleFolderLoaded(folder?: FolderItem | null) {
      handleFolderSelect(folder ?? undefined)
    }
  4. selectedFolder is typed as FolderItem | undefined, but the template may pass null
    If the loaded event emits null, TypeScript may not catch it through the template. Normalize it:

    selectedFolder.value = folder ?? undefined
  5. No refresh when folder ID is the same but folder metadata changes

    if (previousFolderId !== currentFolderId.value) {
      cancelBatchSelection()
      refreshApplications()
    }

    This matches the old behavior, so it is not a regression. But if folder name/permissions can change while the ID stays the same, the UI may need to update more state.

Optimization suggestions

  • Derive currentFolderId from currentFolder instead of storing it separately.
  • Reuse a valid default FolderItem helper instead of constructing an inline fallback object.
  • Consider separating @loaded and @select handlers if their semantics differ.
  • If currentFolder is only used for id and name, consider narrowing its type instead of forcing FolderItem.

Example cleanup:

const selectedFolder = ref<FolderItem>()

const currentFolder = computed<FolderItem>(() =>
  selectedFolder.value ?? getFolderEntry(RESOURCE_TYPE.APPLICATION, 'all', t)
)

const currentFolderId = computed(() => currentFolder.value.id)

function handleFolderSelect(folder?: FolderItem | null) {
  const previousFolderId = currentFolder.value.id
  selectedFolder.value = folder ?? undefined

  if (previousFolderId !== currentFolder.value.id) {
    cancelBatchSelection()
    refreshApplications()
  }
}

Overall, the refactor is reasonable, but the inline fallback FolderItem and duplicated currentFolderId state are the two things I would address before merging.

@loaded="handleFolderSelect"
@select="handleFolderSelect"
/>
</template>

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.

Reviewed. The logic is mostly coherent, but there are a few risks and cleanup opportunities.

  1. Fallback FolderItem may be incomplete

    { id: FOLDER_ENTRY_ID.ALL, name: t('knowledge.categories.all') }

    If FolderItem requires additional fields such as type, source, parentId, children, permissions, etc., this will either fail TypeScript checking or break consumers that expect a full folder object. The previous getFolderEntry(...) likely returned a complete object.

  2. Fallback object is recreated on every currentFolder.value access
    This can cause object identity churn and unnecessary watcher/rendering updates. Prefer a stable computed fallback:

    const allKnowledgeFolder = computed<FolderItem>(() =>
      getFolderEntry(RESOURCE_TYPE.KNOWLEDGE, 'all', t)
    )
    const selectedFolder = ref<FolderItem>()
    const currentFolder = computed<FolderItem>(
      () => selectedFolder.value ?? allKnowledgeFolder.value
    )

    Or, if avoiding getFolderEntry, define a complete stable fallback object.

  3. handleFolderSelect can be clearer and avoid extra computed reads
    Compute the next id once:

    function handleFolderSelect(folder?: FolderItem) {
      const previousFolderId = currentFolder.value.id
      const nextFolderId = folder?.id ?? FOLDER_ENTRY_ID.ALL
    
      selectedFolder.value = folder
      currentFolderId.value = nextFolderId
    
      if (previousFolderId !== nextFolderId) {
        cancelBatchSelection()
        refreshKnowledge()
      }
    }

    This avoids relying on currentFolder.value after mutating selectedFolder.

  4. Check @loaded semantics
    Reusing handleFolderSelect for @loaded means a loaded event with undefined resets the selection to ALL and may call refreshKnowledge() if the previous folder was not ALL. If loaded can fire more than once or after a user selection, this could unexpectedly clear the selection or trigger a duplicate refresh. If loaded only fires once during initialization, this is fine.

  5. currentFolder is now read-only
    It was previously a ref, so any other code assigning currentFolder.value = ... would now fail or silently need refactoring. Make sure there are no remaining assignments outside this diff.

  6. Duplicated translation/label logic
    Hardcoding t('knowledge.categories.all') duplicates what getFolderEntry centralizes. If the label or folder metadata changes, this fallback may drift. Reusing getFolderEntry is safer unless there is a clear reason to remove it.

  7. Potential state duplication
    selectedFolder and currentFolderId are two sources of truth. If FolderTree updates v-model="currentFolderId" without emitting select, they can become inconsistent. Ensure select is always emitted, or derive one from the other.

Overall: the approach is reasonable, but I would keep a stable/full FolderItem fallback and verify the loaded event behavior.

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