Skip to content

Commit f419257

Browse files
committed
refactor(caa edits): migrate to TypeScript, partially drop jQuery
I didn't remove jQuery in the artwork comparison dialog, because it uses jquery-ui and because I'm afraid I'll break something. More jQuery removal is probably something we should do later, but I've decided against doing it in this migration pass. I also updated all dependencies.
1 parent 9b839c0 commit f419257

File tree

14 files changed

+1465
-31
lines changed

14 files changed

+1465
-31
lines changed

package-lock.json

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@
4646
"@types/greasemonkey": "4.0.3",
4747
"@types/jest": "28.1.1",
4848
"@types/jest-when": "3.5.0",
49+
"@types/jquery": "^3.5.14",
50+
"@types/jqueryui": "^1.12.16",
4951
"@types/postcss-preset-env": "6.7.3",
52+
"@types/resemblejs": "^4.1.0",
5053
"@types/rollup__plugin-virtual": "2.0.1",
5154
"@types/rollup-plugin-progress": "1.1.1",
5255
"@types/setup-polly-jest": "0.5.1",
@@ -88,6 +91,7 @@
8891
"jest-html-reporters": "3.0.9",
8992
"jest-when": "3.5.1",
9093
"license-checker": "25.0.1",
94+
"moment": "^2.29.3",
9195
"nativejsx": "github:ROpdebee/nativejsx",
9296
"node-fetch": "3.2.6",
9397
"postcss": "8.4.14",

src/lib/MB/types-api.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ export interface Place {
6969
}
7070

7171
export interface RelationshipDate {
72-
day: number | null;
73-
month: number | null;
74-
year: number | null;
72+
day?: number | null;
73+
month?: number | null;
74+
year?: number | null;
7575
}
7676

7777
interface BaseRecordingRelationship {
@@ -115,6 +115,30 @@ export interface ReleaseRecordingRels {
115115
};
116116
}
117117

118+
export interface Release {
119+
id: string;
120+
packagingID: number;
121+
statusID: number;
122+
combined_format_name?: string;
123+
barcode?: string;
124+
events?: Array<{
125+
country?: { primary_code: string };
126+
date?: RelationshipDate;
127+
}>;
128+
labels?: Array<{
129+
catalogNumber?: string;
130+
label?: {
131+
gid: string;
132+
name: string;
133+
};
134+
}>;
135+
mediums: Array<{
136+
format?: {
137+
name: string;
138+
};
139+
}>;
140+
}
141+
118142
interface BaseAPIResponse {
119143
created: string;
120144
count: number;

src/lib/MB/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ declare global {
3838
GIT_BRANCH: string;
3939
GIT_SHA: string;
4040
};
41+
$c: {
42+
user: {
43+
preferences: {
44+
datetime_format: string;
45+
};
46+
};
47+
stash: {
48+
current_language?: string;
49+
};
50+
};
4151
};
4252
}
4353
}

