Skip to content

Commit

Permalink
#541 - silent account retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Mar 19, 2023
1 parent a274a34 commit 5f1c835
Showing 1 changed file with 64 additions and 62 deletions.
126 changes: 64 additions & 62 deletions src/helpers/Questions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QuickPickItem, QuickPickItemKind, window } from 'vscode';
import { authentication, QuickPickItem, QuickPickItemKind, window } from 'vscode';
import { Backers } from '../commands';
import { Folders } from '../commands/Folders';
import { SETTING_SEO_TITLE_LENGTH, SETTING_SPONSORS_AI_TITLE } from '../constants';
Expand Down Expand Up @@ -30,75 +30,77 @@ export class Questions {
*/
public static async ContentTitle(showWarning: boolean = true): Promise<string | undefined> {
const aiContentTitle = Settings.get<boolean>(SETTING_SPONSORS_AI_TITLE);
const username = await Backers.getUsername();

let title: string | undefined = '';

if (aiContentTitle && username) {
title = await window.showInputBox({
title: 'Title or description',
prompt: `What would you like to write about?`,
placeHolder: `Content title or description`,
ignoreFocusOut: true
});
if (aiContentTitle) {
const githubAuth = await authentication.getSession('github', ['read:user'], { silent: true });

if (title) {
try {
const response = await fetch(`https://frontmatter.codes/api/ai-title`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
accept: 'application/json'
},
body: JSON.stringify({
title: title,
username: username,
nrOfCharacters: Settings.get<number>(SETTING_SEO_TITLE_LENGTH) || 60
})
});
const data: string[] = await response.json();

if (data && data.length > 0) {
const options: QuickPickItem[] = [
{
label: `✏️ your title/description`,
kind: QuickPickItemKind.Separator
},
{
label: title
},
{
label: `🤖 AI generated title`,
kind: QuickPickItemKind.Separator
if (githubAuth && githubAuth.account.label) {
title = await window.showInputBox({
title: 'Title or description',
prompt: `What would you like to write about?`,
placeHolder: `Content title or description`,
ignoreFocusOut: true
});

if (title) {
try {
const response = await fetch(`https://frontmatter.codes/api/ai-title`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
accept: 'application/json'
},
...data.map((d: string) => ({
label: d
}))
];

const selectedTitle = await window.showQuickPick(options, {
title: 'Select a title',
placeHolder: `Select a title for your content`,
ignoreFocusOut: true
body: JSON.stringify({
title: title,
username: githubAuth.account.label,
nrOfCharacters: Settings.get<number>(SETTING_SEO_TITLE_LENGTH) || 60
})
});

if (selectedTitle) {
title = selectedTitle.label;
} else if (!selectedTitle) {
// Reset the title, so the user can enter their own title
title = undefined;
const data: string[] = await response.json();

if (data && data.length > 0) {
const options: QuickPickItem[] = [
{
label: `✏️ your title/description`,
kind: QuickPickItemKind.Separator
},
{
label: title
},
{
label: `🤖 AI generated title`,
kind: QuickPickItemKind.Separator
},
...data.map((d: string) => ({
label: d
}))
];

const selectedTitle = await window.showQuickPick(options, {
title: 'Select a title',
placeHolder: `Select a title for your content`,
ignoreFocusOut: true
});

if (selectedTitle) {
title = selectedTitle.label;
} else if (!selectedTitle) {
// Reset the title, so the user can enter their own title
title = undefined;
}
}
} catch (e) {
Logger.error((e as Error).message);
Notifications.error(
`Failed fetching the AI title. Please try to use your own title or try again later.`
);
title = undefined;
}
} catch (e) {
Logger.error((e as Error).message);
Notifications.error(
`Failed fetching the AI title. Please try to use your own title or try again later.`
);
title = undefined;
} else if (!title && showWarning) {
Notifications.warning(`You did not specify a title for your content.`);
return;
}
} else if (!title && showWarning) {
Notifications.warning(`You did not specify a title for your content.`);
return;
}
}

Expand Down

0 comments on commit 5f1c835

Please sign in to comment.