bigwolfe commited on
Commit
f1f6d9b
·
1 Parent(s): d200fc9

Update widget component

Browse files
Files changed (1) hide show
  1. frontend/src/widget.tsx +59 -51
frontend/src/widget.tsx CHANGED
@@ -62,62 +62,70 @@ const WidgetApp = () => {
62
  const [error, setError] = useState<string | null>(null);
63
 
64
  useEffect(() => {
65
- // In a real ChatGPT app, toolOutput is injected before script execution or available on load.
66
- // We check it here.
67
- const toolOutput = window.openai.toolOutput;
68
- console.log("Widget loaded with toolOutput:", toolOutput);
69
-
70
- if (!toolOutput) {
71
- // Check for toolInput to show loading state (waiting for tool execution)
72
- // Note: window.openai types might vary, we check safely
73
- const toolInput = (window.openai as any).toolInput;
74
- if (toolInput) {
75
- console.log("Tool input present, waiting for output:", toolInput);
76
- setView('loading');
77
- return;
 
 
 
 
78
  }
 
 
79
 
80
- // Fallback for dev testing via URL
81
- const params = new URLSearchParams(window.location.search);
82
- const mockType = params.get('type');
83
- if (mockType === 'note') {
84
- // Mock note data
85
- setView('note');
86
- setData({
87
- title: "Demo Note",
88
- note_path: "demo.md",
89
- body: "# Demo Note\n\nThis is a **markdown** note rendered in the widget.",
90
- version: 1,
91
- size_bytes: 100,
92
- created: new Date().toISOString(),
93
- updated: new Date().toISOString(),
94
- metadata: {}
95
- });
96
- } else if (mockType === 'search') {
97
- setView('search');
98
- setData([
99
- { title: "Result 1", note_path: "res1.md", snippet: "Found match...", score: 1.0, updated: new Date() }
100
- ]);
101
- } else {
102
- setError("No content data found. (window.openai.toolOutput is empty)");
103
- setView('error');
104
- }
105
- return;
106
- }
107
 
108
- // Detect content type based on shape
109
- if (toolOutput.note) {
110
- setView('note');
111
- setData(toolOutput.note);
112
- } else if (toolOutput.results) {
113
- setView('search');
114
- setData(toolOutput.results);
115
  } else {
116
- // Fallback: try to guess or show raw
117
- console.warn("Unknown tool output format", toolOutput);
118
- setError("Unknown content format.");
119
- setView('error');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  }
 
 
 
 
 
 
 
121
  }, []);
122
 
123
  const handleWikilinkClick = (linkText: string) => {
 
62
  const [error, setError] = useState<string | null>(null);
63
 
64
  useEffect(() => {
65
+ // Function to check for data
66
+ const checkData = () => {
67
+ const toolOutput = window.openai.toolOutput;
68
+ if (toolOutput) {
69
+ console.log("Tool output found:", toolOutput);
70
+ if (toolOutput.note) {
71
+ setView('note');
72
+ setData(toolOutput.note);
73
+ } else if (toolOutput.results) {
74
+ setView('search');
75
+ setData(toolOutput.results);
76
+ } else {
77
+ console.warn("Unknown tool output format", toolOutput);
78
+ setError("Unknown content format.");
79
+ setView('error');
80
+ }
81
+ return true;
82
  }
83
+ return false;
84
+ };
85
 
86
+ // Check immediately
87
+ if (checkData()) return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
+ // If not found, check if we have toolInput (loading state)
90
+ // Note: window.openai types might vary, we check safely
91
+ const toolInput = (window.openai as any).toolInput;
92
+ if (toolInput) {
93
+ console.log("Tool input present, waiting for output:", toolInput);
94
+ setView('loading');
 
95
  } else {
96
+ // Fallback for dev testing via URL (only if no toolInput either)
97
+ const params = new URLSearchParams(window.location.search);
98
+ const mockType = params.get('type');
99
+ if (mockType === 'note') {
100
+ // ... existing mock logic ...
101
+ setView('note');
102
+ setData({
103
+ title: "Demo Note",
104
+ note_path: "demo.md",
105
+ body: "# Demo Note\n\nMock note.",
106
+ version: 1,
107
+ size_bytes: 100,
108
+ created: new Date().toISOString(),
109
+ updated: new Date().toISOString(),
110
+ metadata: {}
111
+ });
112
+ return;
113
+ } else if (mockType === 'search') {
114
+ setView('search');
115
+ setData([{ title: "Result 1", note_path: "res1.md", snippet: "Match", score: 1, updated: new Date() }]);
116
+ return;
117
+ }
118
+
119
+ // If absolutely nothing, show error (or keep loading if we suspect latency)
120
+ // But sticking to 'loading' is safer than error if we are polling.
121
  }
122
+
123
+ // Poll for data injection
124
+ const interval = setInterval(() => {
125
+ if (checkData()) clearInterval(interval);
126
+ }, 500);
127
+
128
+ return () => clearInterval(interval);
129
  }, []);
130
 
131
  const handleWikilinkClick = (linkText: string) => {