Skip to content

Commit b873821

Browse files
refactor: Pass accounting settings to sidebar config explicitly
Co-authored-by: aider (gemini/gemini-2.5-flash) <[email protected]>
1 parent ffd22b0 commit b873821

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

src/components/Sidebar.vue

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,21 @@ export default defineComponent({
267267
},
268268
},
269269
async mounted() {
270-
const { companyName } = await fyo.doc.getDoc('AccountingSettings');
271-
this.companyName = companyName as string;
272-
this.groups = await getSidebarConfig(); // Initial load
270+
const accountingSettings = (await fyo.doc.getDoc(
271+
'AccountingSettings'
272+
)) as typeof fyo.singles.AccountingSettings;
273+
274+
this.companyName = accountingSettings.companyName as string;
275+
this.groups = await getSidebarConfig(accountingSettings); // Initial load, pass the fetched instance
273276
274277
this.setActiveGroup();
275278
router.afterEach(() => {
276279
this.setActiveGroup();
277280
});
278281
279-
// Subscribe to AccountingSettings changes to update sidebar reactivity
280-
if (fyo.singles.AccountingSettings) {
281-
this.accountingSettingsUnsubscribe = fyo.singles.AccountingSettings.on(
282+
// Subscribe to changes on this specific instance
283+
if (accountingSettings) {
284+
this.accountingSettingsUnsubscribe = accountingSettings.on(
282285
'change',
283286
this.handleAccountingSettingsChange
284287
);
@@ -305,8 +308,11 @@ export default defineComponent({
305308
reportIssue,
306309
toggleSidebar,
307310
// Handle changes to AccountingSettings to refresh sidebar groups
308-
async handleAccountingSettingsChange() {
309-
this.groups = await getSidebarConfig();
311+
async handleAccountingSettingsChange(
312+
changedDoc: typeof fyo.singles.AccountingSettings
313+
) {
314+
// Pass the updated document instance to getSidebarConfig
315+
this.groups = await getSidebarConfig(changedDoc);
310316
},
311317
openDocumentation() {
312318
ipc.openLink('https://docs.frappe.io/' + docsPathRef.value);

src/utils/sidebarConfig.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import { routeFilters } from 'src/utils/filters';
33
import { fyo } from '../initFyo';
44
import { SidebarConfig, SidebarItem, SidebarRoot } from './types';
55

6-
export async function getSidebarConfig(): Promise<SidebarConfig> {
7-
const sideBar = await getCompleteSidebar();
6+
export async function getSidebarConfig(
7+
accountingSettings?: typeof fyo.singles.AccountingSettings // Accept AccountingSettings here
8+
): Promise<SidebarConfig> {
9+
const sideBar = await getCompleteSidebar(accountingSettings);
810
return getFilteredSidebar(sideBar);
911
}
1012

@@ -33,7 +35,7 @@ function getFilteredSidebar(sideBar: SidebarConfig): SidebarConfig {
3335
}
3436

3537
async function getRegionalSidebar(
36-
accountingSettings: typeof fyo.singles.AccountingSettings
38+
accountingSettings?: typeof fyo.singles.AccountingSettings // Use passed settings
3739
): Promise<SidebarRoot[]> {
3840
const hasGstin = !!accountingSettings?.gstin;
3941
if (!hasGstin) {
@@ -63,7 +65,7 @@ async function getRegionalSidebar(
6365
}
6466

6567
async function getInventorySidebar(
66-
accountingSettings: typeof fyo.singles.AccountingSettings
68+
accountingSettings?: typeof fyo.singles.AccountingSettings // Use passed settings
6769
): Promise<SidebarRoot[]> {
6870
const hasInventory = !!accountingSettings?.enableInventory;
6971
if (!hasInventory) {
@@ -154,19 +156,24 @@ function getReportSidebar(): SidebarRoot {
154156
};
155157
}
156158

157-
async function getCompleteSidebar(): Promise<SidebarConfig> {
158-
// Explicitly load singletons to ensure the latest values are used
159-
const accountingSettings = (await fyo.doc.getDoc(
160-
'AccountingSettings'
161-
)) as typeof fyo.singles.AccountingSettings;
159+
async function getCompleteSidebar(
160+
initialAccountingSettings?: typeof fyo.singles.AccountingSettings
161+
): Promise<SidebarConfig> {
162+
// Use the passed settings or explicitly load singletons if not provided
163+
const accountingSettings =
164+
initialAccountingSettings ||
165+
((await fyo.doc.getDoc(
166+
'AccountingSettings'
167+
)) as typeof fyo.singles.AccountingSettings);
168+
162169
const systemSettings = (await fyo.doc.getDoc(
163170
'SystemSettings'
164171
)) as typeof fyo.singles.SystemSettings;
165172
const inventorySettings = (await fyo.doc.getDoc(
166173
'InventorySettings'
167174
)) as typeof fyo.singles.InventorySettings;
168175

169-
// Pre-compute project visibility for clarity
176+
// Pre-compute project visibility for clarity using the consistent accountingSettings instance
170177
const hideProjects = !accountingSettings?.enableProjects;
171178

172179
return [
@@ -332,9 +339,9 @@ async function getCompleteSidebar(): Promise<SidebarConfig> {
332339
] as SidebarItem[],
333340
},
334341
getReportSidebar(),
335-
...(await getInventorySidebar(accountingSettings)), // Await and spread result
336-
(await getPOSSidebar(inventorySettings)), // Await for POS sidebar
337-
...(await getRegionalSidebar(accountingSettings)), // Await and spread result
342+
...(await getInventorySidebar(accountingSettings)),
343+
await getPOSSidebar(inventorySettings),
344+
...(await getRegionalSidebar(accountingSettings)),
338345
{
339346
label: t`Setup`,
340347
name: 'setup',

0 commit comments

Comments
 (0)