mirror of
https://github.com/ReVanced/revanced-website.git
synced 2026-01-19 01:03:56 +00:00
feat: brand new Modal component (#41)
* feat: brand new Modal component * Apply suggestions from code review Co-authored-by: Ax333l <main@axelen.xyz>
This commit is contained in:
@@ -1,51 +1,56 @@
|
||||
import { browser } from "$app/environment";
|
||||
import { browser } from '$app/environment';
|
||||
|
||||
import { dev_log } from "$lib/utils";
|
||||
import { dev_log } from '$lib/utils';
|
||||
|
||||
const CACHE_KEY_PREFIX = "revanced_api_cache_l1";
|
||||
const CACHE_KEY_PREFIX = 'revanced_api_cache_l1';
|
||||
const L1_CACHE_VALIDITY = 5 * 60 * 1000; // 5 minutes
|
||||
|
||||
function l1_key_name(endpoint: string) {
|
||||
return `${CACHE_KEY_PREFIX}:${endpoint}`;
|
||||
return `${CACHE_KEY_PREFIX}:${endpoint}`;
|
||||
}
|
||||
|
||||
// Get item from the cache
|
||||
export function get(endpoint: string) {
|
||||
if (!browser) {
|
||||
return null;
|
||||
}
|
||||
if (!browser) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const key_name = l1_key_name(endpoint);
|
||||
const ls_data: { valid_until: number; data: any } | null = JSON.parse(localStorage.getItem(key_name));
|
||||
const key_name = l1_key_name(endpoint);
|
||||
const ls_data: { valid_until: number; data: any } | null = JSON.parse(
|
||||
localStorage.getItem(key_name) as string
|
||||
);
|
||||
|
||||
if (ls_data === null || ls_data.valid_until <= Date.now()) {
|
||||
dev_log("Cache", `missed "${endpoint}"`);
|
||||
localStorage.removeItem(key_name);
|
||||
return null;
|
||||
}
|
||||
if (ls_data === null || ls_data.valid_until <= Date.now()) {
|
||||
dev_log('Cache', `missed "${endpoint}"`);
|
||||
localStorage.removeItem(key_name);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
dev_log("Cache", `hit "${endpoint}"`);
|
||||
return ls_data.data;
|
||||
dev_log('Cache', `hit "${endpoint}"`);
|
||||
return ls_data.data;
|
||||
}
|
||||
|
||||
// Update the cache
|
||||
export function update(endpoint: string, data: any) {
|
||||
if (!browser) {
|
||||
return;
|
||||
}
|
||||
if (!browser) {
|
||||
return;
|
||||
}
|
||||
|
||||
localStorage.setItem(l1_key_name(endpoint), JSON.stringify({
|
||||
data,
|
||||
valid_until: Date.now() + L1_CACHE_VALIDITY
|
||||
}));
|
||||
localStorage.setItem(
|
||||
l1_key_name(endpoint),
|
||||
JSON.stringify({
|
||||
data,
|
||||
valid_until: Date.now() + L1_CACHE_VALIDITY
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
// Clear the cache
|
||||
export function clear() {
|
||||
for (const key of Object.keys(localStorage)) {
|
||||
if (key.startsWith(CACHE_KEY_PREFIX)) {
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
}
|
||||
// Clear the cache and reload
|
||||
export function clear_and_reload() {
|
||||
for (const key of Object.keys(localStorage)) {
|
||||
if (key.startsWith(CACHE_KEY_PREFIX)) {
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
}
|
||||
location.reload();
|
||||
}
|
||||
|
||||
@@ -2,9 +2,10 @@ import { browser } from "$app/environment";
|
||||
|
||||
const URL_KEY = "revanced_api_url";
|
||||
|
||||
export const default_base_url = "https://releases.revanced.app";
|
||||
|
||||
// Get base URL
|
||||
export function api_base_url(): string {
|
||||
const default_base_url = "https://releases.revanced.app";
|
||||
if (browser) {
|
||||
return localStorage.getItem(URL_KEY) || default_base_url;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user