Skip to content

Commit

Permalink
🐛 Make sure files are not broken in preview if visibility is Private
Browse files Browse the repository at this point in the history
Closes #1788
  • Loading branch information
baptisteArno committed Sep 12, 2024
1 parent 19b3148 commit 3f15c26
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/embeds/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/js",
"version": "0.3.13",
"version": "0.3.14",
"description": "Javascript library to display typebots on your website",
"type": "module",
"main": "dist/index.js",
Expand Down
8 changes: 5 additions & 3 deletions packages/embeds/js/src/components/bubbles/GuestBubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ const TextGuestBubble = (props: { answer: TextInputSubmitContent }) => {
>
{(attachment, idx) => (
<img
src={attachment.url}
src={attachment.blobUrl ?? attachment.url}
alt={`Attached image ${idx() + 1}`}
class={clsx(
'typebot-guest-bubble-image-attachment cursor-pointer',
props.answer.attachments!.filter((attachment) =>
attachment.type.startsWith('image')
).length > 1 && 'max-w-[90%]'
)}
onClick={() => setClickedImageSrc(attachment.url)}
onClick={() =>
setClickedImageSrc(attachment.blobUrl ?? attachment.url)
}
/>
)}
</For>
Expand Down Expand Up @@ -132,7 +134,7 @@ const AudioGuestBubble = (props: { answer: RecordingInputSubmitContent }) => {
class="p-2 w-full whitespace-pre-wrap typebot-guest-bubble flex flex-col"
data-testid="guest-bubble"
>
<audio controls src={props.answer.url} />
<audio controls src={props.answer.blobUrl ?? props.answer.url} />
</div>
</div>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ export const FileUploadForm = (props: Props) => {
props.block.options?.labels?.success?.single ??
defaultFileInputOptions.labels.success.single,
value: urls[0] ? encodeUrl(urls[0].url) : '',
attachments: [{ type: file.type, url: urls[0]!.url }],
attachments: [
{
type: file.type,
url: urls[0]!.url,
blobUrl: URL.createObjectURL(file),
},
],
})
toaster.create({ description: 'An error occured while uploading the file' })
}
Expand Down Expand Up @@ -121,7 +127,16 @@ export const FileUploadForm = (props: Props) => {
.filter(isDefined)
.map(({ url }) => encodeUrl(url))
.join(', '),
attachments: urls.filter(isDefined),
attachments: urls
.map((urls, index) =>
urls
? {
...urls,
blobUrl: URL.createObjectURL(selectedFiles()[index]),
}
: null
)
.filter(isDefined),
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,16 @@ export const TextInput = (props: Props) => {
})),
onUploadProgress: setUploadProgress,
})
attachments = urls?.filter(isDefined)
attachments = urls
?.map((urls, index) =>
urls
? {
...urls,
blobUrl: URL.createObjectURL(selectedFiles()[index]),
}
: null
)
.filter(isDefined)
}
props.onSubmit({
type: 'text',
Expand Down Expand Up @@ -219,6 +228,7 @@ export const TextInput = (props: Props) => {
props.onSubmit({
type: 'recording',
url: urls[0],
blobUrl: URL.createObjectURL(audioFile),
})
}
mediaRecorder.start()
Expand Down
2 changes: 2 additions & 0 deletions packages/embeds/js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type ChatChunk = Pick<
export type Attachment = {
type: string
url: string
blobUrl?: string
}

export type TextInputSubmitContent = {
Expand All @@ -42,6 +43,7 @@ export type TextInputSubmitContent = {
export type RecordingInputSubmitContent = {
type: 'recording'
url: string
blobUrl?: string
}

export type InputSubmitContent =
Expand Down
18 changes: 17 additions & 1 deletion packages/embeds/js/src/utils/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,23 @@ export function persist<T>(
const storage = parseRememberUserStorage(
params.storage || defaultSettings.general.rememberUser.storage
)
const serialize: (data: T) => string = JSON.stringify.bind(JSON)
const serialize: (data: T) => string = (data: T) => {
const clonedData = JSON.parse(JSON.stringify(data))

if ('blobUrl' in clonedData) {
clonedData.blobUrl = undefined
}

if ('attachments' in clonedData && Array.isArray(clonedData.attachments)) {
clonedData.attachments.forEach((attachment: any) => {
if (attachment && 'blobUrl' in attachment) {
attachment.blobUrl = undefined
}
})
}

return JSON.stringify(clonedData)
}
const deserialize: (data: string) => T = JSON.parse.bind(JSON)
const init = storage.getItem(params.key)
const set =
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/nextjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/nextjs",
"version": "0.3.13",
"version": "0.3.14",
"description": "Convenient library to display typebots on your Next.js website",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/react",
"version": "0.3.13",
"version": "0.3.14",
"description": "Convenient library to display typebots on your React app",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 3f15c26

Please sign in to comment.