ваше сообщение коммита
This commit is contained in:
45
frontend/src/utils/helpers.js
Normal file
45
frontend/src/utils/helpers.js
Normal 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 '';
|
||||
}
|
||||
};
|
||||
53
frontend/src/utils/storage.js
Normal file
53
frontend/src/utils/storage.js
Normal 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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user