Files
modeling-app/src/components/FileMachineProvider.tsx

159 lines
4.5 KiB
TypeScript
Raw Normal View History

Franknoirot/multi file (#844) * Fix unrelated bug, settings button in the home sidebar doesn't go to the home settings after my previous fixes to routes * Turn on "Replay Onboarding" button in home settings * Add icons * Add Tooltip component * Rough-in of sidebar styling and add initial File Tree * Polish basic styling * Show nested files and directories * Add tests * use camelCase for entrypointMetadata * Add ability to switch files via links * Revert "Improve Prop Typings for Modals. Remove instances of `any`. (… (#813) Revert "Improve Prop Typings for Modals. Remove instances of `any`. (#792)" This reverts commit 629f326f4cdf13d83a62f471b4b9a40d3c3a2a88. * ffmpeg instructions (#814) * Formatting * Remove folder names from display in app header * Highlight current file, open folders it's within * Navigate on double click, delete on Cmd + Esc + highlight focused folders * Migrate to an XState machine, add create new file * Add ability to create folders (with naive names) + remove command bar stuff for now * Use route loader data to instantiate the kcl code * Clean up some unused things * Add ability to rename files * Add ability to rename folders * Add keyboard shortcuts for creating files/folders * Tooltip style tweaks * Polish + re-execute when switching files with a connection * Reset code before navigating via file tree * Don't invoke `readProject` if you're in a browser * Show files and folders for projects on home page * Don't highlight folders further down the file tree * @jgomez720 and @jessfraz feedback: + indentation markers + proper file icon + bump down font size + touch up colors * Tune down spacing, allow scroll overflow * Fix formatting * Update src/lib/tauriFS.ts * Add a confirmation dialog when deleting Signed-off-by: Frank Noirot <frank@kittycad.io> --------- Signed-off-by: Frank Noirot <frank@kittycad.io> Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch>
2023-10-16 13:28:41 -04:00
import { useMachine } from '@xstate/react'
import { useNavigate, useRouteLoaderData } from 'react-router-dom'
import { IndexLoaderData, paths } from '../Router'
import React, { createContext } from 'react'
import { toast } from 'react-hot-toast'
import {
AnyStateMachine,
ContextFrom,
EventFrom,
InterpreterFrom,
Prop,
StateFrom,
} from 'xstate'
import { useCommandsContext } from 'hooks/useCommandsContext'
import { DEFAULT_FILE_NAME, fileMachine } from 'machines/fileMachine'
import {
createDir,
removeDir,
removeFile,
renameFile,
writeFile,
} from '@tauri-apps/api/fs'
import { FILE_EXT, readProject } from 'lib/tauriFS'
import { isTauri } from 'lib/isTauri'
import { sep } from '@tauri-apps/api/path'
Franknoirot/multi file (#844) * Fix unrelated bug, settings button in the home sidebar doesn't go to the home settings after my previous fixes to routes * Turn on "Replay Onboarding" button in home settings * Add icons * Add Tooltip component * Rough-in of sidebar styling and add initial File Tree * Polish basic styling * Show nested files and directories * Add tests * use camelCase for entrypointMetadata * Add ability to switch files via links * Revert "Improve Prop Typings for Modals. Remove instances of `any`. (… (#813) Revert "Improve Prop Typings for Modals. Remove instances of `any`. (#792)" This reverts commit 629f326f4cdf13d83a62f471b4b9a40d3c3a2a88. * ffmpeg instructions (#814) * Formatting * Remove folder names from display in app header * Highlight current file, open folders it's within * Navigate on double click, delete on Cmd + Esc + highlight focused folders * Migrate to an XState machine, add create new file * Add ability to create folders (with naive names) + remove command bar stuff for now * Use route loader data to instantiate the kcl code * Clean up some unused things * Add ability to rename files * Add ability to rename folders * Add keyboard shortcuts for creating files/folders * Tooltip style tweaks * Polish + re-execute when switching files with a connection * Reset code before navigating via file tree * Don't invoke `readProject` if you're in a browser * Show files and folders for projects on home page * Don't highlight folders further down the file tree * @jgomez720 and @jessfraz feedback: + indentation markers + proper file icon + bump down font size + touch up colors * Tune down spacing, allow scroll overflow * Fix formatting * Update src/lib/tauriFS.ts * Add a confirmation dialog when deleting Signed-off-by: Frank Noirot <frank@kittycad.io> --------- Signed-off-by: Frank Noirot <frank@kittycad.io> Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch>
2023-10-16 13:28:41 -04:00
type MachineContext<T extends AnyStateMachine> = {
state: StateFrom<T>
context: ContextFrom<T>
send: Prop<InterpreterFrom<T>, 'send'>
}
export const FileContext = createContext(
{} as MachineContext<typeof fileMachine>
)
export const FileMachineProvider = ({
children,
}: {
children: React.ReactNode
}) => {
const navigate = useNavigate()
Command bar: add extrude command, nonlinear editing, etc (#1204) * Tweak toaster look and feel * Add icons, tweak plus icon names * Rename commandBarMeta to commandBarConfig * Refactor command bar, add support for icons * Create a tailwind plugin for aria-pressed button state * Remove overlay from behind command bar * Clean up toolbar * Button and other style tweaks * Icon tweaks follow-up: make old icons work with new sizing * Delete unused static icons * More CSS tweaks * Small CSS tweak to project sidebar * Add command bar E2E test * fumpt * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * fix typo in a comment * Fix icon padding (built version only) * Update onboarding and warning banner icons padding * Misc minor style fixes * Get Extrude opening and canceling from command bar * Iconography tweaks * Get extrude kind of working * Refactor command bar config types and organization * Move command bar configs to be co-located with each other * Start building a state machine for the command bar * Start converting command bar to state machine * Add support for multiple args, confirmation step * Submission behavior, hotkeys, code organization * Add new test for extruding from command bar * Polish step back and selection hotkeys, CSS tweaks * Loading style tweaks * Validate selection inputs, polish UX of args re-editing * Prevent submission with multiple selection on singlular arg * Remove stray console logs * Tweak test, CSS nit, remove extrude "result" argument * Fix linting warnings * Show Ctrl+/ instead of ⌘K on all platforms but Mac * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * Add "Enter sketch" to command bar * fix command bar test * Fix flaky cmd bar extrude test by waiting for engine select response * Cover both button labels '⌘K' and 'Ctrl+/' in test --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2023-12-13 12:49:01 -05:00
const { commandBarSend } = useCommandsContext()
Franknoirot/multi file (#844) * Fix unrelated bug, settings button in the home sidebar doesn't go to the home settings after my previous fixes to routes * Turn on "Replay Onboarding" button in home settings * Add icons * Add Tooltip component * Rough-in of sidebar styling and add initial File Tree * Polish basic styling * Show nested files and directories * Add tests * use camelCase for entrypointMetadata * Add ability to switch files via links * Revert "Improve Prop Typings for Modals. Remove instances of `any`. (… (#813) Revert "Improve Prop Typings for Modals. Remove instances of `any`. (#792)" This reverts commit 629f326f4cdf13d83a62f471b4b9a40d3c3a2a88. * ffmpeg instructions (#814) * Formatting * Remove folder names from display in app header * Highlight current file, open folders it's within * Navigate on double click, delete on Cmd + Esc + highlight focused folders * Migrate to an XState machine, add create new file * Add ability to create folders (with naive names) + remove command bar stuff for now * Use route loader data to instantiate the kcl code * Clean up some unused things * Add ability to rename files * Add ability to rename folders * Add keyboard shortcuts for creating files/folders * Tooltip style tweaks * Polish + re-execute when switching files with a connection * Reset code before navigating via file tree * Don't invoke `readProject` if you're in a browser * Show files and folders for projects on home page * Don't highlight folders further down the file tree * @jgomez720 and @jessfraz feedback: + indentation markers + proper file icon + bump down font size + touch up colors * Tune down spacing, allow scroll overflow * Fix formatting * Update src/lib/tauriFS.ts * Add a confirmation dialog when deleting Signed-off-by: Frank Noirot <frank@kittycad.io> --------- Signed-off-by: Frank Noirot <frank@kittycad.io> Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch>
2023-10-16 13:28:41 -04:00
const { project } = useRouteLoaderData(paths.FILE) as IndexLoaderData
const [state, send] = useMachine(fileMachine, {
context: {
project,
selectedDirectory: project,
},
actions: {
navigateToFile: (
context: ContextFrom<typeof fileMachine>,
event: EventFrom<typeof fileMachine>
) => {
if (event.data && 'name' in event.data) {
Command bar: add extrude command, nonlinear editing, etc (#1204) * Tweak toaster look and feel * Add icons, tweak plus icon names * Rename commandBarMeta to commandBarConfig * Refactor command bar, add support for icons * Create a tailwind plugin for aria-pressed button state * Remove overlay from behind command bar * Clean up toolbar * Button and other style tweaks * Icon tweaks follow-up: make old icons work with new sizing * Delete unused static icons * More CSS tweaks * Small CSS tweak to project sidebar * Add command bar E2E test * fumpt * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * fix typo in a comment * Fix icon padding (built version only) * Update onboarding and warning banner icons padding * Misc minor style fixes * Get Extrude opening and canceling from command bar * Iconography tweaks * Get extrude kind of working * Refactor command bar config types and organization * Move command bar configs to be co-located with each other * Start building a state machine for the command bar * Start converting command bar to state machine * Add support for multiple args, confirmation step * Submission behavior, hotkeys, code organization * Add new test for extruding from command bar * Polish step back and selection hotkeys, CSS tweaks * Loading style tweaks * Validate selection inputs, polish UX of args re-editing * Prevent submission with multiple selection on singlular arg * Remove stray console logs * Tweak test, CSS nit, remove extrude "result" argument * Fix linting warnings * Show Ctrl+/ instead of ⌘K on all platforms but Mac * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * Add "Enter sketch" to command bar * fix command bar test * Fix flaky cmd bar extrude test by waiting for engine select response * Cover both button labels '⌘K' and 'Ctrl+/' in test --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2023-12-13 12:49:01 -05:00
commandBarSend({ type: 'Close' })
Franknoirot/multi file (#844) * Fix unrelated bug, settings button in the home sidebar doesn't go to the home settings after my previous fixes to routes * Turn on "Replay Onboarding" button in home settings * Add icons * Add Tooltip component * Rough-in of sidebar styling and add initial File Tree * Polish basic styling * Show nested files and directories * Add tests * use camelCase for entrypointMetadata * Add ability to switch files via links * Revert "Improve Prop Typings for Modals. Remove instances of `any`. (… (#813) Revert "Improve Prop Typings for Modals. Remove instances of `any`. (#792)" This reverts commit 629f326f4cdf13d83a62f471b4b9a40d3c3a2a88. * ffmpeg instructions (#814) * Formatting * Remove folder names from display in app header * Highlight current file, open folders it's within * Navigate on double click, delete on Cmd + Esc + highlight focused folders * Migrate to an XState machine, add create new file * Add ability to create folders (with naive names) + remove command bar stuff for now * Use route loader data to instantiate the kcl code * Clean up some unused things * Add ability to rename files * Add ability to rename folders * Add keyboard shortcuts for creating files/folders * Tooltip style tweaks * Polish + re-execute when switching files with a connection * Reset code before navigating via file tree * Don't invoke `readProject` if you're in a browser * Show files and folders for projects on home page * Don't highlight folders further down the file tree * @jgomez720 and @jessfraz feedback: + indentation markers + proper file icon + bump down font size + touch up colors * Tune down spacing, allow scroll overflow * Fix formatting * Update src/lib/tauriFS.ts * Add a confirmation dialog when deleting Signed-off-by: Frank Noirot <frank@kittycad.io> --------- Signed-off-by: Frank Noirot <frank@kittycad.io> Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch>
2023-10-16 13:28:41 -04:00
navigate(
`${paths.FILE}/${encodeURIComponent(
context.selectedDirectory + sep + event.data.name
Franknoirot/multi file (#844) * Fix unrelated bug, settings button in the home sidebar doesn't go to the home settings after my previous fixes to routes * Turn on "Replay Onboarding" button in home settings * Add icons * Add Tooltip component * Rough-in of sidebar styling and add initial File Tree * Polish basic styling * Show nested files and directories * Add tests * use camelCase for entrypointMetadata * Add ability to switch files via links * Revert "Improve Prop Typings for Modals. Remove instances of `any`. (… (#813) Revert "Improve Prop Typings for Modals. Remove instances of `any`. (#792)" This reverts commit 629f326f4cdf13d83a62f471b4b9a40d3c3a2a88. * ffmpeg instructions (#814) * Formatting * Remove folder names from display in app header * Highlight current file, open folders it's within * Navigate on double click, delete on Cmd + Esc + highlight focused folders * Migrate to an XState machine, add create new file * Add ability to create folders (with naive names) + remove command bar stuff for now * Use route loader data to instantiate the kcl code * Clean up some unused things * Add ability to rename files * Add ability to rename folders * Add keyboard shortcuts for creating files/folders * Tooltip style tweaks * Polish + re-execute when switching files with a connection * Reset code before navigating via file tree * Don't invoke `readProject` if you're in a browser * Show files and folders for projects on home page * Don't highlight folders further down the file tree * @jgomez720 and @jessfraz feedback: + indentation markers + proper file icon + bump down font size + touch up colors * Tune down spacing, allow scroll overflow * Fix formatting * Update src/lib/tauriFS.ts * Add a confirmation dialog when deleting Signed-off-by: Frank Noirot <frank@kittycad.io> --------- Signed-off-by: Frank Noirot <frank@kittycad.io> Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch>
2023-10-16 13:28:41 -04:00
)}`
)
}
},
toastSuccess: (_, event) =>
event.data && toast.success((event.data || '') + ''),
toastError: (_, event) => toast.error((event.data || '') + ''),
},
services: {
readFiles: async (context: ContextFrom<typeof fileMachine>) => {
const newFiles = isTauri()
? await readProject(context.project.path)
: []
return {
...context.project,
children: newFiles,
}
},
createFile: async (
context: ContextFrom<typeof fileMachine>,
event: EventFrom<typeof fileMachine, 'Create file'>
) => {
let name = event.data.name.trim() || DEFAULT_FILE_NAME
if (event.data.makeDir) {
await createDir(context.selectedDirectory.path + sep + name)
Franknoirot/multi file (#844) * Fix unrelated bug, settings button in the home sidebar doesn't go to the home settings after my previous fixes to routes * Turn on "Replay Onboarding" button in home settings * Add icons * Add Tooltip component * Rough-in of sidebar styling and add initial File Tree * Polish basic styling * Show nested files and directories * Add tests * use camelCase for entrypointMetadata * Add ability to switch files via links * Revert "Improve Prop Typings for Modals. Remove instances of `any`. (… (#813) Revert "Improve Prop Typings for Modals. Remove instances of `any`. (#792)" This reverts commit 629f326f4cdf13d83a62f471b4b9a40d3c3a2a88. * ffmpeg instructions (#814) * Formatting * Remove folder names from display in app header * Highlight current file, open folders it's within * Navigate on double click, delete on Cmd + Esc + highlight focused folders * Migrate to an XState machine, add create new file * Add ability to create folders (with naive names) + remove command bar stuff for now * Use route loader data to instantiate the kcl code * Clean up some unused things * Add ability to rename files * Add ability to rename folders * Add keyboard shortcuts for creating files/folders * Tooltip style tweaks * Polish + re-execute when switching files with a connection * Reset code before navigating via file tree * Don't invoke `readProject` if you're in a browser * Show files and folders for projects on home page * Don't highlight folders further down the file tree * @jgomez720 and @jessfraz feedback: + indentation markers + proper file icon + bump down font size + touch up colors * Tune down spacing, allow scroll overflow * Fix formatting * Update src/lib/tauriFS.ts * Add a confirmation dialog when deleting Signed-off-by: Frank Noirot <frank@kittycad.io> --------- Signed-off-by: Frank Noirot <frank@kittycad.io> Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch>
2023-10-16 13:28:41 -04:00
} else {
await writeFile(
context.selectedDirectory.path +
sep +
Franknoirot/multi file (#844) * Fix unrelated bug, settings button in the home sidebar doesn't go to the home settings after my previous fixes to routes * Turn on "Replay Onboarding" button in home settings * Add icons * Add Tooltip component * Rough-in of sidebar styling and add initial File Tree * Polish basic styling * Show nested files and directories * Add tests * use camelCase for entrypointMetadata * Add ability to switch files via links * Revert "Improve Prop Typings for Modals. Remove instances of `any`. (… (#813) Revert "Improve Prop Typings for Modals. Remove instances of `any`. (#792)" This reverts commit 629f326f4cdf13d83a62f471b4b9a40d3c3a2a88. * ffmpeg instructions (#814) * Formatting * Remove folder names from display in app header * Highlight current file, open folders it's within * Navigate on double click, delete on Cmd + Esc + highlight focused folders * Migrate to an XState machine, add create new file * Add ability to create folders (with naive names) + remove command bar stuff for now * Use route loader data to instantiate the kcl code * Clean up some unused things * Add ability to rename files * Add ability to rename folders * Add keyboard shortcuts for creating files/folders * Tooltip style tweaks * Polish + re-execute when switching files with a connection * Reset code before navigating via file tree * Don't invoke `readProject` if you're in a browser * Show files and folders for projects on home page * Don't highlight folders further down the file tree * @jgomez720 and @jessfraz feedback: + indentation markers + proper file icon + bump down font size + touch up colors * Tune down spacing, allow scroll overflow * Fix formatting * Update src/lib/tauriFS.ts * Add a confirmation dialog when deleting Signed-off-by: Frank Noirot <frank@kittycad.io> --------- Signed-off-by: Frank Noirot <frank@kittycad.io> Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch>
2023-10-16 13:28:41 -04:00
name +
(name.endsWith(FILE_EXT) ? '' : FILE_EXT),
''
)
}
return `Successfully created "${name}"`
},
renameFile: async (
context: ContextFrom<typeof fileMachine>,
event: EventFrom<typeof fileMachine, 'Rename file'>
) => {
const { oldName, newName, isDir } = event.data
let name = newName ? newName : DEFAULT_FILE_NAME
await renameFile(
context.selectedDirectory.path + sep + oldName,
Franknoirot/multi file (#844) * Fix unrelated bug, settings button in the home sidebar doesn't go to the home settings after my previous fixes to routes * Turn on "Replay Onboarding" button in home settings * Add icons * Add Tooltip component * Rough-in of sidebar styling and add initial File Tree * Polish basic styling * Show nested files and directories * Add tests * use camelCase for entrypointMetadata * Add ability to switch files via links * Revert "Improve Prop Typings for Modals. Remove instances of `any`. (… (#813) Revert "Improve Prop Typings for Modals. Remove instances of `any`. (#792)" This reverts commit 629f326f4cdf13d83a62f471b4b9a40d3c3a2a88. * ffmpeg instructions (#814) * Formatting * Remove folder names from display in app header * Highlight current file, open folders it's within * Navigate on double click, delete on Cmd + Esc + highlight focused folders * Migrate to an XState machine, add create new file * Add ability to create folders (with naive names) + remove command bar stuff for now * Use route loader data to instantiate the kcl code * Clean up some unused things * Add ability to rename files * Add ability to rename folders * Add keyboard shortcuts for creating files/folders * Tooltip style tweaks * Polish + re-execute when switching files with a connection * Reset code before navigating via file tree * Don't invoke `readProject` if you're in a browser * Show files and folders for projects on home page * Don't highlight folders further down the file tree * @jgomez720 and @jessfraz feedback: + indentation markers + proper file icon + bump down font size + touch up colors * Tune down spacing, allow scroll overflow * Fix formatting * Update src/lib/tauriFS.ts * Add a confirmation dialog when deleting Signed-off-by: Frank Noirot <frank@kittycad.io> --------- Signed-off-by: Frank Noirot <frank@kittycad.io> Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch>
2023-10-16 13:28:41 -04:00
context.selectedDirectory.path +
sep +
Franknoirot/multi file (#844) * Fix unrelated bug, settings button in the home sidebar doesn't go to the home settings after my previous fixes to routes * Turn on "Replay Onboarding" button in home settings * Add icons * Add Tooltip component * Rough-in of sidebar styling and add initial File Tree * Polish basic styling * Show nested files and directories * Add tests * use camelCase for entrypointMetadata * Add ability to switch files via links * Revert "Improve Prop Typings for Modals. Remove instances of `any`. (… (#813) Revert "Improve Prop Typings for Modals. Remove instances of `any`. (#792)" This reverts commit 629f326f4cdf13d83a62f471b4b9a40d3c3a2a88. * ffmpeg instructions (#814) * Formatting * Remove folder names from display in app header * Highlight current file, open folders it's within * Navigate on double click, delete on Cmd + Esc + highlight focused folders * Migrate to an XState machine, add create new file * Add ability to create folders (with naive names) + remove command bar stuff for now * Use route loader data to instantiate the kcl code * Clean up some unused things * Add ability to rename files * Add ability to rename folders * Add keyboard shortcuts for creating files/folders * Tooltip style tweaks * Polish + re-execute when switching files with a connection * Reset code before navigating via file tree * Don't invoke `readProject` if you're in a browser * Show files and folders for projects on home page * Don't highlight folders further down the file tree * @jgomez720 and @jessfraz feedback: + indentation markers + proper file icon + bump down font size + touch up colors * Tune down spacing, allow scroll overflow * Fix formatting * Update src/lib/tauriFS.ts * Add a confirmation dialog when deleting Signed-off-by: Frank Noirot <frank@kittycad.io> --------- Signed-off-by: Frank Noirot <frank@kittycad.io> Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch>
2023-10-16 13:28:41 -04:00
name +
(name.endsWith(FILE_EXT) || isDir ? '' : FILE_EXT)
)
return (
oldName !== name && `Successfully renamed "${oldName}" to "${name}"`
)
},
deleteFile: async (
context: ContextFrom<typeof fileMachine>,
event: EventFrom<typeof fileMachine, 'Delete file'>
) => {
const isDir = !!event.data.children
if (isDir) {
await removeDir(event.data.path, {
recursive: true,
}).catch((e) => console.error('Error deleting directory', e))
} else {
await removeFile(event.data.path).catch((e) =>
console.error('Error deleting file', e)
)
}
return `Successfully deleted ${isDir ? 'folder' : 'file'} "${
event.data.name
}"`
},
},
guards: {
'Has at least 1 file': (_, event: EventFrom<typeof fileMachine>) => {
if (event.type !== 'done.invoke.read-files') return false
return !!event?.data?.children && event.data.children.length > 0
},
},
})
return (
<FileContext.Provider
value={{
send,
state,
context: state.context, // just a convenience, can remove if we need to save on memory
}}
>
{children}
</FileContext.Provider>
)
}
export default FileMachineProvider