Skip to content

Commit

Permalink
feat: 🔥 Remove save button
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Feb 21, 2022
1 parent fd822a3 commit 1b900b3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const GoogleSheetsSettingsBody = ({
onOptionsChange,
stepId,
}: Props) => {
const { save, hasUnsavedChanges } = useTypebot()
const { save } = useTypebot()
const { sheets, isLoading } = useSheets({
credentialsId: options?.credentialsId,
spreadsheetId: options?.spreadsheetId,
Expand Down Expand Up @@ -83,10 +83,7 @@ export const GoogleSheetsSettingsBody = ({
}

const handleCreateNewClick = async () => {
if (hasUnsavedChanges) {
const errorToastId = await save()
if (errorToastId) return
}
await save()
const linkElement = document.createElement('a')
linkElement.href = getGoogleSheetsConsentScreenUrl(
window.location.href,
Expand Down
33 changes: 0 additions & 33 deletions apps/builder/components/shared/TypebotHeader/SaveButton.tsx

This file was deleted.

32 changes: 26 additions & 6 deletions apps/builder/components/shared/TypebotHeader/TypebotHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Flex, HStack, Button, IconButton, Tooltip } from '@chakra-ui/react'
import {
Flex,
HStack,
Button,
IconButton,
Tooltip,
Spinner,
Text,
} from '@chakra-ui/react'
import { ChevronLeftIcon, RedoIcon, UndoIcon } from 'assets/icons'
import { NextChakraLink } from 'components/nextChakra/NextChakraLink'
import { RightPanel, useEditor } from 'contexts/EditorContext'
Expand All @@ -7,7 +15,6 @@ import { useRouter } from 'next/router'
import React from 'react'
import { PublishButton } from '../buttons/PublishButton'
import { EditableTypebotName } from './EditableTypebotName'
import { SaveButton } from './SaveButton'

export const headerHeight = 56

Expand All @@ -21,7 +28,7 @@ export const TypebotHeader = () => {
redo,
canUndo,
canRedo,
publishedTypebot,
isSavingLoading,
} = useTypebot()
const { setRightPanel } = useEditor()

Expand Down Expand Up @@ -96,7 +103,13 @@ export const TypebotHeader = () => {
</Button>
)}
</HStack>
<Flex pos="absolute" left="1rem" justify="center" align="center">
<HStack
pos="absolute"
left="1rem"
justify="center"
align="center"
spacing="6"
>
<HStack alignItems="center">
<IconButton
aria-label="Back"
Expand Down Expand Up @@ -129,10 +142,17 @@ export const TypebotHeader = () => {
/>
</Tooltip>
</HStack>
</Flex>
{isSavingLoading && (
<HStack>
<Spinner speed="0.7s" size="sm" color="gray.400" />
<Text fontSize="sm" color="gray.400">
Saving...
</Text>
</HStack>
)}
</HStack>

<HStack right="40px" pos="absolute">
<SaveButton />
<Button onClick={handlePreviewClick}>Preview</Button>
<PublishButton />
</HStack>
Expand Down
43 changes: 25 additions & 18 deletions apps/builder/contexts/TypebotContext/TypebotContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ToastId, useToast } from '@chakra-ui/react'
import { useToast } from '@chakra-ui/react'
import { PublicTypebot, Settings, Theme, Typebot } from 'models'
import { useRouter } from 'next/router'
import {
Expand Down Expand Up @@ -32,7 +32,8 @@ import useUndo from 'services/utils/useUndo'
import { useDebounce } from 'use-debounce'
import { itemsAction, ItemsActions } from './actions/items'
import { generate } from 'short-uuid'
const autoSaveTimeout = 40000
import { deepEqual } from 'fast-equals'
const autoSaveTimeout = 10000

type UpdateTypebotPayload = Partial<{
theme: Theme
Expand All @@ -49,9 +50,8 @@ const typebotContext = createContext<
publishedTypebot?: PublicTypebot
isPublished: boolean
isPublishing: boolean
hasUnsavedChanges: boolean
isSavingLoading: boolean
save: () => Promise<ToastId | undefined>
save: () => Promise<void>
undo: () => void
redo: () => void
canRedo: boolean
Expand Down Expand Up @@ -93,6 +93,13 @@ export const TypebotContext = ({
}),
})

useEffect(() => {
if (!typebot || !localTypebot || deepEqual(typebot, localTypebot)) return
if (typebot?.blocks.length === localTypebot?.blocks.length)
setLocalTypebot({ ...typebot })
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [typebot])

const [
{ present: localTypebot },
{
Expand All @@ -106,12 +113,16 @@ export const TypebotContext = ({
] = useUndo<Typebot | undefined>(undefined)

const saveTypebot = async () => {
if (deepEqual(typebot, localTypebot)) return
const typebotToSave = currentTypebotRef.current
if (!typebotToSave) return
setIsSavingLoading(true)
const { error } = await updateTypebot(typebotToSave.id, typebotToSave)
setIsSavingLoading(false)
if (error) return toast({ title: error.name, description: error.message })
if (error) {
toast({ title: error.name, description: error.message })
return
}
mutate({ typebot: typebotToSave })
window.removeEventListener('beforeunload', preventUserFromRefreshing)
}
Expand All @@ -130,18 +141,9 @@ export const TypebotContext = ({
})
}

const hasUnsavedChanges = useMemo(
() =>
isDefined(typebot) &&
isDefined(localTypebot) &&
!checkIfTypebotsAreEqual(localTypebot, typebot),
[typebot, localTypebot]
)

useAutoSave({
handler: saveTypebot,
item: localTypebot,
canSave: hasUnsavedChanges,
debounceTimeout: autoSaveTimeout,
})

Expand Down Expand Up @@ -257,7 +259,6 @@ export const TypebotContext = ({
value={{
typebot: localTypebot,
publishedTypebot,
hasUnsavedChanges,
isSavingLoading,
save: saveTypebot,
undo,
Expand Down Expand Up @@ -306,18 +307,24 @@ export const useFetchedTypebot = ({
const useAutoSave = <T,>({
handler,
item,
canSave,
debounceTimeout,
}: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
handler: (item?: T) => Promise<any>
item?: T
canSave: boolean
debounceTimeout: number
}) => {
const [debouncedItem] = useDebounce(item, debounceTimeout)
useEffect(() => {
const save = () => handler(item)
document.addEventListener('visibilitychange', save)
return () => {
document.removeEventListener('visibilitychange', save)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
return useEffect(() => {
if (canSave) handler(item)
handler(item)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [debouncedItem])
}

2 comments on commit 1b900b3

@vercel
Copy link

@vercel vercel bot commented on 1b900b3 Feb 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

app.typebot.io
builder-v2-typebot-io.vercel.app
builder-v2-git-main-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 1b900b3 Feb 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.