Repository navigation
refactor: centralize initial workspace selection for system pages #8292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,25 @@ | ||
| /** 工作空间选择。 */ | ||
| /** 系统用户组与资源授权页的工作空间初始化。 */ | ||
| import type { WorkspaceItem } from '@/api/types' | ||
| import { ROLE_TYPE } from '@/api/enums' | ||
| import { ADMIN_STORAGE_KEY } from '@/constants/storage-key' | ||
|
|
||
| function hasWorkspaceManagerRole(workspace: WorkspaceItem) { | ||
| return workspace.role_info?.some(({ role_type }) => role_type === ROLE_TYPE.WORKSPACE_MANAGE) ?? false | ||
| } | ||
| /** 在具有空间管理员角色的工作空间中优先选择缓存项,其次选择首个有效项,最后回退到 `default`。 */ | ||
| export function getInitialWorkspaceId(workspaceList?: WorkspaceItem[]): string { | ||
| let preferredWorkspaceId = localStorage.getItem(ADMIN_STORAGE_KEY.WORKSPACE_ID) | ||
| let firstManagedWorkspaceId: string | undefined | ||
|
|
||
| for (const { id, role_info } of workspaceList ?? []) { | ||
| if (!id) continue | ||
|
|
||
| /** 优先使用当前用户具有空间管理员角色的缓存工作空间,否则选择第一个可管理的工作空间。 */ | ||
| export function getInitialWorkspaceId(cachedWorkspaceId: string | null, workspaceList?: WorkspaceItem[]): string { | ||
| if (cachedWorkspaceId) { | ||
| const cachedWorkspace = workspaceList?.find(({ id }) => id === cachedWorkspaceId) | ||
| if (cachedWorkspace && hasWorkspaceManagerRole(cachedWorkspace)) { | ||
| return cachedWorkspaceId | ||
| const hasManagerRole = role_info?.some(({ role_type }) => role_type === ROLE_TYPE.WORKSPACE_MANAGE) | ||
| if (id === preferredWorkspaceId) { | ||
| if (hasManagerRole) return id | ||
| preferredWorkspaceId = null | ||
| } | ||
|
|
||
| if (hasManagerRole) firstManagedWorkspaceId ??= id | ||
| if (!preferredWorkspaceId && firstManagedWorkspaceId) return firstManagedWorkspaceId | ||
| } | ||
|
|
||
| return workspaceList?.find((workspace) => workspace.id && hasWorkspaceManagerRole(workspace))?.id ?? 'default' | ||
| return firstManagedWorkspaceId ?? 'default' | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,6 @@ import WorkspaceDropdown from '@/components/business/workspace-dropdown/index.vu | |
| import CreateGroupMemberDialog from './dialog/CreateGroupMemberDialog.vue' | ||
| import CreateOrUpdateGroupDialog from './dialog/CreateOrUpdateGroupDialog.vue' | ||
| import { LOGIN_METHOD_LABELS } from '@/constants' | ||
| import { ADMIN_STORAGE_KEY } from '@/constants/storage-key' | ||
| import { useStore } from '@/stores' | ||
| import { MsgConfirm, MsgSuccess } from '@/utils/message' | ||
| import { getRoleName } from '@/utils/role-name' | ||
|
|
@@ -19,9 +18,7 @@ const { auth, user } = useStore() | |
| const { t } = useI18n({ useScope: 'global' }) | ||
|
|
||
| /* 选择工作空间列表 */ | ||
| const selectedWorkspaceId = ref( | ||
| getInitialWorkspaceId(localStorage.getItem(ADMIN_STORAGE_KEY.WORKSPACE_ID), user.userInfo?.workspace_list), | ||
| ) | ||
| const selectedWorkspaceId = ref(getInitialWorkspaceId(user.userInfo?.workspace_list)) | ||
| const workspaceOptions = ref<WorkspaceItem[]>([]) | ||
| const loadingView = ref(false) | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here are my observations: 1. Behavior change risk (most important) 2. Function signature consistency 3. Fallback / edge cases
4. Removal of the import 5. Reactivity of user info 6. Type safety (minor) Optimization suggestions
Overall the change itself is clean; the main concern is the dropped localStorage-backed selection — please verify that's deliberate. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,6 @@ import PermissionTable from './components/PermissionTable.vue' | |
| import UserAuthorizationList from './components/UserAuthorizationList.vue' | ||
| import UserGroupAuthorizationList from './components/user-group/UserGroupAuthorizationList.vue' | ||
| import { RESOURCE_AUTHORIZATION_LABELS } from './constants' | ||
| import { ADMIN_STORAGE_KEY } from '@/constants/storage-key' | ||
| import { useStore } from '@/stores' | ||
| import { MsgSuccess } from '@/utils/message' | ||
| import { getInitialWorkspaceId } from '@/utils/workspace-selection' | ||
|
|
@@ -54,9 +53,7 @@ const resourceTypeOptions = computed<OptionItem<ResourceAuthorizationType>[]>(() | |
|
|
||
| /* 选择工作空间列表 */ | ||
| const loadingView = ref(false) | ||
| const selectedWorkspaceId = ref( | ||
| getInitialWorkspaceId(localStorage.getItem(ADMIN_STORAGE_KEY.WORKSPACE_ID), user.userInfo?.workspace_list), | ||
| ) | ||
| const selectedWorkspaceId = ref(getInitialWorkspaceId(user.userInfo?.workspace_list)) | ||
| const workspaceOptions = ref<WorkspaceItem[]>([]) | ||
|
|
||
| function handleWorkspaceSelect(workspace: WorkspaceItem) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code Review1. Removed import — make sure it's truly unused
2. Behavior change in
|
||
|
|
||
This file was deleted.
There was a problem hiding this comment.
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
preferredWorkspaceIdis typed asstring | nullimplicitly but declared withletand reassigned tonull— TypeScript may infer the type fromlocalStorage.getItem(...)which returnsstring | null. That's fine, but the mutation (preferredWorkspaceId = null) inside the loop makes the logic harder to follow than necessary. Consider a clearer variable naming (e.g.,cachedWorkspaceId, keep the original signature).Early-return micro-optimization is fragile.
This runs at the end of every iteration. It works, but it relies on the invariant that once the cached ID is cleared, the first managed workspace has already been captured. If the loop order or the meaning of "first" changes later, this becomes a subtle bug source. A cleaner structure: collect
firstManagedWorkspaceIdin one pass, then decide at the end.Behavioral change: the function signature changed from
(cachedWorkspaceId, workspaceList)to(workspaceList?). The function now reads fromlocalStoragedirectly. This:localStorage).ADMIN_STORAGE_KEY.WORKSPACE_IDscope assumption. Reading globallocalStorageassumes browser environment (no SSR guard). If this module can run server-side, this throwsReferenceError. Consider a guard or injection.Duplicate
role_infolookup logic —hasWorkspaceManagerRolewas removed and inlined. That loses the named helper's readability/reuse. Minor, but the inline check appears once, so acceptable; just note it reduces future reuse.Empty-string
idhandling —if (!id) continueskips empty IDs, good. But for the cached branch, ifid === preferredWorkspaceIdwhere both are''...preferredWorkspaceIdcan't be''unless stored; edge case, low risk.'default'fallback semantics unchanged, fine.Optimization Suggestions
cachedWorkspaceIdas a parameter (better testability and separation of concerns), or at minimum wrap localStorage access behind a safe helper.This removes the inline early-return and the mutable
preferredWorkspaceId = nulltrick while preserving the same behavior (cached-if-managed, else first managed, elsedefault).Summary of most important fixes
localStorageaccess (testability/SSR safety).