Skip to content

Commit

Permalink
fix(ui): linter issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dshemin committed Oct 30, 2023
1 parent a10c6be commit 9a31b68
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
59 changes: 29 additions & 30 deletions ui/src/entities/book/model/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { combine, createEffect, createStore } from "effector";
import type { Book, Bookmark, Highlight } from "shared/api/book";
import { combine, createEffect, createStore } from "effector";
import api from "shared/api";
import { useUnit } from "effector-react";

Expand All @@ -9,12 +9,12 @@ export const $getBookListFxLoading = getBookListFx.pending;
export const getBookFx = createEffect((id: string) => api.book.get(id));
export const $getBookFxLoading = getBookFx.pending;

export type NormalizedBookHighlights = Record<number, Highlight[]>;
export type NormalizedBookBookmarks = Record<number, Bookmark>;
export type NormalizedHighlights = Record<number, Highlight[]>;
export type NormalizedBookmarks = Record<number, Bookmark>;

export type NormalizedBook = Omit<Book, "highlights" | "bookmarks"> & {
highlights: NormalizedBookHighlights,
bookmarks: NormalizedBookBookmarks,
export type NormalizedBook = Omit<Book, "bookmarks" | "highlights"> & {
highlights: NormalizedHighlights,
bookmarks: NormalizedBookmarks,
}

type BooksState = Record<string, NormalizedBook>;
Expand All @@ -40,34 +40,33 @@ const normalizeBook = (book: Book): NormalizedBook => {
title: book.title,
uri: book.uri,
tags: book.tags,
highlights: normalizeBookHighlights(book.highlights),
bookmarks: normalizeBookBookmarks(book.bookmarks),
}
}
highlights: normalizeHighlights(book.highlights),
bookmarks: normalizeBookmarks(book.bookmarks),
};
};

const normalizeBookHighlights = (highlights: Highlight[]): NormalizedBookHighlights =>
highlights.reduce(
(prev: NormalizedBookHighlights, curr: Highlight) => {
const key = curr.page;
const normalizeHighlights = (highlights: Highlight[]): NormalizedHighlights => highlights.reduce(
(prev: NormalizedHighlights, curr: Highlight) => {
const key = curr.page;

if (!prev[key]) {
prev[key] = [];
}
if (!prev[key]) {
prev[key] = [];
}

prev[key].push(curr);
return prev;
},
{},
);
prev[key].push(curr);

const normalizeBookBookmarks = (bookmarks: Bookmark[]): NormalizedBookBookmarks =>
bookmarks.reduce(
(prev: NormalizedBookBookmarks, curr: Bookmark) => ({
...prev,
[curr.page]: curr,
}),
{},
);
return prev;
},
{},
);

const normalizeBookmarks = (bookmarks: Bookmark[]): NormalizedBookmarks => bookmarks.reduce(
(prev: NormalizedBookmarks, curr: Bookmark) => ({
...prev,
[curr.page]: curr,
}),
{},
);

export const $booksList = combine($books, (books) => Object.values(books));
export const $booksListEmpty = $booksList.map((list) => list.length === 0);
Expand Down
21 changes: 11 additions & 10 deletions ui/src/entities/book/ui/book-card/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Card, Pagination, PaginationProps } from "antd";
import { Document, Page, PageProps, pdfjs } from "react-pdf";
import { useCallback, useEffect, useState } from "react";
import { EffectCallback, useCallback, useEffect, useState } from "react";
import { NormalizedBook } from "entities/book/model";

export interface BookCardProps {
Expand Down Expand Up @@ -33,23 +33,24 @@ export const BookCard: React.FC<BookCardProps> = ({ book, isLoading }) => {

useEffect(() => {
const event = "click";
const handler = (ev: MouseEvent) => {
//@ts-ignore
const handler = (ev: MouseEvent): EffectCallbackReturn => {
//@ts-expect-error 'cause we didn't have enough types.
if (!ev || !ev.target || ev.target.localName !== "mark") {
return;
}

//@ts-ignore
//@ts-expect-error 'cause we didn't have enough types.
const index = ev.target.dataset.index;

// eslint-disable-next-line no-alert
window.alert(book?.highlights[page][index].title);
}
};
document.addEventListener(event, handler);

return () => {
document.removeEventListener(event, handler);
};
}, []);
}, [book?.highlights, page]);

const textRenderer = useCallback(
(textItem: TextItem) => {
Expand All @@ -58,8 +59,8 @@ export const BookCard: React.FC<BookCardProps> = ({ book, isLoading }) => {

book?.highlights[page]
.forEach(({ lineStart, lineEnd, symbolStart, symbolEnd }, hIndex) => {
if ((lineStart < index) && (index > lineEnd)) {
return
if (lineStart < index && index > lineEnd) {
return;
}
const before = str.substring(0, symbolStart);
const marked = str.substring(symbolStart, symbolEnd);
Expand All @@ -70,7 +71,7 @@ export const BookCard: React.FC<BookCardProps> = ({ book, isLoading }) => {

return str;
},
[],
[book?.highlights, page],
);

if (!book || isLoading) {
Expand All @@ -92,7 +93,6 @@ export const BookCard: React.FC<BookCardProps> = ({ book, isLoading }) => {
<Document
file={book.uri}
onLoadSuccess={onDocumentLoaded}
onItemClick={(t) => console.log("item", t)}
>
<Page
pageNumber={page}
Expand All @@ -105,3 +105,4 @@ export const BookCard: React.FC<BookCardProps> = ({ book, isLoading }) => {
};

type TextItem = Parameters<NonNullable<PageProps["customTextRenderer"]>>[0];
type EffectCallbackReturn = ReturnType<EffectCallback>;

0 comments on commit 9a31b68

Please sign in to comment.