src/lib/util/xhr.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ export class HTTPResponseError extends ResponseError {
2424
public readonly statusCode: number;
2525
public readonly statusText: string;
2626
// eslint-disable-next-line no-restricted-globals
27-
public readonly response: GM.Response<never>;
27+
public readonly response: GM.Response<never> | Response;
2828

2929
// eslint-disable-next-line no-restricted-globals
30-
public constructor(url: string | URL, response: GM.Response<never>) {
30+
public constructor(url: string | URL, response: GM.Response<never> | Response) {
3131
/* istanbul ignore else: Should not happen */
3232
if (response.statusText.trim()) {
3333
super(url, `HTTP error ${response.status}: ${response.statusText}`);

src/mb_caa_dimensions/exports.ts

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,25 @@
22

33
import { logFailure } from '@lib/util/async';
44

5-
import type { ImageInfo } from './ImageInfo';
5+
import type { Dimensions, ImageInfo } from './ImageInfo';
66
import type { InfoCache } from './InfoCache';
77
import { CAAImageWithFullSizeURL, displayInfoWhenInView } from './DisplayedImage';
88
import { CAAImage } from './Image';
99

10-
interface LegacyImageInfo {
11-
url: string;
12-
width: number;
13-
height: number;
14-
size?: number;
15-
format?: string;
16-
}
10+
export type ROpdebee_getDimensionsWhenInView = (imgElement: HTMLImageElement) => void;
11+
export type ROpdebee_getCAAImageInfo = (imgUrl: string) => Promise<ImageInfo>;
12+
export type ROpdebee_getImageDimensions = (imgUrl: string) => Promise<Dimensions | undefined>;
1713

1814
declare global {
1915
interface Window {
20-
ROpdebee_getDimensionsWhenInView: (imgElement: HTMLImageElement) => void;
21-
ROpdebee_getCAAImageInfo: (imgUrl: string) => Promise<ImageInfo>;
22-
ROpdebee_loadImageDimensions: (imgUrl: string) => Promise<LegacyImageInfo>;
16+
ROpdebee_getDimensionsWhenInView: ROpdebee_getDimensionsWhenInView;
17+
ROpdebee_getCAAImageInfo: ROpdebee_getCAAImageInfo;
18+
ROpdebee_getImageDimensions: ROpdebee_getImageDimensions;
2319
}
2420
}
2521

2622
export function setupExports(cachePromise: Promise<InfoCache>): void {
2723
async function getCAAImageInfo(imgUrl: string): Promise<ImageInfo> {
28-
if (new URL(imgUrl).hostname !== 'archive.org') {
29-
throw new Error('Unsupported URL: Need direct image URL');
30-
}
31-
3224
const cache = await cachePromise;
3325
const image = new CAAImage(imgUrl, cache);
3426
return image.getImageInfo();
@@ -41,19 +33,14 @@ export function setupExports(cachePromise: Promise<InfoCache>): void {
4133
}), `Something went wrong when attempting to load image info for ${imgElement.src}`);
4234
}
4335

44-
async function loadImageDimensions(imgUrl: string): Promise<LegacyImageInfo> {
45-
const imageInfo = await getCAAImageInfo(imgUrl);
46-
return {
47-
url: imgUrl,
48-
...imageInfo.dimensions ?? { width: 0, height: 0 },
49-
size: imageInfo.size,
50-
format: imageInfo.fileType,
51-
};
36+
async function getImageDimensions(imgUrl: string): Promise<Dimensions | undefined> {
37+
const cache = await cachePromise;
38+
const image = new CAAImage(imgUrl, cache);
39+
return image.getDimensions();
5240
}
5341

5442
// Expose the function for use in other scripts that may load images.
5543
window.ROpdebee_getDimensionsWhenInView = getDimensionsWhenInView;
56-
// Deprecated, use `ROpdebee_getImageInfo` instead.
57-
window.ROpdebee_loadImageDimensions = loadImageDimensions;
44+
window.ROpdebee_getImageDimensions = getImageDimensions;
5845
window.ROpdebee_getCAAImageInfo = getCAAImageInfo;
5946
}

src/mb_enhanced_cover_art_uploads/providers/archive.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ import { gmxhr } from '@lib/util/xhr';
1010
import type { CoverArt } from './base';
1111
import { CoverArtProvider } from './base';
1212

13-
interface CAAIndex {
13+
// TODO: This should probably be put in lib.
14+
export interface CAAIndex {
1415
images: Array<{
1516
comment: string;
1617
types: string[];
1718
id: string | number; // Used to be string in the past, hasn't been applied retroactively yet, see CAA-129
1819
image: string;
20+
thumbnails: {
21+
'250': string;
22+
'500': string;
23+
'1200'?: string;
24+
small: string;
25+
large: string;
26+
};
1927
}>;
2028
}
2129

0 commit comments

Comments
 (0)