mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-04-19 22:09:37 +00:00
## Summary Refactor: workspaces related functionality into DDD structure. Note: this is the 1st PR of 2 more refactoring. ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-8921-refactor-DDD-3096d73d3650812bb7f6eb955f042663) by [Unito](https://www.unito.io)
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import { storeToRefs } from 'pinia'
|
|
import { useI18n } from 'vue-i18n'
|
|
|
|
import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
|
|
import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
|
|
import { useDialogService } from '@/services/dialogService'
|
|
|
|
export function useWorkspaceSwitch() {
|
|
const { t } = useI18n()
|
|
const workspaceStore = useTeamWorkspaceStore()
|
|
const { activeWorkspace } = storeToRefs(workspaceStore)
|
|
const workflowStore = useWorkflowStore()
|
|
const dialogService = useDialogService()
|
|
|
|
function hasUnsavedChanges(): boolean {
|
|
return workflowStore.modifiedWorkflows.length > 0
|
|
}
|
|
|
|
async function switchWithConfirmation(workspaceId: string): Promise<boolean> {
|
|
if (activeWorkspace.value?.id === workspaceId) {
|
|
return true
|
|
}
|
|
|
|
if (hasUnsavedChanges()) {
|
|
const confirmed = await dialogService.confirm({
|
|
title: t('workspace.unsavedChanges.title'),
|
|
message: t('workspace.unsavedChanges.message'),
|
|
type: 'dirtyClose'
|
|
})
|
|
|
|
if (!confirmed) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
try {
|
|
await workspaceStore.switchWorkspace(workspaceId)
|
|
// Note: switchWorkspace triggers page reload internally
|
|
return true
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return {
|
|
hasUnsavedChanges,
|
|
switchWithConfirmation
|
|
}
|
|
}
|