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

refactor(web): continue TypeScript migration #1857

Merged
merged 31 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
767b8d7
refactor(web): migrate core/EmailInput to TypeScript
dgdavid Dec 19, 2024
7a412fd
refactor(web): migrate src/client to TypeScript
dgdavid Dec 23, 2024
77cf091
refactor(web): migrate contexts to TypeScript
dgdavid Dec 26, 2024
ea242a3
refactor(web): migrate src/Protected to TypeScript
dgdavid Dec 26, 2024
760a780
refactor(web): migrate i18n to TypeScript
dgdavid Dec 26, 2024
1e777ec
refactor(web): migrate src/agama to TypeScript
dgdavid Dec 26, 2024
fd989ff
refactor(web): migrate src/utils to TypeScript
dgdavid Dec 27, 2024
5c38fc8
refactor(web): migrate test-utils to TypeScript
dgdavid Dec 27, 2024
7ecdd9e
refactor utils
dgdavid Dec 27, 2024
4f8dcdb
fix(web): adjust some types in ZFCP components
dgdavid Dec 27, 2024
8c63e13
fix(web): type adjustments
dgdavid Dec 27, 2024
b6e8984
fix(web): please ESLint
dgdavid Dec 27, 2024
02b4a4b
refactor(web): migrate router to TypeScript
dgdavid Dec 27, 2024
96f3db6
refactor(web): migrate src/index to TypeScript
dgdavid Dec 27, 2024
6ccb663
refactor(web): migrate setupTests to TypeScript
dgdavid Dec 27, 2024
815415d
refactor(web): migrate hooks to TypeScript
dgdavid Dec 30, 2024
c1684a8
refactor(web): migrate user utils to TypeScript
dgdavid Dec 30, 2024
e97c575
refactor(web): move index files to TypeScript
dgdavid Jan 2, 2025
62e1b04
refactor(web): migrate core/RowActions to TypeScript
dgdavid Jan 2, 2025
aea35db
refactor(web): migrate core/NumericTextInput to TypeScript
dgdavid Jan 2, 2025
b01c27e
fix(web): please ESLint after migrating NumericTextInput
dgdavid Jan 2, 2025
ef15c76
refactor(web): migrate core/ListSearch to TypeScript
dgdavid Jan 2, 2025
7ec3d97
refactor(web): drop core/SectionSkeleton
dgdavid Jan 2, 2025
dfa51aa
refactor(web): migratre layout/Center to TypeScript
dgdavid Jan 2, 2025
8876cf5
refactor(web): migrate to core/IssuesHint to TypeScript
dgdavid Jan 2, 2025
39bdcb6
refactor(web): migrate L10n page and sections to TypeScript
dgdavid Jan 2, 2025
0bb51fd
refactor(web): migrate core/ProgressText to TypeScript
dgdavid Jan 2, 2025
d6815ce
refactor(web): migrate ProductSelectionProgress to TypeScript
dgdavid Jan 2, 2025
b1f89d3
fix(web): improve useNodeSiblings type readbility
dgdavid Jan 3, 2025
6d803cc
fix(web): stop using translations in test files
dgdavid Jan 3, 2025
6ecc316
fix(web): revert copyright miss-updated
dgdavid Jan 3, 2025
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
2 changes: 1 addition & 1 deletion web/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ module.exports = {

// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],
setupFilesAfterEnv: ["<rootDir>/src/setupTests.js"],
setupFilesAfterEnv: ["<rootDir>/src/setupTests.ts"],

// The number of seconds after which a test is considered as slow and reported as such in the results.
// slowTestThreshold: 5,
Expand Down
File renamed without changes.
99 changes: 0 additions & 99 deletions web/src/agama.js

This file was deleted.

99 changes: 99 additions & 0 deletions web/src/agama.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) [2024] SUSE LLC
*
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, contact SUSE LLC.
*
* To contact SUSE LLC about this file by physical or electronic mail, you may
* find current contact information at www.suse.com.
*/

/**
* This module provides a global "agama" object which can be use from other
* scripts like "po.js".
*/

// mapping with the current translations
let translations = {};
// function used for computing the plural form index
let plural_fn: (n: number) => boolean;

