Skip to content

Commit 1b4a22a

Browse files
committed
fix #7720 -- Document Name Overwritten when Generating Jupyter Notebook using "Help me write"
- this was surprisingly painful to write!
1 parent 546919e commit 1b4a22a

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

src/packages/frontend/project/new/file-type-selector.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ interface Props {
4040
selectedExt?: string;
4141
filename: string;
4242
makeNewFilename?: (ext: string) => void;
43+
filenameChanged?: boolean;
4344
}
4445

4546
// Use Rows and Cols to append more buttons to this class.
@@ -55,6 +56,7 @@ export function FileTypeSelector({
5556
children,
5657
filename,
5758
makeNewFilename,
59+
filenameChanged,
5860
}: Props) {
5961
const { project_id } = useProjectContext();
6062

@@ -103,6 +105,7 @@ export function FileTypeSelector({
103105
btnActive={btnActive}
104106
grid={[sm, md]}
105107
filename={filename}
108+
filenameChanged={filenameChanged}
106109
makeNewFilename={() => makeNewFilename?.("ipynb")}
107110
/>
108111
{renderLaTeX()}
@@ -362,6 +365,7 @@ export function FileTypeSelector({
362365
project_id={project_id}
363366
mode="flyout"
364367
ext={ext}
368+
filename={filenameChanged ? filename : undefined}
365369
/>
366370
</Flex>
367371
</Flex>
@@ -375,6 +379,7 @@ export function FileTypeSelector({
375379
project_id={project_id}
376380
mode="full"
377381
ext={ext}
382+
filename={filenameChanged ? filename : undefined}
378383
/>
379384
</Col>
380385
);

src/packages/frontend/project/new/jupyter-buttons.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ interface JupyterNotebookButtonsProps {
5252
btnActive: (name: string) => boolean;
5353
grid: [number, number];
5454
filename: string;
55+
filenameChanged?: boolean;
5556
mode: "full" | "flyout";
5657
makeNewFilename?: () => void;
5758
}
@@ -71,6 +72,7 @@ export function JupyterNotebookButtons({
7172
btnActive,
7273
grid,
7374
filename,
75+
filenameChanged,
7476
mode,
7577
makeNewFilename,
7678
}: JupyterNotebookButtonsProps) {
@@ -244,6 +246,7 @@ export function JupyterNotebookButtons({
244246
project_id={project_id}
245247
mode="flyout"
246248
ext="ipynb"
249+
filename={filename}
247250
/>
248251
</Col>
249252
</>
@@ -286,6 +289,7 @@ export function JupyterNotebookButtons({
286289
project_id={project_id}
287290
mode="flyout"
288291
ext="ipynb"
292+
filename={filenameChanged ? filename : undefined}
289293
/>
290294
</Flex>
291295
</Flex>
@@ -299,6 +303,7 @@ export function JupyterNotebookButtons({
299303
project_id={project_id}
300304
mode="full"
301305
ext="ipynb"
306+
filename={filenameChanged ? filename : undefined}
302307
/>
303308
</Col>
304309
);

src/packages/frontend/project/new/new-file-page.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export default function NewFilePage(props: Props) {
6969
const [filename, setFilename] = useState<string>(
7070
filename0 ? filename0 : default_filename(undefined, project_id),
7171
);
72+
const [filenameChanged, setFilenameChanged] = useState<boolean>(false);
7273
const file_creation_error = useTypedRedux(
7374
{ project_id },
7475
"file_creation_error",
@@ -391,6 +392,7 @@ export default function NewFilePage(props: Props) {
391392
"Name your file, folder, or a URL to download from..."
392393
}
393394
onChange={(e) => {
395+
setFilenameChanged(true);
394396
if (extensionWarning) {
395397
setExtensionWarning(false);
396398
} else {
@@ -429,6 +431,7 @@ export default function NewFilePage(props: Props) {
429431
projectActions={actions}
430432
availableFeatures={availableFeatures}
431433
filename={filename}
434+
filenameChanged={filenameChanged}
432435
>
433436
<Tip
434437
title={"Download files from the Internet"}

src/packages/frontend/project/page/flyouts/new.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ export function NewFlyout({
406406
create_file={handleOnClick}
407407
availableFeatures={availableFeatures}
408408
filename={filename}
409+
filenameChanged={manual}
409410
makeNewFilename={(ext: string) => setFilename(getNewFilename(ext))}
410411
/>
411412
<Tag color={COLORS.ANTD_ORANGE}>Additional types</Tag>

src/packages/frontend/project/page/home-page/ai-generate-document.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,21 @@ type Ipynb = {
118118
metadata: { kernelspec: KernelSpec };
119119
};
120120

121+
function ensureExtension(filename, ext) {
122+
if (!filename) return filename;
123+
if (!filename.endsWith("." + ext)) {
124+
return filename + "." + ext;
125+
}
126+
return filename;
127+
}
128+
121129
interface Props {
122130
project_id: string;
123131
onSuccess: () => void;
124132
ext: Ext;
125133
docName: string;
126134
show: boolean;
135+
filename?: string;
127136
}
128137

129138
function AIGenerateDocument({
@@ -132,6 +141,7 @@ function AIGenerateDocument({
132141
project_id,
133142
ext,
134143
docName,
144+
filename: filename0,
135145
}: Props) {
136146
const projectActions = useActions({ project_id });
137147
const current_path = useTypedRedux({ project_id }, "current_path");
@@ -145,7 +155,12 @@ function AIGenerateDocument({
145155
const [saving, setSaving] = useState<boolean>(false);
146156
const [error, setError] = useState<string>("");
147157
const [preview, setPreview] = useState<string | null>(null);
148-
const [filename, setFilename] = useState<string>("");
158+
const [filename, setFilename] = useState<string>(
159+
ensureExtension(filename0 ?? "", ext),
160+
);
161+
useEffect(() => {
162+
setFilename(ensureExtension(filename0 ?? "", ext));
163+
}, [filename0]);
149164
const promptRef = useRef<HTMLElement>(null);
150165

151166
const [kernelSpecs, setKernelSpecs] = useState<KernelSpec[] | null | string>(
@@ -452,6 +467,9 @@ function AIGenerateDocument({
452467
}
453468

454469
function updateFilename(fnNext: string) {
470+
if (filename) {
471+
return;
472+
}
455473
const fn = sanitizeFilename(fnNext, ext);
456474
const timestamp = getTimestamp();
457475
setFilename(`${fn}-${timestamp}.${ext}`);
@@ -957,12 +975,14 @@ export function AIGenerateDocumentModal({
957975
setShow,
958976
project_id,
959977
ext,
978+
filename,
960979
}: {
961980
show: boolean;
962981
setShow: (val: boolean) => void;
963982
project_id: string;
964983
style?: CSS;
965984
ext: Props["ext"];
985+
filename?: string;
966986
}) {
967987
const docName = file_options(`x.${ext}`).name ?? `${capitalize(ext)}`;
968988

@@ -984,6 +1004,7 @@ export function AIGenerateDocumentModal({
9841004
onSuccess={() => setShow(false)}
9851005
ext={ext}
9861006
docName={docName}
1007+
filename={filename}
9871008
/>
9881009
</Modal>
9891010
);
@@ -994,11 +1015,13 @@ export function AIGenerateDocumentButton({
9941015
style,
9951016
mode = "full",
9961017
ext,
1018+
filename,
9971019
}: {
9981020
project_id: string;
9991021
style?: CSS;
10001022
mode?: "full" | "flyout";
10011023
ext: Props["ext"];
1024+
filename?: string;
10021025
}) {
10031026
const [show, setShow] = useState<boolean>(false);
10041027

@@ -1044,6 +1067,7 @@ export function AIGenerateDocumentButton({
10441067
show={show}
10451068
setShow={setShow}
10461069
project_id={project_id}
1070+
filename={filename}
10471071
/>
10481072
</>
10491073
);

0 commit comments

Comments
 (0)