Skip to content

Commit

Permalink
🐛 Fix inconsistent updatedAt when timezone is different fro…
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed May 4, 2024
1 parent 86441a5 commit ad4d1a1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
6 changes: 3 additions & 3 deletions apps/builder/src/features/editor/components/TypebotHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ const RightElements = ({
}: StackProps & { isResultsDisplayed: boolean }) => {
const router = useRouter()
const { t } = useTranslate()
const { typebot, currentUserMode, save } = useTypebot()
const { typebot, currentUserMode, save, isSavingLoading } = useTypebot()
const {
setRightPanel,
rightPanel,
Expand All @@ -264,7 +264,7 @@ const RightElements = ({
const handlePreviewClick = async () => {
setStartPreviewAtGroup(undefined)
setStartPreviewAtEvent(undefined)
save().then()
await save()
setRightPanel(RightPanel.PREVIEW)
}

Expand All @@ -282,7 +282,7 @@ const RightElements = ({
<Button
colorScheme="gray"
onClick={handlePreviewClick}
isLoading={isNotDefined(typebot)}
isLoading={isNotDefined(typebot) || isSavingLoading}
leftIcon={<PlayIcon />}
size="sm"
iconSpacing={{ base: 0, xl: 2 }}
Expand Down
16 changes: 15 additions & 1 deletion apps/builder/src/features/editor/hooks/useUndo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { isDefined } from '@typebot.io/lib'

export interface Actions<T extends { updatedAt: Date }> {
set: (newPresent: T | ((current: T) => T) | undefined) => void
setUpdateDate: (updateDate: Date) => void
undo: () => void
redo: () => void
flush: () => void
Expand Down Expand Up @@ -107,6 +108,16 @@ export const useUndo = <T extends { updatedAt: Date }>(
[history, params?.isReadOnly]
)

const setUpdateDate = useCallback(
(updatedAt: Date) => {
set((current) => ({
...current,
updatedAt,
}))
},
[set]
)

const flush = useCallback(() => {
if (params?.isReadOnly) return
setHistory({
Expand All @@ -116,5 +127,8 @@ export const useUndo = <T extends { updatedAt: Date }>(
})
}, [params?.isReadOnly])

return [history.present, { set, undo, redo, flush, canUndo, canRedo }]
return [
history.present,
{ set, undo, redo, flush, setUpdateDate, canUndo, canRedo },
]
}
25 changes: 21 additions & 4 deletions apps/builder/src/features/editor/providers/TypebotProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,15 @@ export const TypebotProvider = ({

const [
localTypebot,
{ redo, undo, flush, canRedo, canUndo, set: setLocalTypebot },
{
redo,
undo,
flush,
canRedo,
canUndo,
set: setLocalTypebot,
setUpdateDate,
},
] = useUndo<TypebotV6>(undefined, {
isReadOnly,
onUndo: (t) => {
Expand Down Expand Up @@ -216,24 +224,33 @@ export const TypebotProvider = ({
const typebotToSave = {
...localTypebot,
...updates,
updatedAt: new Date(),
}
if (dequal(omit(typebot, 'updatedAt'), omit(typebotToSave, 'updatedAt')))
return
const newParsedTypebot = typebotV6Schema.parse({ ...typebotToSave })
setLocalTypebot(newParsedTypebot)
try {
await updateTypebot({
const {
typebot: { updatedAt },
} = await updateTypebot({
typebotId: newParsedTypebot.id,
typebot: newParsedTypebot,
})
setUpdateDate(updatedAt)
} catch {
setLocalTypebot({
...localTypebot,
})
}
},
[isReadOnly, localTypebot, setLocalTypebot, typebot, updateTypebot]
[
isReadOnly,
localTypebot,
setLocalTypebot,
setUpdateDate,
typebot,
updateTypebot,
]
)

useAutoSave(
Expand Down
4 changes: 1 addition & 3 deletions apps/builder/src/features/typebot/api/updateTypebot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ export const updateTypebot = authenticatedProcedure

if (
typebot.updatedAt &&
new Date(existingTypebot?.updatedAt).getTime() >
typebot.updatedAt.getTime()
existingTypebot.updatedAt.getTime() > typebot.updatedAt.getTime()
)
throw new TRPCError({
code: 'CONFLICT',
Expand Down Expand Up @@ -197,7 +196,6 @@ export const updateTypebot = authenticatedProcedure
}),
isClosed: typebot.isClosed,
whatsAppCredentialsId: typebot.whatsAppCredentialsId ?? undefined,
updatedAt: typebot.updatedAt,
},
})

Expand Down

0 comments on commit ad4d1a1

Please sign in to comment.