Refactor version data
This commit is contained in:
@@ -1,12 +1,26 @@
|
||||
export interface VersionData {
|
||||
version: string
|
||||
previousVersion?: string
|
||||
nextVersion?: string
|
||||
previousVersion: string
|
||||
nextVersion: string
|
||||
name: string
|
||||
krName?: string
|
||||
duration: [string, string]
|
||||
verified: boolean
|
||||
newBattlesuits: string[]
|
||||
newWeapons: string[]
|
||||
description: string
|
||||
supplyEvents: SupplyEventData[]
|
||||
}
|
||||
|
||||
export interface SupplyEventData {
|
||||
name: string
|
||||
duration: [string, string]
|
||||
track: number
|
||||
verified: boolean
|
||||
featured: {
|
||||
type: string
|
||||
id: string
|
||||
}[]
|
||||
others: {
|
||||
type: string
|
||||
id: string
|
||||
}[]
|
||||
}
|
||||
|
||||
@@ -13,15 +13,12 @@ import NextLink from 'next/link'
|
||||
import SquareImageBox from '../../../components/atoms/SquareImageBox'
|
||||
import { getWeaponById } from '../../../server/data/honkai3rd/weapons'
|
||||
import ScrollContainer from 'react-indiana-drag-scroll'
|
||||
import { listSupplyEventsByVersion } from '../../../server/data/honkai3rd/supplyEvents'
|
||||
import { addDateToDateString, getDateString } from '../../../lib/string'
|
||||
import { BattlesuitData } from '../../../lib/honkai3rd/battlesuits'
|
||||
import { WeaponData } from '../../../lib/honkai3rd/weapons'
|
||||
import { SupplyEventData } from '../../../lib/honkai3rd/supplyEvents'
|
||||
import { VersionData } from '../../../lib/honkai3rd/versions'
|
||||
import { generateI18NPaths, getI18NProps } from '../../../server/i18n'
|
||||
import { translate, useTranslation } from '../../../lib/i18n'
|
||||
import { useRouter } from 'next/router'
|
||||
import { useTranslation } from '../../../lib/i18n'
|
||||
import Head from '../../../components/atoms/Head'
|
||||
import PageLink from '../../../components/atoms/PageLink'
|
||||
import { assetsBucketBaseUrl } from '../../../lib/consts'
|
||||
@@ -31,31 +28,23 @@ interface VersionShowPageProps {
|
||||
versionData: VersionData
|
||||
battlesuits: BattlesuitData[]
|
||||
weapons: WeaponData[]
|
||||
supplyEvents: SupplyEventData[]
|
||||
}
|
||||
|
||||
const VersionShowPage = ({
|
||||
versionData,
|
||||
battlesuits,
|
||||
weapons,
|
||||
supplyEvents,
|
||||
}: VersionShowPageProps) => {
|
||||
const { t } = useTranslation()
|
||||
const { locale } = useRouter()
|
||||
const versionName = translate(
|
||||
locale,
|
||||
{ 'ko-KR': versionData.krName },
|
||||
versionData.name
|
||||
)
|
||||
return (
|
||||
<Honkai3rdLayout>
|
||||
<Head
|
||||
title={`v${versionData.version} ${versionName} - ${t(
|
||||
title={`v${versionData.version} ${versionData.name} - ${t(
|
||||
'common.honkai-3rd'
|
||||
)} - ${t('common.abyss-lab')}`}
|
||||
description={`${t('common.honkai-3rd')} ${t(
|
||||
'versions.version'
|
||||
)} ${versionName} / ${t('versions.new-battlesuits')}: ${battlesuits
|
||||
description={`${t('common.honkai-3rd')} ${t('versions.version')} ${
|
||||
versionData.name
|
||||
} / ${t('versions.new-battlesuits')}: ${battlesuits
|
||||
.map((battlesuit) => {
|
||||
return battlesuit.name
|
||||
})
|
||||
@@ -94,7 +83,7 @@ const VersionShowPage = ({
|
||||
)}
|
||||
</Box>
|
||||
<Heading as='h1'>
|
||||
v{versionData.version} : {versionName}
|
||||
v{versionData.version} : {versionData.name}
|
||||
</Heading>
|
||||
<Box mb={4}>
|
||||
{formatDate(new Date(versionData.duration[0]), 'PP')} -{' '}
|
||||
@@ -165,10 +154,10 @@ const VersionShowPage = ({
|
||||
<Box mb={2}>
|
||||
<ScrollContainer vertical={false}>
|
||||
<GanttChart
|
||||
items={supplyEvents.map((supplyEventData) => {
|
||||
items={versionData.supplyEvents.map((supplyEventData) => {
|
||||
const imgSrc = getIconSrcFromItem(supplyEventData.featured[0])
|
||||
return {
|
||||
id: supplyEventData.id,
|
||||
id: `${supplyEventData.duration[0]}/${supplyEventData.duration[1]}/${supplyEventData.name}`,
|
||||
label: (
|
||||
<Flex sx={{ alignItems: 'center' }}>
|
||||
{!supplyEventData.verified && (
|
||||
@@ -214,23 +203,20 @@ export async function getStaticProps({
|
||||
params,
|
||||
locale,
|
||||
}: NextPageContext & { params: { versionId: string } }) {
|
||||
const versionData = getVersion(params.versionId)!
|
||||
const versionData = getVersion(params.versionId, locale)!
|
||||
|
||||
const battlesuits = versionData.newBattlesuits.map((battlesuitId) => {
|
||||
return getBattlesuitById(battlesuitId, locale)
|
||||
})
|
||||
const weapons = versionData.newWeapons.map((weaponId) => {
|
||||
return getWeaponById(weaponId)
|
||||
return getWeaponById(weaponId, locale)
|
||||
})
|
||||
|
||||
const supplyEvents = listSupplyEventsByVersion(versionData.version)
|
||||
|
||||
return {
|
||||
props: {
|
||||
versionData,
|
||||
battlesuits,
|
||||
weapons,
|
||||
supplyEvents,
|
||||
...(await getI18NProps(locale)),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import SquareImageBox from '../../../components/atoms/SquareImageBox'
|
||||
import Breadcrumb from '../../../components/organisms/Breadcrumb'
|
||||
import GanttChart from '../../../components/organisms/GanttChart'
|
||||
import { getBattlesuitById } from '../../../server/data/honkai3rd/battlesuits'
|
||||
import { listSupplyEventsByVersion } from '../../../server/data/honkai3rd/supplyEvents'
|
||||
import {
|
||||
getCurrentVersion,
|
||||
listVersionData,
|
||||
@@ -17,11 +16,9 @@ import { format as formatDate } from 'date-fns'
|
||||
import { BattlesuitData } from '../../../lib/honkai3rd/battlesuits'
|
||||
import { WeaponData } from '../../../lib/honkai3rd/weapons'
|
||||
import { VersionData } from '../../../lib/honkai3rd/versions'
|
||||
import { SupplyEventData } from '../../../lib/honkai3rd/supplyEvents'
|
||||
import { getI18NProps } from '../../../server/i18n'
|
||||
import { NextPageContext } from 'next'
|
||||
import { useTranslation, translate } from '../../../lib/i18n'
|
||||
import { useRouter } from 'next/router'
|
||||
import { useTranslation } from '../../../lib/i18n'
|
||||
import Head from '../../../components/atoms/Head'
|
||||
import PageLink from '../../../components/atoms/PageLink'
|
||||
import { assetsBucketBaseUrl } from '../../../lib/consts'
|
||||
@@ -32,7 +29,6 @@ interface VersionIndexPageProps {
|
||||
currentVersionData: VersionData
|
||||
currentVersionNewBattlesuits: BattlesuitData[]
|
||||
currentVersionNewWeapons: WeaponData[]
|
||||
currentVersionSupplyEvents: SupplyEventData[]
|
||||
}
|
||||
|
||||
const VersionIndexPage = ({
|
||||
@@ -40,10 +36,8 @@ const VersionIndexPage = ({
|
||||
currentVersionNewBattlesuits,
|
||||
versionDataList,
|
||||
currentVersionNewWeapons,
|
||||
currentVersionSupplyEvents,
|
||||
}: VersionIndexPageProps) => {
|
||||
const { t } = useTranslation()
|
||||
const { locale } = useRouter()
|
||||
|
||||
return (
|
||||
<Honkai3rdLayout>
|
||||
@@ -82,12 +76,7 @@ const VersionIndexPage = ({
|
||||
</Box>
|
||||
|
||||
<Heading as='h1'>
|
||||
v{currentVersionData.version} :{' '}
|
||||
{translate(
|
||||
locale,
|
||||
{ 'ko-KR': currentVersionData.krName },
|
||||
currentVersionData.name
|
||||
)}{' '}
|
||||
v{currentVersionData.version} : {currentVersionData.name}{' '}
|
||||
<small>({t('versions.current')})</small>
|
||||
</Heading>
|
||||
<Box mb={4}>
|
||||
@@ -159,30 +148,34 @@ const VersionIndexPage = ({
|
||||
<Box mb={2}>
|
||||
<ScrollContainer vertical={false}>
|
||||
<GanttChart
|
||||
items={currentVersionSupplyEvents.map((supplyEventData) => {
|
||||
const imgSrc = getIconSrcFromItem(supplyEventData.featured[0])
|
||||
return {
|
||||
id: supplyEventData.id,
|
||||
label: (
|
||||
<Flex sx={{ alignItems: 'center' }}>
|
||||
{!supplyEventData.verified && (
|
||||
<Text sx={{ flexShrink: 0 }}>❓</Text>
|
||||
)}
|
||||
{imgSrc != null && (
|
||||
<SquareImageBox
|
||||
size={20}
|
||||
src={imgSrc}
|
||||
alt={supplyEventData.featured[0].id}
|
||||
mr={1}
|
||||
/>
|
||||
)}
|
||||
<Text>{supplyEventData.name}</Text>
|
||||
</Flex>
|
||||
),
|
||||
duration: supplyEventData.duration,
|
||||
row: supplyEventData.track,
|
||||
items={currentVersionData.supplyEvents.map(
|
||||
(supplyEventData) => {
|
||||
const imgSrc = getIconSrcFromItem(
|
||||
supplyEventData.featured[0]
|
||||
)
|
||||
return {
|
||||
id: `${supplyEventData.duration[0]}/${supplyEventData.duration[1]}/${supplyEventData.name}`,
|
||||
label: (
|
||||
<Flex sx={{ alignItems: 'center' }}>
|
||||
{!supplyEventData.verified && (
|
||||
<Text sx={{ flexShrink: 0 }}>❓</Text>
|
||||
)}
|
||||
{imgSrc != null && (
|
||||
<SquareImageBox
|
||||
size={20}
|
||||
src={imgSrc}
|
||||
alt={supplyEventData.featured[0].id}
|
||||
mr={1}
|
||||
/>
|
||||
)}
|
||||
<Text>{supplyEventData.name}</Text>
|
||||
</Flex>
|
||||
),
|
||||
duration: supplyEventData.duration,
|
||||
row: supplyEventData.track,
|
||||
}
|
||||
}
|
||||
})}
|
||||
)}
|
||||
today={getDateString(new Date())}
|
||||
startDate={currentVersionData.duration[0]}
|
||||
endDate={
|
||||
@@ -211,13 +204,8 @@ const VersionIndexPage = ({
|
||||
passHref
|
||||
>
|
||||
<Link>
|
||||
{versionData.version} :{' '}
|
||||
{translate(
|
||||
locale,
|
||||
{ 'ko-KR': versionData.krName },
|
||||
versionData.name
|
||||
)}{' '}
|
||||
({formatDate(new Date(versionData.duration[0]), 'PP')} -{' '}
|
||||
{versionData.version} : {versionData.name} (
|
||||
{formatDate(new Date(versionData.duration[0]), 'PP')} -{' '}
|
||||
{versionData.duration[1] != null
|
||||
? formatDate(new Date(versionData.duration[1]), 'PP')
|
||||
: ''}
|
||||
@@ -235,30 +223,25 @@ const VersionIndexPage = ({
|
||||
}
|
||||
|
||||
export async function getStaticProps({ locale }: NextPageContext) {
|
||||
const currentVersionData = getCurrentVersion()!
|
||||
const currentVersionData = getCurrentVersion(locale)!
|
||||
|
||||
const currentVersionNewBattlesuits = currentVersionData.newBattlesuits.map(
|
||||
(battlesuitId) => {
|
||||
return getBattlesuitById(battlesuitId)
|
||||
return getBattlesuitById(battlesuitId, locale)
|
||||
}
|
||||
)
|
||||
const currentVersionNewWeapons = currentVersionData.newWeapons.map(
|
||||
(weaponId) => {
|
||||
return getWeaponById(weaponId)
|
||||
return getWeaponById(weaponId, locale)
|
||||
}
|
||||
)
|
||||
|
||||
const currentVersionSupplyEvents = listSupplyEventsByVersion(
|
||||
currentVersionData.version
|
||||
)
|
||||
|
||||
return {
|
||||
props: {
|
||||
currentVersionData,
|
||||
currentVersionNewBattlesuits,
|
||||
currentVersionNewWeapons,
|
||||
currentVersionSupplyEvents,
|
||||
versionDataList: listVersionData(),
|
||||
versionDataList: listVersionData(locale),
|
||||
...(await getI18NProps(locale)),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
import { SupplyEventData } from '../../../lib/honkai3rd/supplyEvents'
|
||||
import { readdirSync, readJsonFileSync } from '../fs'
|
||||
|
||||
export function listSupplyEventsByVersion(version: string) {
|
||||
const supplyEventsDirectoryPathname = `honkai3rd/versions/${version}/supply-events`
|
||||
|
||||
const supplyEventFileNameList = readdirSync(supplyEventsDirectoryPathname)
|
||||
const supplyEventList = supplyEventFileNameList
|
||||
.map((fileName) => {
|
||||
const filePathname = `${supplyEventsDirectoryPathname}/` + fileName
|
||||
const data = readJsonFileSync(filePathname)
|
||||
|
||||
return { ...data, id: fileName.replace(/\.json/, '') } as SupplyEventData
|
||||
})
|
||||
.sort((a, b) => {
|
||||
return -a.id.localeCompare(b.id)
|
||||
})
|
||||
|
||||
return supplyEventList
|
||||
}
|
||||
@@ -1,41 +1,45 @@
|
||||
import { format } from 'date-fns'
|
||||
import { readdirSync, readFileSync, readJsonFileSync } from '../fs'
|
||||
import { compareVersion } from '../../../lib/string'
|
||||
import { VersionData } from '../../../lib/honkai3rd/versions'
|
||||
import yaml from 'yaml'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { omit } from 'ramda'
|
||||
|
||||
const versionDirectoryList = readdirSync('honkai3rd/versions')
|
||||
let cachedData: any = null
|
||||
export function listVersionData(locale?: string): VersionData[] {
|
||||
if (cachedData == null) {
|
||||
cachedData = yaml.parse(
|
||||
fs
|
||||
.readFileSync(path.join(process.cwd(), 'data/honkai3rd/versions.yaml'))
|
||||
.toString('utf-8')
|
||||
)
|
||||
}
|
||||
const versions = cachedData
|
||||
const localized = versions
|
||||
.map((version: any) => {
|
||||
return {
|
||||
...omit(['krName'], version),
|
||||
name: locale === 'ko-KR' ? version.krName : version.name,
|
||||
}
|
||||
})
|
||||
.sort((a: any, b: any) => {
|
||||
return compareVersion(b.version, a.version)
|
||||
})
|
||||
|
||||
const versionDataList = versionDirectoryList
|
||||
.map((directoryName) => {
|
||||
const directoryPathname = 'honkai3rd/versions/' + directoryName
|
||||
const data: VersionData = {
|
||||
version: directoryName,
|
||||
...readJsonFileSync(directoryPathname + '/version-data.json'),
|
||||
description: readFileSync(
|
||||
directoryPathname + '/description.md'
|
||||
).toString(),
|
||||
}
|
||||
|
||||
return data
|
||||
})
|
||||
.sort((a, b) => {
|
||||
return compareVersion(b.version, a.version)
|
||||
})
|
||||
|
||||
export function listVersionData() {
|
||||
return versionDataList
|
||||
return localized
|
||||
}
|
||||
|
||||
export function getVersion(version: number | string) {
|
||||
return versionDataList.find((versionData) => {
|
||||
export function getVersion(version: number | string, locale?: string) {
|
||||
return listVersionData(locale).find((versionData) => {
|
||||
return versionData.version.toString() === version.toString()
|
||||
})
|
||||
}
|
||||
|
||||
export function getCurrentVersion() {
|
||||
export function getCurrentVersion(locale?: string) {
|
||||
const todayDateString = format(new Date(), 'yyyy-MM-dd')
|
||||
|
||||
return versionDataList.slice().find((versionData) => {
|
||||
return listVersionData(locale).find((versionData) => {
|
||||
const [startDateString] = versionData.duration
|
||||
|
||||
return startDateString.localeCompare(todayDateString) <= 0
|
||||
|
||||
Reference in New Issue
Block a user