2023-04-05 18:30:26 +03:00
|
|
|
import { useEffect, useRef, useState } from 'preact/hooks';
|
|
|
|
|
|
|
|
import { api } from '../utils/api';
|
2023-08-09 14:08:42 +03:00
|
|
|
import supports from '../utils/supports';
|
2023-04-05 18:30:26 +03:00
|
|
|
|
2023-04-20 11:10:57 +03:00
|
|
|
import Icon from './icon';
|
2023-07-17 16:01:00 +03:00
|
|
|
import MenuConfirm from './menu-confirm';
|
2023-04-20 11:10:57 +03:00
|
|
|
|
|
|
|
function ListAddEdit({ list, onClose }) {
|
2023-04-05 18:30:26 +03:00
|
|
|
const { masto } = api();
|
|
|
|
const [uiState, setUiState] = useState('default');
|
|
|
|
const editMode = !!list;
|
|
|
|
const nameFieldRef = useRef();
|
|
|
|
const repliesPolicyFieldRef = useRef();
|
2023-08-09 14:08:42 +03:00
|
|
|
const exclusiveFieldRef = useRef();
|
2023-04-05 18:30:26 +03:00
|
|
|
useEffect(() => {
|
|
|
|
if (editMode) {
|
|
|
|
nameFieldRef.current.value = list.title;
|
|
|
|
repliesPolicyFieldRef.current.value = list.repliesPolicy;
|
2023-08-09 14:08:42 +03:00
|
|
|
if (exclusiveFieldRef.current) {
|
|
|
|
exclusiveFieldRef.current.checked = list.exclusive;
|
|
|
|
}
|
2023-04-05 18:30:26 +03:00
|
|
|
}
|
|
|
|
}, [editMode]);
|
2023-08-09 14:08:42 +03:00
|
|
|
const supportsExclusive = supports('@mastodon/list-exclusive');
|
|
|
|
|
2023-04-05 18:30:26 +03:00
|
|
|
return (
|
|
|
|
<div class="sheet">
|
2023-04-20 11:10:57 +03:00
|
|
|
{!!onClose && (
|
|
|
|
<button type="button" class="sheet-close" onClick={onClose}>
|
|
|
|
<Icon icon="x" />
|
|
|
|
</button>
|
|
|
|
)}{' '}
|
2023-04-05 18:30:26 +03:00
|
|
|
<header>
|
|
|
|
<h2>{editMode ? 'Edit list' : 'New list'}</h2>
|
|
|
|
</header>
|
|
|
|
<main>
|
|
|
|
<form
|
|
|
|
class="list-form"
|
|
|
|
onSubmit={(e) => {
|
|
|
|
e.preventDefault(); // Get form values
|
|
|
|
|
|
|
|
const formData = new FormData(e.target);
|
|
|
|
const title = formData.get('title');
|
|
|
|
const repliesPolicy = formData.get('replies_policy');
|
2023-08-09 14:08:42 +03:00
|
|
|
const exclusive = formData.get('exclusive') === 'on';
|
2023-04-05 18:30:26 +03:00
|
|
|
console.log({
|
|
|
|
title,
|
|
|
|
repliesPolicy,
|
2023-08-09 14:08:42 +03:00
|
|
|
exclusive,
|
2023-04-05 18:30:26 +03:00
|
|
|
});
|
|
|
|
setUiState('loading');
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
let listResult;
|
|
|
|
|
|
|
|
if (editMode) {
|
|
|
|
listResult = await masto.v1.lists.update(list.id, {
|
|
|
|
title,
|
|
|
|
replies_policy: repliesPolicy,
|
2023-08-09 14:08:42 +03:00
|
|
|
exclusive,
|
2023-04-05 18:30:26 +03:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
listResult = await masto.v1.lists.create({
|
|
|
|
title,
|
|
|
|
replies_policy: repliesPolicy,
|
2023-08-09 14:08:42 +03:00
|
|
|
exclusive,
|
2023-04-05 18:30:26 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(listResult);
|
|
|
|
setUiState('default');
|
2023-04-20 11:10:57 +03:00
|
|
|
onClose?.({
|
2023-04-05 18:30:26 +03:00
|
|
|
state: 'success',
|
|
|
|
list: listResult,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
setUiState('error');
|
|
|
|
alert(
|
|
|
|
editMode ? 'Unable to edit list.' : 'Unable to create list.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div class="list-form-row">
|
|
|
|
<label for="list-title">
|
|
|
|
Name{' '}
|
|
|
|
<input
|
|
|
|
ref={nameFieldRef}
|
|
|
|
type="text"
|
|
|
|
id="list-title"
|
|
|
|
name="title"
|
|
|
|
required
|
|
|
|
disabled={uiState === 'loading'}
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
<div class="list-form-row">
|
|
|
|
<select
|
|
|
|
ref={repliesPolicyFieldRef}
|
|
|
|
name="replies_policy"
|
|
|
|
required
|
|
|
|
disabled={uiState === 'loading'}
|
|
|
|
>
|
|
|
|
<option value="list">Show replies to list members</option>
|
|
|
|
<option value="followed">Show replies to people I follow</option>
|
|
|
|
<option value="none">Don't show replies</option>
|
|
|
|
</select>
|
|
|
|
</div>
|
2023-08-09 14:08:42 +03:00
|
|
|
{supportsExclusive && (
|
|
|
|
<div class="list-form-row">
|
|
|
|
<label class="label-block">
|
|
|
|
<input
|
|
|
|
ref={exclusiveFieldRef}
|
|
|
|
type="checkbox"
|
|
|
|
name="exclusive"
|
|
|
|
disabled={uiState === 'loading'}
|
|
|
|
/>{' '}
|
|
|
|
Hide posts on this list from Home/Following
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
)}
|
2023-04-05 18:30:26 +03:00
|
|
|
<div class="list-form-footer">
|
|
|
|
<button type="submit" disabled={uiState === 'loading'}>
|
|
|
|
{editMode ? 'Save' : 'Create'}
|
|
|
|
</button>
|
|
|
|
{editMode && (
|
2023-07-17 16:01:00 +03:00
|
|
|
<MenuConfirm
|
2023-04-05 18:30:26 +03:00
|
|
|
disabled={uiState === 'loading'}
|
2023-07-17 16:01:00 +03:00
|
|
|
align="end"
|
|
|
|
menuItemClassName="danger"
|
|
|
|
confirmLabel="Delete this list?"
|
2023-04-05 18:30:26 +03:00
|
|
|
onClick={() => {
|
2023-07-17 16:01:00 +03:00
|
|
|
// const yes = confirm('Delete this list?');
|
|
|
|
// if (!yes) return;
|
2023-04-05 18:30:26 +03:00
|
|
|
setUiState('loading');
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
await masto.v1.lists.remove(list.id);
|
|
|
|
setUiState('default');
|
2023-04-20 11:10:57 +03:00
|
|
|
onClose?.({
|
2023-04-05 18:30:26 +03:00
|
|
|
state: 'deleted',
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
setUiState('error');
|
|
|
|
alert('Unable to delete list.');
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}}
|
|
|
|
>
|
2023-07-17 16:01:00 +03:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="light danger"
|
|
|
|
disabled={uiState === 'loading'}
|
|
|
|
>
|
|
|
|
Delete…
|
|
|
|
</button>
|
|
|
|
</MenuConfirm>
|
2023-04-05 18:30:26 +03:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</main>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ListAddEdit;
|