날짜 형식 처리 안정성 강화 및 트랜잭션 삭제 시 앱 먹통 문제 해결
This commit is contained in:
36
src/utils/imageUtils.ts
Normal file
36
src/utils/imageUtils.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { Capacitor } from '@capacitor/core';
|
||||
|
||||
/**
|
||||
* 이미지 URL을 환경에 맞게 변환하는 유틸리티 함수
|
||||
*
|
||||
* 웹과 앱 환경에서 모두 정상적으로 이미지가 표시되도록 경로를 처리합니다.
|
||||
*
|
||||
* @param imagePath 이미지 경로 (예: '/zellyy.png')
|
||||
* @returns 환경에 맞게 변환된 이미지 URL
|
||||
*/
|
||||
export function getImageUrl(imagePath: string): string {
|
||||
// 이미지 경로가 이미 http로 시작하면 그대로 반환
|
||||
if (imagePath.startsWith('http://') || imagePath.startsWith('https://')) {
|
||||
return imagePath;
|
||||
}
|
||||
|
||||
// 경로의 첨 글자가 '/'이 아니면 추가
|
||||
const normalizedPath = imagePath.startsWith('/') ? imagePath : `/${imagePath}`;
|
||||
|
||||
// Capacitor 앱에서 실행 중인 경우
|
||||
if (Capacitor.isNativePlatform()) {
|
||||
// 안드로이드 플랫폼 확인
|
||||
const platform = Capacitor.getPlatform();
|
||||
|
||||
if (platform === 'android') {
|
||||
// 안드로이드 환경에서는 assets 폴더 경로 사용
|
||||
return `file:///android_asset/public${normalizedPath}`;
|
||||
} else if (platform === 'ios') {
|
||||
// iOS 환경에서는 다른 경로 사용
|
||||
return `${normalizedPath}`;
|
||||
}
|
||||
}
|
||||
|
||||
// 웹 환경에서는 일반 경로 사용
|
||||
return normalizedPath;
|
||||
}
|
||||
@@ -47,11 +47,32 @@ export const normalizeDate = (dateStr: string): string => {
|
||||
* ISO 형식의 날짜 문자열을 사용자 친화적인 형식으로 변환
|
||||
*/
|
||||
export const formatDateForDisplay = (isoDateStr: string): string => {
|
||||
// 입력값이 유효한지 보호 처리
|
||||
if (!isoDateStr || typeof isoDateStr !== 'string') {
|
||||
console.warn('유효하지 않은 날짜 입력:', isoDateStr);
|
||||
return '날짜 없음';
|
||||
}
|
||||
|
||||
try {
|
||||
// 이미 포맷된 날짜 문자열(예: "오늘, 14:30")이면 그대로 반환
|
||||
if (isoDateStr.includes('오늘,') ||
|
||||
isoDateStr.includes('년') && isoDateStr.includes('월') && isoDateStr.includes('일')) {
|
||||
return isoDateStr;
|
||||
}
|
||||
|
||||
// 유효한 ISO 날짜인지 확인
|
||||
const date = parseISO(isoDateStr);
|
||||
if (!isValid(date)) {
|
||||
return isoDateStr; // 유효하지 않으면 원본 반환
|
||||
let date;
|
||||
if (isoDateStr.match(/^\d{4}-\d{2}-\d{2}T/)) {
|
||||
// ISO 형식인 경우
|
||||
date = parseISO(isoDateStr);
|
||||
} else {
|
||||
// ISO 형식이 아닌 경우 일반 Date 생성자 시도
|
||||
date = new Date(isoDateStr);
|
||||
}
|
||||
|
||||
if (!isValid(date) || isNaN(date.getTime())) {
|
||||
console.warn('유효하지 않은 날짜 형식:', isoDateStr);
|
||||
return '유효하지 않은 날짜';
|
||||
}
|
||||
|
||||
// 현재 날짜와 비교
|
||||
@@ -68,7 +89,8 @@ export const formatDateForDisplay = (isoDateStr: string): string => {
|
||||
// 그 외의 경우 YYYY년 MM월 DD일 형식으로 반환
|
||||
return `${date.getFullYear()}년 ${date.getMonth() + 1}월 ${date.getDate()}일 ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
|
||||
} catch (error) {
|
||||
console.error('날짜 포맷 변환 오류:', error);
|
||||
return isoDateStr; // 오류 발생 시 원본 반환
|
||||
console.error('날짜 포맷 변환 오류:', error, isoDateStr);
|
||||
// 오류 발생 시 기본값 반환
|
||||
return '날짜 오류';
|
||||
}
|
||||
};
|
||||
|
||||
@@ -31,15 +31,39 @@ export const downloadTransactions = async (userId: string): Promise<void> => {
|
||||
console.log(`서버에서 ${data.length}개의 트랜잭션 다운로드`);
|
||||
|
||||
// 서버 데이터를 로컬 형식으로 변환
|
||||
const serverTransactions = data.map(t => ({
|
||||
id: t.transaction_id || t.id,
|
||||
title: t.title,
|
||||
amount: t.amount,
|
||||
date: t.date ? formatDateForDisplay(t.date) : '날짜 없음',
|
||||
category: t.category,
|
||||
type: t.type,
|
||||
notes: t.notes
|
||||
}));
|
||||
const serverTransactions = data.map(t => {
|
||||
// 날짜 형식 변환 시 오류 방지 처리
|
||||
let formattedDate = '날짜 없음';
|
||||
try {
|
||||
if (t.date) {
|
||||
// ISO 형식이 아닌 경우 기본 변환 수행
|
||||
if (!t.date.match(/^\d{4}-\d{2}-\d{2}T/)) {
|
||||
console.log(`비표준 날짜 형식 감지: ${t.date}, ID: ${t.transaction_id || t.id}`);
|
||||
// 유효한 Date 객체로 변환 가능한지 확인
|
||||
const testDate = new Date(t.date);
|
||||
if (isNaN(testDate.getTime())) {
|
||||
console.warn(`잘못된 날짜 형식 감지, 현재 날짜 사용: ${t.date}`);
|
||||
t.date = new Date().toISOString(); // 잘못된 날짜는 현재 날짜로 대체
|
||||
}
|
||||
}
|
||||
formattedDate = formatDateForDisplay(t.date);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`날짜 변환 오류 (ID: ${t.transaction_id || t.id}):`, err);
|
||||
// 오류 발생 시 기본값 사용
|
||||
formattedDate = new Date().toLocaleString('ko-KR');
|
||||
}
|
||||
|
||||
return {
|
||||
id: t.transaction_id || t.id,
|
||||
title: t.title,
|
||||
amount: t.amount,
|
||||
date: formattedDate,
|
||||
category: t.category,
|
||||
type: t.type,
|
||||
notes: t.notes
|
||||
};
|
||||
});
|
||||
|
||||
// 기존 로컬 데이터 불러오기
|
||||
const localDataStr = localStorage.getItem('transactions');
|
||||
|
||||
Reference in New Issue
Block a user