Skip to content

Commit

Permalink
Move patch function for updating gist files
Browse files Browse the repository at this point in the history
  • Loading branch information
magland committed Jul 29, 2024
1 parent 5ce1088 commit e045ccd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
21 changes: 20 additions & 1 deletion gui/src/app/gists/saveAsGitHubGist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export const updateGitHubGist = async (
files[path] = { content };
}
}
console.log("--------------- files", files);
await octokit.request(`PATCH /gists/${gistId}`, {
gist_id: gistId,
files,
Expand All @@ -77,4 +76,24 @@ export const updateGitHubGist = async (
});
};

export const createPatchForUpdatingGist = (
existingFiles: { [key: string]: string },
newFiles: { [key: string]: string },
) => {
const patch: { [key: string]: string | null } = {};
for (const fname in newFiles) {
const newContent = newFiles[fname];
if (existingFiles[fname] === newContent) continue;
if (!newContent.trim()) continue;
patch[fname] = newContent;
}
// handle deleted files
for (const fname in existingFiles) {
if (!newFiles[fname] || !newFiles[fname].trim()) {
patch[fname] = null;
}
}
return patch;
};

export default saveAsGitHubGist;
21 changes: 1 addition & 20 deletions gui/src/app/pages/HomePage/SaveProjectWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { serializeAsZip } from "@SpCore/ProjectSerialization";
import { FileRegistry, mapModelToFileManifest } from "@SpCore/FileMapping";
import { ProjectContext } from "@SpCore/ProjectContextProvider";
import saveAsGitHubGist, {
createPatchForUpdatingGist,
updateGitHubGist,
} from "@SpCore/gists/saveAsGitHubGist";
import { triggerDownload } from "@SpUtil/triggerDownload";
Expand Down Expand Up @@ -309,26 +310,6 @@ const SpecifyGistUrlToUpdateComponent: FunctionComponent<
);
};

const createPatchForUpdatingGist = (
existingFiles: { [key: string]: string },
newFiles: { [key: string]: string },
) => {
const patch: { [key: string]: string | null } = {};
for (const fname in newFiles) {
const newContent = newFiles[fname];
if (existingFiles[fname] === newContent) continue;
if (!newContent.trim()) continue;
patch[fname] = newContent;
}
// handle deleted files
for (const fname in existingFiles) {
if (!newFiles[fname] || !newFiles[fname].trim()) {
patch[fname] = null;
}
}
return patch;
};

const makeSPShareableLinkFromGistUrl = (gistUrl: string) => {
const protocol = window.location.protocol;
const host = window.location.host;
Expand Down

0 comments on commit e045ccd

Please sign in to comment.