Refs #11: finishing up and testing wih a create contacts folder button for new users so that they do not need to start with opening the settings

This commit is contained in:
Roland Broekema 2025-05-26 22:29:56 +02:00
parent 86be278326
commit 0c6eff2c13
2 changed files with 47 additions and 14 deletions

View file

@ -18,14 +18,17 @@ import { processAvatar } from "src/util/avatarActions";
import { Sort } from "src/util/constants";
import myScrollTo from "src/util/myScrollTo";
export const SidebarRootView = () => {
interface SidebarRootViewProps {
createDefaultPluginFolder: () => Promise<void>;
}
export const SidebarRootView = (props: SidebarRootViewProps) => {
const app = getApp();
const { vault, workspace } = app;
const [contacts, setContacts] = React.useState<Contact[]>([]);
const [displayInsightsView, setDisplayInsightsView] = React.useState<boolean>(false);
const [sort, setSort] = React.useState<Sort>(Sort.NAME);
const settings = getSettings();
let settings = getSettings();
const parseContacts = () => {
const contactsFolder = vault.getAbstractFileByPath(
@ -44,7 +47,10 @@ export const SidebarRootView = () => {
React.useEffect(() => {
parseContacts();
const offSettings = onSettingsChange(parseContacts.bind(this));
const offSettings = onSettingsChange(() => {
settings = getSettings();
parseContacts.call(this);
});
return () => {
offSettings();
@ -155,15 +161,18 @@ export const SidebarRootView = () => {
}} />
:
<>
<div className="action-card">
<div className="action-card-content">
<p>Your contacts folder is currently set to the <strong>root of your vault</strong>.</p>
<p>We advise to create a specific folder prevent system processing</p>
<button className="action-card-button"
>Make contacts folder
</button>
{!settings.contactsFolder ?
<div className="action-card">
<div className="action-card-content">
<p>
Your contacts folder is currently set to the <strong>root of your vault</strong>. We advise to create a specific folder prevent system processing.
</p>
<p>
<button onClick={props.createDefaultPluginFolder} className="action-card-button">Make contacts folder</button>
</p>
</div>
</div>
</div>
: null }
<div className="action-card">
<div className="action-card-content">

View file

@ -1,4 +1,4 @@
import { ItemView, WorkspaceLeaf } from "obsidian";
import { ItemView, Notice, WorkspaceLeaf } from "obsidian";
import * as React from "react";
import { createRoot } from "react-dom/client";
import { clearApp, setApp } from "src/context/sharedAppContext";
@ -15,6 +15,28 @@ export class ContactsView extends ItemView {
super(leaf);
this.plugin = plugin;
}
async createDefaultPluginFolder(): Promise<void> {
const vault = this.app.vault;
const folderPath = "Contacts"; // You can move this to a constant if you prefer
try {
const folderExists = await vault.adapter.exists(folderPath);
if (!folderExists) {
await vault.createFolder(folderPath);
this.plugin.settings.contactsFolder = folderPath;
await this.plugin.saveSettings();
setSettings(this.plugin.settings);
} else {
this.plugin.settings.contactsFolder = folderPath;
await this.plugin.saveSettings();
setSettings(this.plugin.settings);
}
} catch (err) {
new Notice('Failed to create folder default Contacts folder');
}
}
getViewType(): string {
return CONTACTS_VIEW_CONFIG.type;
}
@ -31,7 +53,9 @@ export class ContactsView extends ItemView {
setApp(this.app);
setSettings(this.plugin.settings);
this.root.render(
<SidebarRootView />
<SidebarRootView
createDefaultPluginFolder={this.createDefaultPluginFolder.bind(this)}
/>
);
}