2022-03-03 02:34:19 +03:00
|
|
|
import { DEFAULT_SESSION } from './constants';
|
|
|
|
import React, { useCallback } 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-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;
|
|
|
|
});
|
|
|
|
useTermState.getState().sessions[name]?.term?.focus();
|
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
|
|
|
|
|
|
|
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}
|
|
|
|
{' '}
|
|
|
|
</a>
|
|
|
|
{name === DEFAULT_SESSION ? null : <a className="delete-session" onClick={onDelete}>x</a>}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|