mirror of
https://github.com/priyanshujain/margin.git
synced 2026-10-02 11:07:04 +00:00
feat(library): open example as a saved copy, hide it once a book exists
This commit is contained in:
1 parent
4410b2037f
commit
8b9a7081f7
1 file changed
+24
-5
@@ -7,14 +7,31 @@ import { Icon } from "./Icon";
|
|||||||
|
|
||||||
export function Library({ onOpen }: { onOpen: (book: Book) => void }) {
|
export function Library({ onOpen }: { onOpen: (book: Book) => void }) {
|
||||||
const [books, setBooks] = useState<BookSummary[]>([]);
|
const [books, setBooks] = useState<BookSummary[]>([]);
|
||||||
|
const [loaded, setLoaded] = useState(false);
|
||||||
const [busy, setBusy] = useState(false);
|
const [busy, setBusy] = useState(false);
|
||||||
const [notice, setNotice] = useState<string | null>(null);
|
const [notice, setNotice] = useState<string | null>(null);
|
||||||
|
|
||||||
const refresh = () => listBooks().then(setBooks).catch(() => setBooks([]));
|
const refresh = () =>
|
||||||
|
listBooks()
|
||||||
|
.then(setBooks)
|
||||||
|
.catch(() => setBooks([]))
|
||||||
|
.finally(() => setLoaded(true));
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
refresh();
|
refresh();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const handleExample = async () => {
|
||||||
|
const copy = exampleBook();
|
||||||
|
if (isDesktop) {
|
||||||
|
try {
|
||||||
|
await saveBook(copy);
|
||||||
|
} catch (e) {
|
||||||
|
setNotice(`Could not save copy: ${e}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
onOpen(copy);
|
||||||
|
};
|
||||||
|
|
||||||
const removeBook = async (e: MouseEvent, id: string) => {
|
const removeBook = async (e: MouseEvent, id: string) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
await deleteBook(id);
|
await deleteBook(id);
|
||||||
@@ -51,10 +68,12 @@ export function Library({ onOpen }: { onOpen: (book: Book) => void }) {
|
|||||||
<Icon d="M12 3v10m0 0l-4-4m4 4l4-4M5 19h14" size={20} />
|
<Icon d="M12 3v10m0 0l-4-4m4 4l4-4M5 19h14" size={20} />
|
||||||
<span>{busy ? "Importing…" : "Import EPUB"}</span>
|
<span>{busy ? "Importing…" : "Import EPUB"}</span>
|
||||||
</button>
|
</button>
|
||||||
<button className="card card-example" onClick={() => onOpen(exampleBook())}>
|
{loaded && books.length === 0 && (
|
||||||
<span className="card-title">The Lighthouse</span>
|
<button className="card card-example" onClick={handleExample}>
|
||||||
<span className="card-badge">Example</span>
|
<span className="card-title">The Lighthouse</span>
|
||||||
</button>
|
<span className="card-badge">Example</span>
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
{books.map((b) => (
|
{books.map((b) => (
|
||||||
<div key={b.id} className="card card-book" onClick={() => loadBook(b.id).then(onOpen)}>
|
<div key={b.id} className="card card-book" onClick={() => loadBook(b.id).then(onOpen)}>
|
||||||
<span className="card-title">{b.title || "Untitled"}</span>
|
<span className="card-title">{b.title || "Untitled"}</span>
|
||||||
|
|||||||
Reference in new issue
Block a user