tldraw/www/hooks/useFileSystem.ts
Steve Ruiz be2c6d6d1f
[refactor] events (#230)
* bumps rko, adds events

* rename tlstate to state, fix env for multiplayer test

* Fix multiplayer

* rename data tldrawstate to tldrawsnapshot

* Update multiplayer-editor.tsx

* Fix shhhmp

* Update 2.tldr

* Add API to the README
2021-11-08 14:21:37 +00:00

43 lines
1.1 KiB
TypeScript

import * as React from 'react'
import { TLDrawState } from '@tldraw/tldraw'
export function useFileSystem(state: TLDrawState) {
const promptSaveBeforeChange = React.useCallback(async () => {
if (state.isDirty) {
if (state.fileSystemHandle) {
if (window.confirm('Do you want to save changes to your current project?')) {
await state.saveProject()
}
} else {
if (window.confirm('Do you want to save your current project?')) {
await state.saveProject()
}
}
}
}, [state])
const onNewProject = React.useCallback(async () => {
await promptSaveBeforeChange()
state.newProject()
}, [state, promptSaveBeforeChange])
const onSaveProject = React.useCallback(() => {
state.saveProject()
}, [state])
const onSaveProjectAs = React.useCallback(() => {
state.saveProjectAs()
}, [state])
const onOpenProject = React.useCallback(async () => {
await promptSaveBeforeChange()
state.openProject()
}, [state, promptSaveBeforeChange])
return {
onNewProject,
onSaveProject,
onSaveProjectAs,
onOpenProject,
}
}