2022-12-01 15:46:26 +09:00
|
|
|
import type { Status } from 'masto'
|
|
|
|
|
2022-12-29 06:44:23 +09:00
|
|
|
type Action = 'reblogged' | 'favourited' | 'bookmarked' | 'pinned' | 'muted'
|
2022-12-01 15:46:26 +09:00
|
|
|
type CountField = 'reblogsCount' | 'favouritesCount'
|
|
|
|
|
|
|
|
export interface StatusActionsProps {
|
|
|
|
status: Status
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useStatusActions(props: StatusActionsProps) {
|
|
|
|
let status = $ref<Status>({ ...props.status })
|
2022-12-25 23:04:50 +09:00
|
|
|
const masto = useMasto()
|
2022-12-01 15:46:26 +09:00
|
|
|
|
|
|
|
watch(
|
|
|
|
() => props.status,
|
|
|
|
val => status = { ...val },
|
|
|
|
{ deep: true, immediate: true },
|
|
|
|
)
|
|
|
|
|
|
|
|
// Use different states to let the user press different actions right after the other
|
|
|
|
const isLoading = $ref({
|
|
|
|
reblogged: false,
|
|
|
|
favourited: false,
|
|
|
|
bookmarked: false,
|
|
|
|
pinned: false,
|
|
|
|
translation: false,
|
2022-12-29 06:44:23 +09:00
|
|
|
muted: false,
|
2022-12-01 15:46:26 +09:00
|
|
|
})
|
|
|
|
|
2022-12-03 01:55:52 +09:00
|
|
|
async function toggleStatusAction(action: Action, fetchNewStatus: () => Promise<Status>, countField?: CountField) {
|
2022-12-02 11:18:57 +09:00
|
|
|
// check login
|
|
|
|
if (!checkLogin())
|
|
|
|
return
|
2022-12-03 01:55:52 +09:00
|
|
|
isLoading[action] = true
|
|
|
|
fetchNewStatus().then((newStatus) => {
|
|
|
|
Object.assign(status, newStatus)
|
2022-12-25 23:52:37 +09:00
|
|
|
cacheStatus(newStatus, undefined, true)
|
2022-12-03 01:55:52 +09:00
|
|
|
}).finally(() => {
|
|
|
|
isLoading[action] = false
|
|
|
|
})
|
2022-12-02 11:18:57 +09:00
|
|
|
// Optimistic update
|
2022-12-01 15:46:26 +09:00
|
|
|
status[action] = !status[action]
|
2022-12-25 23:52:37 +09:00
|
|
|
cacheStatus(status, undefined, true)
|
2022-12-01 15:46:26 +09:00
|
|
|
if (countField)
|
|
|
|
status[countField] += status[action] ? 1 : -1
|
|
|
|
}
|
|
|
|
const toggleReblog = () => toggleStatusAction(
|
|
|
|
'reblogged',
|
2022-12-25 23:04:50 +09:00
|
|
|
() => masto.statuses[status.reblogged ? 'unreblog' : 'reblog'](status.id).then((res) => {
|
2022-12-01 15:46:26 +09:00
|
|
|
if (status.reblogged)
|
|
|
|
// returns the original status
|
|
|
|
return res.reblog!
|
|
|
|
return res
|
|
|
|
}),
|
|
|
|
'reblogsCount',
|
|
|
|
)
|
|
|
|
|
|
|
|
const toggleFavourite = () => toggleStatusAction(
|
|
|
|
'favourited',
|
2022-12-25 23:04:50 +09:00
|
|
|
() => masto.statuses[status.favourited ? 'unfavourite' : 'favourite'](status.id),
|
2022-12-01 15:46:26 +09:00
|
|
|
'favouritesCount',
|
|
|
|
)
|
|
|
|
|
|
|
|
const toggleBookmark = () => toggleStatusAction(
|
|
|
|
'bookmarked',
|
2022-12-25 23:04:50 +09:00
|
|
|
() => masto.statuses[status.bookmarked ? 'unbookmark' : 'bookmark'](status.id),
|
2022-12-01 15:46:26 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
const togglePin = async () => toggleStatusAction(
|
|
|
|
'pinned',
|
2022-12-25 23:04:50 +09:00
|
|
|
() => masto.statuses[status.pinned ? 'unpin' : 'pin'](status.id),
|
2022-12-01 15:46:26 +09:00
|
|
|
)
|
|
|
|
|
2022-12-29 06:44:23 +09:00
|
|
|
const toggleMute = async () => toggleStatusAction(
|
|
|
|
'muted',
|
|
|
|
() => masto.statuses[status.muted ? 'unmute' : 'mute'](status.id),
|
|
|
|
)
|
|
|
|
|
2022-12-01 15:46:26 +09:00
|
|
|
return {
|
|
|
|
status: $$(status),
|
|
|
|
isLoading: $$(isLoading),
|
2022-12-29 06:44:23 +09:00
|
|
|
toggleMute,
|
2022-12-01 15:46:26 +09:00
|
|
|
toggleReblog,
|
|
|
|
toggleFavourite,
|
|
|
|
toggleBookmark,
|
|
|
|
togglePin,
|
|
|
|
}
|
|
|
|
}
|