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

Fix: ダウンロードダイアログのOS変更時の挙動を修正、OSによって初期選択が変わるように #232

Merged
Merged
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
41 changes: 33 additions & 8 deletions src/components/downloadModal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Link, graphql, useStaticQuery } from "gatsby"
import React, { useState } from "react"
import React, { useEffect, useState } from "react"
import { APP_VERSION } from "../constants"
import DownloadModalSelecter from "./downloadModalSelecter"

Expand All @@ -13,10 +13,7 @@ const modeAvailables: Record<OsType, ModeType[]> = {
Linux: ["GPU / CPU", "CPU"],
}

const packageAvailables: Record<
OsType,
Partial<Record<ModeType, PackageType[]>>
> = {
const packageAvailables = {
Windows: {
"GPU / CPU": ["インストーラー", "Zip"],
CPU: ["インストーラー", "Zip"],
Expand All @@ -26,7 +23,7 @@ const packageAvailables: Record<
"CPU (Apple)": ["インストーラー", "Zip"],
},
Linux: { "GPU / CPU": ["インストーラー"], CPU: ["インストーラー", "tar.gz"] },
}
} as const satisfies Record<OsType, Partial<Record<ModeType, PackageType[]>>>

export const DownloadModal: React.FC<{
isActive: boolean
Expand Down Expand Up @@ -136,6 +133,34 @@ export const DownloadModal: React.FC<{
? selectedPackage
: packageAvailables[selectedOs][selectedOrDefaultMode]![0]

useEffect(() => {
const userAgent = window.navigator.userAgent
if (userAgent.includes("Windows")) {
selectOs("Windows")
} else if (userAgent.includes("Mac")) {
selectOs("Mac")
} else if (userAgent.includes("Linux")) {
selectOs("Linux")
}
}, [])

const selectOs = (os: OsType) => {
setSelectedOs(os)
// 変更先のOSで選択できないモードの場合、最初のモードを選択する
selectMode(
os,
modeAvailables[os].includes(selectedMode)
? selectedMode
: modeAvailables[os][0]
)
}
const selectMode = (os: OsType, mode: ModeType) => {
setSelectedMode(mode)
if (!packageAvailables[os][mode]!.includes(selectedPackage)) {
setSelectedPackage(packageAvailables[os][mode]![0])
}
}

return (
<div
className={"modal-download modal" + (props.isActive ? " is-active" : "")}
Expand All @@ -160,7 +185,7 @@ export const DownloadModal: React.FC<{
<DownloadModalSelecter
label="OS"
selected={selectedOs}
setSelected={setSelectedOs}
setSelected={selectOs}
candidates={["Windows", "Mac", "Linux"]}
/>

Expand All @@ -169,7 +194,7 @@ export const DownloadModal: React.FC<{
<DownloadModalSelecter
label="対応モード"
selected={selectedOrDefaultMode}
setSelected={setSelectedMode}
setSelected={mode => selectMode(selectedOs, mode)}
candidates={modeAvailables[selectedOs]}
/>
<p className="has-text-centered is-size-7">
Expand Down
Loading