Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion chuchi-core/src/api/ApiError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,16 @@ export default class ApiError {
static fromJson(obj: any): ApiError {
if (typeof obj === 'string') return new ApiError(obj, null);

if (!obj || typeof obj !== 'object')
return ApiError.newOther('unexpected error response: ' + obj);

// {kind, data}
if ('kind' in obj) return new ApiError(obj.kind, obj?.data ?? null);

// {'kind': data}
const [kind, data] = Object.entries(obj)[0];
const entry = Object.entries(obj)[0];
if (!entry) return ApiError.newOther('empty error response');
const [kind, data] = entry;
return new ApiError(kind, data);
}

Expand Down
6 changes: 2 additions & 4 deletions chuchi-legacy/src/dom/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ export function inViewY(el: Element) {
const wHeight = window.innerHeight;
const { top, bottom } = el.getBoundingClientRect();

const topInView = top >= 0 && top <= wHeight;
const bottomInView = bottom >= 0 && bottom <= wHeight;

return topInView || bottomInView;
// also true if the element is taller than the viewport and covers it
return top <= wHeight && bottom >= 0;
}

// name --my-var
Expand Down
3 changes: 2 additions & 1 deletion chuchi-utils/src/collections/SlotArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export default class SlotArray<T> {
* @param id - The index to remove the item from.
*/
remove(id: number) {
if (this.inner.length <= id) return;
// removing twice would add the slot twice to free
if (this.inner.length <= id || this.inner[id] === null) return;

this.inner[id] = null;
this.free.push(id);
Expand Down
10 changes: 4 additions & 6 deletions chuchi-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,12 @@ export function randomToken(length: number = 8): string {
* @param step - The step size between values, defaults to 1.
* @returns The generated array.
*/
// todo improve this function
export function range(start: number, end: number, step: number = 1): number[] {
const len = end - start / step;
// preallocating is a lot faster than push for bigger ranges
const len = step > 0 ? Math.max(0, Math.ceil((end - start) / step)) : 0;
const ar = new Array(len);
let c = 0;
for (let i = start; i < end; i += step) {
ar[c] = i;
c += 1;
for (let c = 0; c < len; c++) {
ar[c] = start + c * step;
}
return ar;
}
Expand Down
5 changes: 1 addition & 4 deletions chuchi-utils/src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function searchScore(search: string, val: string): number {
if (i === -1) return 0;

const distLeft = searchSpaceLeft(i, val);
const distRight = searchSpaceLeft(i + search.length, val);
const distRight = searchSpaceRight(i + search.length - 1, val);

return 1 + distLeft + distRight;
}
Expand All @@ -35,9 +35,6 @@ function searchSpaceLeft(idx: number, val: string): number {
}

// calculate distance to space right of
// todo is this used???
// @ts-ignore
/* eslint-disable @typescript-eslint/no-unused-vars */
function searchSpaceRight(idx: number, val: string): number {
let dist = 0;
while (idx < val.length - 1) {
Expand Down
22 changes: 18 additions & 4 deletions chuchi/src/routing/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export default class Router<C = any> {
*/
pushReq(req: Request): void {
this.currentRequest.setSilent(req);
window.history.pushState(req.toHistoryState(), '', req.uri);
window.history.pushState(req.toHistoryState(), '', historyUrl(req));
this.currentRequest.notify();
}

Expand All @@ -174,7 +174,7 @@ export default class Router<C = any> {
*/
replaceReq(req: Request): void {
this.currentRequest.setSilent(req);
window.history.replaceState(req.toHistoryState(), '', req.uri);
window.history.replaceState(req.toHistoryState(), '', historyUrl(req));
this.currentRequest.notify();
}

Expand Down Expand Up @@ -273,9 +273,13 @@ export default class Router<C = any> {
}

if (history === 'replace') {
window.history.replaceState(req.toHistoryState(), '', req.uri);
window.history.replaceState(
req.toHistoryState(),
'',
historyUrl(req),
);
} else if (history === 'push') {
window.history.pushState(req.toHistoryState(), '', nUri);
window.history.pushState(req.toHistoryState(), '', historyUrl(req));
}

this.currentRequest.set(req);
Expand Down Expand Up @@ -403,6 +407,16 @@ export default class Router<C = any> {
}
}

/**
* Returns the url to store in the history
*
* The uri alone is not enough, a path starting with // would be read as
* another host and pushState would throw
*/
function historyUrl(req: Request): string {
return window.location.origin + req.uri;
}

/**
* Returns true if the click should be intercepted
*/
Expand Down
Loading