Repository navigation
fix: user workspace select error - #7715
Conversation
| const selectedWorkspaceId = ref(user.userInfo?.workspace_list?.[0]?.id ?? 'default') | ||
| const workspaceOptions = ref<WorkspaceItem[]>([]) | ||
| const loadingView = ref(false) | ||
|
|
There was a problem hiding this comment.
Assuming this is Vue 3 + Pinia, the change is reasonable but has a few potential issues.
Potential issues
-
Pinia destructuring may break reactivity
const { auth, user } = useStore()
Destructuring state/getters from a Pinia store can lose reactivity if the property is later replaced. If
useris a state/getter and may update asynchronously, prefer:const store = useStore() const { auth } = storeToRefs(store) // access user via store.user
If
useris an action or a stable nested object and you only read it once, this is less critical. -
Initialization timing / async user data
const selectedWorkspaceId = ref( user.userInfo?.workspace_list?.[0]?.id ?? 'default' )
This reads
userInfoonly once during setup. IfuserInfoorworkspace_listis loaded asynchronously after component creation,selectedWorkspaceIdwill stay'default'and will not update when data arrives.Use a
watchwithimmediate: true, and avoid overwriting a valid user selection. -
Fallback only handles
null/undefined
?? 'default'does not handle an empty string or an invalid workspace ID. If empty IDs are possible, validate explicitly:const id = user.userInfo?.workspace_list?.[0]?.id const selectedWorkspaceId = ref(id || 'default')
Be careful if
0is a valid ID — in that case use an explicit check instead of||. -
Type inference may be wrong
IfWorkspaceItem['id']isnumber, then:ref(user.userInfo?.workspace_list?.[0]?.id ?? 'default')
may infer
number | "default", which can conflict with code expecting astring. Consider:const selectedWorkspaceId = ref<string | number>('default')
or a proper
WorkspaceIdtype. -
usermay be undefined
If there is any chanceuseris not always defined, use:user?.userInfo?.workspace_list?.[0]?.id
Otherwise the current optional chaining only protects
userInfo, notuser. -
Selected workspace may not exist in
workspaceOptions
Defaulting to the first workspace fromuserInfocan produce an invalid selection ifworkspaceOptionsis loaded later or filtered. Make sureselectedWorkspaceIdis always present inworkspaceOptions, or initialize it after options are loaded. -
Product behavior change
Previously the default was'default'; now it is the first workspace. Confirm this is intentional. If'default'is a special backend value, switching to a real workspace ID may change API behavior.
Suggested safer pattern
const store = useStore()
const { auth } = storeToRefs(store)
const selectedWorkspaceId = ref<string | number>('default')
const initSelectedWorkspace = () => {
const list = store.user?.userInfo?.workspace_list
if (!list?.length) return
const current = selectedWorkspaceId.value
const isValid = list.some((item) => item.id === current)
if (!current || current === 'default' || !isValid) {
selectedWorkspaceId.value = list[0].id
}
}
watch(
() => store.user?.userInfo?.workspace_list,
initSelectedWorkspace,
{ immediate: true }
)Summary
The main risks are:
- losing reactivity through Pinia destructuring,
- initializing
selectedWorkspaceIdbefore async user/workspace data is available, - weak fallback/type handling,
- and possibly selecting a workspace that is not yet valid in the loaded options list.
The change is safe only if user.userInfo is guaranteed to be available synchronously and if workspace_list[0] is always a valid selectable workspace.
| const selectedWorkspaceId = ref(user.userInfo?.workspace_list?.[0]?.id ?? 'default') | ||
| const workspaceOptions = ref<WorkspaceItem[]>([]) | ||
|
|
||
| function handleWorkspaceSelect(workspace: WorkspaceItem) { |
There was a problem hiding this comment.
The diff is syntactically fine, but there are a few potential issues to review:
-
Initialization timing / stale value
const selectedWorkspaceId = ref(user.userInfo?.workspace_list?.[0]?.id ?? 'default')
This runs only once during component setup. If
user.userInfoorworkspace_listis loaded asynchronously,selectedWorkspaceIdwill remain'default'even after the user data arrives.If that is possible, add a watcher:
watch( () => user.userInfo?.workspace_list, (list) => { if (selectedWorkspaceId.value === 'default') { selectedWorkspaceId.value = list?.[0]?.id ?? 'default' } }, { immediate: true } )
Be careful not to override a workspace the user has already selected manually.
-
The first workspace may not be the correct default
workspace_list?.[0]?.idassumes the first workspace is the desired one. If the API provides a default/current workspace flag, prefer that:const defaultWorkspaceId = user.userInfo?.workspace_list?.find(w => w.isDefault)?.id ?? user.userInfo?.workspace_list?.[0]?.id ?? 'default'
Otherwise, document that the first workspace is intentionally used.
-
Possible TypeScript type change
IfWorkspaceItem.idis notstring, thenselectedWorkspaceIdmay becomestring | numberinstead of the previousstring. This can cause type errors downstream. Use an explicit type or normalize the value:const selectedWorkspaceId = ref<string>( String(user.userInfo?.workspace_list?.[0]?.id ?? 'default') )
Only do this if IDs are expected to be strings.
-
'default'as a sentinel value
If'default'is also a real workspace ID, the fallback is ambiguous. Preferundefined/nullinternally, or make sure'default'is always handled specially. -
Reactivity/destructuring concern
IfuseStore()is a Pinia store anduseris a reactive store/state object, this is likely fine. Ifuseris a ref or computed value, destructuring may lose reactivity. In that case, access it through the store or usestoreToRefs. -
Validate selected workspace after options load
AfterworkspaceOptionsis fetched, ensureselectedWorkspaceIdexists in the options list. Otherwise the select component may appear blank or issue requests with a workspace ID not available to the user. -
Optional guard
Ifuseritself can be undefined, use:user?.userInfo?.workspace_list?.[0]?.id
Overall, the main risk is async user/workspace data loading. If user.userInfo.workspace_list is guaranteed to be available synchronously, the change is acceptable; otherwise, add a watcher or initialize after the workspace/user data is fetched.
fix: user workspace select error