Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Display custom widget content titles #1650

Merged
merged 9 commits into from
Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/ScalarAuthClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,35 @@ class ScalarAuthClient {
return defer.promise;
}

getScalarPageTitle(url) {
const defer = Promise.defer();

let scalarPageLookupUrl = SdkConfig.get().integrations_rest_url + '/widgets/title_lookup';
scalarPageLookupUrl = this.getStarterLink(scalarPageLookupUrl);
scalarPageLookupUrl += '&curl=' + encodeURIComponent(url);
request({
method: 'GET',
uri: scalarPageLookupUrl,
json: true,
}, (err, response, body) => {
if (err) {
defer.reject(err);
} else if (response.statusCode / 100 !== 2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can 404 fall back to the widget's name, please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only affects the "variable" component of the AppTile title, the widget's name will always be displayed in the AppTile menu bar.

The variable component of the widget name is not displayed if it is the same as the "widget name". Adding it in here would just be unnecessary duplication (unless I am missing something?).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see what this is here for now. I thought it was replacing the existing title, but it's just a hint next to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep :)

defer.reject({statusCode: response.statusCode});
} else if (!body) {
defer.reject(new Error("Missing page title in response"));
} else {
let title = "";
if (body.page_title_cache_item && body.page_title_cache_item.cached_title) {
title = body.page_title_cache_item.cached_title;
}
defer.resolve(title);
}
});

return defer.promise;
}

getScalarInterfaceUrlForRoom(roomId, screen, id) {
let url = SdkConfig.get().integrations_ui_url;
url += "?scalar_token=" + encodeURIComponent(this.scalarToken);
Expand Down
23 changes: 16 additions & 7 deletions src/components/views/elements/AppTile.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default React.createClass({
hasPermissionToLoad: hasPermissionToLoad === 'true' || newProps.userId === newProps.creatorUserId,
error: null,
deleting: false,
widgetPageTitle: null,
widgetPageTitle: newProps.widgetPageTitle,
};
},

Expand Down Expand Up @@ -196,6 +196,11 @@ export default React.createClass({
widgetUrl: u.format(),
initialising: false,
});

// Fetch page title from remote content if not already set
if (!this.state.widgetPageTitle && params.url) {
this._fetchWidgetTitle(params.url);
}
}, (err) => {
console.error("Failed to get scalar_token", err);
this.setState({
Expand Down Expand Up @@ -299,12 +304,16 @@ export default React.createClass({

/**
* Set remote content title on AppTile
* @param {string} title Title string to set on the AppTile
* @param {string} url Url to check for title
*/
_updateWidgetTitle(title) {
if (title) {
this.setState({widgetPageTitle: null});
}
_fetchWidgetTitle(url) {
this._scalarClient.getScalarPageTitle(url).then((widgetPageTitle) => {
if (widgetPageTitle) {
this.setState({widgetPageTitle: widgetPageTitle});
}
}, (err) =>{
console.error("Failed to get page title", err);
});
},

// Widget labels to render, depending upon user permissions
Expand Down Expand Up @@ -444,7 +453,7 @@ export default React.createClass({
height="10"
/>
<b>{ this.formatAppTileName() }</b>
{ this.state.widgetPageTitle && (
{ this.state.widgetPageTitle && this.state.widgetPageTitle != this.formatAppTileName() && (
<span>&nbsp;-&nbsp;{ this.state.widgetPageTitle }</span>
) }
</span>
Expand Down
1 change: 1 addition & 0 deletions src/components/views/rooms/AppsDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ module.exports = React.createClass({
userId={this.props.userId}
show={this.props.showApps}
creatorUserId={app.creatorUserId}
widgetPageTitle={(app.data && app.data.title) ? app.data.title : ''}
/>);
});

Expand Down