@@ -9,9 +9,10 @@ import useInternetAccounts from './useInternetAccounts.ts'
99import { notEmpty } from '../../util/index.ts'
1010import { isUriLocation } from '../../util/types/index.ts'
1111
12+ import type { BaseInternetAccountModel } from '../../pluggableElementTypes/index.ts'
1213import type { AbstractRootModel , FileLocation } from '../../util/types/index.ts'
1314
14- function getInitialToggleValue ( location ?: FileLocation ) {
15+ export function getInitialSourceType ( location ?: FileLocation ) {
1516 if (
1617 location &&
1718 'internetAccountId' in location &&
@@ -22,6 +23,16 @@ function getInitialToggleValue(location?: FileLocation) {
2223 return ! location || isUriLocation ( location ) ? 'url' : 'file'
2324}
2425
26+ export function addAccountToLocation (
27+ location : FileLocation ,
28+ account ?: BaseInternetAccountModel ,
29+ ) : FileLocation {
30+ if ( account && isUriLocation ( location ) ) {
31+ return { ...location , internetAccountId : account . internetAccountId }
32+ }
33+ return location
34+ }
35+
2536const FileSelector = observer ( function FileSelector ( {
2637 inline,
2738 location,
@@ -36,10 +47,9 @@ const FileSelector = observer(function FileSelector({
3647 inline ?: boolean
3748 rootModel ?: AbstractRootModel
3849 setLocation : ( param : FileLocation ) => void
39- setName ?: ( str : string ) => void
4050} ) {
41- const [ toggleButtonValue , setToggleButtonValue ] = useState ( ( ) =>
42- getInitialToggleValue ( location ) ,
51+ const [ sourceType , setSourceType ] = useState ( ( ) =>
52+ getInitialSourceType ( location ) ,
4353 )
4454
4555 const {
@@ -50,43 +60,39 @@ const FileSelector = observer(function FileSelector({
5060 setRecentlyUsed,
5161 } = useInternetAccounts ( rootModel )
5262
53- const selectedAccount = accountMap [ toggleButtonValue ]
63+ const selectedAccount = accountMap [ sourceType ]
5464
55- const setLocationWithAccount = useCallback (
65+ const handleLocationChange = useCallback (
5666 ( loc : FileLocation ) => {
57- setLocation ( {
58- ...loc ,
59- ...( selectedAccount && isUriLocation ( loc )
60- ? { internetAccountId : selectedAccount . internetAccountId }
61- : { } ) ,
62- } )
67+ setLocation ( addAccountToLocation ( loc , selectedAccount ) )
6368 } ,
6469 [ setLocation , selectedAccount ] ,
6570 )
6671
72+ // Sync account ID to location when account selection changes
6773 useEffect ( ( ) => {
6874 if (
6975 selectedAccount &&
7076 isUriLocation ( location ) &&
7177 location . internetAccountId !== selectedAccount . internetAccountId
7278 ) {
73- setLocationWithAccount ( location )
79+ handleLocationChange ( location )
7480 }
75- } , [ location , selectedAccount , setLocationWithAccount ] )
81+ } , [ location , selectedAccount , handleLocationChange ] )
7682
77- const selectSourceType = useCallback (
83+ const handleSourceTypeChange = useCallback (
7884 ( newValue : string | null ) => {
79- setRecentlyUsed ( [
80- ...new Set ( [ newValue , ...recentlyUsed ] . filter ( notEmpty ) ) ,
81- ] )
8285 if ( newValue ) {
83- setToggleButtonValue ( newValue )
84- }
85- if ( isUriLocation ( location ) ) {
86- setLocationWithAccount ( location )
86+ setRecentlyUsed ( [
87+ ...new Set ( [ newValue , ...recentlyUsed ] . filter ( notEmpty ) ) ,
88+ ] )
89+ setSourceType ( newValue )
90+ if ( isUriLocation ( location ) ) {
91+ handleLocationChange ( location )
92+ }
8793 }
8894 } ,
89- [ location , recentlyUsed , setRecentlyUsed , setLocationWithAccount ] ,
95+ [ location , recentlyUsed , setRecentlyUsed , handleLocationChange ] ,
9096 )
9197
9298 return (
@@ -97,21 +103,21 @@ const FileSelector = observer(function FileSelector({
97103 < FormGroup >
98104 < Box display = "flex" flexDirection = { inline ? 'row' : 'column' } gap = { 0.5 } >
99105 < SourceTypeSelector
100- value = { toggleButtonValue }
106+ value = { sourceType }
101107 shownAccountIds = { shownAccountIds }
102108 hiddenAccountIds = { hiddenAccountIds }
103109 accountMap = { accountMap }
104110 onChange = { ( _event , newValue ) => {
105- selectSourceType ( newValue )
111+ handleSourceTypeChange ( newValue )
106112 } }
107- onHiddenAccountSelect = { selectSourceType }
113+ onHiddenAccountSelect = { handleSourceTypeChange }
108114 />
109115 < LocationInput
110- toggleButtonValue = { toggleButtonValue }
116+ toggleButtonValue = { sourceType }
111117 selectedAccount = { selectedAccount }
112118 location = { location }
113119 inline = { inline }
114- setLocation = { setLocationWithAccount }
120+ setLocation = { handleLocationChange }
115121 />
116122 </ Box >
117123 </ FormGroup >
0 commit comments