Skip to content

Commit 53563ad

Browse files
committed
Lint
1 parent ea22a27 commit 53563ad

File tree

11 files changed

+428
-233
lines changed

11 files changed

+428
-233
lines changed

packages/core/src/pluggableElementTypes/RpcMethodType.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import PluggableElementBase from './PluggableElementBase.ts'
22
import mapObject from '../util/map-obj/index.ts'
33
import { isRpcResult } from '../util/rpc.ts'
4-
import {
5-
getBlobMap,
6-
getFileFromCache,
7-
getFileHandleCache,
8-
setBlobMap,
9-
setFileHandleCache,
10-
} from '../util/tracks.ts'
4+
import { getBlobMap, getFileFromCache, setBlobMap } from '../util/tracks.ts'
115
import {
126
RetryError,
137
isAppRootModel,
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { getInitialSourceType, addAccountToLocation } from './FileSelector.tsx'
2+
3+
import type { BaseInternetAccountModel } from '../../pluggableElementTypes/index.ts'
4+
5+
describe('getInitialSourceType', () => {
6+
test('returns internetAccountId when location has one', () => {
7+
const location = {
8+
locationType: 'UriLocation' as const,
9+
uri: 'https://example.com/file.bam',
10+
internetAccountId: 'google-drive-account',
11+
}
12+
expect(getInitialSourceType(location)).toBe('google-drive-account')
13+
})
14+
15+
test('returns "url" for UriLocation without internetAccountId', () => {
16+
const location = {
17+
locationType: 'UriLocation' as const,
18+
uri: 'https://example.com/file.bam',
19+
}
20+
expect(getInitialSourceType(location)).toBe('url')
21+
})
22+
23+
test('returns "url" for undefined location', () => {
24+
expect(getInitialSourceType(undefined)).toBe('url')
25+
})
26+
27+
test('returns "file" for BlobLocation', () => {
28+
const location = {
29+
locationType: 'BlobLocation' as const,
30+
blobId: 'b123',
31+
name: 'test.bam',
32+
}
33+
expect(getInitialSourceType(location)).toBe('file')
34+
})
35+
36+
test('returns "file" for FileHandleLocation', () => {
37+
const location = {
38+
locationType: 'FileHandleLocation' as const,
39+
handleId: 'fh123',
40+
name: 'test.bam',
41+
}
42+
expect(getInitialSourceType(location)).toBe('file')
43+
})
44+
45+
test('returns "file" for LocalPathLocation', () => {
46+
const location = {
47+
locationType: 'LocalPathLocation' as const,
48+
localPath: '/path/to/file.bam',
49+
}
50+
expect(getInitialSourceType(location)).toBe('file')
51+
})
52+
})
53+
54+
describe('addAccountToLocation', () => {
55+
const mockAccount = {
56+
internetAccountId: 'test-account-id',
57+
} as BaseInternetAccountModel
58+
59+
test('adds internetAccountId to UriLocation', () => {
60+
const location = {
61+
locationType: 'UriLocation' as const,
62+
uri: 'https://example.com/file.bam',
63+
}
64+
const result = addAccountToLocation(location, mockAccount)
65+
expect(result).toEqual({
66+
locationType: 'UriLocation',
67+
uri: 'https://example.com/file.bam',
68+
internetAccountId: 'test-account-id',
69+
})
70+
})
71+
72+
test('returns location unchanged when no account provided', () => {
73+
const location = {
74+
locationType: 'UriLocation' as const,
75+
uri: 'https://example.com/file.bam',
76+
}
77+
const result = addAccountToLocation(location, undefined)
78+
expect(result).toEqual(location)
79+
})
80+
81+
test('returns non-UriLocation unchanged even with account', () => {
82+
const location = {
83+
locationType: 'BlobLocation' as const,
84+
blobId: 'b123',
85+
name: 'test.bam',
86+
}
87+
const result = addAccountToLocation(location, mockAccount)
88+
expect(result).toEqual(location)
89+
})
90+
})

packages/core/src/ui/FileSelector/FileSelector.tsx

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import useInternetAccounts from './useInternetAccounts.ts'
99
import { notEmpty } from '../../util/index.ts'
1010
import { isUriLocation } from '../../util/types/index.ts'
1111

12+
import type { BaseInternetAccountModel } from '../../pluggableElementTypes/index.ts'
1213
import 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+
2536
const 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

Comments
 (0)