Skip to content

Commit

Permalink
Avoid cyclic dependency in token code (refined-github#7738)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Aug 24, 2024
1 parent 5e15dd5 commit d405b15
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
2 changes: 1 addition & 1 deletion source/features/quick-review.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
getConversationNumber, getUsername, scrollIntoViewIfNeeded, triggerConversationUpdate,
} from '../github-helpers/index.js';
import {randomArrayItem} from '../helpers/math.js';
import {getToken} from '../github-helpers/github-token.js';
import {getToken} from '../options-storage.js';

const emojis = [...'🚀🐿️⚡️🤌🥳🥰🤩🥸😎🤯🚢🛫🏳️🏁'];

Expand Down
37 changes: 35 additions & 2 deletions source/features/rgh-improve-new-issue-form.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import React from 'dom-chef';
import {$} from 'select-dom';
import delegate, {DelegateEvent} from 'delegate-it';
import * as pageDetect from 'github-url-detection';

import features from '../feature-manager.js';
import openOptions from '../helpers/open-options.js';
import clearCacheHandler from '../helpers/clear-cache-handler.js';
import {expectTokenScope, getToken} from '../github-helpers/github-token.js';
import {expectTokenScope} from '../github-helpers/github-token.js';
import {getToken} from '../options-storage.js';
import {isRefinedGitHubRepo} from '../github-helpers/index.js';

const isSetTheTokenSelector = 'input[name^="issue_form[token]"]';
const liesGif = 'https://github.com/user-attachments/assets/f417264f-f230-4156-b020-16e4390562bd';

function addNotice(adjective: JSX.Element | string): void {
$('#issue_body_template_name')!.before(
<div className="flash flash-error h3 my-9" style={{animation: 'pulse-in 0.3s 2'}}>
Expand All @@ -34,7 +39,7 @@ async function checkToken(): Promise<void> {
}

// Thank you for following the instructions. I'll save you a click.
$('input[name^="issue_form[token]')!.checked = true;
$(isSetTheTokenSelector)!.checked = true;
}

async function setVersion(): Promise<void> {
Expand All @@ -60,6 +65,33 @@ async function linkifyCacheRefresh(): Promise<void> {
);
}

function Lies(): JSX.Element {
return (
<a href="https://www.youtube.com/watch?v=YWdD206eSv0">
<img src={liesGif} alt="Just go on the internet and tell lies?" className="d-inline-block"/>
</a>
);
}

async function lieDetector({delegateTarget}: DelegateEvent<MouseEvent, HTMLInputElement>): Promise<void> {
if (delegateTarget.checked) {
delegateTarget.closest('fieldset')!.append(<Lies/>);
}
}

async function validateTokenCheckbox(): Promise<void> {
if (await getToken()) {
return;
}

// eslint-disable-next-line new-cap -- Preload image
Lies();

delegate(isSetTheTokenSelector, 'click', lieDetector, {
once: true,
});
}

void features.add(import.meta.url, {
asLongAs: [
isRefinedGitHubRepo,
Expand All @@ -71,6 +103,7 @@ void features.add(import.meta.url, {
init: [
linkifyCacheRefresh,
checkToken,
validateTokenCheckbox,
setVersion,
],
});
Expand Down
2 changes: 1 addition & 1 deletion source/github-helpers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {JsonObject, AsyncReturnType} from 'type-fest';

import features from '../feature-manager.js';
import {getRepo} from './index.js';
import {getToken} from './github-token.js';
import {getToken} from '../options-storage.js';

type JsonError = {
message: string;
Expand Down
9 changes: 1 addition & 8 deletions source/github-helpers/github-token.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import {v3} from './api.js';
import optionsStorage from '../options-storage.js';

const settings = optionsStorage.getAll();

export async function getToken(): Promise<string | undefined> {
const {personalToken} = await settings;
return personalToken;
}
import {getToken} from '../options-storage.js';

export async function expectToken(): Promise<string> {
const personalToken = await getToken();
Expand Down
10 changes: 9 additions & 1 deletion source/options-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ const migrations = [
];

export const perDomainOptions = new OptionsSyncPerDomain({defaults, migrations});
export default perDomainOptions.getOptionsForOrigin();
const optionsStorage = perDomainOptions.getOptionsForOrigin();
export default optionsStorage;

const cachedSettings = optionsStorage.getAll();

export async function getToken(): Promise<string | undefined> {
const {personalToken} = await cachedSettings;
return personalToken;
}

0 comments on commit d405b15

Please sign in to comment.