-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathOpenModelDialog.jsx
More file actions
165 lines (160 loc) · 5.57 KB
/
OpenModelDialog.jsx
File metadata and controls
165 lines (160 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import React, {ReactElement} from 'react'
import {Button, Stack, Typography, TextField} from '@mui/material'
import {useAuth0} from '../../Auth0/Auth0Proxy'
import {checkOPFSAvailability} from '../../OPFS/utils'
import {looksLikeLink, githubUrlOrPathToSharePath} from '../../net/github/utils'
import {getUserTier, canLoadModel, recordModelLoad} from '../../privacy/usageTracking'
import useStore from '../../store/useStore'
import {loadLocalFile, loadLocalFileFallback} from '../../utils/loader'
import {disablePageReloadApprovalCheck} from '../../utils/event'
import {navigateToModel} from '../../utils/navigate'
import Dialog from '../Dialog'
import {useIsMobile} from '../Hooks'
import Tabs from '../Tabs'
import GitHubFileBrowser from './GitHubFileBrowser'
import PleaseLogin from './PleaseLogin'
import SampleModels from './SampleModels'
import {LABEL_LOCAL, LABEL_GITHUB, LABEL_SAMPLES} from './component'
import {FolderOpen as FolderOpenIcon} from '@mui/icons-material'
/**
* @property {boolean} isDialogDisplayed Passed to dialog to be controlled
* @property {Function} setIsDialogDisplayed Passed to dialog to be controlled
* @property {Function} navigate Callback from CadView to change page url
* @property {Array<string>} orgNamesArr List of org names for the current user.
* @return {ReactElement}
*/
export default function OpenModelDialog({
isDialogDisplayed,
setIsDialogDisplayed,
navigate,
orgNamesArr,
}) {
const tabLabels = [LABEL_LOCAL, LABEL_GITHUB, LABEL_SAMPLES]
const {isAuthenticated, user} = useAuth0()
const appPrefix = useStore((state) => state.appPrefix)
const appMetadata = useStore((state) => state.appMetadata)
const setCurrentTab = useStore((state) => state.setCurrentTab)
const currentTab = useStore((state) => state.currentTab)
const setIsUsageLimitDialogVisible = useStore((state) => state.setIsUsageLimitDialogVisible)
const isOpfsAvailable = checkOPFSAvailability()
const isMobile = useIsMobile()
const userTier = getUserTier(isAuthenticated, appMetadata)
/**
* Show usage limit dialog and close this dialog.
*
* @param {object} check Rate limit check result
*/
const showLimitDialog = (check) => {
setIsDialogDisplayed(false)
setIsUsageLimitDialogVisible(true, {reason: check.reason, stats: check.stats})
}
const openFile = () => {
// Rate-limit check for local file open
if (userTier !== 'pro') {
const check = canLoadModel(userTier)
if (!check.allowed) {
showLimitDialog(check)
return
}
}
const onLoad = (filename) => {
recordModelLoad()
// Use full reload when opening a new local file
disablePageReloadApprovalCheck()
navigateToModel(`${appPrefix}/v/new/${filename}`, navigate)
}
if (isOpfsAvailable) {
loadLocalFile(onLoad, false)
} else {
loadLocalFileFallback(onLoad, false)
}
setIsDialogDisplayed(false)
}
return (
<Dialog
headerIcon={<FolderOpenIcon className='icon-share'/>}
headerText='Open'
isDialogDisplayed={isDialogDisplayed}
setIsDialogDisplayed={setIsDialogDisplayed}
>
<Tabs
tabLabels={tabLabels}
currentTab={currentTab}
actionCb={(value) => setCurrentTab(value)}
isScrollable={false}
/>
<Stack
spacing={1}
direction='column'
sx={{
mt: 2,
justifyContent: 'center',
alignItems: 'center',
}}
data-testid={`dialog-open-model-tabs-stack`}
>
{ currentTab === 0 &&
<Stack data-testid='dialog-open-model-local' spacing={1}>
{!isMobile &&
<>
<Typography
variant='caption'
>
Drag and Drop files into viewport to open
</Typography>
<Typography
variant='caption'
sx={{textAlign: 'center', color: 'text.secondary'}}
>
— or —
</Typography>
</>
}
<Button onClick={openFile} variant='contained' data-testid='button_open_file'>
Browse files...
</Button>
</Stack>
}
{ currentTab === 1 &&
<Stack data-testid={`dialog-open-model-github`} spacing={1}>
<TextField
label='GitHub Model URL'
value={name}
onChange={(event) => {
const ghPath = event.target.value
if (looksLikeLink(ghPath)) {
if (userTier !== 'pro') {
const check = canLoadModel(userTier)
if (!check.allowed) {
showLimitDialog(check)
return
}
recordModelLoad()
}
setIsDialogDisplayed(false)
navigateToModel(githubUrlOrPathToSharePath(ghPath), navigate)
}
}}
/>
{isAuthenticated &&
<GitHubFileBrowser
navigate={navigate}
orgNamesArr={orgNamesArr}
user={user}
setIsDialogDisplayed={setIsDialogDisplayed}
userTier={userTier}
onLimitReached={(check) => showLimitDialog(check)}
/>}
{!isAuthenticated && <PleaseLogin/>}
</Stack>
}
{ currentTab === 2 &&
<SampleModels
navigate={navigate}
setIsDialogDisplayed={setIsDialogDisplayed}
/>
}
</Stack>
</Dialog>
)
}