-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #394 from dappforce/deploy/batch-view
Add batch post view
- Loading branch information
Showing
7 changed files
with
178 additions
and
151 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { useEffect } from 'react' | ||
import { POST_VIEW_DURATION } from 'src/config/constants' | ||
import { LocalStorage } from 'src/utils/storage' | ||
import { useMyAddress } from '../auth/MyAccountsContext' | ||
import { addPostViews } from '../utils/datahub/post-view' | ||
|
||
const postViewStorage = new LocalStorage(() => 'post-view') | ||
function addPostViewsToStorage(postId: string) { | ||
const string = postViewStorage.get() ?? '' | ||
postViewStorage.set(`${string},${postId}`) | ||
} | ||
function getPostViewsFromStorage() { | ||
const postViewsString = postViewStorage.get() | ||
if (!postViewsString) return [] | ||
const postViews = postViewsString.split(',').filter(Boolean) | ||
if (!Array.isArray(postViews)) return [] | ||
|
||
const filteredIds = new Set<string>(postViews) | ||
return Array.from(filteredIds) | ||
} | ||
|
||
export function usePostViewTracker(postId: string, sharedPostId?: string, enabled?: boolean) { | ||
useEffect(() => { | ||
if (!enabled) return | ||
|
||
const timeoutId = setTimeout(async () => { | ||
addPostViewsToStorage(postId) | ||
if (sharedPostId) addPostViewsToStorage(sharedPostId) | ||
}, POST_VIEW_DURATION) | ||
|
||
return () => { | ||
clearTimeout(timeoutId) | ||
} | ||
}, [enabled]) | ||
} | ||
|
||
const BATCH_TIMEOUT = 10_000 | ||
export default function PostViewSubmitter() { | ||
const myAddress = useMyAddress() | ||
|
||
useEffect(() => { | ||
if (!myAddress) return | ||
|
||
console.log('masuk?') | ||
const intervalId = setInterval(async () => { | ||
try { | ||
const postViews = getPostViewsFromStorage() | ||
console.log('postviews?', postViews) | ||
if (!postViews.length) return | ||
|
||
postViewStorage.remove() | ||
await addPostViews({ | ||
args: { | ||
views: Array.from(postViews).map(value => ({ | ||
duration: POST_VIEW_DURATION, | ||
viewerId: myAddress, | ||
postPersistentId: value, | ||
})), | ||
}, | ||
}) | ||
} catch {} | ||
}, BATCH_TIMEOUT) | ||
|
||
return () => { | ||
clearTimeout(intervalId) | ||
} | ||
}, [myAddress]) | ||
|
||
return null | ||
} |
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
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
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
Oops, something went wrong.