aboutsummaryrefslogtreecommitdiff
path: root/src/lib.ts
blob: c284de4b1ef8417df35e79777eee265bee332b47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Project-wide globals
 */

import {TolNode} from './tol';

// ========== For server requests ==========

const SERVER_DATA_URL = (new URL(window.location.href)).origin + '/data/'
const SERVER_IMG_PATH = '/tol_data/img/'

export async function queryServer(params: URLSearchParams){
	// Construct URL
	const url = new URL(SERVER_DATA_URL);
	url.search = params.toString();
	// Query server
	let responseObj;
	try {
		const response = await fetch(url.toString());
		responseObj = await response.json();
	} catch (error){
		console.log(`Error with querying ${url.toString()}: ${error}`);
		return null;
	}
	return responseObj;
}

export function getImagePath(imgName: string): string {
	return SERVER_IMG_PATH + imgName.replaceAll('\'', '\\\'');
}

// ========== For server responses (matches backend/tilo.py) ==========

// Represents a search-string suggestion
export type SearchSugg = {
	name: string,
	canonicalName: string | null,
	pop: number,
};

// Holds search suggestions and an indication of if there was more
export type SearchSuggResponse = {
	suggs: SearchSugg[],
	hasMore: boolean,
};

export type DescInfo = {
	text: string,
	wikiId: number,
	fromRedirect: boolean,
	fromDbp: boolean,
};

export type ImgInfo = {
	id: number,
	src: string,
	url: string,
	license: string,
	artist: string,
	credit: string,
};

export type NodeInfo = {
	tolNode: TolNode,
	descInfo: null | DescInfo,
	imgInfo: null | ImgInfo,
};

export type InfoResponse = {
	nodeInfo: NodeInfo,
	subNodesInfo: [] | [NodeInfo | null, NodeInfo | null],
};

// ========== Used by auto-mode and tutorial-pane ==========

export type Action =
	'expand' | 'collapse' | 'expandToView' | 'unhideAncestor' |
	'tileInfo' | 'search' | 'autoMode' | 'settings' | 'help';