-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix edit timer stuck at 00:00 (#1673)
* Fix edit timer stuck at 00:00 * refactor with useCanEdit hook
- Loading branch information
Showing
3 changed files
with
32 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { useEffect, useState } from 'react' | ||
import { datePivot } from '@/lib/time' | ||
import { useMe } from '@/components/me' | ||
import { USER_ID } from '@/lib/constants' | ||
|
||
export default function useCanEdit (item) { | ||
const editThreshold = datePivot(new Date(item.invoice?.confirmedAt ?? item.createdAt), { minutes: 10 }) | ||
const { me } = useMe() | ||
|
||
// deleted items can never be edited and every item has a 10 minute edit window | ||
// except bios, they can always be edited but they should never show the countdown | ||
const noEdit = !!item.deletedAt || (Date.now() >= editThreshold) || item.bio | ||
const authorEdit = me && item.mine | ||
const [canEdit, setCanEdit] = useState(!noEdit && authorEdit) | ||
|
||
useEffect(() => { | ||
// allow anon edits if they have the correct hmac for the item invoice | ||
// (the server will verify the hmac) | ||
const invParams = window.localStorage.getItem(`item:${item.id}:hash:hmac`) | ||
const anonEdit = !!invParams && !me && Number(item.user.id) === USER_ID.anon | ||
// anonEdit should not override canEdit, but only allow edits if they aren't already allowed | ||
setCanEdit(canEdit => canEdit || anonEdit) | ||
}, []) | ||
|
||
return [canEdit, setCanEdit, editThreshold] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters