Skip to content

Commit

Permalink
Fix an issue with drag and dropping the files (tldraw#5013)
Browse files Browse the repository at this point in the history
We got an error saying there was a bad request when drag and dropping
files. This is because we wanted to create a file state as the file
owner, but the file did not exist yet (which is not allowed, we only
allow that for guest files). So we now also create a file together with
the file state.


![image](https://github.com/user-attachments/assets/2bde9622-e511-4f84-ad08-b1d6dd54e3cc)

I also noticed that the `createFile` function does not need to be async
and also extracted document names from the files if we have them.

### Change type

- [x] `bugfix`
- [ ] `improvement`
- [ ] `feature`
- [ ] `api`
- [ ] `other`

### Test plan

1. Drag some files to the sidebar.
2. They should correctly appear (with the correct names if they have
time) and you should not see any rejected mutation toasts (like the one
above).
  • Loading branch information
MitjaBezensek authored Nov 28, 2024
1 parent aa5aba3 commit 2d89d14
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 15 deletions.
20 changes: 17 additions & 3 deletions apps/dotcom/client/src/tla/app/TldrawApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Result, assert, fetch, structuredClone, throttle, uniqueId } from '@tld
import pick from 'lodash.pick'
import {
Signal,
TLDocument,
TLSessionStateSnapshot,
TLStoreSnapshot,
TLUiToastsContextType,
Expand All @@ -22,6 +23,7 @@ import {
createTLUser,
defaultUserPreferences,
getUserPreferences,
isDocument,
objectMapFromEntries,
objectMapKeys,
react,
Expand Down Expand Up @@ -245,9 +247,9 @@ export class TldrawApp {
return numberOfFiles < this.config.maxNumberOfFiles
}

async createFile(
createFile(
fileOrId?: string | Partial<TlaFile>
): Promise<Result<{ file: TlaFile }, 'max number of files reached'>> {
): Result<{ file: TlaFile }, 'max number of files reached'> {
if (!this.canCreateNewFile()) {
return Result.err('max number of files reached')
}
Expand Down Expand Up @@ -355,7 +357,19 @@ export class TldrawApp {

// Also create a file state record for the new file
this.z.mutate((tx) => {
for (const slug of response.slugs) {
for (let i = 0; i < response.slugs.length; i++) {
const slug = response.slugs[i]
const entries = Object.entries(snapshots[i].store)
const documentEntry = entries.find(([_, value]) => isDocument(value)) as
| [string, TLDocument]
| undefined
const name = documentEntry ? documentEntry[1].name : ''

const result = this.createFile({ id: slug, name })
if (!result.ok) {
console.error('Could not create file', result.error)
continue
}
tx.file_state.create({
userId: this.userId,
fileId: slug,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ export function TlaDeleteFileDialog({ fileId, onClose }: { fileId: string; onClo
await app.deleteOrForgetFile(fileId)
const recentFiles = app.getUserRecentFiles()
if (recentFiles.length === 0) {
app.createFile().then((res) => {
if (res.ok) {
navigate(getFilePath(res.value.file.id), { state: { mode: 'create' } })
trackEvent('delete-file', { source: 'file-menu' })
}
})
const result = app.createFile()
if (result.ok) {
navigate(getFilePath(result.value.file.id), { state: { mode: 'create' } })
trackEvent('delete-file', { source: 'file-menu' })
}
} else {
navigate(getFilePath(recentFiles[0].fileId))
}
Expand Down
9 changes: 4 additions & 5 deletions apps/dotcom/client/src/tla/pages/local.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ export function Component() {
if (!app) return
if (app.getUserRecentFiles().length === 0) {
creatingFile.current = true
app.createFile().then((res) => {
if (res.ok) {
navigate(getFilePath(res.value.file.id), { state: { mode: 'create' } })
}
})
const result = app.createFile()
if (result.ok) {
navigate(getFilePath(result.value.file.id), { state: { mode: 'create' } })
}
}
}, [app, navigate])

Expand Down
3 changes: 3 additions & 0 deletions packages/tlschema/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ export function isBinding(record?: UnknownRecord): record is TLBinding;
// @public (undocumented)
export function isBindingId(id?: string): id is TLBindingId;

// @public (undocumented)
export function isDocument(record?: UnknownRecord): record is TLDocument;

// @public (undocumented)
export function isPageId(id: string): id is TLPageId;

Expand Down
7 changes: 6 additions & 1 deletion packages/tlschema/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ export {
type TLUnknownBinding,
} from './records/TLBinding'
export { CameraRecordType, type TLCamera, type TLCameraId } from './records/TLCamera'
export { DocumentRecordType, TLDOCUMENT_ID, type TLDocument } from './records/TLDocument'
export {
DocumentRecordType,
TLDOCUMENT_ID,
isDocument,
type TLDocument,
} from './records/TLDocument'
export {
TLINSTANCE_ID,
pluckPreservingValues,
Expand Down
7 changes: 7 additions & 0 deletions packages/tlschema/src/records/TLDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createRecordMigrationSequence,
createRecordType,
RecordId,
UnknownRecord,
} from '@tldraw/store'
import { JsonObject } from '@tldraw/utils'
import { T } from '@tldraw/validate'
Expand Down Expand Up @@ -31,6 +32,12 @@ export const documentValidator: T.Validator<TLDocument> = T.model(
})
)

/** @public */
export function isDocument(record?: UnknownRecord): record is TLDocument {
if (!record) return false
return record.typeName === 'document'
}

/** @public */
export const documentVersions = createMigrationIds('com.tldraw.document', {
AddName: 1,
Expand Down

0 comments on commit 2d89d14

Please sign in to comment.