const agama = {
// the current language
language: "en",

// set the current translations, called from po.<lang>.js
locale: (po) => {
if (po) {
Object.assign(translations, po);

const header = po[""];
if (header) {
if (header["plural-forms"]) plural_fn = header["plural-forms"];
if (header.language) agama.language = header.language;
}
} else if (po === null) {
translations = {};
plural_fn = undefined;
agama.language = "en";
}
},

/**
* Get a translation for a singular text
* @param str input text
* @return translated text or the original text if the translation is not found
*/
gettext: (str: string): string => {
if (translations) {
const translated = translations[str];
if (translated?.[0]) return translated[0];
}

// fallback, return the original text
return str;
},

/**
* get a translation for a plural text
* @param str1 input singular text
* @param strN input plural text
* @param n the actual number which decides whether to use the
* singular or plural form (of which plural form if there are several of them)
* @return translated text or the original text if the translation is not found
*/
ngettext: (str1: string, strN: string, n: number) => {
if (translations && plural_fn) {
// plural form translations are indexed by the singular variant
const translation = translations[str1];

if (translation) {
const plural_index = plural_fn(n);

// the plural function either returns direct index (integer) in the plural
// translations or a boolean indicating simple plural form which
// needs to be converted to index 0 (singular) or 1 (plural)
const index = plural_index === true ? 1 : plural_index || 0;

if (translation[index]) return translation[index];
}
}

// fallback, return the original text
return n === 1 ? str1 : strN;
},
};

export default agama;
50 changes: 31 additions & 19 deletions web/src/client/index.js → web/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) [2021-2023] SUSE LLC
* Copyright (c) [2021-2024] SUSE LLC
*
* All Rights Reserved.
*
Expand All @@ -20,28 +20,40 @@
* find current contact information at www.suse.com.
*/

// @ts-check

import { WSClient } from "./ws";

/**
* @typedef {object} InstallerClient
* @property {() => boolean} isConnected - determines whether the client is connected
* @property {() => boolean} isRecoverable - determines whether the client is recoverable after disconnected
* @property {(handler: () => void) => (() => void)} onConnect - registers a handler to run
* @property {(handler: () => void) => (() => void)} onDisconnect - registers a handler to run
* when the connection is lost. It returns a function to deregister the
* handler.
* @property {(handler: (any) => void) => (() => void)} onEvent - registers a handler to run on events
*/
type VoidFn = () => void;
type BooleanFn = () => boolean;
type EventHandlerFn = (event) => void;

export type InstallerClient = {
/** Whether the client is connected. */
isConnected: BooleanFn;
/** Whether the client is recoverable after disconnecting. */
isRecoverable: BooleanFn;
/**
* Registers a handler to run when connection is set. It returns a function
* for deregistering the handler.
*/
onConnect: (handler: VoidFn) => VoidFn;
/**
* Registers a handler to run when connection is lost. It returns a function
* for deregistering the handler.
*/
onDisconnect: (handler: VoidFn) => VoidFn;
/**
* Registers a handler to run on events. It returns a function for
* deregistering the handler.
*/
onEvent: (handler: EventHandlerFn) => VoidFn;
};

/**
* Creates the Agama client
*
* @param {URL} url - URL of the HTTP API.
* @return {InstallerClient}
* @param url - URL of the HTTP API.
*/
const createClient = (url) => {
const createClient = (url: URL): InstallerClient => {
url.hash = "";
url.pathname = url.pathname.concat("api/ws");
url.protocol = url.protocol === "http:" ? "ws" : "wss";
Expand All @@ -53,9 +65,9 @@ const createClient = (url) => {
return {
isConnected,
isRecoverable,
onConnect: (handler) => ws.onOpen(handler),
onDisconnect: (handler) => ws.onClose(handler),
onEvent: (handler) => ws.onEvent(handler),
onConnect: (handler: VoidFn) => ws.onOpen(handler),
onDisconnect: (handler: VoidFn) => ws.onClose(handler),
onEvent: (handler: EventHandlerFn) => ws.onEvent(handler),
};
};

Expand Down
Loading
Loading