import React, { useState, useEffect, useCallback, useMemo } from 'react';
import { Grid, H4, HelpText } from 'indigo-react';
import { Just, Nothing } from 'folktale/maybe';
import * as ob from 'urbit-ob';
import View from 'components/View';
import Tabs from 'components/Tabs';
import Crumbs from 'components/Crumbs';
import Sigil from 'components/Sigil';
import Blinky from 'components/Blinky';
import NavHeader from 'components/NavHeader';
import { useLocalRouter } from 'lib/LocalRouter';
import useCurrentPointName from 'lib/useCurrentPointName';
import * as need from 'lib/need';
import { usePointCursor } from 'store/pointCursor';
import { usePointCache } from 'store/pointCache';
const NAMES = {
REQUESTS: 'REQUESTS',
ALL: 'ALL',
};
const OPTIONS = [
{ text: 'Requests', value: NAMES.REQUESTS },
{ text: 'All', value: NAMES.ALL },
];
const VIEWS = {
[NAMES.REQUESTS]: Tab,
[NAMES.ALL]: Tab,
};
function Tab({ className, points, onAccept, onDecline }) {
if (Nothing.hasInstance(points)) {
return ;
}
const _points = points.getOrElse([]);
return (
{_points.length === 0 && (
{Just.hasInstance(points) ? (
'No points to display'
) : (
<>
Loading...
>
)}
)}
{_points.map(point => (
))}
);
}
function Resident({ point, onAccept, onDecline }) {
const patp = ob.patp(point);
const sigilSize = 50;
const isRequest = onAccept && onDecline;
return (
<>
{patp}{' '}
{isRequest && (
<>
onAccept(point)}>
Accept
onDecline(point)}>
Decline
>
)}
>
);
}
export default function Residents() {
const { pop, push, names } = useLocalRouter();
const name = useCurrentPointName();
const { pointCursor } = usePointCursor();
const point = need.point(pointCursor);
const { getResidents, syncResidents } = usePointCache();
useEffect(() => {
syncResidents(point);
}, [syncResidents, point]);
const { residents, requests } = getResidents(point);
const [currentTab, setCurrentTab] = useState(NAMES.ALL);
const isRequests = useMemo(() => currentTab === NAMES.REQUESTS, [currentTab]);
const _onAccept = useCallback(adoptee =>
push(names.ADOPT, {
adoptee,
denied: false,
})
);
const onAccept = isRequests && _onAccept;
const _onDecline = useCallback(adoptee =>
push(names.ADOPT, {
adoptee,
denied: true,
})
);
const onDecline = isRequests && _onDecline;
const points = isRequests ? requests : residents;
return (
Residents
);
}