Skip to content

Commit

Permalink
Replace window.alert with toast alert (#1112)
Browse files Browse the repository at this point in the history
* Replace window.alert with toast alert

* Mock jest
  • Loading branch information
huchenlei authored Oct 5, 2024
1 parent 2649d72 commit 4cc6954
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 30 deletions.
15 changes: 9 additions & 6 deletions src/extensions/core/colorPalette.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useToastStore } from '@/stores/toastStore'
import { app } from '../../scripts/app'
import { $el } from '../../scripts/ui'
import type { ColorPalettes, Palette } from '@/types/colorPalette'
Expand Down Expand Up @@ -588,30 +589,30 @@ app.registerExtension({

const addCustomColorPalette = async (colorPalette) => {
if (typeof colorPalette !== 'object') {
alert('Invalid color palette.')
useToastStore().addAlert('Invalid color palette.')
return
}

if (!colorPalette.id) {
alert('Color palette missing id.')
useToastStore().addAlert('Color palette missing id.')
return
}

if (!colorPalette.name) {
alert('Color palette missing name.')
useToastStore().addAlert('Color palette missing name.')
return
}

if (!colorPalette.colors) {
alert('Color palette missing colors.')
useToastStore().addAlert('Color palette missing colors.')
return
}

if (
colorPalette.colors.node_slot &&
typeof colorPalette.colors.node_slot !== 'object'
) {
alert('Invalid color palette colors.node_slot.')
useToastStore().addAlert('Invalid color palette colors.node_slot.')
return
}

Expand Down Expand Up @@ -842,7 +843,9 @@ app.registerExtension({
)

if (colorPalettes[colorPaletteId]) {
alert('You cannot delete a built-in color palette.')
useToastStore().addAlert(
'You cannot delete a built-in color palette.'
)
return
}

Expand Down
3 changes: 2 additions & 1 deletion src/extensions/core/groupNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { LGraphNode } from '@comfyorg/litegraph'
import { LGraphCanvas, LiteGraph } from '@comfyorg/litegraph'
import { useNodeDefStore } from '@/stores/nodeDefStore'
import { ComfyLink, ComfyNode, ComfyWorkflowJSON } from '@/types/comfyWorkflow'
import { useToastStore } from '@/stores/toastStore'

type GroupNodeWorkflowData = {
external: ComfyLink[]
Expand Down Expand Up @@ -75,7 +76,7 @@ class GroupNodeBuilder {
const used = Workflow.isInUseGroupNode(name)
switch (used) {
case Workflow.InUse.InWorkflow:
alert(
useToastStore().addAlert(
'An in use group node with this name already exists embedded in this workflow, please remove any instances or use a new name.'
)
return
Expand Down
3 changes: 2 additions & 1 deletion src/extensions/core/groupNodeManage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
type LGraphNode,
type LGraphNodeConstructor
} from '@comfyorg/litegraph'
import { useToastStore } from '@/stores/toastStore'

const ORDER: symbol = Symbol()
const PREFIX = 'workflow'
Expand Down Expand Up @@ -396,7 +397,7 @@ export class ManageGroupDialog extends ComfyDialog<HTMLDialogElement> {
(n) => n.type === `${PREFIX}${SEPARATOR}` + this.selectedGroup
)
if (node) {
alert(
useToastStore().addAlert(
'This group node is in use in the current workflow, please first remove these.'
)
return
Expand Down
5 changes: 3 additions & 2 deletions src/extensions/core/nodeTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { api } from '../../scripts/api'
import { ComfyDialog, $el } from '../../scripts/ui'
import { GroupNodeConfig, GroupNodeHandler } from './groupNode'
import { LGraphCanvas } from '@comfyorg/litegraph'
import { useToastStore } from '@/stores/toastStore'

// Adds the ability to save and add multiple nodes as a template
// To save:
Expand Down Expand Up @@ -118,7 +119,7 @@ class ManageTemplates extends ComfyDialog {
await api.storeUserData(file, templates, { stringify: false })
} catch (error) {
console.error(error)
alert(error.message)
useToastStore().addAlert(error.message)
}
} else {
localStorage.setItem(id, JSON.stringify(this.templates))
Expand Down Expand Up @@ -151,7 +152,7 @@ class ManageTemplates extends ComfyDialog {

exportAll() {
if (this.templates.length == 0) {
alert('No templates to export.')
useToastStore().addAlert('No templates to export.')
return
}

Expand Down
5 changes: 3 additions & 2 deletions src/extensions/core/uploadAudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { api } from '../../scripts/api'
import type { IWidget } from '@comfyorg/litegraph'
import type { DOMWidget } from '@/scripts/domWidget'
import { ComfyNodeDef } from '@/types/apiTypes'
import { useToastStore } from '@/stores/toastStore'

type FolderType = 'input' | 'output' | 'temp'

Expand Down Expand Up @@ -66,10 +67,10 @@ async function uploadFile(
audioWidget.value = path
}
} else {
alert(resp.status + ' - ' + resp.statusText)
useToastStore().addAlert(resp.status + ' - ' + resp.statusText)
}
} catch (error) {
alert(error)
useToastStore().addAlert(error)
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/extensions/core/webcamCapture.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { app } from '../../scripts/app'
import { api } from '../../scripts/api'
import { useToastStore } from '@/stores/toastStore'

const WEBCAM_READY = Symbol()

Expand Down Expand Up @@ -102,7 +103,7 @@ app.registerExtension({
capture()
} else if (!node.imgs?.length) {
const err = `No webcam image captured`
alert(err)
useToastStore().addAlert(err)
throw new Error(err)
}

Expand All @@ -120,7 +121,7 @@ app.registerExtension({
})
if (resp.status !== 200) {
const err = `Error uploading camera image: ${resp.status} - ${resp.statusText}`
alert(err)
useToastStore().addAlert(err)
throw new Error(err)
}
return `webcam/${name} [temp]`
Expand Down
13 changes: 8 additions & 5 deletions src/scripts/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import {
LGraphCanvas,
LGraph,
LGraphNode,
LiteGraph,
LGraphGroup
LiteGraph
} from '@comfyorg/litegraph'
import { StorageLocation } from '@/types/settingTypes'
import { ExtensionManager } from '@/types/extensionTypes'
Expand All @@ -48,7 +47,7 @@ import {
} from '@/services/dialogService'
import { useSettingStore } from '@/stores/settingStore'
import { useToastStore } from '@/stores/toastStore'
import { ModelStore, useModelStore } from '@/stores/modelStore'
import { useModelStore } from '@/stores/modelStore'
import type { ToastMessageOptions } from 'primevue/toast'
import { useWorkspaceStore } from '@/stores/workspaceStateStore'
import { useExecutionStore } from '@/stores/executionStore'
Expand Down Expand Up @@ -504,7 +503,9 @@ export class ComfyApp {
throw error
}
} catch (error) {
alert('Error copying image: ' + (error.message ?? error))
useToastStore().addAlert(
'Error copying image: ' + (error.message ?? error)
)
}
}
}
Expand Down Expand Up @@ -2301,7 +2302,9 @@ export class ComfyApp {
// TODO: Show validation error in a dialog.
const validatedGraphData = await validateComfyWorkflow(
graphData,
/* onError=*/ alert
/* onError=*/ (err) => {
useToastStore().addAlert(err)
}
)
// If the validation failed, use the original graph data.
// Ideally we should not block users from loading the workflow.
Expand Down
3 changes: 2 additions & 1 deletion src/scripts/logging.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { $el, ComfyDialog } from './ui'
import { api } from './api'
import type { ComfyApp } from './app'
import { useToastStore } from '@/stores/toastStore'

$el('style', {
textContent: `
Expand Down Expand Up @@ -130,7 +131,7 @@ class ComfyLoggingDialog extends ComfyDialog {
throw new Error('Invalid file selected.')
}
} catch (error) {
alert('Unable to load logs: ' + error.message)
useToastStore().addAlert('Unable to load logs: ' + error.message)
}
}
reader.readAsText(fileInput.files[0])
Expand Down
4 changes: 2 additions & 2 deletions src/scripts/ui/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { ComfyApp } from '../app'
import type { Setting, SettingParams } from '@/types/settingTypes'
import { useSettingStore } from '@/stores/settingStore'
import { Settings } from '@/types/apiTypes'
import { useToastStore } from '@/stores/toastStore'

export class ComfySettingsDialog extends ComfyDialog<HTMLDialogElement> {
app: ComfyApp
Expand Down Expand Up @@ -157,8 +158,7 @@ export class ComfySettingsDialog extends ComfyDialog<HTMLDialogElement> {

setSettingValue<K extends keyof Settings>(id: K, value: Settings[K]) {
this.setSettingValueAsync(id, value).catch((err) => {
alert(`Error saving setting '${id}'`)
console.error(err)
useToastStore().addAlert(`Error saving setting '${id}': ${err}`)
})
}

Expand Down
5 changes: 3 additions & 2 deletions src/scripts/widgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ComfyApp } from './app'
import type { IWidget, LGraphNode } from '@comfyorg/litegraph'
import { InputSpec } from '@/types/apiTypes'
import { useSettingStore } from '@/stores/settingStore'
import { useToastStore } from '@/stores/toastStore'

export type ComfyWidgetConstructor = (
node: LGraphNode,
Expand Down Expand Up @@ -578,10 +579,10 @@ export const ComfyWidgets: Record<string, ComfyWidgetConstructor> = {
imageWidget.value = path
}
} else {
alert(resp.status + ' - ' + resp.statusText)
useToastStore().addAlert(resp.status + ' - ' + resp.statusText)
}
} catch (error) {
alert(error)
useToastStore().addAlert(error)
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/scripts/workflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { useExecutionStore } from '@/stores/executionStore'
import { markRaw, toRaw } from 'vue'
import { UserDataFullInfo } from '@/types/apiTypes'
import { useToastStore } from '@/stores/toastStore'

export class ComfyWorkflowManager extends EventTarget {
executionStore: ReturnType<typeof useExecutionStore> | null
Expand Down Expand Up @@ -76,7 +77,9 @@ export class ComfyWorkflowManager extends EventTarget {
}
})
} catch (error) {
alert('Error loading workflows: ' + (error.message ?? error))
useToastStore().addAlert(
'Error loading workflows: ' + (error.message ?? error)
)
}
}

Expand Down Expand Up @@ -227,7 +230,7 @@ export class ComfyWorkflow {
async getWorkflowData() {
const resp = await api.getUserData('workflows/' + this.path)
if (resp.status !== 200) {
alert(
useToastStore().addAlert(
`Error loading workflow file '${this.path}': ${resp.status} ${resp.statusText}`
)
return
Expand Down Expand Up @@ -268,7 +271,7 @@ export class ComfyWorkflow {
this.manager.workflowBookmarkStore?.setBookmarked(this.path, value)
this.manager.dispatchEvent(new CustomEvent('favorite', { detail: this }))
} catch (error) {
alert(
useToastStore().addAlert(
'Error favoriting workflow ' +
this.path +
'\n' +
Expand Down Expand Up @@ -299,7 +302,7 @@ export class ComfyWorkflow {
}

if (resp.status !== 200) {
alert(
useToastStore().addAlert(
`Error renaming workflow file '${this.path}': ${resp.status} ${resp.statusText}`
)
return
Expand Down Expand Up @@ -343,7 +346,7 @@ export class ComfyWorkflow {
}
const resp = await api.deleteUserData('workflows/' + this.path)
if (resp.status !== 204) {
alert(
useToastStore().addAlert(
`Error removing user data file '${this.path}': ${resp.status} ${resp.statusText}`
)
}
Expand Down Expand Up @@ -395,7 +398,7 @@ export class ComfyWorkflow {
}

if (resp.status !== 200) {
alert(
useToastStore().addAlert(
`Error saving workflow '${this.path}': ${resp.status} ${resp.statusText}`
)
return
Expand Down
3 changes: 3 additions & 0 deletions src/stores/toastStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export const useToastStore = defineStore('toast', {
},
removeAll() {
this.removeAllRequested = true
},
addAlert(message: string) {
this.add({ severity: 'warn', summary: 'Alert', detail: message })
}
}
})
8 changes: 8 additions & 0 deletions tests-ui/globalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ module.exports = async function () {
}
})

jest.mock('@/stores/toastStore', () => {
return {
useToastStore: () => ({
addAlert: jest.fn()
})
}
})

jest.mock('vue-i18n', () => {
return {
useI18n: jest.fn()
Expand Down

0 comments on commit 4cc6954

Please sign in to comment.