This repository has been archived by the owner on Nov 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6f4e84
commit 9fa96f8
Showing
14 changed files
with
141 additions
and
60 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import withWebId from './withWebId'; | ||
import useWebId from '../hooks/useWebId'; | ||
|
||
/** Pane that only shows its contents when the user is logged in. */ | ||
export default withWebId(function LoggedIn({ webId, children }) { | ||
export default function LoggedIn({ children }) { | ||
const webId = useWebId(); | ||
return webId && children || 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import withWebId from './withWebId'; | ||
import useWebId from '../hooks/useWebId'; | ||
|
||
/** Pane that only shows its contents when the user is logged out. */ | ||
export default withWebId(function LoggedOut({ webId, children }) { | ||
export default function LoggedOut({ children }) { | ||
const webId = useWebId(); | ||
return !webId && children || 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,10 @@ | ||
import React from 'react'; | ||
import auth from 'solid-auth-client'; | ||
import { getDisplayName } from '../util'; | ||
|
||
// Track all instances to inform them of WebID changes | ||
const instances = new Set(); | ||
let authState = { webId: undefined }; | ||
import useWebId from '../hooks/useWebId'; | ||
import { higherOrderComponent } from '../util'; | ||
|
||
/** | ||
* Higher-order component that passes the WebID of the logged-in user | ||
* to the webId property of the wrapped component. | ||
*/ | ||
export default function withWebId(Component) { | ||
return class WithWebID extends React.Component { | ||
static displayName = `WithWebId(${getDisplayName(Component)})`; | ||
|
||
state = authState; | ||
|
||
componentDidMount() { | ||
instances.add(this); | ||
} | ||
|
||
componentWillUnmount() { | ||
instances.delete(this); | ||
} | ||
|
||
render() { | ||
return <Component webId={this.state.webId} {...this.props} />; | ||
} | ||
}; | ||
} | ||
|
||
// Inform all instances when the WebID changes | ||
auth.trackSession(session => { | ||
authState = { webId: session && session.webId }; | ||
for (const instance of instances) | ||
instance.setState(authState); | ||
}); | ||
export default higherOrderComponent('WithWebId', Component => | ||
props => <Component {...props} webId={useWebId()} />); |
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,29 @@ | ||
import { useState, useEffect } from 'react'; | ||
import auth from 'solid-auth-client'; | ||
|
||
// Keep track of the WebID and the state setters following it | ||
let webId; | ||
const setters = new Set(); | ||
|
||
/** | ||
* Returns the WebID (string) of the active user, | ||
* `null` if there is no user, | ||
* or `undefined` if the user state is pending. | ||
*/ | ||
export default function useWebId() { | ||
const [, setWebId] = useState(webId); | ||
|
||
useEffect(() => { | ||
setters.add(setWebId); | ||
return () => setters.delete(setWebId); | ||
}, []); | ||
|
||
return webId; | ||
} | ||
|
||
// Inform all setters when the WebID changes | ||
auth.trackSession(session => { | ||
webId = session && session.webId; | ||
for (const setter of setters) | ||
setter(webId); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,6 @@ | |
func-style: off, | ||
new-cap: off, | ||
no-empty-function: off, | ||
no-void: off, | ||
}, | ||
} |
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
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 { useWebId } from '../../src/'; | ||
import { renderHook, cleanup, act } from 'react-hooks-testing-library'; | ||
import auth from 'solid-auth-client'; | ||
|
||
describe('useWebId', () => { | ||
let result, unmount; | ||
beforeAll(() => { | ||
({ result, unmount } = renderHook(() => useWebId())); | ||
}); | ||
afterAll(() => unmount()); | ||
afterAll(cleanup); | ||
|
||
it('returns undefined when the login status is unknown', () => { | ||
expect(result.current).toBeUndefined(); | ||
}); | ||
|
||
it('returns null when the user is logged out', () => { | ||
act(() => void auth.mockWebId(null)); | ||
expect(result.current).toBeNull(); | ||
}); | ||
|
||
it('returns the WebID when the user is logged in', () => { | ||
act(() => void auth.mockWebId('https://example.org/#me')); | ||
expect(result.current).toBe('https://example.org/#me'); | ||
}); | ||
}); |
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