Skip to content

Commit

Permalink
add better handlign for when the user cant edit gist plugins or acces…
Browse files Browse the repository at this point in the history
…s them
  • Loading branch information
blurymind committed Aug 12, 2024
1 parent e8557da commit 67cf84d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
21 changes: 19 additions & 2 deletions src/js/classes/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,13 @@ export const StorageJs = (type = 'gist') => {
this.lastStorageHost = newHost;
},
token: undefined,
isTokenInvalid: true,
gistId: undefined,
setCredentials: function(token, gistId){
//console.log("using gist credentials", {token, gistId})
this.token = token
this.gistId = gistId
this.isTokenInvalid = !token || token.length < 5
},
filesInGist: {},
getFilesInGist: function(fileKey) {
Expand All @@ -215,6 +217,9 @@ export const StorageJs = (type = 'gist') => {
console.error(`${fileKey} not found in gist`, this.filesInGist);
},
getGist: function(gistId, onFail = () => {}) {
if (!gistId) {
throw new Error('No gist id specified');
}
const fetchAddress = `https://api.github.com/gists/${gistId}`;
return fetch(fetchAddress, {
method: 'GET',
Expand All @@ -231,15 +236,24 @@ export const StorageJs = (type = 'gist') => {
401: "Is your Gist Token valid?",
404: `Is your Gist ID online?\n\naddress:\n${fetchAddress}`
}
alert(`Failed to get:\n${fetchAddress}...\n\nSTATUS: ${data.status}\n${data.status in GistStatusHints ? GistStatusHints[data.status] : ""}`)
console.error('GOT -- ', { data, fetchAddress, data })
onFail(`Failed to get:\n${fetchAddress}...\n\nSTATUS: ${data.status}\n${data.status in GistStatusHints ? GistStatusHints[data.status] : ""}`)
if (data.status in GistStatusHints) onFail(data.status);
if (data.status === 404) window.open(fetchAddress, '_blank').focus();

// try to fetch without authorisation
return fetch(fetchAddress).then(data=> {
console.warn("Got data without authorisation")
this.isTokenInvalid = true;
return data.json()
})
}
this.isTokenInvalid = false;
return data.json();
})
.then(content => {
console.log('NEW from get::', { content });
this.filesInGist = content.files;
this.filesInGist = content.files || {};
const inputOptions = {};
Object.keys(this.filesInGist).forEach(key => {
inputOptions[key] = key;
Expand All @@ -256,6 +270,9 @@ export const StorageJs = (type = 'gist') => {
hasGistSettings: function() {
return this.gistId && this.gistId.length > 0;
},
getIsTokenInvalid: function() {
return !this.token || this.token.length < 5 || this.isTokenInvalid
},
getGistFile: function(onFail = () => {}) {
return this.getGist(this.gistId, onFail);
},
Expand Down
10 changes: 7 additions & 3 deletions src/public/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,12 @@ export var Plugins = function (app) {
});
};

const isGistTokenInvalid = () => {
return app.data.storage.getIsTokenInvalid();
}
const getGistPluginFiles = () => {
return new Promise((resolve, reject) => {
if (!app.settings.gistPluginsFile()) reject();
return new Promise((resolve) => {
// if (!app.settings.gistPluginsFile()) reject("No");
app.data.storage
.getGist(app.settings.gistPluginsFile())
.then(({ filesInGist }) => {
Expand Down Expand Up @@ -291,7 +294,8 @@ export var Plugins = function (app) {
setVloatilePlugin,
setVloatilePlugins,
getGistPluginFiles,
saveGistPlugin
saveGistPlugin,
isGistTokenInvalid
};

// built in plugin initiation
Expand Down
30 changes: 21 additions & 9 deletions src/public/plugins/plugin-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export var PluginEditor = function ({
setVloatilePlugin,
setVloatilePlugins,
getGistPluginFiles,
saveGistPlugin
saveGistPlugin,
isGistTokenInvalid
}) {
const self = this;
this.name = 'PluginEditor';
Expand Down Expand Up @@ -123,7 +124,7 @@ export var PluginEditor = function ({
document.getElementById('js-editor').style.display =
mode === 'edit' ? 'block' : 'none';
document.getElementById('diff-editor').style.display =
mode === 'commit' && app.settings.gistPluginsFile() ? 'block' : 'none';
mode === 'commit' ? 'block' : 'none';
document.getElementById('plugin-differ-commit').style.display =
mode === 'commit' ? 'block' : 'none';
document.getElementById('plugin-output-previewer').style.display =
Expand Down Expand Up @@ -151,22 +152,33 @@ export var PluginEditor = function ({
beautify.beautify(this.editor.session);

if (this.mode === 'commit') {
fileContents = this.editor.getValue();
this.differ
.getEditors()
.left.getSession()
.setValue(fileContents);
getGistPluginFiles().then(gistPluginFiles => {
const gistPluginFile = gistPluginFiles.find(
item => item.filename == fileName
);
console.log({ gistPluginFile }, this.differ.getEditors());
fileContents = this.editor.getValue();

const isTokenInvalid = isGistTokenInvalid()
const gistAccesError = isTokenInvalid? `//Access to gist writing failed\n\n//Do you have a valid token.\n// It needs to have permission to edit the gist file.`: `//${fileName}\n\n//Gist with this filename is missing.\n// Have you deleted/renamed it?`
this.differ
.getEditors()
.left.getSession()
.setValue(fileContents);

.right.getSession()
.setValue(gistPluginFile && !isTokenInvalid ? gistPluginFile.content : gistAccesError);

this.differ.getEditors().right.setReadOnly(isTokenInvalid);
document.getElementById('plugin-differ-commit').className = isTokenInvalid ? "disabled" : ""
})
.catch(error=>{
this.differ.getEditors().right.setReadOnly(true);
document.getElementById('plugin-differ-commit').className = "disabled";
this.differ
.getEditors()
.right.getSession()
.setValue(gistPluginFile ? gistPluginFile.content : `//${fileName}\n\n//Gist with this filename is missing.\n// Have you deleted/renamed it?`);
// this.differ.getEditors().right.setReadOnly(this.mode === 'commit');
.setValue(error.toString())
});
}
if (this.mode === 'test') {
Expand Down

0 comments on commit 67cf84d

Please sign in to comment.