SHA256
65 lines
2.6 KiB
JavaScript
65 lines
2.6 KiB
JavaScript
import { t } from '../i18n/index.js';
|
|
import { readShare, deleteShare } from './share-inbox.js';
|
|
const KEY = 'shine-share-pending-id';
|
|
let pending = null;
|
|
let activePath = '';
|
|
let permittedPath = '';
|
|
let owner = '';
|
|
let sending = false;
|
|
export const getPendingShare = () => pending;
|
|
export const isShareSending = () => sending;
|
|
export const setShareSending = value => { sending = Boolean(value); };
|
|
export const isShareChoosing = () => Boolean(pending && activePath === '/contacts');
|
|
export const shareForChat = chatId => pending && pending.recipient === chatId ? pending : null;
|
|
export async function restoreShare() {
|
|
const url = new URL(location.href);
|
|
let id = url.searchParams.get('shine-share');
|
|
try { id ||= sessionStorage.getItem(KEY); } catch { if (!id) return ''; }
|
|
const error = url.searchParams.get('shine-share-error');
|
|
if (url.searchParams.has('shine-share') || error) {
|
|
url.searchParams.delete('shine-share'); url.searchParams.delete('shine-share-error');
|
|
history.replaceState(history.state, '', url.pathname + url.search + url.hash);
|
|
}
|
|
if (error) { try { sessionStorage.removeItem(KEY); } catch {} return ['limit', 'empty', 'storage'].includes(error) ? error : 'storage'; }
|
|
if (!id) return '';
|
|
try {
|
|
pending = await readShare(id);
|
|
if (!pending) { sessionStorage.removeItem(KEY); return 'expired'; }
|
|
try { sessionStorage.setItem(KEY, id); } catch {}
|
|
return '';
|
|
} catch { return 'storage'; }
|
|
}
|
|
export function activateShare(login) {
|
|
if (!pending || activePath) return false;
|
|
owner = String(login || '');
|
|
activePath = '/contacts';
|
|
permittedPath = '/contacts';
|
|
return true;
|
|
}
|
|
export function chooseShareRecipient(chatId, path) {
|
|
if (!isShareChoosing()) return;
|
|
pending.recipient = chatId;
|
|
permittedPath = path;
|
|
}
|
|
export function finishShare() {
|
|
const id = pending?.id;
|
|
pending = null; activePath = ''; permittedPath = ''; owner = ''; sending = false;
|
|
try { sessionStorage.removeItem(KEY); } catch {}
|
|
if (id) void deleteShare(id).catch(() => {});
|
|
}
|
|
export function checkShareOwner(login) {
|
|
if (owner && owner !== String(login || '')) finishShare();
|
|
}
|
|
// All SPA navigation, including browser Back, goes through the same cancellation rule.
|
|
export function allowShareNavigation(path) {
|
|
if (!pending || !activePath || path === activePath) return true;
|
|
if (path === permittedPath && !sending) { activePath = path; permittedPath = ''; return true; }
|
|
if (sending) return false;
|
|
if (!window.confirm(t('remaining.share.cancel_confirm'))) return false;
|
|
finishShare();
|
|
return true;
|
|
}
|
|
export function restoreSharePath() {
|
|
if (activePath) history.pushState({ shineFrom: '/messages' }, '', activePath);
|
|
}
|