Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 72 additions & 31 deletions src/components/DevCompanion.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import React, { useEffect, useState } from 'react';
import { initFetchInterceptor, NetworkLog } from '../core/fetchInterceptor';
import { StorageInspector } from './StorageInspector';

export const DevCompanion: React.FC = () => {
const [isOpen, setIsOpen] = useState<boolean>(false);
const [activeTab, setActiveTab] = useState<'network' | 'storage'>('network');
const [logs, setLogs] = useState<NetworkLog[]>([]);

useEffect(() => {
Expand All @@ -26,7 +28,7 @@ export const DevCompanion: React.FC = () => {
boxShadow: '0 4px 12px rgba(0,0,0,0.15)'
}}
>
🛠️ Dev Companion ({logs.length})
Dev Companion ({logs.length})
</button>

{isOpen && (
Expand All @@ -35,44 +37,83 @@ export const DevCompanion: React.FC = () => {
position: 'absolute',
bottom: 50,
right: 0,
width: 360,
height: 300,
width: 380,
height: 340,
background: '#0f172a',
color: '#f8fafc',
borderRadius: '8px',
padding: '12px',
boxShadow: '0 8px 24px rgba(0,0,0,0.3)',
overflowY: 'auto'
display: 'flex',
flexDirection: 'column'
}}
>
<h4 style={{ margin: '0 0 10px 0', borderBottom: '1px solid #334155', paddingBottom: '6px' }}>
Tráfego de API
</h4>
{logs.length === 0 ? (
<p style={{ color: '#94a3b8', fontSize: '13px' }}>Nenhuma requisição capturada ainda.</p>
{/* Main Panel Navigation Header */}
<div style={{ display: 'flex', gap: '8px', borderBottom: '1px solid #334155', paddingBottom: '8px', marginBottom: '8px' }}>
<button
onClick={() => setActiveTab('network')}
style={{
background: activeTab === 'network' ? '#0284c7' : '#1e293b',
color: '#fff',
border: 'none',
padding: '6px 12px',
borderRadius: '4px',
fontSize: '12px',
fontWeight: 'bold',
cursor: 'pointer'
}}
>
Network
</button>
<button
onClick={() => setActiveTab('storage')}
style={{
background: activeTab === 'storage' ? '#0284c7' : '#1e293b',
color: '#fff',
border: 'none',
padding: '6px 12px',
borderRadius: '4px',
fontSize: '12px',
fontWeight: 'bold',
cursor: 'pointer'
}}
>
Storage
</button>
</div>

{/* Panel Content */}
{activeTab === 'network' ? (
<div style={{ flex: 1, overflowY: 'auto' }}>
{logs.length === 0 ? (
<p style={{ color: '#94a3b8', fontSize: '13px' }}>No requests captured yet.</p>
) : (
logs.map((log: NetworkLog) => (
<div
key={log.id}
style={{
display: 'flex',
justifyContent: 'space-between',
padding: '6px 0',
borderBottom: '1px solid #1e293b',
fontSize: '12px'
}}
>
<span style={{ fontWeight: 'bold', color: log.method === 'GET' ? '#38bdf8' : '#4ade80' }}>
[{log.method}]
</span>
<span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', maxWidth: '180px' }}>
{log.url}
</span>
<span style={{ color: log.status && log.status >= 400 ? '#f87171' : '#facc15' }}>
{log.status || 'PENDING'}
</span>
</div>
))
)}
</div>
) : (
logs.map((log: NetworkLog) => (
<div
key={log.id}
style={{
display: 'flex',
justifyContent: 'space-between',
padding: '6px 0',
borderBottom: '1px solid #1e293b',
fontSize: '12px'
}}
>
<span style={{ fontWeight: 'bold', color: log.method === 'GET' ? '#38bdf8' : '#4ade80' }}>
[{log.method}]
</span>
<span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', maxWidth: '180px' }}>
{log.url}
</span>
<span style={{ color: log.status && log.status >= 400 ? '#f87171' : '#facc15' }}>
{log.status || 'PENDING'}
</span>
</div>
))
<StorageInspector />
)}
</div>
)}
Expand Down
118 changes: 118 additions & 0 deletions src/components/StorageInspector.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import React, { useState, useEffect } from 'react';
import {
StorageItem,
getLocalStorageItems,
getSessionStorageItems,
getCookieItems,
deleteStorageItem,
setStorageItem
} from '../core/storageManager';

export const StorageInspector: React.FC = () => {
const [activeTab, setActiveTab] = useState<'local' | 'session' | 'cookie'>('local');
const [items, setItems] = useState<StorageItem[]>([]);
const [newKey, setNewKey] = useState('');
const [newValue, setNewValue] = useState('');

const refreshItems = () => {
if (activeTab === 'local') setItems(getLocalStorageItems());
else if (activeTab === 'session') setItems(getSessionStorageItems());
else if (activeTab === 'cookie') setItems(getCookieItems());
};

useEffect(() => {
refreshItems();
}, [activeTab]);

const handleDelete = (key: string) => {
deleteStorageItem(key, activeTab);
refreshItems();
};

const handleAdd = (e: React.FormEvent) => {
e.preventDefault();
if (!newKey.trim()) return;
setStorageItem(newKey.trim(), newValue.trim(), activeTab);
setNewKey('');
setNewValue('');
refreshItems();
};

return (
<div style={{ display: 'flex', flexDirection: 'column', gap: '10px' }}>
{/* Tab Selectors */}
<div style={{ display: 'flex', gap: '6px', borderBottom: '1px solid #334155', paddingBottom: '6px' }}>
{(['local', 'session', 'cookie'] as const).map((tab) => (
<button
key={tab}
onClick={() => setActiveTab(tab)}
style={{
background: activeTab === tab ? '#3b82f6' : '#1e293b',
color: '#fff',
border: 'none',
padding: '4px 8px',
borderRadius: '4px',
fontSize: '11px',
cursor: 'pointer'
}}
>
{tab.toUpperCase()}
</button>
))}
</div>

{/* Add Item Form */}
<form onSubmit={handleAdd} style={{ display: 'flex', gap: '4px' }}>
<input
placeholder="Key"
value={newKey}
onChange={(e) => setNewKey(e.target.value)}
style={{ flex: 1, padding: '4px', fontSize: '11px', background: '#1e293b', color: '#fff', border: '1px solid #334155', borderRadius: '4px' }}
/>
<input
placeholder="Value"
value={newValue}
onChange={(e) => setNewValue(e.target.value)}
style={{ flex: 1, padding: '4px', fontSize: '11px', background: '#1e293b', color: '#fff', border: '1px solid #334155', borderRadius: '4px' }}
/>
<button
type="submit"
style={{ background: '#22c55e', color: '#fff', border: 'none', padding: '4px 8px', borderRadius: '4px', fontSize: '11px', cursor: 'pointer' }}
>
Add
</button>
</form>

{/* List */}
<div style={{ maxHeight: '180px', overflowY: 'auto' }}>
{items.length === 0 ? (
<p style={{ color: '#94a3b8', fontSize: '12px' }}>No items found.</p>
) : (
items.map((item) => (
<div
key={item.key}
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
padding: '4px 0',
borderBottom: '1px solid #1e293b',
fontSize: '11px'
}}
>
<div style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', maxWidth: '220px' }}>
<strong style={{ color: '#38bdf8' }}>{item.key}:</strong> {item.value}
</div>
<button
onClick={() => handleDelete(item.key)}
style={{ background: '#ef4444', color: '#fff', border: 'none', padding: '2px 6px', borderRadius: '4px', fontSize: '10px', cursor: 'pointer' }}
>
Del
</button>
</div>
))
)}
</div>
</div>
);
};
59 changes: 59 additions & 0 deletions src/core/storageManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export interface StorageItem {
key: string;
value: string;
type: 'local' | 'session' | 'cookie';
}

export const getLocalStorageItems = (): StorageItem[] => {
const items: StorageItem[] = [];
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key) {
items.push({ key, value: localStorage.getItem(key) || '', type: 'local' });
}
}
return items;
};

export const getSessionStorageItems = (): StorageItem[] => {
const items: StorageItem[] = [];
for (let i = 0; i < sessionStorage.length; i++) {
const key = sessionStorage.key(i);
if (key) {
items.push({ key, value: sessionStorage.getItem(key) || '', type: 'session' });
}
}
return items;
};

export const getCookieItems = (): StorageItem[] => {
if (!document.cookie) return [];
return document.cookie.split('; ').map((cookie) => {
const [key, ...val] = cookie.split('=');
return {
key: key.trim(),
value: decodeURIComponent(val.join('=')),
type: 'cookie'
};
});
};

export const deleteStorageItem = (key: string, type: 'local' | 'session' | 'cookie') => {
if (type === 'local') {
localStorage.removeItem(key);
} else if (type === 'session') {
sessionStorage.removeItem(key);
} else if (type === 'cookie') {
document.cookie = `${key}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
}
};

export const setStorageItem = (key: string, value: string, type: 'local' | 'session' | 'cookie') => {
if (type === 'local') {
localStorage.setItem(key, value);
} else if (type === 'session') {
sessionStorage.setItem(key, value);
} else if (type === 'cookie') {
document.cookie = `${key}=${encodeURIComponent(value)}; path=/;`;
}
};
Loading