@airtable/blocks/ui#useLoadable JavaScript Examples
The following examples show how to use
@airtable/blocks/ui#useLoadable.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UpdateRecordsApp.js From apps-update-records with MIT License | 6 votes |
function UpdateRecordsApp() {
const base = useBase();
const cursor = useCursor();
const tableToUpdate = base.getTableByName(TABLE_NAME);
const numberField = tableToUpdate.getFieldByName(FIELD_NAME);
// cursor.selectedRecordIds isn't loaded by default, so we need to load it
// explicitly with the useLoadable hook. The rest of the code in the
// component will not run until it has loaded.
useLoadable(cursor);
// Re-render the app whenever the selected records change.
useWatchable(cursor, ['selectedRecordIds']);
if (cursor.activeTableId !== tableToUpdate.id) {
return (
<Container>
<Text>Switch to the “{tableToUpdate.name}” table to use this app.</Text>
</Container>
);
}
return (
<Container>
<UpdateSelectedRecordsButton
tableToUpdate={tableToUpdate}
fieldToUpdate={numberField}
selectedRecordIds={cursor.selectedRecordIds}
/>
</Container>
);
}
Example #2
Source File: index.js From apps-url-preview with MIT License | 4 votes |
// How this app chooses a preview to show:
//
// Without a specified Table & Field:
//
// - When the user selects a cell in grid view and the field's content is
// a supported preview URL, the app uses this URL to construct an embed
// URL and inserts this URL into an iframe.
//
// To Specify a Table & Field:
//
// - The user may use "Settings" to toggle a specified table and specified
// field constraint. If the constraint switch is set to "Yes",he user must
// set a specified table and specified field for URL previews.
//
// With a specified table & specified field:
//
// - When the user selects a cell in grid view and the active table matches
// the specified table or when the user opens a record from a button field
// in the specified table:
// The app looks in the selected record for the
// specified field containing a supported URL (e.g. https://www.youtube.com/watch?v=KYz2wyBy3kc),
// and uses this URL to construct an embed URL and inserts this URL into
// an iframe.
//
function UrlPreviewApp() {
const [isSettingsOpen, setIsSettingsOpen] = useState(false);
useSettingsButton(() => setIsSettingsOpen(!isSettingsOpen));
const {
isValid,
settings: {isEnforced, urlTable},
} = useSettings();
// Caches the currently selected record and field in state. If the user
// selects a record and a preview appears, and then the user de-selects the
// record (but does not select another), the preview will remain. This is
// useful when, for example, the user resizes the apps pane.
const [selectedRecordId, setSelectedRecordId] = useState(null);
const [selectedFieldId, setSelectedFieldId] = useState(null);
const [recordActionErrorMessage, setRecordActionErrorMessage] = useState('');
// cursor.selectedRecordIds and selectedFieldIds aren't loaded by default,
// so we need to load them explicitly with the useLoadable hook. The rest of
// the code in the component will not run until they are loaded.
useLoadable(cursor);
// Update the selectedRecordId and selectedFieldId state when the selected
// record or field change.
useWatchable(cursor, ['selectedRecordIds', 'selectedFieldIds'], () => {
// If the update was triggered by a record being de-selected,
// the current selectedRecordId will be retained. This is
// what enables the caching described above.
if (cursor.selectedRecordIds.length > 0) {
// There might be multiple selected records. We'll use the first
// one.
setSelectedRecordId(cursor.selectedRecordIds[0]);
}
if (cursor.selectedFieldIds.length > 0) {
// There might be multiple selected fields. We'll use the first
// one.
setSelectedFieldId(cursor.selectedFieldIds[0]);
}
});
// Close the record action error dialog whenever settings are opened or the selected record
// is updated. (This means you don't have to close the modal to see the settings, or when
// you've opened a different record.)
useEffect(() => {
setRecordActionErrorMessage('');
}, [isSettingsOpen, selectedRecordId]);
// Register a callback to be called whenever a record action occurs (via button field)
// useCallback is used to memoize the callback, to avoid having to register/unregister
// it unnecessarily.
const onRecordAction = useCallback(
data => {
// Ignore the event if settings are already open.
// This means we can assume settings are valid (since we force settings to be open if
// they are invalid).
if (!isSettingsOpen) {
if (isEnforced) {
if (data.tableId === urlTable.id) {
setSelectedRecordId(data.recordId);
} else {
// Record is from a mismatching table.
setRecordActionErrorMessage(
`This app is set up to preview URLs using records from the "${urlTable.name}" table, but was opened from a different table.`,
);
}
} else {
// Preview is not supported in this case, as we wouldn't know what field to preview.
// Show a dialog to the user instead.
setRecordActionErrorMessage(
'You must enable "Use a specific field for previews" to preview URLs with a button field.',
);
}
}
},
[isSettingsOpen, isEnforced, urlTable],
);
useEffect(() => {
// Return the unsubscribe function to ensure we clean up the handler.
return registerRecordActionDataCallback(onRecordAction);
}, [onRecordAction]);
// This watch deletes the cached selectedRecordId and selectedFieldId when
// the user moves to a new table or view. This prevents the following
// scenario: User selects a record that contains a preview url. The preview appears.
// User switches to a different table. The preview disappears. The user
// switches back to the original table. Weirdly, the previously viewed preview
// reappears, even though no record is selected.
useWatchable(cursor, ['activeTableId', 'activeViewId'], () => {
setSelectedRecordId(null);
setSelectedFieldId(null);
});
const base = useBase();
const activeTable = base.getTableByIdIfExists(cursor.activeTableId);
useEffect(() => {
// Display the settings form if the settings aren't valid.
if (!isValid && !isSettingsOpen) {
setIsSettingsOpen(true);
}
}, [isValid, isSettingsOpen]);
// activeTable is briefly null when switching to a newly created table.
if (!activeTable) {
return null;
}
return (
<Box>
{isSettingsOpen ? (
<SettingsForm setIsSettingsOpen={setIsSettingsOpen} />
) : (
<RecordPreviewWithDialog
activeTable={activeTable}
selectedRecordId={selectedRecordId}
selectedFieldId={selectedFieldId}
setIsSettingsOpen={setIsSettingsOpen}
/>
)}
{recordActionErrorMessage && (
<Dialog onClose={() => setRecordActionErrorMessage('')} maxWidth={400}>
<Dialog.CloseButton />
<Heading size="small">Can't preview URL</Heading>
<Text variant="paragraph" marginBottom={0}>
{recordActionErrorMessage}
</Text>
</Dialog>
)}
</Box>
);
}