Skip to content

Commit 4cc8fd6

Browse files
authored
Merge pull request #496 from narumincho/dev-next
一部 Next.js Vercel に移植
2 parents 2b8e4ac + a08f90d commit 4cc8fd6

29 files changed

+2509
-182
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ node_modules/
2020
/core/**/**.js
2121
/.next/
2222
tsconfig.tsbuildinfo
23+
/.vercel/
File renamed without changes.

static/icon.png assets/icon.png

File renamed without changes.

client/hook/useDefinyApp.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import * as d from "../../localData";
22
import * as indexedDB from "../indexedDB";
33
import { AddMessage, useNotification } from "./useNotification";
44
import { ReactElement, useCallback, useEffect, useMemo, useState } from "react";
5-
import { locationAndLanguageToUrl, urlToUrlData } from "../../common/url";
65
import type { TypePartIdAndMessage } from "../../core/TypePartIdAndMessage";
76
import { api } from "../api";
87
import { generateTypeScriptCode } from "../../core/main";
98
import { jsTs } from "../../gen/main";
9+
import { urlToUrlData } from "../../common/url";
1010
import { useResourceState } from "./resourceState";
1111
import { useTypePartIdListInProject } from "./typePartIdListInProject";
1212

@@ -253,21 +253,16 @@ export const useDefinyApp = (): UseDefinyAppResult => {
253253
*/
254254
const jump = useCallback(
255255
(newLocationAndLanguage: d.LocationAndLanguage): void => {
256-
window.history.pushState(
257-
undefined,
258-
"",
259-
locationAndLanguageToUrl(newLocationAndLanguage).toString()
256+
console.log(
257+
JSON.stringify(newLocationAndLanguage) + "にjump したいようだ"
260258
);
261-
setLocationAndLanguage(newLocationAndLanguage);
262259
},
263260
[]
264261
);
265262

266263
const replace = (newLocationAndLanguage: d.LocationAndLanguage): void => {
267-
window.history.replaceState(
268-
undefined,
269-
"",
270-
locationAndLanguageToUrl(newLocationAndLanguage).toString()
264+
console.log(
265+
JSON.stringify(newLocationAndLanguage) + "にURLを変えたいようだ"
271266
);
272267
setLocationAndLanguage(newLocationAndLanguage);
273268
};

common/url.ts

+64-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import * as d from "../localData";
22
import * as s from "../staticResource";
3-
import { structuredUrlToUrl, urlToStructuredUrl } from "../gen/url/main";
3+
import {
4+
StructuredUrl,
5+
structuredUrlToNodeUrlObject,
6+
structuredUrlToUrl,
7+
urlToStructuredUrl,
8+
} from "../gen/url/main";
9+
import type { UrlObject } from "url";
410
import { clientScriptPath } from "../clientScriptHash";
511

6-
const languageQueryKey = "hl";
12+
export const languageQueryKey = "hl";
713
export const defaultLanguage: d.Language = "English";
814

915
const origin =
@@ -19,23 +25,64 @@ export const locationAndLanguageToUrl = (
1925
searchParams: new Map<string, string>([
2026
[
2127
languageQueryKey,
22-
dataLanguageToHlQueryValue(locationAndLanguage.language),
28+
dataLanguageToQueryValue(locationAndLanguage.language),
2329
],
2430
]),
2531
});
2632
};
2733

28-
const dataLanguageToHlQueryValue = (language: d.Language): string => {
34+
export const locationAndLanguageToStructuredUrl = (
35+
locationAndLanguage: d.LocationAndLanguage
36+
): StructuredUrl => {
37+
return {
38+
path: locationToPathList(locationAndLanguage.location),
39+
searchParams: new Map<string, string>([
40+
[
41+
languageQueryKey,
42+
dataLanguageToQueryValue(locationAndLanguage.language),
43+
],
44+
]),
45+
};
46+
};
47+
48+
export const locationAndLanguageToNodeUrlObject = (
49+
locationAndLanguage: d.LocationAndLanguage
50+
): UrlObject => {
51+
return structuredUrlToNodeUrlObject(
52+
locationAndLanguageToStructuredUrl(locationAndLanguage)
53+
);
54+
};
55+
56+
export const dataLanguageToQueryValue = (language: d.Language): string => {
2957
switch (language) {
3058
case "English":
31-
return "en";
59+
return englishId;
3260
case "Japanese":
33-
return "ja";
61+
return japaneseId;
3462
case "Esperanto":
35-
return "eo";
63+
return esperantoId;
3664
}
3765
};
3866

67+
export const queryValueToDataLanguage = (language: string): d.Language => {
68+
switch (language) {
69+
case englishId:
70+
return "English";
71+
case japaneseId:
72+
return "Japanese";
73+
case esperantoId:
74+
return "Esperanto";
75+
}
76+
return defaultLanguage;
77+
};
78+
79+
export const isValidLanguageQueryValue = (value: unknown): boolean => {
80+
if (typeof value !== "string") {
81+
return false;
82+
}
83+
return new Set([englishId, japaneseId, esperantoId]).has(value);
84+
};
85+
3986
const locationToPathList = (location: d.Location): ReadonlyArray<string> => {
4087
switch (location._) {
4188
case "Home":
@@ -76,7 +123,9 @@ const toolToPath = (toolName: d.ToolName): string => {
76123
* URLのパスを場所のデータに変換する
77124
* @param url `https://definy.app/project/580d8d6a54cf43e4452a0bba6694a4ed?hl=ja` のようなURL
78125
*/
79-
export const urlToUrlData = (url: URL): d.UrlData => {
126+
export const urlToUrlData = (
127+
url: Readonly<Pick<URL, "pathname" | "search">>
128+
): d.UrlData => {
80129
const structuredUrl = urlToStructuredUrl(url.pathname, url.search);
81130
if (
82131
structuredUrl.path[0] === "logInCallback" &&
@@ -153,11 +202,11 @@ const locationFromUrl = (path: ReadonlyArray<string>): d.Location => {
153202

154203
const languageFromIdString = (languageAsString: string): d.Language => {
155204
switch (languageAsString) {
156-
case "ja":
205+
case japaneseId:
157206
return "Japanese";
158-
case "en":
207+
case englishId:
159208
return "English";
160-
case "eo":
209+
case esperantoId:
161210
return "Esperanto";
162211
}
163212
return defaultLanguage;
@@ -191,3 +240,7 @@ const localProjectPath = "local-project";
191240
const toolPath = "tool";
192241
const toolThemeColorRainbow = "theme-color-rainbow";
193242
const toolSoundQuiz = "sound-quiz";
243+
244+
const englishId = "en";
245+
const esperantoId = "eo";
246+
const japaneseId = "ja";

0 commit comments

Comments
 (0)