Skip to content

Commit

Permalink
pre-select current or active journals
Browse files Browse the repository at this point in the history
- put search store into context (todo: refactor component drilling to pull from context)
- when creating new documents, if the search is scoped to a journal, default the new document to the selected journal
- when creating new documents, if search is NOT scoped, default to the first ACTIVE journal
  • Loading branch information
cloverich committed Jan 21, 2024
1 parent 5f4186a commit aae8477
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
30 changes: 19 additions & 11 deletions src/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { Alert, Pane } from "evergreen-ui";
import { Routes, Route, Navigate } from "react-router-dom";
import { useSearchParams } from "react-router-dom";
import useClient from "./hooks/useClient";
import { SearchV2Store } from "./views/documents/SearchStore";
import {
SearchV2Store,
SearchStoreContext,
} from "./views/documents/SearchStore";

export default observer(function Container() {
const { journalsStore, loading, loadingErr } = useJournalsLoader();
Expand Down Expand Up @@ -63,16 +66,21 @@ export default observer(function Container() {

return (
<JournalsStoreContext.Provider value={journalsStore!}>
<Layout>
<Routes>
<Route element={<Journals />} path="journals" />
<Route element={<Preferences />} path="preferences" />
<Route element={<Editor />} path="edit/new" />
<Route element={<Editor />} path="edit/:document" />
<Route element={<Documents store={searchStore} />} path="documents" />
<Route path="*" element={<Navigate to="documents" replace />} />
</Routes>
</Layout>
<SearchStoreContext.Provider value={searchStore}>
<Layout>
<Routes>
<Route element={<Journals />} path="journals" />
<Route element={<Preferences />} path="preferences" />
<Route element={<Editor />} path="edit/new" />
<Route element={<Editor />} path="edit/:document" />
<Route
element={<Documents store={searchStore} />}
path="documents"
/>
<Route path="*" element={<Navigate to="documents" replace />} />
</Routes>
</Layout>
</SearchStoreContext.Provider>
</JournalsStoreContext.Provider>
);
});
1 change: 1 addition & 0 deletions src/preload/client/journals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class JournalsClient {
};

remove = (journal: { id: string }): Promise<JournalResponse[]> => {
// TODO: ensure there is always at least one journal. Deleting the last journal breaks the app.
this.db
.prepare("delete from journals where id = :id")
.run({ id: journal.id });
Expand Down
11 changes: 11 additions & 0 deletions src/views/documents/SearchStore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createContext } from "react";
import { IClient } from "../../hooks/useClient";
import { observable, IObservableArray, computed, action } from "mobx";
import { JournalsStore } from "../../hooks/stores/journals";
Expand All @@ -19,6 +20,8 @@ interface SearchQuery {
limit?: number;
}

export const SearchStoreContext = createContext<SearchV2Store>(null as any);

export class SearchV2Store {
@observable docs: SearchItem[] = [];
@observable loading = true;
Expand Down Expand Up @@ -156,6 +159,14 @@ export class SearchV2Store {
this.addTokens(searchStr);
};

@computed get selectedJournals(): string[] {
// Grab the journal names from the tokens
// todo: Typescript doesn't know when I filter to type === 'in' its InTokens
return this._tokens
.filter((t) => t.type === "in")
.map((t) => t.value) as string[];
}

@computed
get searchTokens() {
return this.tagSeachStore.searchTokens;
Expand Down
6 changes: 5 additions & 1 deletion src/views/edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ import { JournalsStoreContext } from "../../hooks/useJournalsLoader";
import Toolbar from "./toolbar";
import { useParams, useNavigate } from "react-router-dom";
import { DebugView } from "./DebugView";
import { SearchStoreContext } from "../documents/SearchStore";

// Loads document, with loading and error placeholders
function DocumentLoadingContainer() {
const journalsStore = useContext(JournalsStoreContext);
const searchStore = useContext(SearchStoreContext);
const { document: documentId } = useParams();

const { document, loadingError } = useEditableDocument(
journalsStore.journals,
searchStore,
journalsStore,
documentId,
);

Expand Down
31 changes: 26 additions & 5 deletions src/views/edit/useEditableDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,36 @@ import React from "react";
import { JournalResponse } from "../../preload/client/journals";
import useClient from "../../hooks/useClient";
import { EditableDocument } from "./EditableDocument";
import { SearchV2Store } from "../documents/SearchStore";
import { JournalsStore } from "../../hooks/stores/journals";

/**
* Determines the default journal to use when creating a new document.
*
* todo(test): When one or multiple journals are selected, returns the first
* todo(test): When no journals are selected, returns the first active journal
* todo(test): When archived journal selected, returns the selected (archived) journal
*/
function defaultJournal(selectedJournals: string[], jstore: JournalsStore) {
const selectedId = jstore.journals.find((j) =>
selectedJournals.includes(j.name),
)?.id;

if (selectedId) {
return selectedId;
} else {
// todo: defaulting to first journal, but could use logic such as the last selected
// journal, etc, once that is in place
return jstore.active[0].id;
}
}

/**
* Load a new or existing document into a view model
*/
export function useEditableDocument(
journals: JournalResponse[],
search: SearchV2Store,
jstore: JournalsStore,
documentId?: string,
) {
const [document, setDocument] = React.useState<EditableDocument | null>(null);
Expand All @@ -31,9 +55,7 @@ export function useEditableDocument(
setDocument(
new EditableDocument(client, {
content: "",
// todo: defaulting to first journal, but could use logic such as the last selected
// journal, etc, once that is in place
journalId: journals[0].id,
journalId: defaultJournal(search.selectedJournals, jstore),
}),
);
}
Expand All @@ -51,7 +73,6 @@ export function useEditableDocument(
}, [documentId]);

return {
journals,
document,
loadingError: loadingError,
};
Expand Down

0 comments on commit aae8477

Please sign in to comment.