Skip to content

Commit

Permalink
Merge pull request #21 from cmgriffing/issue-14
Browse files Browse the repository at this point in the history
feat: seed initial data of a book with a chapter and a snippet
  • Loading branch information
cmgriffing authored Jan 25, 2024
2 parents 9b727fc + f350ca9 commit 8c406f5
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 40 deletions.
21 changes: 17 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { CreateOrUpdateModal } from "./components/CreateOrUpdateModal";
import { SettingsModal } from "./components/SettingsModal";

import {
appReady,
currentBooks,
currentChapter,
currentModel,
Expand Down Expand Up @@ -70,15 +71,28 @@ export function App() {

const [currentBook, setCurrentBook] = useState<BookWithChapters>();

const [books] = useAtom(currentBooks);
const [ready, setAppReady] = useAtom(appReady);
const [, setFetchTimestamp] = useAtom(fetchTimestamp);
const [books] = useAtom(currentBooks);
const [whisperModel] = useAtom(currentModel);

useEffect(() => {
if (!whisperModel) {
async function initialize() {
await DBUtils.seed();
startTransition(() => {
setAppReady(true);
setFetchTimestamp(Date.now());
});
}

initialize();
}, [setAppReady, setFetchTimestamp]);

useEffect(() => {
if (!whisperModel && ready) {
openSettingsModal();
}
}, [whisperModel, openSettingsModal]);
}, [whisperModel, openSettingsModal, ready]);

return (
<>
Expand Down Expand Up @@ -265,7 +279,6 @@ export function App() {
leftSection={<IconTextSize />}
onClick={(e) => {
e.stopPropagation();
console.log("hmm", node.data.book);
setCurrentBook(node.data.book);
openEditBookModal();
}}
Expand Down
8 changes: 6 additions & 2 deletions src/components/SettingsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export function SettingsModal({
const [threads, setThreads] = useState(2);

useEffect(() => {
if (settings.selectedModel) {
if (settings?.selectedModel) {
setSelectedModel(settings.selectedModel as WhisperModelName);
}

if (settings.threads) {
if (settings?.threads) {
setThreads(settings.threads);
}
}, [settings]);
Expand Down Expand Up @@ -83,6 +83,10 @@ export function SettingsModal({
return;
}

if (!settings) {
return;
}

// TODO: pass values to be saved at App level

// FOR NOW: save directly from here
Expand Down
36 changes: 14 additions & 22 deletions src/data/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import * as snippetSchema from "./models/snippets";

import { RuntimeMigrationProvider } from "./migrations/provider";

const dbFileName = "database.sqlite3";

const { driver, sql, getDatabaseFile, overwriteDatabaseFile } =
new SQLocalDrizzle("database.sqlite3");
new SQLocalDrizzle(dbFileName);

export const db = drizzle(driver, {
schema: {
Expand All @@ -20,7 +22,7 @@ export const db = drizzle(driver, {
},
});

const { dialect } = new SQLocalKysely("database.sqlite3");
const { dialect } = new SQLocalKysely(dbFileName);
const kyselyDb = new Kysely({
dialect,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -33,33 +35,23 @@ const migrator = new Migrator({

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any).resetDB = async function () {
await sql`DROP TABLE IF EXISTS books`;
await sql`DROP TABLE IF EXISTS chapters`;
await sql`DROP TABLE IF EXISTS snippets`;
await sql`DROP TABLE IF EXISTS settings`;
// await sql`DROP TABLE IF EXISTS books`;
// await sql`DROP TABLE IF EXISTS chapters`;
// await sql`DROP TABLE IF EXISTS snippets`;
// await sql`DROP TABLE IF EXISTS settings`;
await migrator.migrateDown();
await kyselyDb.destroy();
(await navigator.storage.getDirectory()).removeEntry(dbFileName);
};

// IIFE to make sure that the schema is created before the app starts
(async () => {
async function seed() {
await sql`PRAGMA foreign_keys = ON;`;

await sql`INSERT OR IGNORE INTO settings(id,user_id,threads,selected_model,created_at,modified_at)
VALUES (0,1,2,'',0,0)`;

await migrator.migrateToLatest();

// get available books
// if empty, run seed process

// seed book

// seed chapters for book

// seed snippets for chapters
})();
}

export const DBUtils = {
getDatabaseFile,
overwriteDatabaseFile,
sql,
seed,
};
56 changes: 48 additions & 8 deletions src/data/migrations/2024-01-23T23:15:28.004Z-initial-schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Kysely } from "kysely";
import { Kysely, sql } from "kysely";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function up(db: Kysely<any>): Promise<void> {
Expand Down Expand Up @@ -26,8 +26,16 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn("modified_at", "integer", (col) =>
col.defaultTo(Date.now()).notNull()
)
.addColumn("book_id", "integer", (col) => col.notNull().onDelete("cascade"))
.addForeignKeyConstraint("book_id_fk", ["book_id"], "books", ["id"])
.addColumn("book_id", "integer", (col) => col.notNull())
.addForeignKeyConstraint(
"book_id_fk",
["book_id"],
"books",
["id"],
(key) => {
return key.onDelete("cascade");
}
)
.ifNotExists()
.execute();

Expand All @@ -47,12 +55,16 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn("processed_at", "integer", (col) => col.defaultTo(0))
.addColumn("finished_at", "integer", (col) => col.defaultTo(0))
.addColumn("raw_recording_content", "text", (col) => col.defaultTo(""))
.addColumn("chapter_id", "integer", (col) =>
col.notNull().onDelete("cascade")
.addColumn("chapter_id", "integer", (col) => col.notNull())
.addForeignKeyConstraint(
"chapter_id_fk",
["chapter_id"],
"chapters",
["id"],
(key) => {
return key.onDelete("cascade");
}
)
.addForeignKeyConstraint("chapter_id_fk", ["chapter_id"], "chapters", [
"id",
])
.ifNotExists()
.execute();

Expand All @@ -70,6 +82,34 @@ export async function up(db: Kysely<any>): Promise<void> {
)
.ifNotExists()
.execute();

// Seed initial data for user

try {
await sql`
INSERT OR IGNORE INTO settings(id,user_id,threads,selected_model,created_at,modified_at)
VALUES (0,1,2,'',0,0)
`.execute(db);

await sql`
INSERT INTO books(title)
VALUES ('Introduction')
`.execute(db);

await sql`
INSERT INTO chapters(book_id,label,sort_order)
VALUES (1,'Welcome',0)
`.execute(db);

const now = Date.now() + 60 * 60 * 1000;

await sql`
INSERT INTO snippets(chapter_id,label,content,sort_order,recorded_at,processed_at,finished_at)
VALUES (1,'Dark and Stormy Night', 'It was a dark and stormy night; the rain fell in torrents—except at occasional intervals, when it was checked by a violent gust of wind which swept up the streets (for it is in London that our scene lies), rattling along the housetops, and fiercely agitating the scanty flame of the lamps that struggled against the darkness.',0,${now},${now},${now})
`.execute(db);
} catch (e) {
console.log("Error in migration", { e });
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
2 changes: 1 addition & 1 deletion src/routes/Chapter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export function Chapter() {
</Menu>
</Flex>

{snippets.length > 0 && (
{snippets?.length && snippets?.length > 0 && (
<Tree
rowHeight={36}
onMove={async (e) => {
Expand Down
45 changes: 42 additions & 3 deletions src/state/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import { SettingsQueries } from "../data/repositories/settings";
import { loadOrGetModel } from "../utils/model-data";
import { WhisperModelName } from "../types";

export const appReady = atom(false);
export const fetchTimestamp = atom(0);

// Books

export const currentBooks = atom(async (get) => {
get(fetchTimestamp);
const ready = get(appReady);
if (!ready) {
return [];
}

return BooksQueries.getBooksWithChapters();
});
export const currentBook = atom<BookWithChapters | undefined>(undefined);
Expand All @@ -21,28 +27,54 @@ export const currentBook = atom<BookWithChapters | undefined>(undefined);
export const currentChapterId = atom<number | undefined>(undefined);
export const currentChapter = atom((get) => {
get(fetchTimestamp);
const ready = get(appReady);
if (!ready) {
return;
}

const chapterId = get(currentChapterId);
return ChaptersCRUD.read(chapterId || -1);
if (chapterId === 0 || chapterId) {
return ChaptersCRUD.read(chapterId || -1);
}
});
export const currentSnippets = atom((get) => {
get(fetchTimestamp);
const ready = get(appReady);
if (!ready) {
return [];
}

const chapterId = get(currentChapterId);
return SnippetsQueries.getSnippetsForChapter(chapterId || -1);
if (chapterId === 0 || chapterId) {
return SnippetsQueries.getSnippetsForChapter(chapterId);
}
});

// Snippets

export const currentSnippetId = atom<number | undefined>(undefined);
export const currentSnippet = atom((get) => {
get(fetchTimestamp);
const ready = get(appReady);
if (!ready) {
return;
}

const snippetId = get(currentSnippetId);
return SnippetsCRUD.read(snippetId || -1);
if (snippetId === 0 || snippetId) {
return SnippetsCRUD.read(snippetId);
}
});

// Settings

export const currentSettings = atom(async (get) => {
get(fetchTimestamp);
const ready = get(appReady);
if (!ready) {
return;
}

return SettingsQueries.getSettingsForUser();
});

Expand All @@ -52,6 +84,10 @@ declare const Module: any;
export const currentModel = atom(
async (get) => {
const settings = await get(currentSettings);
if (!settings) {
return;
}

return loadOrGetModel(settings.selectedModel as WhisperModelName, () => {});
},
(_get, _set, update) => {
Expand All @@ -61,6 +97,9 @@ export const currentModel = atom(
export const whisperInstance = atom(async (get) => {
const model = await get(currentModel);
const settings = await get(currentSettings);
if (!settings) {
return;
}

if (model) {
try {
Expand Down

0 comments on commit 8c406f5

Please sign in to comment.