Skip to content

Commit

Permalink
Fix develop changelog parsing and DRY it
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Telatynski <[email protected]>
  • Loading branch information
t3chguy committed Oct 18, 2024
1 parent eedeb2c commit f3c0909
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
30 changes: 22 additions & 8 deletions src/components/views/dialogs/ChangelogDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ interface Commit {

const REPOS = ["element-hq/element-web", "matrix-org/matrix-js-sdk"] as const;

/*
* Parse a version string is compatible with the Changelog dialog ([element-version]-js-[js-sdk-version])
*/
export function parseVersion(version: string): Record<(typeof REPOS)[number], string> | null {
const parts = version.split("-");
if (parts.length === 5 && parts[1] === "js") {
const obj: Record<string, string> = {};
for (let i = 0; i < REPOS.length; i++) {
const commit = parts[2 * i];
obj[REPOS[i]] = commit;
}
return obj;
}
return null;
}

export default class ChangelogDialog extends React.Component<IProps, State> {
public constructor(props: IProps) {
super(props);
Expand All @@ -58,14 +74,12 @@ export default class ChangelogDialog extends React.Component<IProps, State> {
}

public componentDidMount(): void {
const version = this.props.newVersion.split("-");
const version2 = this.props.version.split("-");
if (version == null || version2 == null) return;
// parse versions of form: [vectorversion]-react-[react-sdk-version]-js-[js-sdk-version]
for (let i = 0; i < REPOS.length; i++) {
const oldVersion = version2[2 * i];
const newVersion = version[2 * i];
this.fetchChanges(REPOS[i], oldVersion, newVersion);
const commits = parseVersion(this.props.version);
const newCommits = parseVersion(this.props.newVersion);
if (commits == null || newCommits == null) return;

for (const repo of REPOS) {
this.fetchChanges(repo, commits[repo], newCommits[repo]);
}
}

Expand Down
13 changes: 2 additions & 11 deletions src/toasts/UpdateToast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,12 @@ import SdkConfig from "../SdkConfig";
import GenericToast from "../components/views/toasts/GenericToast";
import ToastStore from "../stores/ToastStore";
import QuestionDialog from "../components/views/dialogs/QuestionDialog";
import ChangelogDialog from "../components/views/dialogs/ChangelogDialog";
import ChangelogDialog, { parseVersion } from "../components/views/dialogs/ChangelogDialog";
import PlatformPeg from "../PlatformPeg";
import Modal from "../Modal";

const TOAST_KEY = "update";

/*
* Check a version string is compatible with the Changelog
* dialog ([element-version]-react-[react-sdk-version]-js-[js-sdk-version])
*/
function checkVersion(ver: string): boolean {
const parts = ver.split("-");
return parts.length === 5 && parts[1] === "react" && parts[3] === "js";
}

function installUpdate(): void {
PlatformPeg.get()?.installUpdate();
}
Expand All @@ -52,7 +43,7 @@ export const showToast = (version: string, newVersion: string, releaseNotes?: st
},
});
};
} else if (checkVersion(version) && checkVersion(newVersion)) {
} else if (parseVersion(version) && parseVersion(newVersion)) {
onAccept = () => {
Modal.createDialog(ChangelogDialog, {
version,
Expand Down

0 comments on commit f3c0909

Please sign in to comment.