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
61 changes: 61 additions & 0 deletions blocks/chat-panel/src/view.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Chrome for the openclaWP floating admin-bar chat panel.
*
* Keeps to the panel shell only — the conversation surface inside uses
* `@automattic/agenttic-ui` styles unchanged. The confirmation card inside
* the surface ships its own CSS from the chat block bundle.
*/

.openclawp-panel {
position: fixed;
top: 32px;
right: 0;
bottom: 0;
width: 100%;
max-width: 480px;
background: #fff;
box-shadow: -2px 0 12px rgba( 0, 0, 0, 0.08 );
transform: translateX( 100% );
transition: transform 200ms ease;
display: flex;
flex-direction: column;
z-index: 99998;
}

.openclawp-panel.is-open {
transform: translateX( 0 );
}

@media screen and ( max-width: 782px ) {
.openclawp-panel {
top: 46px;
max-width: 100%;
}
}

.openclawp-panel__header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 8px 12px;
border-bottom: 1px solid #dcdcde;
flex: 0 0 auto;
}

.openclawp-panel__title {
margin: 0;
font-size: 14px;
font-weight: 600;
color: #1d2327;
}

.openclawp-panel__body {
flex: 1 1 auto;
overflow: auto;
padding: 12px;
}

.openclawp-panel__empty {
color: #50575e;
font-size: 13px;
}
156 changes: 156 additions & 0 deletions blocks/chat-panel/src/view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* View entry for the openclaWP floating admin-bar chat panel.
*
* Mounts a slide-in drawer pinned to the right edge of the viewport on every
* wp-admin screen. The drawer hosts the shared `<ChatSurface>` so the
* conversation looks and behaves the same as the wp-admin → openclaWP → Chat
* page.
*
* Open/close state:
* - Toggled from the `openclawp-chat-toggle` admin bar item, which
* dispatches a `openclawp:panel:toggle` event on `document` (the toolbar
* wiring is emitted by `OpenclaWP_Admin_Bar_Panel::print_toggle_script()`).
* - Persisted to `localStorage` under `openclawp:panel:open` so the panel
* re-opens on page nav.
* - `Esc` closes the panel when it's focused.
*/

import { createRoot } from 'react-dom/client';
import { useCallback, useEffect, useState } from '@wordpress/element';
import { Button } from '@wordpress/components';
import { close as closeIcon } from '@wordpress/icons';
import ChatSurface from '../../chat-shared/ChatSurface.jsx';
import './view.css';

const STORAGE_KEY = 'openclawp:panel:open';
const TOGGLE_EVENT = 'openclawp:panel:toggle';

function readInitialOpen() {
try {
return window.localStorage.getItem( STORAGE_KEY ) === '1';
} catch ( e ) {
return false;
}
}

function persistOpen( open ) {
try {
window.localStorage.setItem( STORAGE_KEY, open ? '1' : '0' );
} catch ( e ) {
// localStorage may be disabled — panel still works, just won't persist.
}
}

function Panel( { agents, defaultAgent, bridgeUrl, nonce, restNamespace } ) {
const [ isOpen, setIsOpen ] = useState( () => readInitialOpen() );

const close = useCallback( () => {
setIsOpen( false );
persistOpen( false );
}, [] );

const toggle = useCallback( () => {
setIsOpen( ( prev ) => {
const next = ! prev;
persistOpen( next );
return next;
} );
}, [] );

useEffect( () => {
const onToggle = () => toggle();
document.addEventListener( TOGGLE_EVENT, onToggle );
return () => document.removeEventListener( TOGGLE_EVENT, onToggle );
}, [ toggle ] );

useEffect( () => {
if ( ! isOpen ) {
return undefined;
}
const onKey = ( event ) => {
if ( event.key === 'Escape' ) {
close();
}
};
document.addEventListener( 'keydown', onKey );
return () => document.removeEventListener( 'keydown', onKey );
}, [ isOpen, close ] );

return (
<div
className={
'openclawp-panel' + ( isOpen ? ' is-open' : '' )
}
role="dialog"
aria-modal="false"
aria-hidden={ isOpen ? 'false' : 'true' }
aria-label="openclaWP chat"
>
<header className="openclawp-panel__header">
<h2 className="openclawp-panel__title">openclaWP</h2>
<Button
icon={ closeIcon }
label="Close panel"
onClick={ close }
size="small"
/>
</header>
<div className="openclawp-panel__body">
{ agents.length === 0 ? (
<p className="openclawp-panel__empty">
No conversational agents are registered. Register a
general-purpose agent on the
<code> wp_agents_api_init </code>
hook to chat here.
</p>
) : (
<ChatSurface
agents={ agents }
defaultAgent={ defaultAgent }
bridgeUrl={ bridgeUrl }
nonce={ nonce }
restNamespace={ restNamespace }
/>
) }
</div>
</div>
);
}

function mount() {
const root = document.getElementById( 'openclawp-admin-bar-panel-root' );
if ( ! root ) {
return;
}

let agents = [];
try {
agents = JSON.parse( root.dataset.agents || '[]' );
} catch ( e ) {
agents = [];
}

const defaultAgent = root.dataset.defaultAgent || '';
const restNamespace = root.dataset.restNamespace || 'openclawp/v1';
const config = window.openclaWPConfig || {};
const bridgeUrl =
config.bridgeUrl ||
'/wp-json/' + restNamespace + '/agenttic';
const nonce = root.dataset.nonce || config.nonce || '';

createRoot( root ).render(
<Panel
agents={ agents }
defaultAgent={ defaultAgent }
bridgeUrl={ bridgeUrl }
nonce={ nonce }
restNamespace={ restNamespace }
/>
);
}

if ( document.readyState === 'loading' ) {
document.addEventListener( 'DOMContentLoaded', mount );
} else {
mount();
}
Loading
Loading