Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

feat(settings): let users choose their default workspace

Jonas Kaninda6ce602360ea8
diff --git a/web/src/api/auth.ts b/web/src/api/auth.ts
index 25483ca..eb547b5 100644
--- a/web/src/api/auth.ts
+++ b/web/src/api/auth.ts
@@ -17,6 +17,14 @@ export const authApi = {
updateProfile(data: { name: string; require_verified_domain?: boolean }) {
return api.put<ApiResponse<UserProfile>>('/users/me', data)
},
+ // The workspace a request that sends no X-Posta-Workspace-Id header operates
+ // on. The dashboard always sends the header, so this only affects API keys,
+ // SDKs, and curl.
+ setDefaultWorkspace(workspaceId: number) {
+ return api.put<ApiResponse<{ message: string }>>('/users/me/default-workspace', {
+ workspace_id: workspaceId,
+ })
+ },
changePassword(currentPassword: string, newPassword: string) {
return api.put<ApiResponse<{ message: string }>>('/users/me/password', {
current_password: currentPassword,
diff --git a/web/src/api/types.ts b/web/src/api/types.ts
index a822651..bda3449 100644
--- a/web/src/api/types.ts
+++ b/web/src/api/types.ts
@@ -754,6 +754,7 @@ export interface UserProfile extends User {
require_verified_domain: boolean
scheduled_deletion_at: string | null
email_verification_required: boolean
+ default_workspace_id: number | null
}
// User Data Export/Import
diff --git a/web/src/views/settings/Settings.vue b/web/src/views/settings/Settings.vue
index db1a0b1..a1bcad4 100644
--- a/web/src/views/settings/Settings.vue
+++ b/web/src/views/settings/Settings.vue
@@ -1,14 +1,57 @@
<script setup lang="ts">
-import { ref, onMounted, watch, nextTick } from 'vue'
+import { ref, computed, onMounted, watch, nextTick } from 'vue'
import { settingsApi } from '../../api/settings'
+import { authApi } from '../../api/auth'
import { useThemeStore, type ThemeMode } from '../../stores/theme'
import { useNotificationStore } from '../../stores/notification'
+import { useWorkspaceStore } from '../../stores/workspace'
+import { apiMessage } from '../../composables/apiError'
import type { UserSettings } from '../../api/types'
const theme = useThemeStore()
const notify = useNotificationStore()
+const wsStore = useWorkspaceStore()
const loading = ref(true)
+// The default workspace is saved through its own endpoint, not the debounced
+// settings form above, so it is kept out of `form` deliberately.
+// Two refs on purpose. `savedWorkspaceId` is the server's answer; `selected` is
+// what the control shows. Reverting a failed save has to move `selected`, which
+// the user already changed — writing the old value back to a ref that never
+// changed would not re-render, and the select would keep showing a choice that
+// was never saved.
+const savedWorkspaceId = ref<number | null>(null)
+const selectedWorkspaceId = ref<number | null>(null)
+const savingDefaultWorkspace = ref(false)
+const workspaceChoices = computed(() => wsStore.ownWorkspaces)
+
+async function loadDefaultWorkspace() {
+ try {
+ const res = await authApi.me()
+ savedWorkspaceId.value = res.data.data.default_workspace_id ?? null
+ selectedWorkspaceId.value = savedWorkspaceId.value
+ } catch {
+ // Non-critical: the select falls back to "not set".
+ }
+}
+
+async function saveDefaultWorkspace() {
+ const next = selectedWorkspaceId.value
+ if (!next || next === savedWorkspaceId.value) return
+
+ savingDefaultWorkspace.value = true
+ try {
+ await authApi.setDefaultWorkspace(next)
+ savedWorkspaceId.value = next
+ notify.success('Default workspace updated')
+ } catch (e: any) {
+ selectedWorkspaceId.value = savedWorkspaceId.value
+ notify.error(apiMessage(e, 'Failed to update the default workspace'))
+ } finally {
+ savingDefaultWorkspace.value = false
+ }
+}
+
// Auto-save state. saveStatus drives the header indicator; ready gates the
// watcher so the initial load doesn't trigger a save.
type SaveStatus = 'idle' | 'unsaved' | 'saving' | 'saved' | 'error'
@@ -61,6 +104,8 @@ const themeModes: { value: ThemeMode; label: string; icon: string }[] = [
]
onMounted(async () => {
+ loadDefaultWorkspace()
+ if (!wsStore.workspaces.length) wsStore.fetchWorkspaces()
try {
const res = await settingsApi.getUserSettings()
const u = res.data.data
@@ -206,6 +251,41 @@ async function autoSave() {
</div>
</div>
+ <!-- Default workspace -->
+ <div class="card">
+ <div class="card-header"><h2>Default workspace</h2></div>
+ <div class="card-body">
+ <p class="section-description">
+ Where a request that names no workspace operates. The dashboard always names one,
+ so this applies to API keys, the SDKs, and curl — anything that omits the
+ <code>X-Posta-Workspace-Id</code> header.
+ </p>
+
+ <div v-if="!workspaceChoices.length" class="form-hint">
+ You do not belong to a workspace yet.
+ </div>
+
+ <div v-else class="form-group">
+ <label class="form-label" for="default-workspace">Workspace</label>
+ <select
+ id="default-workspace"
+ v-model.number="selectedWorkspaceId"
+ class="form-select"
+ :disabled="savingDefaultWorkspace"
+ @change="saveDefaultWorkspace"
+ >
+ <option v-if="savedWorkspaceId === null" :value="null" disabled>Not set</option>
+ <option v-for="ws in workspaceChoices" :key="ws.id" :value="ws.id">
+ {{ ws.name }}
+ </option>
+ </select>
+ <span class="form-hint">
+ Your role in the chosen workspace applies to those requests, so a viewer stays a viewer.
+ </span>
+ </div>
+ </div>
+ </div>
+
<!-- Theme -->
<div class="card">
<div class="card-header"><h2>Theme</h2></div>