Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save project as gist #114

Merged
merged 3 commits into from
Jul 3, 2024
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
2 changes: 1 addition & 1 deletion gui/src/app/Project/ProjectQueryLoading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
persistStateToEphemera,
} from "./ProjectDataModel";
import { loadFromProjectFiles } from "./ProjectSerialization";
import loadFilesFromGist from "./loadFilesFromGist";
import loadFilesFromGist from "../gists/loadFilesFromGist";

enum QueryParamKeys {
Project = "project",
Expand Down
79 changes: 79 additions & 0 deletions gui/src/app/gists/saveAsGitHubGist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Octokit } from "@octokit/core";

type SaveAsGitHubGistOpts = {
defaultDescription: string;
personalAccessToken: string;
};

const saveAsGitHubGist = async (
files: { [key: string]: string },
o: SaveAsGitHubGistOpts,
) => {
const { defaultDescription, personalAccessToken } = o;
const description = prompt(
"SAVING AS PUBLIC GIST: Enter a description:",
defaultDescription,
);
if (!description) {
throw Error("No description provided");
}
const octokit = new Octokit({
auth: personalAccessToken,
});
const filesForGistExport: { [key: string]: { content: string } } = {};
for (const key in files) {
// gists do not support empty files or whitespace-only files
if (files[key].trim() === "") {
console.warn(`File ${key} is empty or whitespace-only. Not saving.`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll have to think in the future about whether we need to warn this, since in a data.py/r analysis.py/r world we will expect the presence of empty files. (Fine for now.)

} else {
filesForGistExport[key] = { content: files[key] };
}
}
const r = await octokit.request("POST /gists", {
description,
public: true,
files: filesForGistExport,
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
});
// const gistId = r.data.id;
const gistUrl = r.data.html_url;
if (!gistUrl) {
throw new Error("Problem creating gist. r.data.html_url is null.");
}
return gistUrl;
};

export const updateGitHubGist = async (
gistUri: string,
patch: { [path: string]: string | null },
o: { personalAccessToken: string },
) => {
const octokit = new Octokit({
auth: o.personalAccessToken,
});
const gistId = gistUri.split("/").pop();
if (!gistId) {
throw new Error("Invalid gist URI");
}
// patch
const files: { [key: string]: { content?: string } } = {};
for (const path in patch) {
const content = patch[path];
if (content === null || content.trim() === "") {
files[path] = {};
} else {
files[path] = { content };
}
}
await octokit.request(`PATCH /gists/${gistId}`, {
gist_id: gistId,
files,
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
});
};

export default saveAsGitHubGist;
73 changes: 0 additions & 73 deletions gui/src/app/pages/HomePage/ExportWindow.tsx

This file was deleted.

44 changes: 22 additions & 22 deletions gui/src/app/pages/HomePage/LeftPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import ModalWindow, { useModalWindow } from "@fi-sci/modal-window";
import { FunctionComponent, useCallback, useContext } from "react";
import examplesStanies, { Stanie } from "../../exampleStanies/exampleStanies";
import { ProjectContext } from "../../Project/ProjectContextProvider";
import ExportWindow from "./ExportWindow";
import ImportWindow from "./ImportWindow";
import SaveProjectWindow from "./SaveProjectWindow";
import LoadProjectWindow from "./LoadProjectWindow";
import { ChevronLeft, ChevronRight } from "@mui/icons-material";

type LeftPanelProps = {
Expand Down Expand Up @@ -33,14 +33,14 @@ const LeftPanel: FunctionComponent<LeftPanelProps> = ({
);

const {
visible: exportVisible,
handleOpen: exportOpen,
handleClose: exportClose,
visible: saveProjectVisible,
handleOpen: saveProjectOpen,
handleClose: saveProjectClose,
} = useModalWindow();
const {
visible: importVisible,
handleOpen: importOpen,
handleClose: importClose,
visible: loadProjectVisible,
handleOpen: loadProjectOpen,
handleClose: loadProjectClose,
} = useModalWindow();

if (collapsed) {
Expand Down Expand Up @@ -82,6 +82,16 @@ const LeftPanel: FunctionComponent<LeftPanelProps> = ({
</div>
))}
<hr />
<div>
<button onClick={loadProjectOpen} disabled={hasUnsavedChanges}>
Load project
</button>
&nbsp;
<button onClick={saveProjectOpen} disabled={hasUnsavedChanges}>
Save project
</button>
</div>
<div>&nbsp;</div>
<div>
{/* This will probably be removed or replaced in the future. It's just for convenience during development. */}
<button
Expand All @@ -96,22 +106,12 @@ const LeftPanel: FunctionComponent<LeftPanelProps> = ({
Clear all
</button>
</div>
<div>&nbsp;</div>
<div>
<button onClick={importOpen} disabled={hasUnsavedChanges}>
Import
</button>
&nbsp;
<button onClick={exportOpen} disabled={hasUnsavedChanges}>
Export
</button>
</div>
</div>
<ModalWindow visible={importVisible} onClose={importClose}>
<ImportWindow onClose={importClose} />
<ModalWindow visible={loadProjectVisible} onClose={loadProjectClose}>
<LoadProjectWindow onClose={loadProjectClose} />
</ModalWindow>
<ModalWindow visible={exportVisible} onClose={exportClose}>
<ExportWindow onClose={exportClose} />
<ModalWindow visible={saveProjectVisible} onClose={saveProjectClose}>
<SaveProjectWindow onClose={saveProjectClose} />
</ModalWindow>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ import {
} from "../../Project/ProjectSerialization";
import UploadFilesArea from "./UploadFilesArea";

type ImportWindowProps = {
type LoadProjectWindowProps = {
onClose: () => void;
};

const ImportWindow: FunctionComponent<ImportWindowProps> = ({ onClose }) => {
const LoadProjectWindow: FunctionComponent<LoadProjectWindowProps> = ({
onClose,
}) => {
const { update } = useContext(ProjectContext);
const [errorText, setErrorText] = useState<string | null>(null);
const [filesUploaded, setFilesUploaded] = useState<
Expand Down Expand Up @@ -105,7 +107,7 @@ const ImportWindow: FunctionComponent<ImportWindowProps> = ({ onClose }) => {

return (
<div>
<h3>Import project</h3>
<h3>Load project</h3>
<div>
You can upload:
<ul>
Expand All @@ -132,18 +134,18 @@ const ImportWindow: FunctionComponent<ImportWindowProps> = ({ onClose }) => {
{showReplaceProjectOptions && (
<div>
<button onClick={() => importUploadedFiles({ replaceProject: true })}>
Import into a NEW project
Load into a NEW project
</button>
&nbsp;
<button
onClick={() => importUploadedFiles({ replaceProject: false })}
>
Import into EXISTING project
Load into EXISTING project
</button>
</div>
)}
</div>
);
};

export default ImportWindow;
export default LoadProjectWindow;
Loading