This commit is contained in:
seb
2026-10-28 19:37:28 +01:00
parent 550586f46e
commit b13d994058
2 changed files with 25 additions and 0 deletions

View File

@@ -182,6 +182,23 @@ function connect() {
const es = new EventSource(url.href, { withCredentials: true }); const es = new EventSource(url.href, { withCredentials: true });
addTrackedListener(es, 'user', (event) => {
try {
const payload = JSON.parse(event.data || '{}');
const nextCompanyId = payload.selectedCompanyId ?? null;
if (authAutomat.state.user?.selectedCompanyId !== nextCompanyId) {
authAutomat.setState((prev) => ({
...prev,
user: prev.user ? { ...prev.user, selectedCompanyId: nextCompanyId } : prev.user,
hasCompany: Boolean((prev.hasAuth ?? (prev.status === 'authed' && prev.user)) && nextCompanyId != null),
}));
}
} catch (err) {
console.error('[sse] failed to handle user event', err);
}
});
addTrackedListener(es, 'company', () => { addTrackedListener(es, 'company', () => {
const comp = Automat.get('company'); const comp = Automat.get('company');
if (comp?.actions?.reload) { if (comp?.actions?.reload) {

View File

@@ -157,6 +157,9 @@ class CompanySetupDialog extends PureComponent {
componentDidMount() { componentDidMount() {
this.unsubscribe = companyAutomat.subscribe(this, mapCompanyState); this.unsubscribe = companyAutomat.subscribe(this, mapCompanyState);
this.unsubscribeAuth = authAutomat.subscribe(this, (state) => ({
selectedCompanyId: state.user?.selectedCompanyId ?? null,
}));
this.setState(mapCompanyState(companyAutomat.state)); this.setState(mapCompanyState(companyAutomat.state));
if (companyAutomat.state.status !== 'succeeded') { if (companyAutomat.state.status !== 'succeeded') {
void companyAutomat.actions.load().catch(() => {}); void companyAutomat.actions.load().catch(() => {});
@@ -165,6 +168,7 @@ class CompanySetupDialog extends PureComponent {
componentWillUnmount() { componentWillUnmount() {
this.unsubscribe?.(); this.unsubscribe?.();
this.unsubscribeAuth?.();
} }
async handleSelect(companyId) { async handleSelect(companyId) {
@@ -245,6 +249,10 @@ class CompanySetupDialog extends PureComponent {
} }
render() { render() {
if (authAutomat.state.user?.selectedCompanyId != null) {
return null;
}
const { const {
companies, companies,
loading, loading,