fix. utils 함수 리팩토링

This commit is contained in:
clkim
2025-12-01 14:23:35 +09:00
parent 10ce30e149
commit 9b3b7b4ee3
20 changed files with 120 additions and 162 deletions

View File

@@ -44,6 +44,7 @@ export const getSupportedPlatforms = (
return storePlatforms
}
}
/**
* 페이지 데이터를 기반으로 레이아웃 타입을 결정합니다.
* @param pageData 페이지 데이터
@@ -62,7 +63,10 @@ export const getLayoutType = (
*/
export const isTypeImage = (type: PageDataResourceGroupType): boolean => {
return (
type === 'IMG_COMM' || type === 'IMG_LANG' || type === 'IMG_COMM_GLOBAL'
type === 'IMG_COMM' ||
type === 'IMG_LANG' ||
type === 'IMG_COMM_GLOBAL' ||
type === 'IMG'
)
}
@@ -163,20 +167,6 @@ export const getComponentGroupAry = (
return components[componentName]?.groups || []
}
export const getImagePaths = (resourcesData: PageDataResourceGroup) => {
if (!resourcesData?.res_path) return null
const pcPath = resourcesData.res_path.path_pc
const moPath = resourcesData.res_path.path_mo
if (!pcPath || !moPath) return pcPath || moPath
return {
pc: pcPath,
mo: moPath,
}
}
/**
* 컴포넌트 그룹의 첫 번째 데이터를 반환합니다.
* @param components props.components 또는 group 객체
@@ -191,3 +181,34 @@ export const getComponentGroup = (
return components[componentName]?.groups?.[0] || null
}
/**
* 리소스 데이터를 기반으로 리소스 경로를 반환합니다.
* @param resourcesData 리소스 데이터
* @param resourcesType 리소스 타입
* - IMG: 이미지
* - VID: 비디오
* @returns 리소스 경로 객체 (pc: PC 버전, mo: 모바일 버전) 또는 null
*/
export const getResourceSrc = (
resourcesData: PageDataResourceGroup,
resourcesType?: 'IMG' | 'VID'
) => {
if (!resourcesData) return null
const resPath = resourcesData.res_path
const resType = resourcesType || resourcesData.resource_type
const pcField = isTypeVideo(resType) ? 'path_vid_pc' : 'path_pc'
const mobileField = isTypeVideo(resType) ? 'path_vid_mo' : 'path_mo'
const pcPath = resPath[pcField] || resPath[mobileField]
const mobilePath = resPath[mobileField] || resPath[pcField]
// 경로가 없으면 null 반환
if (!pcPath && !mobilePath) return null
return {
pc: pcPath,
mo: mobilePath,
}
}

View File

@@ -53,3 +53,34 @@ export const formatPathWithoutLocale = (path: string): string => {
export const isInternalUrl = (url?: string): boolean => {
return !!url && !url.startsWith('http')
}
/**
* 리소스 경로를 완전한 호스트 URL로 변환합니다.
* @param path 리소스 경로
* @returns 완전한 리소스 URL
*/
export const formatPathHost = (
path: string,
options: { imageType?: 'common' | 'game'; isSkipHost?: boolean } = {}
): string => {
if (!path) return ''
if (/^(https?:\/\/|www\.)/.test(path)) return path
const runtimeConfig = useRuntimeConfig()
const { staticUrl, assetsUrl } = runtimeConfig.public
const { imageType = 'game', isSkipHost = false } = options
const isDevelopment = import.meta.dev
const isTypeGame = imageType === 'game'
if (isSkipHost) return path
// 개발 환경일 때는 루트 경로 생략
if (!isTypeGame && isDevelopment) return path
// 게임/공통 여부에 따른 경로 결정
const basePath = isTypeGame ? staticUrl : assetsUrl
return `${basePath}${path}`
}

View File

@@ -3,84 +3,12 @@
* @description ui 처리에 필요한 유틸리티 함수를 제공합니다.
*/
import { isTypeVideo } from '#layers/utils/dataUtil'
import type { GameDataResourceGroupBtnInfo } from '#layers/types/api/gameData'
import type {
PageDataResourceGroups,
PageDataResourceGroup,
PageDataResourceGroupResPath,
PageDataResourceGroupBtnInfo,
} from '#layers/types/api/pageData'
/**
* 리소스 경로를 완전한 호스트 URL로 변환합니다.
* @param path 리소스 경로
* @returns 완전한 리소스 URL
*/
export const getResourceHost = (
path: string,
options: { imageType?: 'common' | 'game' } = {}
): string => {
if (!path) return ''
if (/^(https?:\/\/|www\.)/.test(path)) return path
const runtimeConfig = useRuntimeConfig()
const { staticUrl, assetsUrl } = runtimeConfig.public
const { imageType = 'game' } = options
const isDevelopment = import.meta.dev
const isTypeGame = imageType === 'game'
// * [TODO] 수정 필요 : 게임별 이미지 로컬에 추가 필요.
if (isTypeGame) return `${staticUrl}${path}`
// 개발 환경일 때는 루트 경로 생략
if (isDevelopment) return path
// 게임/공통 여부에 따른 경로 결정
const basePath = isTypeGame ? staticUrl : assetsUrl
return `${basePath}${path}`
}
/**
* 디바이스 리소스(이미지/비디오)를 처리하여 PC/모바일 버전을 반환합니다.
* @param pathArray 리소스 경로 배열
* @param options 리소스 타입 옵션
* @returns 디바이스 리소스 객체 또는 null
*/
export const getDeviceSrc = (
pathArray: PageDataResourceGroupResPath,
options?: {
resourcesType?: 'image' | 'video'
imageType?: 'common' | 'game'
}
) => {
// pathArray가 없으면 null 반환
if (!pathArray) return null
const { resourcesType = 'image', imageType = 'game' } = options ?? {}
const pcField = resourcesType === 'video' ? 'path_vid_pc' : 'path_pc'
const mobileField = resourcesType === 'video' ? 'path_vid_mo' : 'path_mo'
const pcPath = pathArray[pcField] || pathArray[mobileField]
const mobilePath = pathArray[mobileField] || pathArray[pcField]
// 경로가 없으면 null 반환
if (!pcPath && !mobilePath) return null
const resolvedImages = {
pc: pcPath ? getResourceHost(pcPath, { imageType }) : '',
mobile: mobilePath ? getResourceHost(mobilePath, { imageType }) : '',
}
return {
mobileSrc: resolvedImages.mobile,
pcSrc: resolvedImages.pc,
}
}
/**
* 색상값을 반환합니다.
* @param colorName 색상 이름
@@ -147,26 +75,3 @@ export const getPaginationClass = (
'--pagination-disabled': paginationDisabled,
}
}
/**
* 미디어 이미지를 반환합니다. (이미지 / 유튜브 썸네일)
* @param resourceGroups - 미디어 리소스 그룹 객체
* @param quality - 썸네일 품질
* @returns 미디어 이미지 소스 (이미지 / 유튜브 썸네일)
*/
export const getMediaImgSrc = (
resourceGroups: PageDataResourceGroup,
quality?
): string => {
if (!resourceGroups) return ''
const mediaSrc = resourceGroups?.display?.text
const mediaType = resourceGroups?.resource_type
if (isTypeVideo(mediaType) && mediaSrc) {
const thumbnailUrl = getYouTubeThumbnail(mediaSrc, quality)
return thumbnailUrl
}
return mediaSrc || ''
}