Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

fix(nc-gui): show success and error state after triggering button action

Ramesh Mane5baac13cb389
diff --git a/packages/nc-gui/components/smartsheet/grid/canvas/cells/Button.ts b/packages/nc-gui/components/smartsheet/grid/canvas/cells/Button.ts
index d673ebd237..5331a3be1b 100644
--- a/packages/nc-gui/components/smartsheet/grid/canvas/cells/Button.ts
+++ b/packages/nc-gui/components/smartsheet/grid/canvas/cells/Button.ts
@@ -239,6 +239,8 @@ export const ButtonCellRenderer: CellRenderer = {
t,
} = props
const isLoading = actionManager.isLoading(pk, column.id!)
+ const isSuccess = actionManager.isSuccess(pk, column.id!)
+ const isError = actionManager.isError(pk, column.id!)
let disabledState = isLoading || disabled?.isInvalid
ctx.textAlign = 'left'
@@ -276,7 +278,7 @@ export const ButtonCellRenderer: CellRenderer = {
})
}
- const hasIcon = !!buttonMeta.icon
+ const hasIcon = !!buttonMeta.icon || isLoading || isSuccess || isError
const hasLabel = !!buttonMeta.label
const maxButtonWidth = width - 8
@@ -339,6 +341,15 @@ export const ButtonCellRenderer: CellRenderer = {
renderSpinner(ctx, contentX, contentY, iconSize, colors.loader, loadingStartTime, 1.5)
contentX += iconSize + (hasLabel ? iconSpacing : 0)
}
+ } else if (isSuccess || isError) {
+ spriteLoader.renderIcon(ctx, {
+ icon: isSuccess ? 'ncCheck' : 'ncInfo',
+ size: iconSize,
+ x: contentX,
+ y: contentY,
+ color: colors.text,
+ })
+ contentX += iconSize + (hasLabel ? iconSpacing : 0)
} else if (hasIcon) {
spriteLoader.renderIcon(ctx, {
icon: buttonMeta.icon,
diff --git a/packages/nc-gui/components/smartsheet/grid/canvas/loaders/ActionManager.ts b/packages/nc-gui/components/smartsheet/grid/canvas/loaders/ActionManager.ts
index 23605696e5..28c16fdffb 100644
--- a/packages/nc-gui/components/smartsheet/grid/canvas/loaders/ActionManager.ts
+++ b/packages/nc-gui/components/smartsheet/grid/canvas/loaders/ActionManager.ts
@@ -38,6 +38,8 @@ export class ActionManager {
// key is rowId-columnId, value is startTime
private loadingColumns = new Map<string, number>()
+ private successColumns = new Map<string, number>()
+ private errorColumns = new Map<string, number>()
private rafId: number | null = null
private getKey(rowId: string, columnId: string): string {
@@ -56,18 +58,69 @@ export class ActionManager {
rowIds.forEach((id) => {
this.loadingColumns.set(this.getKey(id, columnId), startTime)
+
+ // If action is triggered again, clear success and error columns
+ this.successColumns.delete(this.getKey(id, columnId))
+ this.errorColumns.delete(this.getKey(id, columnId))
+
affectedColumnIds.forEach((colId) => {
this.loadingColumns.set(this.getKey(id, colId), startTime)
+
+ // If action is triggered again, clear success and error columns
+ this.successColumns.delete(this.getKey(id, colId))
+ this.errorColumns.delete(this.getKey(id, colId))
})
})
this.startAnimationLoop()
try {
- return await action()
+ const res = await action()
+
+ rowIds.forEach((id) => {
+ this.successColumns.set(this.getKey(id, columnId), startTime)
+
+ affectedColumnIds.forEach((colId) => {
+ this.successColumns.set(this.getKey(id, colId), startTime)
+ })
+ })
+
+ // Remove success columns after 2 seconds
+ ncDelay(2000).then(() => {
+ rowIds.forEach((id) => {
+ this.successColumns.delete(this.getKey(id, columnId))
+
+ affectedColumnIds.forEach((colId) => {
+ this.successColumns.delete(this.getKey(id, colId))
+ })
+ })
+ })
+
+ return res
+ } catch (e) {
+ rowIds.forEach((id) => {
+ this.errorColumns.set(this.getKey(id, columnId), startTime)
+
+ affectedColumnIds.forEach((colId) => {
+ this.errorColumns.set(this.getKey(id, colId), startTime)
+ })
+ })
+
+ // Remove error columns after 3 seconds
+ ncDelay(3000).then(() => {
+ rowIds.forEach((id) => {
+ this.errorColumns.delete(this.getKey(id, columnId))
+
+ affectedColumnIds.forEach((colId) => {
+ this.errorColumns.delete(this.getKey(id, colId))
+ })
+ })
+ })
+ throw e
} finally {
rowIds.forEach((id) => {
this.loadingColumns.delete(this.getKey(id, columnId))
+
affectedColumnIds.forEach((colId) => {
this.loadingColumns.delete(this.getKey(id, colId))
})
@@ -76,18 +129,18 @@ export class ActionManager {
}
private startAnimationLoop() {
- if (this.rafId === null && this.loadingColumns.size > 0) {
+ if (this.rafId === null && (this.loadingColumns.size > 0 || this.successColumns.size > 0 || this.errorColumns.size > 0)) {
let cooldownTimeout: number | null = null
let isCoolingDown = false
const animate = () => {
- if (this.loadingColumns.size > 0 && cooldownTimeout) {
+ if ((this.loadingColumns.size > 0 || this.successColumns.size > 0 || this.errorColumns.size > 0) && cooldownTimeout) {
clearTimeout(cooldownTimeout)
cooldownTimeout = null
isCoolingDown = false
}
- if (this.loadingColumns.size > 0) {
+ if (this.loadingColumns.size > 0 || this.successColumns.size > 0 || this.errorColumns.size > 0) {
this.triggerRefreshCanvas()
this.rafId = requestAnimationFrame(animate)
} else if (!isCoolingDown) {
@@ -211,7 +264,7 @@ export class ActionManager {
break
}
}
- } catch (e) {
+ } catch (e: any) {
console.error('Error executing button action', e)
}
}
@@ -224,6 +277,14 @@ export class ActionManager {
return this.loadingColumns.get(this.getKey(rowId, columnId)) ?? null
}
+ isSuccess(rowId: string, columnId: string): boolean {
+ return this.successColumns.has(this.getKey(rowId, columnId))
+ }
+
+ isError(rowId: string, columnId: string): boolean {
+ return this.errorColumns.has(this.getKey(rowId, columnId))
+ }
+
clear() {
this.loadingColumns.clear()
}