ваше сообщение коммита

This commit is contained in:
2025-04-30 14:31:48 +03:00
parent dde96d11f5
commit c694c43d82
11 changed files with 1089 additions and 3605 deletions

View File

@@ -0,0 +1,45 @@
/**
* Генерирует уникальный ID
* @returns {string} - Уникальный ID
*/
export const generateUniqueId = () => {
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
};
/**
* Сокращает адрес кошелька
* @param {string} address - Адрес кошелька
* @returns {string} - Сокращенный адрес
*/
export const truncateAddress = (address) => {
if (!address) return '';
// Добавим проверку на длину, чтобы не было ошибок
if (address.length <= 10) return address;
return `${address.substring(0, 6)}...${address.substring(address.length - 4)}`;
};
/**
* Форматирует время в человекочитаемый вид
* @param {string | number | Date} timestamp - Метка времени
* @returns {string} - Форматированное время
*/
export const formatTime = (timestamp) => {
if (!timestamp) return '';
try {
const date = new Date(timestamp);
if (isNaN(date.getTime())) {
console.warn('Invalid timestamp for formatTime:', timestamp);
return '';
}
return date.toLocaleString([], {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
});
} catch (error) {
console.error('Error formatting time:', error, timestamp);
return '';
}
};

View File

@@ -0,0 +1,53 @@
export const isLocalStorageAvailable = () => {
try {
const test = '__storage_test__';
window.localStorage.setItem(test, test);
window.localStorage.removeItem(test);
return true;
} catch (e) {
console.error('localStorage is not available:', e);
return false;
}
};
export const getFromStorage = (key, defaultValue = null) => {
if (!isLocalStorageAvailable()) return defaultValue;
try {
const item = window.localStorage.getItem(key);
// Пытаемся распарсить JSON, если не получается - возвращаем как есть или defaultValue
try {
return item ? JSON.parse(item) : defaultValue;
} catch (e) {
return item || defaultValue;
}
} catch (e) {
console.error(`Error getting ${key} from localStorage:`, e);
return defaultValue;
}
};
export const setToStorage = (key, value) => {
if (!isLocalStorageAvailable()) return false;
try {
// Сериализуем объекты и массивы в JSON
const valueToStore = typeof value === 'object' || Array.isArray(value)
? JSON.stringify(value)
: value;
window.localStorage.setItem(key, valueToStore);
return true;
} catch (e) {
console.error(`Error setting ${key} in localStorage:`, e);
return false;
}
};
export const removeFromStorage = (key) => {
if (!isLocalStorageAvailable()) return false;
try {
window.localStorage.removeItem(key);
return true;
} catch (e) {
console.error(`Error removing ${key} from localStorage:`, e);
return false;
}
};