import * as appstate from '../appstate.js'; import * as shared from './shared/index.js'; import { appRouter } from '../router.js'; import { css, cssManager, customElement, DeesElement, html, state, type TemplateResult, } from '@design.estate/dees-element'; @customElement('sg-view-organizations') export class SgViewOrganizations extends DeesElement { @state() accessor organizationsState: appstate.IOrganizationsState = { organizations: [], currentOrg: null, repositories: [], members: [], redirects: [], }; @state() accessor uiState: appstate.IUiState = { activeView: 'organizations' }; @state() accessor detailOrgId: string | null = null; constructor() { super(); const orgSub = appstate.organizationsStatePart .select((s) => s) .subscribe((s) => { this.organizationsState = s; }); this.rxSubscriptions.push(orgSub); const uiSub = appstate.uiStatePart .select((s) => s) .subscribe((s) => { this.uiState = s; }); this.rxSubscriptions.push(uiSub); } public static styles = [ cssManager.defaultStyles, shared.viewHostCss, ]; async connectedCallback() { super.connectedCallback(); await appstate.organizationsStatePart.dispatchAction(appstate.fetchOrganizationsAction, null); // If there's an entity ID from the URL, copy it to internal state if (this.uiState.activeEntityId) { this.detailOrgId = this.uiState.activeEntityId; await this.loadOrgDetail(this.detailOrgId); } } private async loadOrgDetail(orgId: string) { await appstate.organizationsStatePart.dispatchAction( appstate.fetchOrganizationAction, { organizationId: orgId }, ); await appstate.organizationsStatePart.dispatchAction( appstate.fetchRepositoriesAction, { organizationId: orgId }, ); await appstate.organizationsStatePart.dispatchAction( appstate.fetchMembersAction, { organizationId: orgId }, ); await appstate.organizationsStatePart.dispatchAction( appstate.fetchRedirectsAction, { organizationId: orgId }, ); } public render(): TemplateResult { if (this.detailOrgId && this.organizationsState.currentOrg) { return html` `; } return html` `; } private selectOrg(orgId: string) { this.detailOrgId = orgId; this.loadOrgDetail(orgId); } private selectRepo(repoId: string) { // Navigate to repository within org context // For now, we could switch to packages view } private goBack() { this.detailOrgId = null; appstate.organizationsStatePart.setState({ ...appstate.organizationsStatePart.getState(), currentOrg: null, repositories: [], members: [], redirects: [], }); } private async handleDeleteRedirect(redirectId: string) { if (!this.detailOrgId) return; await appstate.organizationsStatePart.dispatchAction( appstate.deleteRedirectAction, { redirectId, organizationId: this.detailOrgId }, ); } private async handleEditOrg(data: { organizationId: string; name?: string; displayName?: string; description?: string; website?: string; isPublic?: boolean; }) { await appstate.organizationsStatePart.dispatchAction( appstate.updateOrganizationAction, data, ); // Re-load detail to reflect changes if (this.detailOrgId) { await this.loadOrgDetail(this.detailOrgId); } } private async handleDeleteOrg(organizationId: string) { await appstate.organizationsStatePart.dispatchAction( appstate.deleteOrganizationAction, { organizationId }, ); this.goBack(); } private async createOrg(data: { name: string; displayName?: string; description?: string }) { await appstate.organizationsStatePart.dispatchAction( appstate.createOrganizationAction, data, ); } }