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

63 lines
1.6 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';
2022-12-24 01:24:28 +09:00
import ColumnSettings from './components/column_settings';
2022-11-22 00:30:53 +09:00
2022-12-20 04:23:04 +09:00
import ActivityPubVisualization from 'activity-pub-visualization';
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-03 03:50:03 +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) => {
dispatch(['add-log-event', JSON.parse(event.data)]);
};
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
2022-12-24 01:24:28 +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}
>
<ColumnSettings clearLog={() => dispatch(['reset-logs'])} />
</ColumnHeader>
2022-12-24 01:24:28 +09:00
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,
};