Skip to content

Commit

Permalink
Refresh subreddit on different username, fix #72
Browse files Browse the repository at this point in the history
  • Loading branch information
OctoNezd committed Dec 30, 2023
1 parent 88c8fcc commit b83b62e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/features/sidebar/getSubreddits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ function isSubsListData(data: unknown): data is {
);
}

export async function getSubreddits() {
export async function getSubreddits(force?: boolean) {
let subs: subredditDataWrapped[] = [];
const age = parseInt(await localforage.getItem("subredditcache_age") || "NaN");
const now = Math.floor(Date.now() / 1000);
const cached = JSON.parse(await localforage.getItem("subredditcache_act") || "null");
if (age + 60 * 60 < now || isNaN(age) || cached === null) {
if (age + 60 * 60 < now || isNaN(age) || cached === null || force) {
console.log("Updating subreddit cache");
let after: string | null = "";
let nodata = false;
Expand All @@ -50,6 +50,8 @@ export async function getSubreddits() {
`https://old.reddit.com/subreddits/mine.json?limit=100&after=${after}`,
{
credentials: "include",
mode: "cors",
cache: "no-store"
}
);
const responseJson: unknown = await response.json();
Expand Down
6 changes: 3 additions & 3 deletions src/features/sidebar/userSidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async function setupMultireddits(parentContainer: HTMLDivElement) {
}
}

async function setupSubreddits(parentContainer: HTMLDivElement) {
async function setupSubreddits(parentContainer: HTMLDivElement, force?: boolean) {
const container = document.createElement("span");
container.id = "oldlander-subredditlist";
const refreshButton = document.createElement("button");
Expand All @@ -88,7 +88,7 @@ async function setupSubreddits(parentContainer: HTMLDivElement) {
refreshButton.removeEventListener("click", refreshHandler);
console.log("refresh");
refreshButton.classList.add("spin");
setupSubreddits(parentContainer);
setupSubreddits(parentContainer, true);
};
refreshButton.addEventListener("click", refreshHandler);

Expand All @@ -112,7 +112,7 @@ async function setupSubreddits(parentContainer: HTMLDivElement) {
["oldlander-subreddit", "oldlander-randNsfw"]
)
);
const subs = await getSubreddits();
const subs = await getSubreddits(!!force);
for (const subreddit of subs) {
container.appendChild(
createSidebarItem(
Expand Down

0 comments on commit b83b62e

Please sign in to comment.