@@ -2,24 +2,20 @@ import { RestartButton } from '@/components/RestartButton';
22import { Button } from '@/components/ui/button' ;
33import { isLocalStudio } from '@/config/constants' ;
44import { useInstanceClientIdParams } from '@/config/useInstanceClient' ;
5- import {
6- DirectoryIcon ,
7- } from '@/features/instance/applications/components/ApplicationsSidebar/FileTreeExplorer/DirectoryIcon' ;
85import { isDirectory } from '@/features/instance/applications/context/isDirectory' ;
96import { useEditorView } from '@/features/instance/applications/hooks/useEditorView' ;
107import { AddFolderFileModal } from '@/features/instance/applications/modals/AddFolderFileModal' ;
118import { DeleteFolderFileModal } from '@/features/instance/applications/modals/DeleteFolderFileModal' ;
129import { RedeployApplicationModal } from '@/features/instance/applications/modals/RedeployApplicationModal' ;
1310import { useDeleteComponentFolderFile } from '@/features/instance/operations/mutations/deleteComponentFolderFile' ;
1411import { useDeployComponentMutation } from '@/features/instance/operations/mutations/deployComponent' ;
15- import { useUpdateComponentFile } from '@/features/instance/operations/mutations/updateComponentFile' ;
1612import { useEffectedState } from '@/hooks/useEffectedState' ;
1713import { useToggler } from '@/hooks/useToggler' ;
1814import { parseFileExtension } from '@/lib/string/parseFileExtension' ;
1915import { Editor , EditorProps , OnMount } from '@monaco-editor/react' ;
2016import { useQueryClient } from '@tanstack/react-query' ;
2117import { useParams } from '@tanstack/react-router' ;
22- import { FileIcon , PackageIcon , PencilIcon , SaveIcon , TrashIcon } from 'lucide-react' ;
18+ import { FileIcon , FolderIcon , PackageIcon , PencilIcon , SaveIcon , TrashIcon , Undo2Icon } from 'lucide-react' ;
2319import { useCallback , useEffect , useRef , useState } from 'react' ;
2420import './directory-read-me.css' ;
2521import Markdown from 'react-markdown' ;
@@ -52,14 +48,10 @@ export function TextEditorView() {
5248 ) ;
5349 const targetNoun = instanceId || isLocalStudio ? 'Instance' : 'Cluster' ;
5450
55- const editorRef = useRef < Parameters < OnMount > [ 0 ] | null > ( null ) ;
51+ const mountedRef = useRef < Parameters < OnMount > | null > ( null ) ;
5652
5753 // TODO: Split all this logic up into smaller files, right?
5854
59- const handleEditorDidMount : EditorProps [ 'onMount' ] = useCallback < OnMount > ( ( editor ) => {
60- editorRef . current = editor ;
61- } , [ editorRef ] ) ;
62-
6355 useEffect ( ( ) => {
6456 const extension = parseFileExtension ( openedEntry ?. path ) ;
6557 const updatedLanguage = extensionToLanguageMap [ extension ] || 'plaintext' ;
@@ -84,38 +76,51 @@ export function TextEditorView() {
8476 if ( openedEntryContents ) {
8577 setUpdateFileContent ( openedEntryContents ) ;
8678 }
87- if ( editorRef ) {
88- editorRef . current ?. setValue ( openedEntryContents || '' ) ;
79+ if ( mountedRef . current ) {
80+ const [ editor ] = mountedRef . current ;
81+ editor . setValue ( openedEntryContents || '' ) ;
8982 }
9083 } , [ openedEntryContents ] ) ;
9184
92- const [ isAddFolderOrFileClicked , setIsAddFolderOrFileClicked ] = useState ( false ) ;
85+ const handleEditorDidMount : EditorProps [ 'onMount' ] = useCallback < OnMount > ( ( editor , monaco ) => {
86+ mountedRef . current = [ editor , monaco ] ;
87+ } , [ mountedRef , onSaveClick ] ) ;
88+
89+ useEffect ( ( ) => {
90+ if ( ! mountedRef . current ) {
91+ return ;
92+ }
93+ const [ editor , monaco ] = mountedRef . current ;
94+ const disposables = [
95+ editor . addAction ( {
96+ id : 'save-file' ,
97+ label : 'Save Changes' ,
98+ keybindings : [ monaco . KeyMod . CtrlCmd | monaco . KeyCode . KeyS ] ,
99+ run : onSaveClick ,
100+ } ) ,
101+ editor . addAction ( {
102+ id : 'revert-file' ,
103+ label : 'Revert File' ,
104+ run : onDiscardClick ,
105+ } ) ,
106+ ] ;
107+ return ( ) => {
108+ for ( const disposable of disposables ) {
109+ disposable ?. dispose ( ) ;
110+ }
111+ } ;
112+ } , [ mountedRef , onSaveClick , onDiscardClick ] ) ;
113+
114+ const {
115+ toggled : isAddFolderOrFileClicked ,
116+ toggleOn : onNewFileClick ,
117+ setToggled : setIsAddFolderOrFileClicked ,
118+ } = useToggler ( false ) ;
93119 const { toggled : isAddingFolder , toggleOn : setIsAddingFolder } = useToggler ( false ) ;
94120 const { toggled : isDeleteFolderOrFileClicked , setToggled : setIsDeleteFolderOrFileClicked } = useToggler ( false ) ;
95121 const { toggled : isRedeployApplicationClicked , setToggled : setIsRedeployApplicationClicked } = useToggler ( false ) ;
96- const { mutate : addFolderFile , isPending : isAddFolderFilePending } = useUpdateComponentFile ( ) ;
97122 const { mutate : deleteFolderFile , isPending : isDeleteFolderFilePending } = useDeleteComponentFolderFile ( ) ;
98123
99- const handleAddFolderOrFile = useCallback ( async ( name : string ) => {
100- if ( ! openedEntry ) {
101- return ;
102- }
103- addFolderFile (
104- {
105- file : `${ openedEntry . path . split ( '/' ) . slice ( 1 ) . join ( '/' ) } /${ name } ` ,
106- project : openedEntry . project ,
107- payload : isAddingFolder ? undefined : '' ,
108- ...instanceParams ,
109- } ,
110- {
111- onSuccess : ( ) => {
112- // TODO: refetchComponents();
113- setIsAddFolderOrFileClicked ( false ) ;
114- } ,
115- } ,
116- ) ;
117- } , [ addFolderFile , instanceParams , isAddingFolder , openedEntry ] ) ;
118-
119124 const handleDeleteFolderOrFile = useCallback ( async ( ) => {
120125 if ( ! openedEntry ) {
121126 return ;
@@ -196,7 +201,7 @@ export function TextEditorView() {
196201
197202 return (
198203 < >
199- { openedEntryContents && < >
204+ { openedEntryContents !== undefined && < >
200205 { ! isDirectory ( openedEntry )
201206 ? ( < Editor
202207 className = "w-full min-h-full h-80"
@@ -234,7 +239,7 @@ export function TextEditorView() {
234239 accessKey = "s"
235240 >
236241 < SaveIcon />
237- < span > < u > S</ u > ave</ span >
242+ < span className = "hidden lg:inline-block" > < u > S</ u > ave</ span >
238243 </ Button > }
239244
240245 < Button
@@ -249,28 +254,28 @@ export function TextEditorView() {
249254 accessKey = "s"
250255 >
251256 < PencilIcon />
252- < span > < u > R</ u > ename</ span >
257+ < span className = "hidden lg:inline-block" > < u > R</ u > ename</ span >
253258 </ Button >
254259
255- { isDirectory ( openedEntry ) && < Button
260+ < Button
256261 variant = "ghost"
257262 className = "rounded-none"
258- // onClick={onNewFileClick}
263+ onClick = { onNewFileClick }
259264 accessKey = "n"
260265 >
261266 < FileIcon />
262- < span > < u > N</ u > ew File</ span >
263- </ Button > }
267+ < span className = "hidden lg:inline-block" > < u > N</ u > ew File</ span >
268+ </ Button >
264269
265- { isDirectory ( openedEntry ) && < Button
270+ < Button
266271 variant = "ghost"
267272 className = "rounded-none"
268273 onClick = { setIsAddingFolder }
269274 accessKey = "n"
270275 >
271- < DirectoryIcon />
272- < span > < u > A</ u > dd Directory</ span >
273- </ Button > }
276+ < FolderIcon />
277+ < span className = "hidden lg:inline-block" > < u > A</ u > dd Directory</ span >
278+ </ Button >
274279
275280 { openedEntry . package && < Button
276281 variant = "ghost"
@@ -299,7 +304,7 @@ export function TextEditorView() {
299304 accessKey = "n"
300305 >
301306 < TrashIcon />
302- < span > < u > D</ u > elete</ span >
307+ < span className = "hidden xl:inline-block" > < u > D</ u > elete</ span >
303308 </ Button >
304309
305310 { ! isDirectory ( openedEntry ) && < Button
@@ -313,8 +318,8 @@ export function TextEditorView() {
313318 }
314319 accessKey = "d"
315320 >
316- < SaveIcon />
317- < span > < u > D</ u > iscard Changes</ span >
321+ < Undo2Icon />
322+ < span className = "hidden xl:inline-block" > < u > D</ u > iscard Changes</ span >
318323 </ Button > }
319324
320325 </ div >
@@ -323,8 +328,6 @@ export function TextEditorView() {
323328 isModalOpen = { isAddFolderOrFileClicked }
324329 setIsModalOpen = { setIsAddFolderOrFileClicked }
325330 isAddingFolder = { isAddingFolder }
326- handleAddFolderOrFile = { handleAddFolderOrFile }
327- isPending = { isAddFolderFilePending }
328331 />
329332 < DeleteFolderFileModal
330333 isModalOpen = { isDeleteFolderOrFileClicked }
0 commit comments