Skip to content
Prev Previous commit
Next Next commit
Create and use the dataview component
  • Loading branch information
manzoorwanijk committed Sep 4, 2024
commit 1ea7f080b704b2e2341dc59ae329626833c83e89
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { useSelect } from '@wordpress/data';
import { DataViews } from '@wordpress/dataviews';
import { getDate, humanTimeDiff } from '@wordpress/date';
import { __ } from '@wordpress/i18n';
import { store as socialStore } from '../../social-store';
import { ShareStatusItem } from '../../social-store/types';
import ConnectionIcon from '../connection-icon';
import { ShareStatusAction } from './share-status-action';
import { ShareStatusLabel } from './share-status-label';
import styles from './styles.module.scss';

const getItemId = ( item: ShareStatusItem ) => {
return `${ item.external_id || item.connection_id }:${ item.timestamp }`;
};

const noop = () => {};

/**
* The component for the shares data view.
*
* @return {import('react').ReactNode} - The shares data view component.
*/
export function SharesDataView() {
const shareStatus = useSelect( select => select( socialStore ).getPostShareStatus(), [] );

return (
<div className={ styles[ 'dataview-wrapper' ] }>
<DataViews
getItemId={ getItemId }
fields={ [
{
id: 'connection',
label: __( 'Connection', 'jetpack' ),
render: ( { item } ) => (
<div className={ styles[ 'connection-name' ] }>
<ConnectionIcon
serviceName={ item.service }
label={ item.external_name }
profilePicture={ item.profile_picture }
/>
<div className={ styles[ 'share-item-name-wrapper' ] }>
<div className={ styles[ 'share-item-name' ] }>{ item.external_name }</div>
</div>
</div>
),
enableSorting: false,
enableHiding: false,
},
{
id: 'timestamp',
label: __( 'Time', 'jetpack' ),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to call this Shared at or something similar

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then the continuation of the sentence doesn't read nice "Shared at 2 minutes ago", where as, "Time" can be read as its own clause.

Copy link
Contributor

@gmjuhasz gmjuhasz Sep 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, time feels too vague, also I looked up on the designs it's 'Published at' on the admin page, maybe that's where I got the idea from :D I think Shared at still makes more sense than published at. Either way we should use the same term here and there in the future, even if that's time

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But what about the case of failure? We can't say "Shared at".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a point we have to make on the admin page designs too, but let's leave time now then

render: ( { item } ) => {
return humanTimeDiff(
// @ts-expect-error - humanTimeDiff is incorrectly typed, first argument can be a timestamp
item.timestamp * 1000,
getDate( null )
);
},
enableSorting: false,
enableHiding: false,
},
{
id: 'status',
label: __( 'Status', 'jetpack' ),
render: ( { item } ) => (
<ShareStatusLabel status={ item.status } message={ item.message } />
),
enableSorting: false,
enableHiding: false,
},
{
id: 'actions',
label: __( 'Actions', 'jetpack' ),
render: ( { item } ) => (
<ShareStatusAction
connectionId={ item.connection_id }
status={ item.status }
shareLink={ 'success' === item.status ? item.message : '' }
/>
),
enableSorting: false,
enableHiding: false,
},
] }
data={ shareStatus.shares }
view={ { type: 'table' } }
defaultLayouts={ { table: {} } }
onChangeView={ noop }
paginationInfo={ {
totalItems: shareStatus.shares.length,
totalPages: 1,
} }
/>
</div>
);
}