activitypub-academy/app/javascript/mastodon/features/activity_log/index.js

65 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-01-03 03:50:03 +09:00
import React, { useEffect, useReducer, useRef } from 'react';
2022-12-24 01:24:28 +09:00
import PropTypes from 'prop-types';
import Column from 'mastodon/components/column';
import ColumnHeader from 'mastodon/components/column_header';
2023-01-02 23:34:54 +09:00
import dummy_data from './dummy-data.json';
2023-01-09 22:46:10 +09:00
import ActivityPubVisualization from 'activitypub-visualization';
2022-12-20 04:23:04 +09:00
2023-01-03 03:50:03 +09:00
export default function ActivityLog({ multiColumn }) {
2022-11-22 00:30:53 +09:00
2023-01-03 03:50:03 +09:00
const [logs, dispatch] = useReducer((state, [type, data]) => {
switch (type) {
case 'add-log-event':
return [...state, data];
case 'reset-logs':
return [];
default:
return state;
2022-11-30 03:10:06 +09:00
}
2023-01-09 22:39:38 +09:00
// }, dummy_data);
}, []);
2022-11-30 03:10:06 +09:00
2023-01-03 03:50:03 +09:00
const columnElement = useRef(null);
2022-12-24 01:24:28 +09:00
2023-01-03 03:50:03 +09:00
useEffect(() => {
const eventSource = new EventSource('/api/v1/activity_log');
eventSource.onmessage = (event) => {
2023-01-09 22:39:38 +09:00
const parsed = JSON.parse(event.data);
if (parsed.type !== 'keep-alive') {
dispatch(['add-log-event', parsed]);
}
2023-01-03 03:50:03 +09:00
};
2022-12-24 01:24:28 +09:00
2023-01-03 03:50:03 +09:00
return function() {
eventSource.close();
};
}, []);
2022-12-22 03:40:05 +09:00
2023-01-03 03:50:03 +09:00
const darkMode = !(document.body && document.body.classList.contains('theme-mastodon-light'));
2022-12-24 01:24:28 +09:00
2023-01-03 03:50:03 +09:00
return (
<Column bindToDocument={!multiColumn} ref={columnElement} label='Activity Log'>
<ColumnHeader
icon='comments'
title='Activity Log'
onClick={() => { columnElement.current.scrollTop() }}
multiColumn={multiColumn}
2023-01-03 18:03:52 +09:00
/>
2022-12-24 01:24:28 +09:00
2023-01-09 22:39:38 +09:00
<button onClick={() => navigator.clipboard.writeText(JSON.stringify(logs, null, 2))}>Copy logs</button>
<button onClick={() => dispatch(['reset-logs'])}>Clear logs</button>
2023-01-03 03:50:03 +09:00
<div className={`${darkMode ? 'dark' : ''}`}>
<ActivityPubVisualization logs={logs} />
</div>
2022-12-24 01:24:28 +09:00
2023-01-03 03:50:03 +09:00
</Column>
);
2022-12-09 01:57:22 +09:00
}
2023-01-03 03:50:03 +09:00
ActivityLog.propTypes = {
multiColumn: PropTypes.bool,
};