MCPcopy Create free account
hub / github.com/Datakitpage/Datakit / useDemoFileDrops

Function useDemoFileDrops

frontend/src/hooks/useDemoFileDrops.ts:12–77  ·  view source on GitHub ↗
({ processFile, addFile, analytics }: UseDemoFileDropsProps)

Source from the content-addressed store, hash-verified

10}
11
12export const useDemoFileDrops = ({ processFile, addFile, analytics }: UseDemoFileDropsProps) => {
13 useEffect(() => {
14 const handleDemoFileDrops = (event: MessageEvent) => {
15 console.log('Home.tsx received message:', event.data, 'from origin:', event.origin)
16
17 // Security: only accept from your demo domains
18 const allowedOrigins = [
19 'https://datakit.studio',
20 'http://localhost:5174'
21 ]
22
23 const isOriginAllowed = allowedOrigins.some(origin => {
24 if (origin.includes('*')) {
25 const baseOrigin = origin.replace('*', '')
26 return event.origin.endsWith(baseOrigin)
27 }
28 return event.origin === origin
29 })
30
31 if (!isOriginAllowed) {
32 console.log('Origin not allowed:', event.origin)
33 return
34 }
35
36 // Handle handshake from parent iframe
37 if (event.data.type === 'DEMO_READY') {
38 console.log('Received DEMO_READY, sending confirmation back')
39 event.source?.postMessage({
40 type: 'DATAKIT_READY',
41 message: 'DataKit ready to receive files'
42 }, event.origin as WindowPostMessageOptions)
43 return
44 }
45
46 if (event.data.type === 'FILE_DROP') {
47 console.log('Processing FILE_DROP message:', event.data)
48 const { file } = event.data
49
50 // Convert base64 data back to File object
51 const binaryString = atob(file.data)
52 const bytes = new Uint8Array(binaryString.length)
53 for (let i = 0; i < binaryString.length; i++) {
54 bytes[i] = binaryString.charCodeAt(i)
55 }
56
57 const recreatedFile = new File([bytes], file.name, { type: file.type })
58
59 // Use existing file processing logic
60 processFile(recreatedFile, (result) => {
61 addFile(result)
62 analytics.trackFileUpload(result)
63 })
64
65 // Send success confirmation back to demo
66 event.source?.postMessage({
67 type: 'FILE_PROCESSED',
68 fileName: file.name,
69 success: true

Callers 1

useHomePageLogicFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected