Skip to content
Open
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
15 changes: 5 additions & 10 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import ExistingTeam from './ExistingTeam';
import DarkModeToggle from './DarkModeToggle';
import hokLogo from './assets/hokLogo.svg';
import { ProjectDetailsComponent } from './api/types';
import { getProjectContainer } from './api';
import { type DB, insertAllFromContainer, open } from './db';
import { type DB, open } from './db';

function App() {
const [db, setDb] = useState(undefined as DB | undefined);
Expand All @@ -27,7 +26,7 @@ function App() {
setParsedUsers([]);
}, []);

const [containerIsAddedToDb, setContainerIsAddedToDb] = useState(false);
const [dbIsReady, setDbIsReady] = useState(false);

useEffect(() => {
let ignore = false;
Expand All @@ -41,14 +40,10 @@ function App() {
.then((openedDb) => {
setDb(openedDb);
closeDb = () => openedDb.close();

const container = getProjectContainer(projectEntityId);

return insertAllFromContainer(openedDb, container);
})
.then(() => {
if (!ignore) {
setContainerIsAddedToDb(true);
setDbIsReady(true);
}
});

Expand All @@ -62,7 +57,7 @@ function App() {
return undefined;
});

setContainerIsAddedToDb(false);
setDbIsReady(false);
}
}, [selectedProject]);

Expand Down Expand Up @@ -96,7 +91,7 @@ function App() {
/>
<ExistingTeam
project={selectedProject}
containerIsAddedToDb={containerIsAddedToDb}
containerIsAddedToDb={dbIsReady}
db={db}
/>
</div>
Expand Down
17 changes: 16 additions & 1 deletion src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ServiceDetailsComponent,
ServiceGroupComponent,
} from '../api/types';
import { getProjectContainer } from '../api';

export type DB = IDBPDatabase<ComponentDB>;

Expand Down Expand Up @@ -81,8 +82,12 @@ export interface ComponentDB extends DBSchema {
}

export const open = async (projectEntityId: string) => {
return openDB<ComponentDB>(projectEntityId, 1, {
let dbAlreadyExisted = true;

const db = await openDB<ComponentDB>(projectEntityId, 1, {
upgrade(db) {
dbAlreadyExisted = false;

const companyDetailsStore = db.createObjectStore(
ComponentType.CompanyDetails,
{ keyPath: 'id' }
Expand Down Expand Up @@ -147,6 +152,16 @@ export const open = async (projectEntityId: string) => {
]);
},
});

if (!dbAlreadyExisted) {
const container = getProjectContainer(projectEntityId);

await insertAllFromContainer(db, container);
} else {
// TODO: Check if DB container data is stale
}

return db;
};

export const getPeople = async (db: DB) => {
Expand Down