refactor(useDataLoader): drop 16 positional setters, use getState() (P1-22)
Function signature reduced from 16 args to 4; setters sourced from useAppStore.getState() inside callbacks (stable refs, no deps needed). useCompanyController no longer has to thread 14 setters through.
This commit is contained in:
1 parent
12279005fe
commit
4838750009
2 files changed
+28
-51
No files matched your search
@@ -9,28 +9,22 @@ import { useCompanyData } from "./useCompanyData";
|
|||||||
|
|
||||||
export function useCompanyController() {
|
export function useCompanyController() {
|
||||||
const snapshot = useAppStore((s) => s.snapshot);
|
const snapshot = useAppStore((s) => s.snapshot);
|
||||||
const setSnapshot = useAppStore((s) => s.setSnapshot);
|
|
||||||
const section = useAppStore((s) => s.section);
|
const section = useAppStore((s) => s.section);
|
||||||
const setError = useAppStore((s) => s.setError);
|
|
||||||
const setInitialOnboardingLocked = useAppStore((s) => s.setInitialOnboardingLocked);
|
|
||||||
const setSection = useAppStore((s) => s.setSection);
|
|
||||||
const setShowOnboarding = useAppStore((s) => s.setShowOnboarding);
|
|
||||||
const setSelectedRunId = useAppStore((s) => s.setSelectedRunId);
|
|
||||||
const setSelectedAgentId = useAppStore((s) => s.setSelectedAgentId);
|
|
||||||
const setSelectedConnectorId = useAppStore((s) => s.setSelectedConnectorId);
|
|
||||||
const selectedConnectorId = useAppStore((s) => s.selectedConnectorId);
|
const selectedConnectorId = useAppStore((s) => s.selectedConnectorId);
|
||||||
const selectedRunId = useAppStore((s) => s.selectedRunId);
|
const selectedRunId = useAppStore((s) => s.selectedRunId);
|
||||||
const setBadges = useAppStore((s) => s.setBadges);
|
|
||||||
const setInboxItems = useAppStore((s) => s.setInboxItems);
|
|
||||||
const setSocialAccounts = useAppStore((s) => s.setSocialAccounts);
|
|
||||||
const setBrowserActions = useAppStore((s) => s.setBrowserActions);
|
|
||||||
const setCompanyMetrics = useAppStore((s) => s.setCompanyMetrics);
|
|
||||||
const setStandupReport = useAppStore((s) => s.setStandupReport);
|
|
||||||
const setConfirmDeleteCompanyId = useAppStore((s) => s.setConfirmDeleteCompanyId);
|
|
||||||
const setCompanySwitcherOpen = useAppStore((s) => s.setCompanySwitcherOpen);
|
|
||||||
const locale = useAppStore((s) => s.locale);
|
const locale = useAppStore((s) => s.locale);
|
||||||
const setLocale = useAppStore((s) => s.setLocale);
|
|
||||||
const incrementLogRevision = useAppStore((s) => s.incrementLogRevision);
|
const incrementLogRevision = useAppStore((s) => s.incrementLogRevision);
|
||||||
|
const {
|
||||||
|
setError,
|
||||||
|
setInitialOnboardingLocked,
|
||||||
|
setSection,
|
||||||
|
setShowOnboarding,
|
||||||
|
setSelectedRunId,
|
||||||
|
setSelectedAgentId,
|
||||||
|
setConfirmDeleteCompanyId,
|
||||||
|
setCompanySwitcherOpen,
|
||||||
|
setLocale,
|
||||||
|
} = useAppStore.getState();
|
||||||
|
|
||||||
const currentCompany = useMemo<CompanyRecord | null>(() => {
|
const currentCompany = useMemo<CompanyRecord | null>(() => {
|
||||||
if (!snapshot) return null;
|
if (!snapshot) return null;
|
||||||
@@ -56,13 +50,7 @@ export function useCompanyController() {
|
|||||||
store.setSkillImportModalOpen(false);
|
store.setSkillImportModalOpen(false);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const { refresh } = useDataLoader(
|
const { refresh } = useDataLoader(snapshot, section, selectedConnectorId, selectedRunId);
|
||||||
snapshot, section, selectedConnectorId, selectedRunId,
|
|
||||||
setSnapshot, setError, setInitialOnboardingLocked,
|
|
||||||
setSelectedConnectorId, setSelectedRunId,
|
|
||||||
setBadges, setInboxItems, setSocialAccounts, setBrowserActions,
|
|
||||||
setCompanyMetrics, setStandupReport,
|
|
||||||
);
|
|
||||||
|
|
||||||
const logBufferRef = useRef<Record<string, string>>({});
|
const logBufferRef = useRef<Record<string, string>>({});
|
||||||
|
|
||||||
|
|||||||
@@ -8,43 +8,32 @@ export function useDataLoader(
|
|||||||
section: SectionId,
|
section: SectionId,
|
||||||
selectedConnectorId: string | null,
|
selectedConnectorId: string | null,
|
||||||
selectedRunId: string | null,
|
selectedRunId: string | null,
|
||||||
setSnapshot: (snapshot: ProfileSnapshot) => void,
|
|
||||||
setError: (error: string | null) => void,
|
|
||||||
setInitialOnboardingLocked: (locked: boolean) => void,
|
|
||||||
setSelectedConnectorId: (id: string | null) => void,
|
|
||||||
setSelectedRunId: (id: string | null) => void,
|
|
||||||
setBadges: (badges: Parameters<ReturnType<typeof useAppStore.getState>["setBadges"]>[0]) => void,
|
|
||||||
setInboxItems: (items: Parameters<ReturnType<typeof useAppStore.getState>["setInboxItems"]>[0]) => void,
|
|
||||||
setSocialAccounts: (accounts: Parameters<ReturnType<typeof useAppStore.getState>["setSocialAccounts"]>[0]) => void,
|
|
||||||
setBrowserActions: (actions: Parameters<ReturnType<typeof useAppStore.getState>["setBrowserActions"]>[0]) => void,
|
|
||||||
setCompanyMetrics: (metrics: Parameters<ReturnType<typeof useAppStore.getState>["setCompanyMetrics"]>[0]) => void,
|
|
||||||
setStandupReport: (report: Parameters<ReturnType<typeof useAppStore.getState>["setStandupReport"]>[0]) => void,
|
|
||||||
) {
|
) {
|
||||||
const refresh = useCallback(async () => {
|
const refresh = useCallback(async () => {
|
||||||
|
const store = useAppStore.getState();
|
||||||
try {
|
try {
|
||||||
const next = unwrap<ProfileSnapshot>(await window.agentCompany.loadSnapshot());
|
const next = unwrap<ProfileSnapshot>(await window.agentCompany.loadSnapshot());
|
||||||
setSnapshot(next);
|
store.setSnapshot(next);
|
||||||
if (next.companies.length === 0) {
|
if (next.companies.length === 0) {
|
||||||
setInitialOnboardingLocked(true);
|
store.setInitialOnboardingLocked(true);
|
||||||
}
|
}
|
||||||
if (!selectedConnectorId && next.connectors[0]) setSelectedConnectorId(next.connectors[0].id);
|
if (!selectedConnectorId && next.connectors[0]) store.setSelectedConnectorId(next.connectors[0].id);
|
||||||
if (!selectedRunId && next.runs[0]) setSelectedRunId(next.runs[0].id);
|
if (!selectedRunId && next.runs[0]) store.setSelectedRunId(next.runs[0].id);
|
||||||
if (next.currentCompanyId) {
|
if (next.currentCompanyId) {
|
||||||
const badgeResult = await window.agentCompany.getSidebarBadges(next.currentCompanyId);
|
const badgeResult = await window.agentCompany.getSidebarBadges(next.currentCompanyId);
|
||||||
if (badgeResult.ok) setBadges(badgeResult.data);
|
if (badgeResult.ok) store.setBadges(badgeResult.data);
|
||||||
const inboxResult = await window.agentCompany.getInbox(next.currentCompanyId);
|
const inboxResult = await window.agentCompany.getInbox(next.currentCompanyId);
|
||||||
if (inboxResult.ok) setInboxItems(inboxResult.data);
|
if (inboxResult.ok) store.setInboxItems(inboxResult.data);
|
||||||
setSocialAccounts(next.socialAccounts?.filter(a => a.companyId === next.currentCompanyId) ?? []);
|
store.setSocialAccounts(next.socialAccounts?.filter((a) => a.companyId === next.currentCompanyId) ?? []);
|
||||||
const browserActionResult = await window.agentCompany.listBrowserActions({ companyId: next.currentCompanyId, limit: 50 });
|
const browserActionResult = await window.agentCompany.listBrowserActions({ companyId: next.currentCompanyId, limit: 50 });
|
||||||
if (browserActionResult.ok) {
|
if (browserActionResult.ok) {
|
||||||
setBrowserActions(browserActionResult.data);
|
store.setBrowserActions(browserActionResult.data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setError(null);
|
store.setError(null);
|
||||||
} catch (nextError) {
|
} catch (nextError) {
|
||||||
setError(nextError instanceof Error ? nextError.message : String(nextError));
|
store.setError(nextError instanceof Error ? nextError.message : String(nextError));
|
||||||
}
|
}
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps -- Zustand setters are stable references
|
|
||||||
}, [selectedConnectorId, selectedRunId]);
|
}, [selectedConnectorId, selectedRunId]);
|
||||||
|
|
||||||
// Section-based data loading
|
// Section-based data loading
|
||||||
@@ -52,33 +41,33 @@ export function useDataLoader(
|
|||||||
if (!snapshot?.currentCompanyId) return;
|
if (!snapshot?.currentCompanyId) return;
|
||||||
const companyId = snapshot.currentCompanyId;
|
const companyId = snapshot.currentCompanyId;
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
|
const store = useAppStore.getState();
|
||||||
|
|
||||||
if (section === "inbox") {
|
if (section === "inbox") {
|
||||||
void window.agentCompany.getInbox(companyId).then((result) => {
|
void window.agentCompany.getInbox(companyId).then((result) => {
|
||||||
if (!cancelled && result.ok) setInboxItems(result.data);
|
if (!cancelled && result.ok) store.setInboxItems(result.data);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (section === "performance") {
|
if (section === "performance") {
|
||||||
void window.agentCompany.getCompanyMetrics({ companyId, days: 30 }).then((result) => {
|
void window.agentCompany.getCompanyMetrics({ companyId, days: 30 }).then((result) => {
|
||||||
if (!cancelled && result.ok) setCompanyMetrics(result.data);
|
if (!cancelled && result.ok) store.setCompanyMetrics(result.data);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (section === "standup") {
|
if (section === "standup") {
|
||||||
void window.agentCompany.getStandupReport(companyId).then((result) => {
|
void window.agentCompany.getStandupReport(companyId).then((result) => {
|
||||||
if (!cancelled && result.ok) setStandupReport(result.data);
|
if (!cancelled && result.ok) store.setStandupReport(result.data);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (section === "social") {
|
if (section === "social") {
|
||||||
setSocialAccounts(snapshot.socialAccounts?.filter((a) => a.companyId === companyId) ?? []);
|
store.setSocialAccounts(snapshot.socialAccounts?.filter((a) => a.companyId === companyId) ?? []);
|
||||||
}
|
}
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
cancelled = true;
|
cancelled = true;
|
||||||
};
|
};
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps -- Zustand setters are stable references
|
|
||||||
}, [section, snapshot]);
|
}, [section, snapshot]);
|
||||||
|
|
||||||
return { refresh };
|
return { refresh };
|
||||||
|
|||||||
Reference in new issue
Block a user