atagen

malice

public
Star0
Watch0
719f7cdd974e47e6ab6bdad3523014a8d0a64771
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git
web/src/lib/protection.tsTypescript38 lines1.3 KB
1
import { writable } from 'svelte/store';
2
import {
3
  isDismissed,
4
  requestPersistence,
5
  setDismissed,
6
  type InstallContext
7
} from './protection-model';
8
9
export const storageUnprotected = writable(false);
10
export const recoveryDismissed = writable(false);
11
12
export const isDeviceBuild = import.meta.env.VITE_MALICE_TARGET === 'device';
13
14
export function installContext(): InstallContext {
15
  const standalone =
16
    window.matchMedia('(display-mode: standalone)').matches ||
17
    (navigator as Navigator & { standalone?: boolean }).standalone === true;
18
  return { device: isDeviceBuild, standalone };
19
}
20
21
export function loadDismissals(account: string): void {
22
  recoveryDismissed.set(isDismissed(localStorage, 'recovery', account));
23
}
24
25
export async function protectStorage(account: string): Promise<void> {
26
  const result = await requestPersistence(navigator.storage).catch(() => null);
27
  storageUnprotected.set(isDeviceBuild && result !== null && !result.persisted && !isDismissed(localStorage, 'storage', account));
28
}
29
30
export function dismissStorageWarning(account: string): void {
31
  setDismissed(localStorage, 'storage', account);
32
  storageUnprotected.set(false);
33
}
34
35
export function dismissRecoveryPrompt(account: string): void {
36
  setDismissed(localStorage, 'recovery', account);
37
  recoveryDismissed.set(true);
38
}