22import { useBackend } from "@/hooks/use-backend" ;
33import { useEffect , useState } from "react" ;
44import { useSelectedGuildState } from "@/hooks/use-selected-guild-state" ;
5- import { transformNotesResponse , UserResponse } from "@/lib/definitions" ;
5+ import { EMPTY_GUILD_RESPONSE , transformNotesResponse , UserResponse } from "@/lib/definitions" ;
66import { UserNotesTableColumns , UserNotesTableData } from "./columns" ;
77import { DataTable } from "@/components/ui/data-table" ;
88import { NotesDisplay } from "./notes-display" ;
9+ import { useAuthToken } from "@/hooks/use-auth-token" ;
910
1011export default function UserNotes ( ) {
1112
1213 const { actions } = useBackend ( false ) ;
1314 const notes : UserNotesTableData [ ] = [ ] ;
15+ const { token} = useAuthToken ( ) ;
1416 const [ notedata , setnoteData ] = useState ( notes ) ;
1517 const { guild } = useSelectedGuildState ( ) ;
1618
@@ -19,43 +21,47 @@ export default function UserNotes() {
1921
2022 useEffect ( ( ) => {
2123
22- actions . fetchAllGuildNotes ( guild . id ) . then ( x => {
24+ if ( guild !== EMPTY_GUILD_RESPONSE && token ) {
2325
24- const noteDatas : UserNotesTableData [ ] = [ ] ;
25- const notes = transformNotesResponse ( x ) ;
26-
27- const getUsers = async ( ) => {
28-
29- const usersPromises : Promise < UserResponse > [ ] = [ ] ;
26+ actions . fetchAllGuildNotes ( guild . id ) . then ( x => {
3027
31- notes . forEach ( data => {
32- usersPromises . push ( actions . fetchUser ( data . userSnowflake ) ) ;
33- } )
28+ const noteDatas : UserNotesTableData [ ] = [ ] ;
29+ const notes = transformNotesResponse ( x ) ;
30+
31+ const getUsers = async ( ) => {
3432
35- return Promise . all ( usersPromises ) ;
33+ const usersPromises : Promise < UserResponse > [ ] = [ ] ;
34+
35+ notes . forEach ( data => {
36+ usersPromises . push ( actions . fetchUser ( data . userSnowflake ) ) ;
37+ } )
38+
39+ return Promise . all ( usersPromises ) ;
40+
41+ }
42+
43+ getUsers ( ) . then ( users => {
44+
45+ users . forEach ( user => {
46+
47+ const tableData : UserNotesTableData = {
48+ userid : user . id ,
49+ username : user . username ,
50+ notes : notes . filter ( x => x . userSnowflake == user . id ) [ 0 ] . noteCount ( ) ,
51+ lastNote : parseInt ( notes . filter ( x => x . userSnowflake == user . id ) [ 0 ] . getLatestNote ( ) . date ) * 1000
52+ }
53+
54+ noteDatas . push ( tableData ) ;
55+
56+ } )
57+
58+ setnoteData ( noteDatas )
3659
37- }
38-
39- getUsers ( ) . then ( users => {
40-
41- users . forEach ( user => {
42-
43- const tableData : UserNotesTableData = {
44- userid : user . id ,
45- username : user . username ,
46- notes : notes . filter ( x => x . userSnowflake == user . id ) [ 0 ] . noteCount ( ) ,
47- lastNote : parseInt ( notes . filter ( x => x . userSnowflake == user . id ) [ 0 ] . getLatestNote ( ) . date ) * 1000
48- }
49-
50- noteDatas . push ( tableData ) ;
51-
5260 } )
53-
54- setnoteData ( noteDatas )
55-
61+
5662 } )
57-
58- } )
63+
64+ }
5965
6066 } , [ guild . id ] )
6167
0 commit comments