2022-03-03 02:34:19 +03:00
|
|
|
import { DEFAULT_SESSION } from './constants';
|
2022-07-17 07:14:29 +03:00
|
|
|
import React, { useCallback, useEffect } from 'react';
|
2022-03-04 02:09:38 +03:00
|
|
|
import useTermState, { Session } from './state';
|
2022-03-03 02:34:19 +03:00
|
|
|
import api from './api';
|
|
|
|
import { pokeTask } from '@urbit/api/term';
|
2022-07-17 07:14:29 +03:00
|
|
|
import { DelayedSpinner as Spinner } from './Spinner';
|
2022-03-03 02:34:19 +03:00
|
|
|
|
2022-03-04 02:09:38 +03:00
|
|
|
interface TabProps {
|
|
|
|
session: Session;
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Tab = ( { session, name }: TabProps ) => {
|
2022-03-03 02:34:19 +03:00
|
|
|
const isSelected = useTermState().selected === name;
|
|
|
|
|
|
|
|
const onClick = () => {
|
|
|
|
useTermState.getState().set((state) => {
|
|
|
|
state.selected = name;
|
|
|
|
state.sessions[name].hasBell = false;
|
|
|
|
});
|
2022-03-10 01:25:10 +03:00
|
|
|
};
|
2022-03-03 02:34:19 +03:00
|
|
|
|
2022-03-04 02:09:38 +03:00
|
|
|
const onDelete = useCallback(async (e) => {
|
2022-03-03 02:34:19 +03:00
|
|
|
e.stopPropagation();
|
2022-03-04 02:09:38 +03:00
|
|
|
|
|
|
|
// clean up subscription
|
|
|
|
if(session && session.subscriptionId) {
|
|
|
|
await api.unsubscribe(session.subscriptionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE
|
|
|
|
await api.poke(pokeTask(name, { shut: null }));
|
|
|
|
|
|
|
|
// remove from zustand
|
2022-03-10 01:25:10 +03:00
|
|
|
useTermState.getState().set((state) => {
|
2022-03-03 02:34:19 +03:00
|
|
|
if (state.selected === name) {
|
|
|
|
state.selected = DEFAULT_SESSION;
|
|
|
|
}
|
|
|
|
state.names = state.names.filter(n => n !== name);
|
|
|
|
delete state.sessions[name];
|
|
|
|
});
|
2022-03-04 02:09:38 +03:00
|
|
|
}, [session]);
|
2022-03-03 02:34:19 +03:00
|
|
|
|
2022-07-17 07:14:29 +03:00
|
|
|
// TODO: sometimes the pending is not decremented?
|
|
|
|
useEffect(() => {
|
|
|
|
if(session) {
|
|
|
|
console.log(`${session.subscriptionId}: ${session.pending} pending`);
|
|
|
|
}
|
|
|
|
}, [session]);
|
|
|
|
|
2022-03-03 02:34:19 +03:00
|
|
|
return (
|
2022-03-04 03:11:37 +03:00
|
|
|
<div className={'tab ' + (isSelected ? 'selected' : '')} onClick={onClick}>
|
|
|
|
<a className='session-name'>
|
2022-03-03 02:34:19 +03:00
|
|
|
{session?.hasBell ? '🔔 ' : ''}
|
|
|
|
{name === DEFAULT_SESSION ? 'default' : name}
|
2022-07-17 07:14:29 +03:00
|
|
|
{session && session.pending > 0 ? <Spinner /> : null}
|
2022-03-03 02:34:19 +03:00
|
|
|
{' '}
|
|
|
|
</a>
|
|
|
|
{name === DEFAULT_SESSION ? null : <a className="delete-session" onClick={onDelete}>x</a>}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|