Skip to content

Commit

Permalink
feat: clear cache on login change
Browse files Browse the repository at this point in the history
  • Loading branch information
devpanther committed Mar 6, 2024
1 parent 4d20c22 commit fd3385e
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/home/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { displayGitHubUserInformation } from "./rendering/display-github-user-in
import { renderGitHubLoginButton } from "./rendering/render-github-login-button";

export async function authentication() {
const accessToken = getGitHubAccessToken();
const accessToken = await getGitHubAccessToken();
if (!accessToken) {
renderGitHubLoginButton();
}
Expand Down
16 changes: 16 additions & 0 deletions src/home/fetch-github/fetch-and-display-previews.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getGitHubAccessToken } from "../getters/get-github-access-token";
import { getImageFromCache } from "../getters/get-indexed-db";
import { getLocalStore } from "../getters/get-local-store";
import { GITHUB_TASKS_STORAGE_KEY, TaskStorageItems } from "../github-types";
Expand All @@ -16,11 +17,26 @@ export type Options = {

export async function fetchAndDisplayPreviewsFromCache(sorting?: Sorting, options = { ordering: "normal" }) {
let _cachedTasks = getLocalStore(GITHUB_TASKS_STORAGE_KEY) as TaskStorageItems;
const _accessToken = await getGitHubAccessToken();

// Refresh the storage if there is no logged-in object in cachedTasks but there is one now.
if (_cachedTasks && !_cachedTasks.loggedIn && _accessToken) {
localStorage.removeItem(GITHUB_TASKS_STORAGE_KEY);
return fetchAndDisplayPreviewsFromNetwork(sorting, options);
}

// If previously logged in but not anymore, clear cache and fetch from network.
if (_cachedTasks && _cachedTasks.loggedIn && !_accessToken) {
localStorage.removeItem(GITHUB_TASKS_STORAGE_KEY);
return fetchAndDisplayPreviewsFromNetwork(sorting, options);
}

// makes sure tasks have a timestamp to know how old the cache is, or refresh if older than 15 minutes
if (!_cachedTasks || !_cachedTasks.timestamp || _cachedTasks.timestamp + 60 * 1000 * 15 <= Date.now()) {
_cachedTasks = {
timestamp: Date.now(),
tasks: [],
loggedIn: _accessToken !== null,
};
}
const cachedTasks = _cachedTasks.tasks.map((task) => ({ ...task, isNew: false, isModified: false })) as TaskMaybeFull[];
Expand Down
2 changes: 1 addition & 1 deletion src/home/fetch-github/fetch-avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function fetchAvatar(orgName: string) {
}

// If not in IndexedDB, fetch from network
const octokit = new Octokit({ auth: getGitHubAccessToken() });
const octokit = new Octokit({ auth: await getGitHubAccessToken() });
try {
const {
data: { avatar_url: avatarUrl },
Expand Down
2 changes: 1 addition & 1 deletion src/home/fetch-github/fetch-issues-full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TaskMaybeFull, TaskWithFull } from "./preview-to-full-mapping";
export const organizationImageCache = new Map<string, Blob | null>();

export async function fetchIssuesFull(taskPreviews: TaskMaybeFull[]): Promise<TaskWithFull[]> {
const octokit = new Octokit({ auth: getGitHubAccessToken() });
const octokit = new Octokit({ auth: await getGitHubAccessToken() });
const urlPattern = /https:\/\/github\.com\/(?<org>[^/]+)\/(?<repo>[^/]+)\/issues\/(?<issue_number>\d+)/;

const fullTaskPromises = taskPreviews.map(async (task) => {
Expand Down
4 changes: 2 additions & 2 deletions src/home/fetch-github/fetch-issues-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { displayPopupMessage } from "../rendering/display-popup-modal";
import { TaskNoFull } from "./preview-to-full-mapping";

async function checkPrivateRepoAccess(): Promise<boolean> {
const octokit = new Octokit({ auth: getGitHubAccessToken() });
const octokit = new Octokit({ auth: await getGitHubAccessToken() });
const username = getGitHubUserName();

try {
Expand Down Expand Up @@ -34,7 +34,7 @@ async function checkPrivateRepoAccess(): Promise<boolean> {
}

export async function fetchIssuePreviews(): Promise<TaskNoFull[]> {
const octokit = new Octokit({ auth: getGitHubAccessToken() });
const octokit = new Octokit({ auth: await getGitHubAccessToken() });

let freshIssues: GitHubIssue[] = [];
let hasPrivateRepoAccess = false; // Flag to track access to the private repository
Expand Down
6 changes: 4 additions & 2 deletions src/home/getters/get-github-access-token.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { SUPABASE_STORAGE_KEY } from "../github-types";
import { checkSupabaseSession } from "../rendering/render-github-login-button";
import { getLocalStore } from "./get-local-store";

export function getGitHubAccessToken(): string | null {
const oauthToken = getLocalStore(`sb-${SUPABASE_STORAGE_KEY}-auth-token`) as OAuthToken | null;
export async function getGitHubAccessToken(): Promise<string | null> {
// better to use official function, looking up localstorage has flaws
const oauthToken = await checkSupabaseSession();

const expiresAt = oauthToken?.expires_at;
if (expiresAt) {
Expand Down
1 change: 1 addition & 0 deletions src/home/github-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ export const SUPABASE_STORAGE_KEY = supabaseUrl.substring(supabaseUrl.lastIndexO
export type TaskStorageItems = {
timestamp: number;
tasks: TaskNoState[];
loggedIn: boolean;
};
2 changes: 1 addition & 1 deletion src/home/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void (async function home() {
const fullTasks = await fetchIssuesFull(previews);
taskManager.syncTasks(fullTasks);
console.trace({ fullTasks });
taskManager.writeToStorage();
await taskManager.writeToStorage();
return fullTasks;
} catch (error) {
console.error(error);
Expand Down
8 changes: 8 additions & 0 deletions src/home/rendering/render-github-login-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ export function getSupabase() {
return supabase;
}

export async function checkSupabaseSession() {
const {
data: { session },
} = await supabase.auth.getSession();

return session;
}

async function gitHubLoginButtonHandler() {
const { error } = await supabase.auth.signInWithOAuth({ provider: "github" });
if (error) {
Expand Down
6 changes: 4 additions & 2 deletions src/home/task-manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TaskMaybeFull } from "./fetch-github/preview-to-full-mapping";
import { getGitHubAccessToken } from "./getters/get-github-access-token";
import { setLocalStore } from "./getters/get-local-store";
import { GITHUB_TASKS_STORAGE_KEY } from "./github-types";

Expand Down Expand Up @@ -50,7 +51,8 @@ export class TaskManager {
return this._container;
}

public writeToStorage() {
setLocalStore(GITHUB_TASKS_STORAGE_KEY, { timestamp: Date.now(), tasks: this._tasks });
public async writeToStorage() {
const _accessToken = await getGitHubAccessToken();
setLocalStore(GITHUB_TASKS_STORAGE_KEY, { timestamp: Date.now(), tasks: this._tasks, loggedIn: _accessToken !== null });
}
}

0 comments on commit fd3385e

Please sign in to comment.