Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Env vars from query #16

Merged
merged 5 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 53 additions & 11 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,56 @@
const config = {
baseUrl: process.env.NEXT_PUBLIC_API_BASE_URL ?? '',
const envVars = {
apiBaseUrl: process.env.NEXT_PUBLIC_API_BASE_URL ?? '',
eventAbbr: process.env.NEXT_PUBLIC_EVENT_ABBR ?? '',
transTimePage1: parseInt(process.env.NEXT_PUBLIC_TRANS_TIME_PAGE1 ?? '24'),
transTimePage2: parseInt(process.env.NEXT_PUBLIC_TRANS_TIME_PAGE2 ?? '24'),
transTimePage3: parseInt(process.env.NEXT_PUBLIC_TRANS_TIME_PAGE3 ?? '24'),
debug: !!process.env.NEXT_PUBLIC_DEBUG,
excludedTalks:
process.env.NEXT_PUBLIC_EXCLUDED_TALKS?.split(',').map((t) =>
parseInt(t)
) || [],
} as const
transTimePage1: process.env.NEXT_PUBLIC_TRANS_TIME_PAGE1 ?? '24',
transTimePage2: process.env.NEXT_PUBLIC_TRANS_TIME_PAGE2 ?? '24',
transTimePage3: process.env.NEXT_PUBLIC_TRANS_TIME_PAGE3 ?? '24',
debug: process.env.NEXT_PUBLIC_DEBUG ?? '',
excludedTalks: process.env.NEXT_PUBLIC_EXCLUDED_TALKS ?? '',
}

export type EnvVars = typeof envVars

export type Config = {
apiBaseUrl: string
eventAbbr: string
transTimePage1: number
transTimePage2: number
transTimePage3: number
debug: boolean
excludedTalks: number[]
}

const config = makeConfig(envVars) as Config

function makeConfig(vars: Partial<EnvVars>): Partial<Config> {
const conf: Partial<Config> = {}
if (vars.apiBaseUrl) {
conf.apiBaseUrl = vars.apiBaseUrl
}
if (vars.eventAbbr) {
conf.eventAbbr = vars.eventAbbr
}
if (vars.transTimePage1) {
conf.transTimePage1 = parseFloat(vars.transTimePage1)
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Gaku-Kunimi
元々parseIntを使っていて小数点以下が破棄されていたのを、parseFloatに直したので、 #14 が直っているかもしれません。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

おぉ、なるほど
見てみます!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

再現させる環境の都合上マージして確認しちゃいます!

if (vars.transTimePage2) {
conf.transTimePage2 = parseFloat(vars.transTimePage2)
}
if (vars.transTimePage3) {
conf.transTimePage3 = parseFloat(vars.transTimePage3)
}
if (vars.debug) {
conf.debug = !!vars.debug
}
if (vars.excludedTalks) {
conf.excludedTalks =
vars.excludedTalks.split(',').map((t) => parseInt(t)) || []
}
return conf
}

export function extendConfig(query: Record<string, string>) {
Object.assign(config, makeConfig(query))
}

export default config
13 changes: 11 additions & 2 deletions src/pages/break/menu/[confDay].tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { useGetTalksAndTracksForMenu } from '@/components/hooks/useGetTalksAndTracks'
import { MenuView } from '@/components/models/talkView'
import config from '@/config'
import config, { extendConfig } from '@/config'
import { Talk } from '@/generated/dreamkast-api.generated'
import { getTimeStr } from '@/utils/time'
import { Optional } from '@/utils/types'
import Link from 'next/link'
import { useRouter } from 'next/router'
import { useEffect } from 'react'

export default function Index() {
const router = useRouter()
const { confDay } = router.query
useEffect(() => {
extendConfig(router.query as Record<string, string>)
}, [router.query])
const { eventAbbr } = config

const { isLoading, view } = useGetTalksAndTracksForMenu(
Expand Down Expand Up @@ -67,13 +71,18 @@ function TalkMenu({ view }: { view: Optional<MenuView> }) {
}

function TalkMenuItem({ talk }: { talk: Optional<Talk> }) {
const { query } = useRouter()
delete query.confDay
if (!talk) {
return <div />
}
return (
<Link
className="col-span-1 hover:underline"
href={`/break/talks/${talk.id}`}
href={{
pathname: `/break/talks/${talk.id}`,
query,
}}
>
<div>{talk.id}</div>
<div>{talk.title}</div>
Expand Down
5 changes: 4 additions & 1 deletion src/pages/break/talks/[talkId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import Page3 from '@/components/Page3'
import Page4 from '@/components/Page4'
import { useGetTalksAndTracks } from '@/components/hooks/useGetTalksAndTracks'
import { PageCtx, PageCtxProvider } from '@/components/models/pageContext'
import config from '@/config'
import config, { extendConfig } from '@/config'
import { useRouter } from 'next/router'
import { useContext, useEffect } from 'react'

function Pages() {
const router = useRouter()
const { talkId } = router.query
useEffect(() => {
extendConfig(router.query as Record<string, string>)
}, [router.query])

const { current, setTotalPage, goNextPage } = useContext(PageCtx)
const { isLoading, view } = useGetTalksAndTracks(talkId as string | null)
Expand Down
2 changes: 1 addition & 1 deletion src/store/baseApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import config from '@/config'

export const baseApi = createApi({
baseQuery: fetchBaseQuery({ baseUrl: config.baseUrl }),
baseQuery: fetchBaseQuery({ baseUrl: config.apiBaseUrl }),
endpoints: () => ({}),
})
Loading