forked from onlook-dev/onlook
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathCopy.tsx
More file actions
209 lines (197 loc) · 8.01 KB
/
Copy.tsx
File metadata and controls
209 lines (197 loc) · 8.01 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { useProjectsManager } from '@/components/Context';
import { invokeMainChannel } from '@/lib/utils';
import { Button } from '@onlook/ui/button';
import { Icons } from '@onlook/ui/icons/index';
import { Input } from '@onlook/ui/input';
import { MainChannels } from '@onlook/models/constants';
import { observer } from 'mobx-react-lite';
import { useMemo, useState } from 'react';
import { toast } from '@onlook/ui/use-toast';
import { t } from 'i18next';
import {
AlertDialog,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from '@onlook/ui/alert-dialog';
import { RunState } from '@onlook/models';
import { Progress } from '@onlook/ui/progress';
import { Checkbox } from '@onlook/ui/checkbox';
import { Label } from '@onlook/ui/label';
const Copy = observer(() => {
const projectsManager = useProjectsManager();
const project = projectsManager.project;
const folderPath = project?.folderPath || '';
const state = projectsManager.copy.copyStage;
const isTerminalRunning = projectsManager.runner?.state === RunState.RUNNING;
const [showWarningModal, setWarningModal] = useState<boolean>(false);
const [updatedPath, setUpdatedPath] = useState<string>('');
const [isLoading, setIsLoading] = useState<boolean>(false);
const [createCopy, setCreateCopy] = useState<boolean>(false);
const loadingStatus: { status: string; message: string } = useMemo(() => {
switch (state) {
case 'Starting...': {
return {
status: t('projects.copy.loadingModal.status.starting'),
message: t('projects.copy.loadingModal.message.starting'),
};
}
case 'Copying...': {
return {
status: t('projects.copy.loadingModal.status.copying'),
message: t('projects.copy.loadingModal.message.copying'),
};
}
case 'Complete': {
return {
status: t('projects.copy.loadingModal.status.complete'),
message: t('projects.copy.loadingModal.message.complete'),
};
}
case 'Error': {
return {
status: t('projects.copy.loadingModal.status.error'),
message: t('projects.copy.loadingModal.message.error'),
};
}
default: {
return {
status: t('projects.copy.loadingModal.status.copying'),
message: t('projects.copy.loadingModal.message.copying'),
};
}
}
}, [state]);
const progress = useMemo(() => {
switch (state) {
case 'Starting...': {
return 30;
}
case 'Copying...': {
return 60;
}
case 'Complete': {
return 100;
}
case 'Error': {
return 0;
}
}
}, [state]);
const handleUpdatePath = async () => {
const path = (await invokeMainChannel(MainChannels.PICK_COMPONENTS_DIRECTORY)) as
| string
| null;
if (!path || folderPath === path) {
console.error('No path selected');
return;
}
setUpdatedPath(path);
setWarningModal(true);
};
const cancelMoveFolder = () => {
setUpdatedPath('');
setWarningModal(false);
};
const confirmMoveFolder = async () => {
try {
setWarningModal(false);
if (projectsManager.runner?.state === RunState.RUNNING) {
await projectsManager.runner?.stop();
}
if (createCopy) {
setIsLoading(true);
await projectsManager.copy.createCopy(updatedPath);
}
projectsManager.updatePartialProject({
folderPath: updatedPath,
});
toast({
title: t('projects.copy.toasts.success.title'),
description: t('projects.copy.toasts.success.description'),
variant: 'warning',
});
} catch (error) {
toast({
title: t('projects.copy.toasts.error.title'),
description: t('projects.copy.toasts.error.description'),
variant: 'destructive',
});
console.error(error);
}
};
return (
<>
<div className="flex justify-between items-center">
<p className=" text-muted-foreground">Path</p>
<div className="flex items-center gap-2 w-2/3">
<Input id="folderPath" value={folderPath} readOnly={true} />
<Button size={'icon'} variant={'outline'} onClick={handleUpdatePath}>
<Icons.Directory />
</Button>
</div>
</div>
<AlertDialog open={showWarningModal} onOpenChange={setWarningModal}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{t('projects.copy.warningModal.title')}</AlertDialogTitle>
<AlertDialogDescription>
{isTerminalRunning
? t('projects.copy.warningModal.instanceRunning')
: t('projects.copy.warningModal.instanceNotRunning')}
</AlertDialogDescription>
</AlertDialogHeader>
<div className="flex items-center space-x-2">
<Checkbox
id="createCopy"
checked={createCopy}
onCheckedChange={(checked) => setCreateCopy(checked as boolean)}
/>
<Label htmlFor="createCopy">
{t('projects.copy.warningModal.createCopy')}
</Label>
</div>
<AlertDialogFooter>
<Button variant="ghost" onClick={cancelMoveFolder}>
{t('projects.copy.warningModal.cancel')}
</Button>
<Button variant="destructive" onClick={confirmMoveFolder}>
{t('projects.copy.warningModal.confirm')}
</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<AlertDialog open={isLoading} onOpenChange={setIsLoading}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
<span>{t('projects.copy.loadingModal.title')}</span>
<span>
<p className="text-sm font-normal text-muted-foreground mt-2 mb-4">
{loadingStatus.message}
</p>
</span>
</AlertDialogTitle>
<AlertDialogDescription>
{/* <div className="mb-4"></div> */}
<Progress value={progress} className="w-full" />
<p className="mt-2">{loadingStatus.status}</p>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<Button
disabled={state === 'Starting...' || state === 'Copying...'}
variant="secondary"
onClick={() => setIsLoading(false)}
>
{t('projects.copy.loadingModal.finish')}
</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</>
);
});
export default Copy;