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(web): start using TypeScript #1456

Merged
merged 5 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
52 changes: 24 additions & 28 deletions web/src/queries/l10n.js → web/src/queries/l10n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { timezoneUTCOffset } from "~/utils";
const configQuery = () => {
return {
queryKey: ["l10n/config"],
queryFn: () => fetch("/api/l10n/config").then((res) => res.json()),
queryFn: () => fetch("/api/l10n/config").then(res => res.json())
};
};

Expand All @@ -39,10 +39,10 @@ const configQuery = () => {
*/
const localesQuery = () => ({
queryKey: ["l10n/locales"],
queryFn: async () => {
queryFn: async (): Promise<Locale[]> => {
const response = await fetch("/api/l10n/locales");
const locales = await response.json();
return locales.map(({ id, language, territory }) => {
return locales.map(({ id, language, territory }): Locale => {
return { id, name: language, territory };
});
},
Expand All @@ -54,10 +54,10 @@ const localesQuery = () => ({
*/
const timezonesQuery = () => ({
queryKey: ["l10n/timezones"],
queryFn: async () => {
queryFn: async (): Promise<Timezone[]> => {
const response = await fetch("/api/l10n/timezones");
const timezones = await response.json();
return timezones.map(({ code, parts, country }) => {
return timezones.map(({ code, parts, country }): Timezone => {
const offset = timezoneUTCOffset(code);
return { id: code, parts, country, utcOffset: offset };
});
Expand All @@ -70,13 +70,13 @@ const timezonesQuery = () => ({
*/
const keymapsQuery = () => ({
queryKey: ["l10n/keymaps"],
queryFn: async () => {
queryFn: async (): Promise<Keymap[]> => {
const response = await fetch("/api/l10n/keymaps");
const json = await response.json();
const keymaps = json.map(({ id, description }) => {
const keymaps = json.map(({ id, description }): Keymap => {
return { id, name: description };
});
return keymaps.sort((a, b) => (a.name < b.name) ? -1 : 1);
return keymaps.sort((a, b) => (a.name < b.name ? -1 : 1));
},
staleTime: Infinity
});
Expand All @@ -88,13 +88,13 @@ const keymapsQuery = () => ({
*/
const useConfigMutation = () => {
const query = {
mutationFn: (newConfig) =>
mutationFn: newConfig =>
fetch("/api/l10n/config", {
method: "PATCH",
body: JSON.stringify(newConfig),
headers: {
"Content-Type": "application/json",
},
"Content-Type": "application/json"
}
})
};
return useMutation(query);
Expand Down Expand Up @@ -123,26 +123,22 @@ const useL10nConfigChanges = () => {

/// Returns the l10n data.
const useL10n = () => {
const [
{ data: config },
{ data: locales },
{ data: keymaps },
{ data: timezones }
] = useSuspenseQueries({
queries: [
configQuery(),
localesQuery(),
keymapsQuery(),
timezonesQuery()
]
});
const [{ data: config }, { data: locales }, { data: keymaps }, { data: timezones }] =
useSuspenseQueries({
queries: [configQuery(), localesQuery(), keymapsQuery(), timezonesQuery()]
});
Comment on lines -126 to +129
Copy link
Contributor Author

@dgdavid dgdavid Jul 12, 2024

Choose a reason for hiding this comment

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

Ouch, I didn't realize I sent these (and others) format changes made by Zed editor which I'm testing. Will revert them in a new commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After a short discussion with @imobachgs, I will keep it as it is because we're gonna format all the code base for consistence.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

BTW, the format change was not because Zed but because Zed applying the rules we have at .prettierrc file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we're gonna format all the code base for consistence.

Done at #1460


const selectedLocale = locales.find((l) => l.id === config.locales[0]);
const selectedKeymap = keymaps.find((k) => k.id === config.keymap);
const selectedTimezone = timezones.find((t) => t.id === config.timezone);
const selectedLocale = locales.find(l => l.id === config.locales[0]);
const selectedKeymap = keymaps.find(k => k.id === config.keymap);
const selectedTimezone = timezones.find(t => t.id === config.timezone);

return {
locales, keymaps, timezones, selectedLocale, selectedKeymap, selectedTimezone
locales,
keymaps,
timezones,
selectedLocale,
selectedKeymap,
selectedTimezone
};
};

Expand Down
44 changes: 44 additions & 0 deletions web/src/types/l10n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
type Keymap = {
/**
* Keyboard id (e.g., "us").
*/
id: string;
/**
* Keyboard name (e.g., "English (US)").
*/
name: string;
};

type Locale = {
/**
* Language id (e.g., "en_US.UTF-8").
*/
id: string;
/**
* Language name (e.g., "English").
*/
name: string;
/**
* Territory name (e.g., "United States").
*/
territory: string;
};

type Timezone = {
/**
* Timezone id (e.g., "Atlantic/Canary").
*/
id: string;
/**
* Name of the timezone parts (e.g., ["Atlantic", "Canary"]).
*/
parts: string[];
/**
* Name of the country associated to the zone or empty string (e.g., "Spain").
*/
country: string;
/**
* UTC offset.
*/
utcOffset: number;
};
Loading