feat(settings): wire WorkspaceGeneralTab into dirty tracking + modal save bar
This commit is contained in:
@@ -199,7 +199,7 @@ function TabContent({
|
|||||||
// Workspace tabs
|
// Workspace tabs
|
||||||
if (context === 'workspace') {
|
if (context === 'workspace') {
|
||||||
switch (activeTab) {
|
switch (activeTab) {
|
||||||
case 'general': return <WorkspaceGeneralTab />
|
case 'general': return <WorkspaceGeneralTab {...dirtyProps} />
|
||||||
case 'billing': return <WorkspaceBillingTab />
|
case 'billing': return <WorkspaceBillingTab />
|
||||||
case 'members': return <WorkspaceMembersTab />
|
case 'members': return <WorkspaceMembersTab />
|
||||||
case 'notifications': return <WorkspaceNotificationsTab />
|
case 'notifications': return <WorkspaceNotificationsTab />
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { useState, useEffect } from 'react'
|
import { useState, useEffect, useRef, useCallback } from 'react'
|
||||||
import { useRouter } from 'next/navigation'
|
import { useRouter } from 'next/navigation'
|
||||||
import { Input, Button, toast } from '@ciphera-net/ui'
|
import { Input, Button, toast } from '@ciphera-net/ui'
|
||||||
import { Spinner } from '@ciphera-net/ui'
|
import { Spinner } from '@ciphera-net/ui'
|
||||||
@@ -9,17 +9,18 @@ import { getOrganization, updateOrganization, deleteOrganization } from '@/lib/a
|
|||||||
import { getAuthErrorMessage } from '@ciphera-net/ui'
|
import { getAuthErrorMessage } from '@ciphera-net/ui'
|
||||||
import { useUnifiedSettings } from '@/lib/unified-settings-context'
|
import { useUnifiedSettings } from '@/lib/unified-settings-context'
|
||||||
|
|
||||||
export default function WorkspaceGeneralTab() {
|
export default function WorkspaceGeneralTab({ onDirtyChange, onRegisterSave }: { onDirtyChange?: (dirty: boolean) => void; onRegisterSave?: (fn: () => Promise<void>) => void }) {
|
||||||
const { user } = useAuth()
|
const { user } = useAuth()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const { closeUnifiedSettings } = useUnifiedSettings()
|
const { closeUnifiedSettings } = useUnifiedSettings()
|
||||||
const [name, setName] = useState('')
|
const [name, setName] = useState('')
|
||||||
const [slug, setSlug] = useState('')
|
const [slug, setSlug] = useState('')
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
const [saving, setSaving] = useState(false)
|
|
||||||
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false)
|
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false)
|
||||||
const [deleteText, setDeleteText] = useState('')
|
const [deleteText, setDeleteText] = useState('')
|
||||||
const [deleting, setDeleting] = useState(false)
|
const [deleting, setDeleting] = useState(false)
|
||||||
|
const initialRef = useRef('')
|
||||||
|
const hasInitialized = useRef(false)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!user?.org_id) return
|
if (!user?.org_id) return
|
||||||
@@ -28,23 +29,36 @@ export default function WorkspaceGeneralTab() {
|
|||||||
.then(org => {
|
.then(org => {
|
||||||
setName(org.name || '')
|
setName(org.name || '')
|
||||||
setSlug(org.slug || '')
|
setSlug(org.slug || '')
|
||||||
|
if (!hasInitialized.current) {
|
||||||
|
initialRef.current = JSON.stringify({ name: org.name || '', slug: org.slug || '' })
|
||||||
|
hasInitialized.current = true
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch(() => {})
|
.catch(() => {})
|
||||||
.finally(() => setLoading(false))
|
.finally(() => setLoading(false))
|
||||||
}, [user?.org_id])
|
}, [user?.org_id])
|
||||||
|
|
||||||
const handleSave = async () => {
|
// Track dirty state
|
||||||
|
useEffect(() => {
|
||||||
|
if (!initialRef.current) return
|
||||||
|
onDirtyChange?.(JSON.stringify({ name, slug }) !== initialRef.current)
|
||||||
|
}, [name, slug, onDirtyChange])
|
||||||
|
|
||||||
|
const handleSave = useCallback(async () => {
|
||||||
if (!user?.org_id) return
|
if (!user?.org_id) return
|
||||||
setSaving(true)
|
|
||||||
try {
|
try {
|
||||||
await updateOrganization(user.org_id, name, slug)
|
await updateOrganization(user.org_id, name, slug)
|
||||||
|
initialRef.current = JSON.stringify({ name, slug })
|
||||||
|
onDirtyChange?.(false)
|
||||||
toast.success('Organization updated')
|
toast.success('Organization updated')
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
toast.error(getAuthErrorMessage(err as Error) || 'Failed to update organization')
|
toast.error(getAuthErrorMessage(err as Error) || 'Failed to update organization')
|
||||||
} finally {
|
|
||||||
setSaving(false)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}, [user?.org_id, name, slug, onDirtyChange])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
onRegisterSave?.(handleSave)
|
||||||
|
}, [handleSave, onRegisterSave])
|
||||||
|
|
||||||
const handleDelete = async () => {
|
const handleDelete = async () => {
|
||||||
if (!user?.org_id || deleteText !== 'DELETE') return
|
if (!user?.org_id || deleteText !== 'DELETE') return
|
||||||
@@ -91,12 +105,6 @@ export default function WorkspaceGeneralTab() {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex justify-end">
|
|
||||||
<Button onClick={handleSave} variant="primary" disabled={saving}>
|
|
||||||
{saving ? 'Saving...' : 'Save Changes'}
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Danger Zone */}
|
{/* Danger Zone */}
|
||||||
<div className="space-y-3 pt-4 border-t border-neutral-800">
|
<div className="space-y-3 pt-4 border-t border-neutral-800">
|
||||||
<h3 className="text-base font-semibold text-red-500">Danger Zone</h3>
|
<h3 className="text-base font-semibold text-red-500">Danger Zone</h3>
|
||||||
|
|||||||
Reference in New Issue
Block a user