Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 12 additions & 41 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { lazy, Suspense, useCallback, useEffect, useRef, useState } from 'react';
import {
ArrowDownToLine,
ArrowRight,
Box,
BookOpen,
Check,
Expand Down Expand Up @@ -56,10 +55,9 @@ import {
editorCommands,
executeEditorCommand,
redoAvailable,
saveCurrentScene,
undoAvailable,
} from './core/commands';
import { driveAutosave, flushDriveBeforeReplace, useDrive } from './core/drive-sync';
import { driveAutosave, useDrive } from './core/drive-sync';
import { DriveButton, GoogleDriveDialog } from './components/GoogleDrive';
import { ActivityCenter, NotificationToasts } from './components/ActivityCenter';
import { WhatsNew } from './components/WhatsNew';
Expand All @@ -73,8 +71,8 @@ import { PreferencesPanel } from './components/PreferencesPanel';
import { PartsPanel } from './components/PartsPanel';
import { Inspector } from './components/Inspector';
import { moveSteps, rotationSteps } from './components/TransformAdjustments';
import { Dialogs } from './components/Dialogs';
import { IconButton, Keyboard, Logo, Modal, NumericInput, useDismiss } from './components/ui';
import { Dialogs, ReplaceSceneDialog } from './components/Dialogs';
import { IconButton, Keyboard, Logo, NumericInput, useDismiss } from './components/ui';
import { clipLimits } from './core/clipping';
import { partColorModes } from './core/appearance';
import type { SceneDocument } from './core/types';
Expand Down Expand Up @@ -729,7 +727,8 @@ export default function App() {
[right, setRight] = useState(() => window.innerWidth > 1050),
[dragging, setDragging] = useState(false),
[importFiles, setImportFiles] = useState<File[]>([]),
[pendingScene, setPendingScene] = useState<SceneDocument | null>(null);
[pendingScene, setPendingScene] = useState<{ scene: SceneDocument; id: number } | null>(null);
const openRequestId = useRef(0);
const fileInput = useRef<HTMLInputElement>(null),
dragDepth = useRef(0);
const tool = useEditor((s) => s.tool),
Expand Down Expand Up @@ -817,11 +816,10 @@ export default function App() {
if (files.length === 1 && files[0].name.toLowerCase().endsWith('.kitbash')) {
editor.setBusy('Opening scene…');
try {
setPendingScene(
await readSceneFile(files[0], (fraction) =>
editor.setBusy(`Opening scene… ${Math.round(fraction * 100)}%`),
),
const scene = await readSceneFile(files[0], (fraction) =>
editor.setBusy(`Opening scene… ${Math.round(fraction * 100)}%`),
);
setPendingScene({ scene, id: ++openRequestId.current });
} catch (error) {
editor.notify((error as Error).message, 'error');
} finally {
Expand Down Expand Up @@ -1031,38 +1029,11 @@ export default function App() {
/>
<GoogleDriveDialog />
{pendingScene ? (
<Modal
title={`Open ${pendingScene.name}?`}
subtitle="This replaces the current workbench. Save a scene copy if you want to keep it."
<ReplaceSceneDialog
key={pendingScene.id}
scene={pendingScene.scene}
onClose={() => setPendingScene(null)}
>
<div className="open-scene-summary">
<FolderOpen size={29} />
<div>
<strong>
{pendingScene.parts.length} parts · {pendingScene.joints.length} joints
</strong>
<p>All geometry, placement, and joint constraints will be restored.</p>
</div>
</div>
<div className="modal-footer">
<button className="button secondary" onClick={saveCurrentScene}>
Save current scene
</button>
<button
className="button primary"
onClick={async () => {
if (!(await flushDriveBeforeReplace())) return;
useEditor.getState().setScene(pendingScene);
setPendingScene(null);
useEditor.getState().notify('Scene opened');
}}
>
Open scene
<ArrowRight size={14} />
</button>
</div>
</Modal>
/>
) : null}
<EditorContextMenu openScene={openScene} searchParts={searchParts} />
{dragging ? (
Expand Down
164 changes: 132 additions & 32 deletions src/components/Dialogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import {
X,
} from 'lucide-react';
import { useEditor } from '../core/store';
import { saveCurrentScene } from '../core/commands';
import { flushDriveBeforeReplace } from '../core/drive-sync';
import { downloadCurrentScene, saveCurrentScene } from '../core/commands';
import { driveAutosave } from '../core/drive-sync';
import type { SceneDocument } from '../core/types';
import { bindingsFor, formatBindings } from '../core/keybindings';
import type { KeyActionId } from '../core/keybindings';
import { usePreferences } from '../core/preferences';
Expand Down Expand Up @@ -763,41 +764,139 @@ function HelpDialog() {
);
}

function NewDialog() {
const create = async (sample: boolean) => {
if (!(await flushDriveBeforeReplace())) return;
useEditor
.getState()
.setScene(sample ? createSample() : { name: 'Untitled assembly', parts: [], joints: [] });
type SceneTarget = SceneDocument | 'empty' | 'sample';

export function ReplaceSceneDialog({
scene,
onClose,
}: {
scene?: SceneDocument;
onClose: () => void;
}) {
const busy = useEditor((state) => state.busy);
const [checking, setChecking] = useState(false);
const [discard, setDiscard] = useState<{ target: SceneTarget; epoch: number } | null>(null);
const cancelButton = useRef<HTMLButtonElement>(null);
const dismissed = useRef(false);

useEffect(() => {
dismissed.current = false;
return () => {
dismissed.current = true;
};
}, []);
useEffect(() => {
if (discard) cancelButton.current?.focus();
}, [discard]);

const replace = (target: SceneTarget) => {
const state = useEditor.getState();
const next =
target === 'empty'
? { name: 'Untitled assembly', parts: [], joints: [] }
: target === 'sample'
? createSample()
: target;
state.setScene(next);
if (scene) state.notify('Scene opened');
onClose();
};
const requestReplace = async (target: SceneTarget) => {
const epoch = useEditor.getState().sceneEpoch;
setChecking(true);
try {
const saved = await driveAutosave.flush();
if (dismissed.current || useEditor.getState().sceneEpoch !== epoch) return;
if (saved) replace(target);
else setDiscard({ target, epoch });
} finally {
setChecking(false);
}
};
const close = () => {
dismissed.current = true;
onClose();
};

return (
<Modal
title="Start something new."
subtitle="Save your current assembly before opening a fresh workbench."
onClose={() => useEditor.getState().setModal(null)}
title={scene ? `Open ${scene.name}?` : 'Start something new.'}
subtitle={
checking
? 'Saving the current scene to Drive…'
: discard
? undefined
: scene
? 'This replaces the current workbench. Save a scene copy if you want to keep it.'
: 'Save a copy if you want to keep your current assembly.'
}
onClose={close}
>
<div className="new-options">
<button onClick={() => create(false)}>
<Plus size={25} />
<strong>Empty assembly</strong>
<span>A clean space for your own parts.</span>
<ArrowRight size={17} />
</button>
<button onClick={() => create(true)}>
<Box size={25} />
<strong>Scout · MK.04</strong>
<span>Explore the editable sample assembly.</span>
<ArrowRight size={17} />
</button>
</div>
{discard ? (
<p role="alert" className="drive-open-confirmation">
Changes to the current scene that haven’t been saved to Drive will be discarded.
</p>
) : scene ? (
<div className="open-scene-summary">
<FolderOpen size={29} />
<div>
<strong>
{scene.parts.length} parts · {scene.joints.length} joints
</strong>
<p>All geometry, placement, and joint constraints will be restored.</p>
</div>
</div>
) : (
<div className="new-options">
<button disabled={checking} onClick={() => void requestReplace('empty')}>
<Plus size={25} />
<strong>Empty assembly</strong>
<span>A clean space for your own parts.</span>
<ArrowRight size={17} />
</button>
<button disabled={checking} onClick={() => void requestReplace('sample')}>
<Box size={25} />
<strong>Scout · MK.04</strong>
<span>Explore the editable sample assembly.</span>
<ArrowRight size={17} />
</button>
</div>
)}
<div className="modal-footer">
<button className="button secondary" onClick={() => useEditor.getState().setModal(null)}>
Cancel
</button>
<button className="button secondary" onClick={saveCurrentScene}>
<ArrowDownToLine size={14} />
Save current scene
{discard || !scene ? (
<button ref={cancelButton} className="button secondary" onClick={close}>
Cancel
</button>
) : null}
<button
className="button secondary"
disabled={checking || !!busy}
onClick={discard ? downloadCurrentScene : saveCurrentScene}
>
{!scene || discard ? <ArrowDownToLine size={14} /> : null}
{discard ? 'Download scene copy' : 'Save current scene'}
</button>
{discard ? (
<button
className="button primary"
disabled={!!busy}
onClick={() => {
if (useEditor.getState().sceneEpoch === discard.epoch) replace(discard.target);
else onClose();
}}
>
{scene ? 'Discard and open scene' : 'Discard and start new'}
</button>
) : scene ? (
<button
className="button primary"
disabled={checking}
onClick={() => void requestReplace(scene)}
>
Open scene
<ArrowRight size={14} />
</button>
) : null}
</div>
</Modal>
);
Expand All @@ -810,7 +909,8 @@ export function Dialogs({ importFiles }: { importFiles: File[] }) {
if (modal === 'merge') return <MergeDialog />;
if (modal === 'mesh') return <MeshProcessingDialog />;
if (modal === 'help') return <HelpDialog />;
if (modal === 'new') return <NewDialog />;
if (modal === 'new')
return <ReplaceSceneDialog onClose={() => useEditor.getState().setModal(null)} />;
if (modal === 'rename') return <RenameDialog />;
return null;
}
39 changes: 31 additions & 8 deletions src/components/GoogleDrive.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import {
Cloud,
CloudCheck,
Expand Down Expand Up @@ -75,6 +75,8 @@ function DriveDialog() {
const [files, setFiles] = useState<DriveFile[]>([]);
const [nextPage, setNextPage] = useState<string | undefined>();
const [selected, setSelected] = useState<DriveFile | null>(null);
const [discardEpoch, setDiscardEpoch] = useState<number | null>(null);
const cancelOpen = useRef<HTMLButtonElement>(null);
const [refresh, setRefresh] = useState(0);
const [resolution, setResolution] = useState<'load' | 'overwrite' | null>(null);
const close = () => useDrive.setState({ dialogOpen: false });
Expand All @@ -98,6 +100,9 @@ function DriveDialog() {
setWorking(false);
}
};
useEffect(() => {
if (discardEpoch !== null) cancelOpen.current?.focus();
}, [discardEpoch]);
useEffect(() => {
if (!googleClientId) return;
let active = true;
Expand Down Expand Up @@ -332,15 +337,26 @@ function DriveDialog() {
{selected ? (
<div className="drive-open-confirmation">
<strong>Open {selected.name}?</strong>
<p>
This replaces the current workbench. Save or download any changes you want to
keep first.
</p>
{discardEpoch !== null ? (
<p role="alert">
Changes to the current scene that haven’t been saved to Drive will be
discarded.
</p>
) : (
<p>
This replaces the current workbench. Save or download any changes you want
to keep first.
</p>
)}
<div className="drive-inline-actions">
<button
className="button secondary"
ref={cancelOpen}
disabled={working}
onClick={() => setSelected(null)}
onClick={() => {
setSelected(null);
setDiscardEpoch(null);
}}
>
Cancel
</button>
Expand All @@ -349,12 +365,19 @@ function DriveDialog() {
disabled={working}
onClick={() =>
void perform(async () => {
if (await openDriveScene(selected.id)) close();
const epoch = useEditor.getState().sceneEpoch;
if (discardEpoch !== null && discardEpoch !== epoch) {
setDiscardEpoch(null);
return;
}
if (await openDriveScene(selected.id, discardEpoch !== null)) close();
else if (useEditor.getState().sceneEpoch === epoch)
setDiscardEpoch(epoch);
})
}
>
<FolderOpen size={14} />
Open scene
{discardEpoch !== null ? 'Discard and open scene' : 'Open scene'}
</button>
</div>
</div>
Expand Down
Loading
Loading